2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package com.stevesoft.pat;
23 import static org.testng.AssertJUnit.assertEquals;
25 import java.io.IOException;
26 import java.io.StringWriter;
28 import org.testng.annotations.DataProvider;
29 import org.testng.annotations.Test;
32 * Test class refactored from RegexWriter main method
34 public class RegexWriterTest
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
44 * a regular expression
50 @Test(groups = { "Functional" }, dataProvider = "testWriteParam")
51 void test(String re, String inp) throws IOException
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++)
59 rw.write(inp.charAt(i));
62 String res2 = sw.toString();
63 if (!res1.equals(res2))
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);
73 // @Test(groups ={ "Functional" })
74 // public void testWrite() throws IOException
76 // for (int n = 1; n <= 1; n++)
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");
97 @DataProvider(name = "testWriteParam")
98 public Object[][] regexTestParams()
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" } };