JAL-1270 JUnit to TestNG refactoring
[jalview.git] / test / com / stevesoft / pat / RegexWriterTest.java
1 package com.stevesoft.pat;
2
3 import static org.testng.AssertJUnit.assertEquals;
4
5 import java.io.IOException;
6 import java.io.StringWriter;
7
8 import org.testng.annotations.Test;
9
10 /**
11  * Test class refactored from RegexWriter main method
12  */
13 public class RegexWriterTest
14 {
15
16   /**
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
20    * string.
21    * 
22    * @param re
23    *          a regular expression
24    * @param inp
25    *          an input string
26    * @throws Exception
27    */
28   void test(String re, String inp) throws IOException
29   {
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++)
35     {
36       rw.write(inp.charAt(i));
37     }
38     rw.close();
39     String res2 = sw.toString();
40     if (!res1.equals(res2))
41     {
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);
47     }
48   }
49
50   @Test
51   public void testWrite() throws IOException
52   {
53     for (int n = 1; n <= 1; n++)
54     {
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");
67       test("s/^/a/", "");
68       test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
69       test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
70       test("s/x/$&/", "xxx");
71     }
72   }
73 }