namespace MonoTests.stand_alone.WebHarness
{
- public abstract class XmlComparableTest
- {
- public abstract bool XmlCompare(XmlDocument d1, XmlDocument d2, bool ignoreAlmost);
- }
-
- public class HtmlDiff : XmlComparableTest
+ public class HtmlDiff
{
public const string BEGIN_TAG = "begint";
public const string END_TAG = "endt";
public static void AssertAreEqual (string origin, string derived, string msg)
{
- bool test = HtmlComparer (origin, derived);
+ bool test = false;\r
+ try {\r
+ test = HtmlComparer (origin, derived);\r
+ }\r
+ catch (Exception e) {\r
+ //swallow e when there is XML error and fallback\r
+ //to the text comparison\r
+ }
if (!test) {\r
Assert.AreEqual (_compareExpect, _compareActual, msg);
-
}
- }
-
- public static bool HtmlComparer (string origin, string derived)
+ }\r
+\r
+ private static bool HtmlComparer (string origin, string derived)
{
XmlDocument or = new XmlDocument ();
MonoTests.stand_alone.WebHarness.HtmlDiff helper = new MonoTests.stand_alone.WebHarness.HtmlDiff ();
return helper.XmlCompare (or, dr, false);
}
- public override bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
+ private bool XmlCompare(XmlDocument expected, XmlDocument actual, bool ignoreAlmost)
{
XmlComparer comparer = new XmlComparer();
if (ignoreAlmost == false)
_compareActual = comparer.Actual;
_compareExpect = comparer.Expected;
return c;
- }
-
- public string HtmltoXml(string html)
- {
- HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();\r
- doc.LoadHtml (html.Trim (new char [] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack
-
- StringBuilder fixedxml = new StringBuilder();
- StringWriter sw = new StringWriter(fixedxml);
-
- try
- {
- StringBuilder tempxml = new StringBuilder();
- StringWriter tsw = new StringWriter(tempxml);
-
- doc.OptionOutputAsXml = true;
- doc.Save(tsw);
-
- // fix style attribute
- // the reason is that style attribute name-value pairs come in different order
- // in .NET and GH
- // Here I will sort the values of style attribute
- XmlDocument tempDoc = new XmlDocument();
- tempDoc.LoadXml(tempxml.ToString());
-
- XmlNodeList allNodes = tempDoc.SelectNodes("//*");
- foreach (XmlNode n in allNodes)
- {
- if (n.Attributes["style"] != null)
- {
- string att = n.Attributes["style"].Value;
- string [] style = att.Trim(new char[]{' ', ';'}).Split(';');
-
- for (int styleIndex=0; styleIndex<style.Length; styleIndex++)
- {
- style[styleIndex] = FixStyleNameValue(style[styleIndex]);
- }
- Array.Sort(style);
- n.Attributes["style"].Value = string.Join(";", style);
- }
- }
- tempDoc.Save(sw);
- }
- catch (Exception)
- {
- Console.WriteLine("Error parsing html response...");
- Console.WriteLine("Test case aborted");
- return "<TestCaseAborted></TestCaseAborted>";
- }
- return fixedxml.ToString();
+ }\r
+\r
+ public string HtmltoXml (string html) //throws XmlException\r
+ {\r
+ HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument ();\r
+ doc.LoadHtml (html.Trim (new char[] { '\r', '\n', ' ' })); // bug in HtmlAgilityPack\r
+\r
+ StringBuilder fixedxml = new StringBuilder ();\r
+ StringWriter sw = new StringWriter (fixedxml);\r
+\r
+ StringBuilder tempxml = new StringBuilder ();\r
+ StringWriter tsw = new StringWriter (tempxml);\r
+\r
+ doc.OptionOutputAsXml = true;\r
+ doc.Save (tsw);\r
+\r
+ // fix style attribute\r
+ // the reason is that style attribute name-value pairs come in different order\r
+ // in .NET and GH\r
+ // Here I will sort the values of style attribute\r
+ XmlDocument tempDoc = new XmlDocument ();\r
+ tempDoc.LoadXml (tempxml.ToString ());\r
+\r
+ XmlNodeList allNodes = tempDoc.SelectNodes ("//*");\r
+ foreach (XmlNode n in allNodes) {\r
+ if (n.Attributes["style"] != null) {\r
+ string att = n.Attributes["style"].Value;\r
+ string[] style = att.Trim (new char[] { ' ', ';' }).Split (';');\r
+\r
+ for (int styleIndex = 0; styleIndex < style.Length; styleIndex++) {\r
+ style[styleIndex] = FixStyleNameValue (style[styleIndex]);\r
+ }\r
+ Array.Sort (style);\r
+ n.Attributes["style"].Value = string.Join (";", style);\r
+ }\r
+ }\r
+ tempDoc.Save (sw);\r
+ return fixedxml.ToString ();\r
}
private string FixStyleNameValue(string nameValue)