1 package com.stevesoft.pat;
3 import static org.testng.AssertJUnit.assertEquals;
5 import java.io.IOException;
6 import java.io.StringWriter;
8 import org.testng.annotations.DataProvider;
9 import org.testng.annotations.Test;
12 * Test class refactored from RegexWriter main method
14 public class RegexWriterTest
18 * Asserts that the result of performing 'replaceAll' on the input using the
19 * regular expression is the same as the output of a RegexWriter, constructed
20 * from the regular expression, writing out each character of the input
24 * a regular expression
31 { "Functional" }, dataProvider = "testWriteParam")
32 void test(String re, String inp) throws IOException
34 StringWriter sw = new StringWriter();
35 Regex rex = Regex.perlCode(re);
36 String res1 = rex.replaceAll(inp);
37 RegexWriter rw = new RegexWriter(rex, sw);
38 for (int i = 0; i < inp.length(); i++)
40 rw.write(inp.charAt(i));
43 String res2 = sw.toString();
44 if (!res1.equals(res2))
46 System.out.println("re=" + re);
47 System.out.println("inp=" + inp);
48 System.out.println("res1=" + res1);
49 System.out.println("res2=" + res2);
50 assertEquals(res1, res2);
54 // @Test(groups ={ "Functional" })
55 // public void testWrite() throws IOException
57 // for (int n = 1; n <= 1; n++)
59 // test("s/x/y/", "-----x123456789");
60 // test("s/x/y/", "x123456789");
61 // test("s/x/y/", "-----x");
62 // test("s/x.*?x/y/", ".xx..x..x...x...x....x....x");
63 // test("s/x.*x/[$&]/", "--x........x--xx");
64 // test("s/x.*x/[$&]/", "--x........x------");
65 // test("s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb");
66 // test("s/.$/a/", "123");
67 // test("s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
68 // test("s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
69 // test("s/$/a/", "bbb");
70 // test("s/^/a/", "bbb");
71 // test("s/^/a/", "");
72 // test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
73 // test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
74 // test("s/x/$&/", "xxx");
78 @DataProvider(name = "testWriteParam")
79 public Object[][] regexTestParams()
83 { "s/x/y/", "-----x123456789" },
84 { "s/x/y/", "x123456789" },
85 { "s/x/y/", "-----x" },
86 { "s/x.*?x/y/", ".xx..x..x...x...x....x....x" },
87 { "s/x.*x/[$&]/", "--x........x--xx" },
88 { "s/x.*x/[$&]/", "--x........x------" },
89 { "s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb" },
91 { "s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb" },
92 { "s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb" },
96 { "s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
97 { "s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" },
98 { "s/x/$&/", "xxx" } };