Encapsulate regexes in a non-capturing group
[yangtools.git] / yang / yang-model-util / src / test / java / org / opendaylight / yangtools / yang / model / util / Bug4079Test.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.model.util;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.function.Predicate;
16 import java.util.regex.Pattern;
17 import java.util.regex.PatternSyntaxException;
18 import org.junit.Test;
19
20 public class Bug4079Test {
21
22     @Test
23     public void testValidPatternFix() {
24         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+");
25         assertEquals("^(?:(\\p{InArrows})*+)$", fixedUnicodeScriptPattern);
26         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
27
28         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++");
29         assertEquals("^(?:(\\p{InDingbats})++)$", fixedUnicodeScriptPattern);
30         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
31
32         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+");
33         assertEquals("^(?:(\\p{InSpecials})?+)$", fixedUnicodeScriptPattern);
34         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
35
36         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+");
37         assertEquals("^(?:(\\p{IsBatak}){4}+)$", fixedUnicodeScriptPattern);
38         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
39
40         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+");
41         assertEquals("^(?:(\\p{IsLatin}){4,6}+)$", fixedUnicodeScriptPattern);
42         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
43
44         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+");
45         assertEquals("^(?:(\\p{IsTibetan}){4,}+)$", fixedUnicodeScriptPattern);
46         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
47
48         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?");
49         assertEquals("^(?:(\\p{IsAlphabetic}){4}?)$", fixedUnicodeScriptPattern);
50         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
51
52         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?");
53         assertEquals("^(?:(\\p{IsLowercase}){4,6}?)$", fixedUnicodeScriptPattern);
54         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
55
56         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?");
57         assertEquals("^(?:(\\p{IsUppercase}){4,}?)$", fixedUnicodeScriptPattern);
58         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
59
60         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
61         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*)$", fixedUnicodeScriptPattern);
62         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
63
64         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
65         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+)$", fixedUnicodeScriptPattern);
66         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
67
68         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
69         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?)$", fixedUnicodeScriptPattern);
70         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
71
72         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
73         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4})$", fixedUnicodeScriptPattern);
74         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
75
76         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
77         assertEquals("^(?:(\\p{IsLatin}|\\p{IsArmenian}){2,4})$", fixedUnicodeScriptPattern);
78         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
79
80         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
81         assertEquals("^(?:(\\p{IsLatin}|\\p{InBasicLatin}){2,})$", fixedUnicodeScriptPattern);
82         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
83
84         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
85         assertEquals("^(?:(\\p{InBasicLatin}|\\p{IsLatin})*?)$", fixedUnicodeScriptPattern);
86         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
87
88         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
89                 "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsArrows})+?");
90         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?)$", fixedUnicodeScriptPattern);
91         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
92
93         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
94                 "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsLatin})??");
95         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??)$", fixedUnicodeScriptPattern);
96         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
97
98         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+");
99         assertEquals("^(?:(\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
100         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
101
102         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+");
103         assertEquals("^(?:(\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
104         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
105
106         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
107         assertEquals("^(?:(\\\\\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
108         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
109     }
110
111     @Test(expected = PatternSyntaxException.class)
112     public void testInvalidPattern() {
113         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\p{IsBasicLatin})*+");
114         assertEquals("^(?:(\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
115         // should throw exception
116         Pattern.compile(fixedUnicodeScriptPattern);
117     }
118
119     @Test(expected = PatternSyntaxException.class)
120     public void testInvalidPattern2() {
121         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
122             "(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
123         assertEquals("^(?:(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
124         // should throw exception
125         Pattern.compile(fixedUnicodeScriptPattern);
126     }
127
128     @Test(expected = PatternSyntaxException.class)
129     public void testInvalidPattern3() {
130         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
131             "(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
132         assertEquals("^(?:(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+)$", fixedUnicodeScriptPattern);
133         // should throw exception
134         Pattern.compile(fixedUnicodeScriptPattern);
135     }
136
137     @Test
138     public void testCorrectBranches() {
139         String str = RegexUtils.getJavaRegexFromXSD("a|bb");
140         assertEquals("^(?:a|bb)$", str);
141         Predicate<String> pred = Pattern.compile(str).asPredicate();
142
143         assertTrue(pred.test("a"));
144         assertTrue(pred.test("bb"));
145         assertFalse(pred.test("ab"));
146         assertFalse(pred.test("abb"));
147         assertFalse(pred.test("ac"));
148     }
149 }