8c7ad491c1b85a96a4f1cf0d01a253824544893a
[yangtools.git] / parser / yang-parser-rfc7950 / src / test / java / org / opendaylight / yangtools / yang / parser / rfc7950 / stmt / pattern / 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.parser.rfc7950.stmt.pattern;
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.assertThrows;
14 import static org.junit.Assert.assertTrue;
15
16 import java.util.function.Predicate;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
19 import org.junit.Test;
20
21 public class Bug4079Test {
22
23     @Test
24     public void testValidPatternFix() {
25         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+");
26         assertEquals("^(?:(\\p{InArrows})*+)$", fixedUnicodeScriptPattern);
27         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
28
29         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++");
30         assertEquals("^(?:(\\p{InDingbats})++)$", fixedUnicodeScriptPattern);
31         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
32
33         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+");
34         assertEquals("^(?:(\\p{InSpecials})?+)$", fixedUnicodeScriptPattern);
35         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
36
37         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+");
38         assertEquals("^(?:(\\p{IsBatak}){4}+)$", fixedUnicodeScriptPattern);
39         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
40
41         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+");
42         assertEquals("^(?:(\\p{IsLatin}){4,6}+)$", fixedUnicodeScriptPattern);
43         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
44
45         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+");
46         assertEquals("^(?:(\\p{IsTibetan}){4,}+)$", fixedUnicodeScriptPattern);
47         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
48
49         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?");
50         assertEquals("^(?:(\\p{IsAlphabetic}){4}?)$", fixedUnicodeScriptPattern);
51         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
52
53         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?");
54         assertEquals("^(?:(\\p{IsLowercase}){4,6}?)$", fixedUnicodeScriptPattern);
55         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
56
57         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?");
58         assertEquals("^(?:(\\p{IsUppercase}){4,}?)$", fixedUnicodeScriptPattern);
59         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
60
61         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
62         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*)$", fixedUnicodeScriptPattern);
63         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
64
65         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
66         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+)$", fixedUnicodeScriptPattern);
67         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
68
69         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
70         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?)$", fixedUnicodeScriptPattern);
71         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
72
73         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
74         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4})$", fixedUnicodeScriptPattern);
75         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
76
77         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
78         assertEquals("^(?:(\\p{IsLatin}|\\p{IsArmenian}){2,4})$", fixedUnicodeScriptPattern);
79         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
80
81         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
82         assertEquals("^(?:(\\p{IsLatin}|\\p{InBasicLatin}){2,})$", fixedUnicodeScriptPattern);
83         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
84
85         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
86         assertEquals("^(?:(\\p{InBasicLatin}|\\p{IsLatin})*?)$", fixedUnicodeScriptPattern);
87         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
88
89         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
90                 "(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsArrows})+?");
91         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{InArrows})+?)$", fixedUnicodeScriptPattern);
92         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
93
94         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
95                 "(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}|\\p{IsLatin})??");
96         assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}|\\p{IsLatin})??)$", fixedUnicodeScriptPattern);
97         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
98
99         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+");
100         assertEquals("^(?:(\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
101         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
102
103         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+");
104         assertEquals("^(?:(\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
105         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
106
107         fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
108         assertEquals("^(?:(\\\\\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
109         assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
110     }
111
112     @Test
113     public void testInvalidPattern() {
114         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\p{IsBasicLatin})*+");
115         assertEquals("^(?:(\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
116         // should throw exception
117         assertThrows(PatternSyntaxException.class, () -> Pattern.compile(fixedUnicodeScriptPattern));
118     }
119
120     @Test
121     public void testInvalidPattern2() {
122         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
123             "(\\p{IsSpecials}|\\\\\\\\p{IsBasicLatin})*+");
124         assertEquals("^(?:(\\p{InSpecials}|\\\\\\\\p{IsBasicLatin})*+)$", fixedUnicodeScriptPattern);
125         // should throw exception
126         assertThrows(PatternSyntaxException.class, () -> Pattern.compile(fixedUnicodeScriptPattern));
127     }
128
129     @Test
130     public void testInvalidPattern3() {
131         String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD(
132             "(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+");
133         assertEquals("^(?:(\\\\\\\\\\\\p{IsBasicLatin}|\\p{IsTags})*+)$", fixedUnicodeScriptPattern);
134         // should throw exception
135         assertThrows(PatternSyntaxException.class, () -> Pattern.compile(fixedUnicodeScriptPattern));
136     }
137
138     @Test
139     public void testCorrectBranches() {
140         String str = RegexUtils.getJavaRegexFromXSD("a|bb");
141         assertEquals("^(?:a|bb)$", str);
142         Predicate<String> pred = Pattern.compile(str).asPredicate();
143
144         assertTrue(pred.test("a"));
145         assertTrue(pred.test("bb"));
146         assertFalse(pred.test("ab"));
147         assertFalse(pred.test("abb"));
148         assertFalse(pred.test("ac"));
149     }
150 }