KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > parser > PatternTest


1 /*
2  * PatternTest.java
3  * JUnit based test
4  *
5  * Created on February 9, 2006, 10:00 AM
6  */

7
8 package org.netbeans.modules.languages.parser;
9
10 import junit.framework.TestCase;
11 import org.netbeans.api.languages.CharInput;
12 import org.netbeans.api.languages.ParseException;
13 import org.netbeans.modules.languages.parser.StringInput;
14
15 /**
16  *
17  * @author Jan Jancura
18  */

19 public class PatternTest extends TestCase {
20     
21     public PatternTest (String JavaDoc testName) {
22         super (testName);
23     }
24
25     public void testNext () throws ParseException {
26         testReadToken ("'/'?'>'", "/>>", true, ">");
27         testReadToken ("'/'?'>'", ">>", true, ">");
28         testReadToken ("'/'?'>'", "//>>", false, "//>>");
29         testReadToken ("[^'a']*'a'", "", false, "");
30         testReadToken ("[^'a']*'a'", "ab", true, "b");
31         testReadToken ("[^'a']*'a'", "qqab", true, "b");
32         testReadToken ("[^'a']*'a'", "qabaw", true, "baw");
33         testReadToken ("([^'a']*'a')+", "qabaw", true, "w");
34         testReadToken ("([^'a']*'a')+", "aaabw", true, "bw");
35         testReadToken ("([^'a']*'a')+", "aabaw", true, "w");
36         testReadToken ("([^'a']*'a')+", "bb", false, "bb");
37         testReadToken ("([^'a']*'a')*", "qabaw", true, "w");
38         testReadToken ("([^'a']*'a')*", "aaabw", true, "bw");
39         testReadToken ("([^'a']*'a')*", "aabaw", true, "w");
40         testReadToken ("([^'a']*'a')*", "bb", true, "bb");
41         testReadToken ("([^'a']*'a')+'b'", "qwabw", true, "w");
42         testReadToken ("([^'a']*'a')+'b'", "qaabw", true, "w");
43         testReadToken ("([^'a']*'a')+'b'", "qabdfabw", true, "w");
44         testReadToken ("('ab')*|('dc')*", "ababdc", true, "dc");
45         testReadToken ("('ab')*|('dc')*", "dcdcab", true, "ab");
46         testReadToken ("('ab')*|('dc')*", "", true, "");
47         testReadToken ("('ab'|'dc')*", "abdcabbb", true, "bb");
48         testReadToken ("('ab'|'dc')*", "", true, "");
49         testReadToken ("('ab')*('dc')*", "ababdcdcc", true, "c");
50         testReadToken ("('ab')*('dc')*", "dcab", true, "ab");
51         testReadToken ("('ab')*('dc')*", "aba", true, "a");
52         testReadToken ("('ab')*('dc')*", "", true, "");
53         testReadToken ("'abc'|'a'.'d'", "abd", true, "");
54         testReadToken ("('a'.'c')*'abd'", "abcabdab", true, "ab");
55         testReadToken ("('a'.'c')*'abd'", "abdab", true, "ab");
56         testReadToken ("('a'.'c')*'abd'", "abcaxcabda", true, "a");
57         testReadToken ("('a'.'c')+'abc'", "axcabcabca", true, "a");
58         testReadToken ("('a'..)*'abd'", "abdabda", true, "a");
59         testReadToken ("('a'..)*'abd'", "abda", true, "a");
60         testReadToken ("('a'..)*'abd'", "axxabda", true, "a");
61         testReadToken ("('a'.'c'|'asd')|'a'.'d'", "asd", true, "");
62         testReadToken ("('a'.'c'|'asd')|'a'.'d'", "axd", true, "");
63         testReadToken ("'as'['q''w''e']'as'", "asqass", true, "s");
64         testReadToken ("'as'['q''w''e']'as'", "aseass", true, "s");
65         testReadToken ("'as'['q''w''e']'as'", "asqwass", false, "asqwass");
66         testReadToken ("'a''s'*'d'", "ad", true, "");
67         testReadToken ("'a''s'*'d'", "assdw", true, "w");
68         testReadToken ("'a'.'d'", "ad", false, "ad");
69         testReadToken ("'a'.'dd'", "adddd", true, "d");
70         testReadToken ("'as'['q''w']+'zx'", "aszxx", false, "aszxx");
71         testReadToken ("'as'['q''w']+'zx'", "asqwzxx", true, "x");
72         testReadToken ("'as'['q''w']+'zx'", "asqwqwzxx", true, "x");
73         testReadToken ("'as'['q''w']*'zx'", "asqqwzxx", true, "x");
74         testReadToken ("'as'['q''w']*'zx'", "aszxx", true, "x");
75         testReadToken ("'as'['q''w']*'zx'", "asqwzzx", false, "asqwzzx");
76         testReadToken ("'ab'|'cd'", "ab", true, "");
77         testReadToken ("('ab'|'cd'|'de')*", "ab", true, "");
78         testReadToken ("('ab'|'cd'|'de')*", "ababdeaq", true, "aq");
79         testMatches ("('ab'|'cd'|'de')*", "", true);
80         testReadToken ("('ab')*|('abc')*", "ab", true, "");
81         testReadToken ("('ab')*|('abc')*", "ababcabd", true, "cabd");
82         testReadToken ("('ab')*|('abc')*", "abcabcabd", true, "abd");
83         testReadToken ("('ab')*|('abc')*", "abcad", true, "ad");
84         testReadToken ("('abc')*|('ab')*", "ab", true, "");
85         testReadToken ("'a'[^'b']|'acd'", "acdd", true, "d");
86         testReadToken ("'a'[^'b']|'acd'", "ab", false, "ab");
87         testReadToken ("'a'[^'b''c''d']*", "aswsqqsbx", true, "bx");
88         testMatches ("('a')*'a'", "", false);
89         testReadToken ("('a')*'a'", "ac", true, "c");
90         testReadToken ("('a')*'a'", "aac", true, "c");
91         testMatches ("('a''a'*)*", "", true);
92         testReadToken ("('a''a'*)*", "a", true, "");
93         testReadToken ("('a''a'*)*", "aaaa", true, "");
94         testReadToken ("[^'a']*'a'('a'|[^'a''b'][^'a']*'a')*'b'", "sdasdaadsaabfabf", true, "fabf");
95         testReadToken ("'/*'-'*/'", "/**//", true, "/");
96         testReadToken ("'/*'-'*/'", "/***/*", true, "*");
97         testReadToken ("'/*'-'*/'", "/*asdf*sdf*sdf//sdf/sdf*/a", true, "a");
98         testReadToken ("'/*'-'*/'", "/*", false, "/*");
99         testReadToken ("'/*'-'*/'", "/**qwe", false, "/**qwe");
100         testReadToken ("'/*'-'*/'", "/*a*/*/qwe", true, "*/qwe");
101         testReadToken ("-'abc'", "abaabbbabcd", true, "d");
102         testReadToken ("-'abc'", "abaabcbbabcd", true, "bbabcd");
103         testReadToken ("-'abc'", "abaabacbbabacd", false, "abaabacbbabacd");
104         //testReadToken ("'\"' ( [^ '\"' '\n' '\r'] | ('\\' ['r' 'n' 't' '\\' '\'' '\"']) )* '\"'", "\\\\", true, "");
105
}
106     
107     private static void testReadToken (
108         String JavaDoc expression,
109         String JavaDoc text,
110         boolean result,
111         String JavaDoc ext
112     ) throws ParseException {
113         StringInput in = new StringInput (text, "test");
114         Pattern p = Pattern.create (expression);
115         assertEquals (expression + " <" + text + ">", result, p.next (in) != null);
116         assertEquals (expression + " <" + text + ">", ext, in.getAsText ());
117     }
118     
119     private static void testMatches (
120         String JavaDoc expression,
121         String JavaDoc text,
122         boolean result
123     ) throws ParseException {
124         Pattern p = Pattern.create (expression);
125         assertEquals (expression + " <" + text + ">", result, p.matches (text));
126     }
127 }
128
Popular Tags