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.Test;
11 * Test class refactored from RegexWriter main method
13 public class RegexWriterTest
17 * Asserts that the result of performing 'replaceAll' on the input using the
18 * regular expression is the same as the output of a RegexWriter, constructed
19 * from the regular expression, writing out each character of the input
23 * a regular expression
28 void test(String re, String inp) throws IOException
30 StringWriter sw = new StringWriter();
31 Regex rex = Regex.perlCode(re);
32 String res1 = rex.replaceAll(inp);
33 RegexWriter rw = new RegexWriter(rex, sw);
34 for (int i = 0; i < inp.length(); i++)
36 rw.write(inp.charAt(i));
39 String res2 = sw.toString();
40 if (!res1.equals(res2))
42 System.out.println("re=" + re);
43 System.out.println("inp=" + inp);
44 System.out.println("res1=" + res1);
45 System.out.println("res2=" + res2);
46 assertEquals(res1, res2);
51 public void testWrite() throws IOException
53 for (int n = 1; n <= 1; n++)
55 test("s/x/y/", "-----x123456789");
56 test("s/x/y/", "x123456789");
57 test("s/x/y/", "-----x");
58 test("s/x.*?x/y/", ".xx..x..x...x...x....x....x");
59 test("s/x.*x/[$&]/", "--x........x--xx");
60 test("s/x.*x/[$&]/", "--x........x------");
61 test("s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb");
62 test("s/.$/a/", "123");
63 test("s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
64 test("s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
65 test("s/$/a/", "bbb");
66 test("s/^/a/", "bbb");
68 test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
69 test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
70 test("s/x/$&/", "xxx");