Source Viewer:  RegexVerifier.cs

Font Size:
Arrays.asmx  asmx2wsdl.cs  assyq.cs  hash.cs  ListTest.cs  ListTest.VB.vb  MyIpConfig.cs  Printer.cs  Printers.aspx  PrivateReflection2.cs  RegexVerifier.cs  rtfcontrol.cs  wakeup.cs 
AES-example  books  CustomTabControl  data  emacs  Embedded Resources  ftp  interop  itext  Java  js-unzip  misc  MQ  MSOffice  Mvc.HelloWorld  perl  streams  Tar  timer  web-services  WPF  xml  xml-serialization  xml1  zips 

// // RegexVerifier.cs // // Test harness for Regex's // This test program uses xml serialization to get the test input, // including the regexp string and the various samples to test. // // // Fri, 15 Aug 2003 11:28 // namespace Ionic.Test.RegexVerifier { /// <remarks> /// Represents all the input for the test, including the regex to test, /// and an array of test cases. /// </remarks> [System.Xml.Serialization.XmlRootAttribute("Ionic.RegexVerifier", Namespace="", IsNullable=false)] public class TestInput { [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string DesiredOutputFormat; /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string Regexp; /// <remarks/> [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("Case", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] public TestCase[] TestList; } /// <remarks> /// This is the type that stores a single test case. /// We need a bunch of these to verify that the regex works as /// expected. Each test case has an input and an output. In our /// case, the input is a string, and the output is a bool value, /// which indicates whether the Regex should match or not. /// Other tests will have different input and output. /// </remarks> public class TestCase { /// <remarks/> [System.Xml.Serialization.XmlAttribute("Input", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string Input; /// <remarks/> [System.Xml.Serialization.XmlAttribute("IsMatch", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public bool IsMatch; } [System.Xml.Serialization.XmlRootAttribute("Ionic.RegexVerifier.Results", Namespace="", IsNullable=false)] public class TestResults { /// <remarks/> [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string Regexp; /// <remarks/> [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("Case", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] public ResultCase[] ResultList; } public class ResultCase { /// <remarks/> [System.Xml.Serialization.XmlAttribute("Input", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string Input; /// <remarks/> [System.Xml.Serialization.XmlAttribute("expected", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public bool expected; [System.Xml.Serialization.XmlAttribute("actual", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public bool actual; [System.Xml.Serialization.XmlText()] public string Status; public ResultCase(string input, bool e, bool a, string status) { Input=input; expected=e; actual=a; Status=status; } public ResultCase(){} } /// <remarks> /// This is the test app. The main routine de-serializes from /// an XML file, then runs the tests, comparing the expected /// (or desired) output with the actual result. It then prints a report /// </remarks> public class TestDriver { public static void Main() { string InputPath= "RegexVerifier.xml"; try { System.IO.FileStream fs = new System.IO.FileStream(InputPath, System.IO.FileMode.Open); System.Xml.Serialization.XmlSerializer s1= new System.Xml.Serialization.XmlSerializer(typeof(TestInput)); TestInput Input= (TestInput) s1.Deserialize(fs); fs.Close(); System.Text.RegularExpressions.Regex regex= new System.Text.RegularExpressions.Regex (Input.Regexp); System.Collections.ArrayList list= new System.Collections.ArrayList(); // run the tests, store the results: foreach (TestCase tc in Input.TestList) { bool result= regex.IsMatch(tc.Input); ResultCase r= new ResultCase(tc.Input, tc.IsMatch, result, (result == tc.IsMatch) ? "PASS" : "FAIL"); list.Add(r); } // Generate the report (XML format or CSV) bool XmlFormat= ((Input.DesiredOutputFormat!=null) && ("XML"==Input.DesiredOutputFormat.ToUpper())); if (XmlFormat) { TestResults tr= new TestResults(); tr.Regexp= Input.Regexp; tr.ResultList= (ResultCase[]) list.ToArray(typeof(ResultCase)); System.Xml.Serialization.XmlSerializer s2= new System.Xml.Serialization.XmlSerializer(typeof(TestResults)); // use this to "suppress" the default xsd and xsd-instance namespaces System.Xml.Serialization.XmlSerializerNamespaces ns = new System.Xml.Serialization.XmlSerializerNamespaces(); ns.Add( "", "" ); s2.Serialize(System.Console.Out, tr, ns); } else { string FormatString= "\"{0}\",{1},{2},{3}"; foreach (ResultCase r in list) { System.Console.WriteLine(FormatString, r.Input,r.expected,r.actual,r.Status); } } } catch (System.Exception e1) { System.Console.WriteLine("\n\n****\nException: " + e1); } } } } #if NOTUSED // This is input data. Store this in the XML file that is de-serialized for this test. <Ionic.RegexVerifier> <TestList> <!-- ================================================================== --> <!-- =================== True test cases ============================== --> <!-- ================================================================== --> <Case Input="Rogers,James" IsMatch="true"/> <Case Input="Rogers, James" IsMatch="true"/> <Case Input="Rogers , James" IsMatch="true"/> <Case Input="Rogers ,James" IsMatch="true"/> <!-- ================================================================== --> <!-- =================== False test cases ============================== --> <!-- ================================================================== --> <Case Input="RogersJ,ames" IsMatch="false"/> <Case Input="Rogers," IsMatch="false"/> <Case Input="RogersJames" IsMatch="false"/> </TestList> <Regexp>^[A-Z][\w]*\,[\s|\w](\w*)$</Regexp> <DesiredOutputFormat>XML</DesiredOutputFormat> </Ionic.RegexVerifier> #endif

The srcview page has been enjoyed 291584 times since 18 September 2003