Browse Source
- Improved formatting of test output - Included subset of W3C XML Conformance Test Suite git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@4694 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
5 changed files with 143 additions and 103 deletions
@ -0,0 +1,141 @@
@@ -0,0 +1,141 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Diagnostics; |
||||
using System.IO; |
||||
using System.Text; |
||||
|
||||
using ICSharpCode.AvalonEdit.Xml; |
||||
using ICSharpCode.SharpZipLib.Zip; |
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.AvalonEdit.Xml.Tests |
||||
{ |
||||
[TestFixture] |
||||
public class W3C |
||||
{ |
||||
readonly string zipFileName = @"XmlParser\W3C.zip"; |
||||
|
||||
ZipFile zipFile; |
||||
List<ZipEntry> files = new List<ZipEntry>(); |
||||
Dictionary<string, string> descriptions = new Dictionary<string, string>(); |
||||
|
||||
[TestFixtureSetUp] |
||||
public void OpenZipFile() |
||||
{ |
||||
zipFile = new ZipFile(zipFileName); |
||||
files.AddRange(zipFile.Cast<ZipEntry>().Where(zip => zip.IsFile)); |
||||
foreach(ZipEntry metaData in GetXmlFilesStartingWith("ibm/ibm_oasis")) { |
||||
string conent = Decompress(metaData); |
||||
var doc = System.Xml.Linq.XDocument.Parse(conent); |
||||
foreach(var testElem in doc.Descendants("TEST")) { |
||||
descriptions.Add("ibm/" + testElem.Attribute("URI").Value, testElem.Value.Replace("\n ", "\n").TrimStart('\n')); |
||||
} |
||||
} |
||||
} |
||||
|
||||
string Decompress(ZipEntry zipEntry) |
||||
{ |
||||
Stream stream = zipFile.GetInputStream(zipEntry); |
||||
return new StreamReader(stream).ReadToEnd(); |
||||
} |
||||
|
||||
IEnumerable<ZipEntry> GetXmlFilesStartingWith(string directory) |
||||
{ |
||||
return files.Where(f => f.Name.StartsWith(directory) && f.Name.EndsWith(".xml")); |
||||
} |
||||
|
||||
[Test] |
||||
public void Valid() |
||||
{ |
||||
string exlucde = "ibm32v02"; // Too long enity name
|
||||
TestFiles(GetXmlFilesStartingWith("ibm/valid/").Where(f => !f.Name.Contains(exlucde)), true); |
||||
} |
||||
|
||||
[Test] |
||||
public void Invalid() |
||||
{ |
||||
TestFiles(GetXmlFilesStartingWith("ibm/invalid/"), true); |
||||
} |
||||
|
||||
[Test] |
||||
[Ignore] |
||||
public void NotWellformed() |
||||
{ |
||||
TestFiles(GetXmlFilesStartingWith("ibm/not-wf/"), false); |
||||
} |
||||
|
||||
StringBuilder errorOutput; |
||||
|
||||
void TestFiles(IEnumerable<ZipEntry> files, bool areWellFormed) |
||||
{ |
||||
errorOutput = new StringBuilder(); |
||||
int testsRun = 0; |
||||
foreach (ZipEntry file in files) { |
||||
testsRun++; |
||||
TestFile(file, areWellFormed); |
||||
} |
||||
if (testsRun == 0) { |
||||
Assert.Fail("Test files not found"); |
||||
} |
||||
if (errorOutput.Length > 0) { |
||||
Assert.Fail(errorOutput.ToString()); |
||||
} |
||||
} |
||||
|
||||
void TestFile(ZipEntry zipEntry, bool isWellFormed) |
||||
{ |
||||
string fileName = zipEntry.Name; |
||||
Debug.WriteLine("Testing " + fileName + "..."); |
||||
string content = Decompress(zipEntry); |
||||
string description = null; |
||||
descriptions.TryGetValue(fileName, out description); |
||||
AXmlParser parser = new AXmlParser(content); |
||||
parser.EntityReferenceIsError = false; |
||||
var document = parser.Parse(); |
||||
|
||||
string printed = PrettyPrintAXmlVisitor.PrettyPrint(document); |
||||
if (content != printed) { |
||||
errorOutput.AppendFormat("Output of pretty printed XML for \"{0}\" does not match the original.\n", fileName); |
||||
errorOutput.AppendFormat("File content:\n{0}\n", Indent(content)); |
||||
errorOutput.AppendFormat("Pretty printed:\n{0}\n", Indent(printed)); |
||||
errorOutput.AppendLine(); |
||||
} |
||||
|
||||
bool hasErrors = document.SyntaxErrors.FirstOrDefault() != null; |
||||
if (isWellFormed && hasErrors) { |
||||
errorOutput.AppendFormat("Syntax error(s) in well formed file \"{0}\":\n", fileName); |
||||
foreach (var error in document.SyntaxErrors) { |
||||
string followingText = content.Substring(error.StartOffset, Math.Min(10, content.Length - error.StartOffset)); |
||||
errorOutput.AppendFormat("Error ({0}-{1}): {2} (followed by \"{3}\")\n", error.StartOffset, error.EndOffset, error.Message, followingText); |
||||
} |
||||
if (description != null) { |
||||
errorOutput.AppendFormat("Test description:\n{0}\n", Indent(description)); |
||||
} |
||||
errorOutput.AppendFormat("File content:\n{0}\n", Indent(content)); |
||||
errorOutput.AppendLine(); |
||||
} |
||||
|
||||
if (!isWellFormed && !hasErrors) { |
||||
errorOutput.AppendFormat("No syntax errors reported for mallformed file \"{0}\"\n", fileName); |
||||
if (description != null) { |
||||
errorOutput.AppendFormat("Test description:\n{0}\n", Indent(description)); |
||||
} |
||||
errorOutput.AppendFormat("File content:\n{0}\n", Indent(content)); |
||||
errorOutput.AppendLine(); |
||||
} |
||||
} |
||||
|
||||
string Indent(string text) |
||||
{ |
||||
return " " + text.TrimEnd().Replace("\n", "\n "); |
||||
} |
||||
} |
||||
} |
||||
Binary file not shown.
@ -1,101 +0,0 @@
@@ -1,101 +0,0 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Siegfried Pammer" email="sie_pam@gmx.at"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Diagnostics; |
||||
using System.IO; |
||||
using System.Text; |
||||
|
||||
using ICSharpCode.AvalonEdit.Xml; |
||||
using ICSharpCode.SharpZipLib.Zip; |
||||
using NUnit.Framework; |
||||
|
||||
namespace ICSharpCode.AvalonEdit.Xml.Tests |
||||
{ |
||||
[TestFixture] |
||||
public class XmlParserTests |
||||
{ |
||||
readonly string zipFileName = @"XmlParser\testcases.zip"; |
||||
|
||||
ZipFile zipFile; |
||||
List<ZipEntry> files = new List<ZipEntry>(); |
||||
|
||||
[TestFixtureSetUp] |
||||
public void OpenZipFile() |
||||
{ |
||||
zipFile = new ZipFile(zipFileName); |
||||
files.AddRange(zipFile.Cast<ZipEntry>().Where(zip => zip.IsFile)); |
||||
} |
||||
|
||||
string Decompress(ZipEntry zipEntry) |
||||
{ |
||||
byte[] data = new byte[zipEntry.Size]; |
||||
Stream stream = zipFile.GetInputStream(zipEntry); |
||||
string text = new StreamReader(stream).ReadToEnd(); |
||||
return text; |
||||
} |
||||
|
||||
IEnumerable<ZipEntry> GetFiles(string directory) |
||||
{ |
||||
return files.Where(f => f.Name.StartsWith(directory + @"/") && f.Name.EndsWith(".xml")); |
||||
} |
||||
|
||||
[Test] |
||||
public void Valid() |
||||
{ |
||||
foreach (ZipEntry file in GetFiles("valid")) { |
||||
if (file.Name == "valid/042.xml") continue; // Long entity reference
|
||||
if (file.Name == "valid/056.xml") continue; // Long entity reference
|
||||
ParseTest(file, true); |
||||
} |
||||
} |
||||
|
||||
[Test] |
||||
public void Invalid() |
||||
{ |
||||
foreach (ZipEntry file in GetFiles("invalid")) { |
||||
ParseTest(file, true); |
||||
} |
||||
} |
||||
|
||||
[Test] |
||||
[Ignore("Unfinished")] |
||||
public void NotWellformed() |
||||
{ |
||||
foreach (ZipEntry file in GetFiles("not-wellformed")) { |
||||
ParseTest(file, false); |
||||
} |
||||
} |
||||
|
||||
void ParseTest(ZipEntry zipEntry, bool isWellFormed) |
||||
{ |
||||
string fileName = zipEntry.Name; |
||||
System.Diagnostics.Debug.WriteLine("\nTesting " + fileName + "..."); |
||||
string content = Decompress(zipEntry); |
||||
AXmlParser parser = new AXmlParser(content); |
||||
parser.EntityReferenceIsError = false; |
||||
var document = parser.Parse(); |
||||
string printed = PrettyPrintAXmlVisitor.PrettyPrint(document); |
||||
|
||||
int errorCount = 0; |
||||
StringBuilder errorsOutput = new StringBuilder(); |
||||
foreach (var error in document.SyntaxErrors) { |
||||
errorCount++; |
||||
string followingText = content.Substring(error.StartOffset, Math.Min(16, content.Length - error.StartOffset)); |
||||
errorsOutput.AppendFormat("Error ({0}-{1}): {2}\nFollowing text: {3}\n", error.StartOffset, error.EndOffset, error.Message, followingText); |
||||
} |
||||
if (isWellFormed && errorCount != 0) |
||||
Assert.Fail("Syntax error(s) in well formed file \"{0}\":\n{1}File content:\n{2}\n\n", fileName, errorsOutput, content); |
||||
if (!isWellFormed && errorCount == 0) |
||||
Assert.Fail("No syntax error reported for mallformed file \"{0}\"\nFile Content:\n{1}\n\n", fileName, content); |
||||
|
||||
Assert.AreEqual(content, printed, "Output of pretty printed XML for \"{0}\" does not match the original.", fileName); |
||||
} |
||||
} |
||||
} |
||||
Binary file not shown.
Loading…
Reference in new issue