e5d087eb9ad4c7bd0ee0f706c04dc097847779d6
[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.DataProvider;
9 import org.testng.annotations.Test;
10
11 /**
12  * Test class refactored from RegexWriter main method
13  */
14 public class RegexWriterTest
15 {
16
17   /**
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
21    * string.
22    * 
23    * @param re
24    *          a regular expression
25    * @param inp
26    *          an input string
27    * @throws Exception
28    */
29
30   @Test(groups = { "Functional" }, dataProvider = "testWriteParam")
31   void test(String re, String inp) throws IOException
32   {
33     StringWriter sw = new StringWriter();
34     Regex rex = Regex.perlCode(re);
35     String res1 = rex.replaceAll(inp);
36     RegexWriter rw = new RegexWriter(rex, sw);
37     for (int i = 0; i < inp.length(); i++)
38     {
39       rw.write(inp.charAt(i));
40     }
41     rw.close();
42     String res2 = sw.toString();
43     if (!res1.equals(res2))
44     {
45       System.out.println("re=" + re);
46       System.out.println("inp=" + inp);
47       System.out.println("res1=" + res1);
48       System.out.println("res2=" + res2);
49       assertEquals(res1, res2);
50     }
51   }
52
53   // @Test(groups ={ "Functional" })
54   // public void testWrite() throws IOException
55   // {
56   // for (int n = 1; n <= 1; n++)
57   // {
58   // test("s/x/y/", "-----x123456789");
59   // test("s/x/y/", "x123456789");
60   // test("s/x/y/", "-----x");
61   // test("s/x.*?x/y/", ".xx..x..x...x...x....x....x");
62   // test("s/x.*x/[$&]/", "--x........x--xx");
63   // test("s/x.*x/[$&]/", "--x........x------");
64   // test("s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb");
65   // test("s/.$/a/", "123");
66   // test("s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
67   // test("s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
68   // test("s/$/a/", "bbb");
69   // test("s/^/a/", "bbb");
70   // test("s/^/a/", "");
71   // test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
72   // test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
73   // test("s/x/$&/", "xxx");
74   // }
75   // }
76
77   @DataProvider(name = "testWriteParam")
78   public Object[][] regexTestParams()
79   {
80     return new Object[][] { { "s/x/y/", "-----x123456789" },
81         { "s/x/y/", "x123456789" }, { "s/x/y/", "-----x" },
82         { "s/x.*?x/y/", ".xx..x..x...x...x....x....x" },
83         { "s/x.*x/[$&]/", "--x........x--xx" },
84         { "s/x.*x/[$&]/", "--x........x------" },
85         { "s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb" },
86         { "s/.$/a/", "123" },
87         { "s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb" },
88         { "s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb" },
89         { "s/$/a/", "bbb" }, { "s/^/a/", "bbb" }, { "s/^/a/", "" },
90         { "s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
91         { "s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" },
92         { "s/x/$&/", "xxx" } };
93   }
94 }