JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / test / com / stevesoft / pat / RegexWriterTest.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
3  * Copyright (C) 2015 The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package com.stevesoft.pat;
22
23 import static org.testng.AssertJUnit.assertEquals;
24
25 import java.io.IOException;
26 import java.io.StringWriter;
27
28 import org.testng.annotations.DataProvider;
29 import org.testng.annotations.Test;
30
31 /**
32  * Test class refactored from RegexWriter main method
33  */
34 public class RegexWriterTest
35 {
36
37   /**
38    * Asserts that the result of performing 'replaceAll' on the input using the
39    * regular expression is the same as the output of a RegexWriter, constructed
40    * from the regular expression, writing out each character of the input
41    * string.
42    * 
43    * @param re
44    *          a regular expression
45    * @param inp
46    *          an input string
47    * @throws Exception
48    */
49
50   @Test(groups = { "Functional" }, dataProvider = "testWriteParam")
51   void test(String re, String inp) throws IOException
52   {
53     StringWriter sw = new StringWriter();
54     Regex rex = Regex.perlCode(re);
55     String res1 = rex.replaceAll(inp);
56     RegexWriter rw = new RegexWriter(rex, sw);
57     for (int i = 0; i < inp.length(); i++)
58     {
59       rw.write(inp.charAt(i));
60     }
61     rw.close();
62     String res2 = sw.toString();
63     if (!res1.equals(res2))
64     {
65       System.out.println("re=" + re);
66       System.out.println("inp=" + inp);
67       System.out.println("res1=" + res1);
68       System.out.println("res2=" + res2);
69       assertEquals(res1, res2);
70     }
71   }
72
73   // @Test(groups ={ "Functional" })
74   // public void testWrite() throws IOException
75   // {
76   // for (int n = 1; n <= 1; n++)
77   // {
78   // test("s/x/y/", "-----x123456789");
79   // test("s/x/y/", "x123456789");
80   // test("s/x/y/", "-----x");
81   // test("s/x.*?x/y/", ".xx..x..x...x...x....x....x");
82   // test("s/x.*x/[$&]/", "--x........x--xx");
83   // test("s/x.*x/[$&]/", "--x........x------");
84   // test("s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb");
85   // test("s/.$/a/", "123");
86   // test("s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
87   // test("s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb");
88   // test("s/$/a/", "bbb");
89   // test("s/^/a/", "bbb");
90   // test("s/^/a/", "");
91   // test("s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
92   // test("s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
93   // test("s/x/$&/", "xxx");
94   // }
95   // }
96
97   @DataProvider(name = "testWriteParam")
98   public Object[][] regexTestParams()
99   {
100     return new Object[][] { { "s/x/y/", "-----x123456789" },
101         { "s/x/y/", "x123456789" }, { "s/x/y/", "-----x" },
102         { "s/x.*?x/y/", ".xx..x..x...x...x....x....x" },
103         { "s/x.*x/[$&]/", "--x........x--xx" },
104         { "s/x.*x/[$&]/", "--x........x------" },
105         { "s/.$/a/m", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbbbbbbbbbbbb" },
106         { "s/.$/a/", "123" },
107         { "s/.$/a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb" },
108         { "s/^./a/", "bb\nbbb\nbbbb\nbbbbb\nbbbbbb\nbb" },
109         { "s/$/a/", "bbb" }, { "s/^/a/", "bbb" }, { "s/^/a/", "" },
110         { "s{.*}{N}", "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" },
111         { "s/.{0,7}/y/", "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" },
112         { "s/x/$&/", "xxx" } };
113   }
114 }