2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.parser.rfc7950.stmt.pattern;
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;
16 import java.util.function.Predicate;
17 import java.util.regex.Pattern;
18 import java.util.regex.PatternSyntaxException;
19 import org.junit.Test;
21 public class Bug4079Test {
24 public void testValidPatternFix() {
25 String fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsArrows})*+");
26 assertEquals("^(?:(\\p{InArrows})*+)$", fixedUnicodeScriptPattern);
27 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
29 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsDingbats})++");
30 assertEquals("^(?:(\\p{InDingbats})++)$", fixedUnicodeScriptPattern);
31 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
33 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsSpecials})?+");
34 assertEquals("^(?:(\\p{InSpecials})?+)$", fixedUnicodeScriptPattern);
35 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
37 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBatak}){4}+");
38 assertEquals("^(?:(\\p{IsBatak}){4}+)$", fixedUnicodeScriptPattern);
39 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
41 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}){4,6}+");
42 assertEquals("^(?:(\\p{IsLatin}){4,6}+)$", fixedUnicodeScriptPattern);
43 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
45 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsTibetan}){4,}+");
46 assertEquals("^(?:(\\p{IsTibetan}){4,}+)$", fixedUnicodeScriptPattern);
47 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
49 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsAlphabetic}){4}?");
50 assertEquals("^(?:(\\p{IsAlphabetic}){4}?)$", fixedUnicodeScriptPattern);
51 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
53 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLowercase}){4,6}?");
54 assertEquals("^(?:(\\p{IsLowercase}){4,6}?)$", fixedUnicodeScriptPattern);
55 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
57 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsUppercase}){4,}?");
58 assertEquals("^(?:(\\p{IsUppercase}){4,}?)$", fixedUnicodeScriptPattern);
59 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
61 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin-1Supplement})*");
62 assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})*)$", fixedUnicodeScriptPattern);
63 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
65 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+");
66 assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})+)$", fixedUnicodeScriptPattern);
67 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
69 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{InLatin-1Supplement})?");
70 assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement})?)$", fixedUnicodeScriptPattern);
71 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
73 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{InBasicLatin}|\\p{IsLatin-1Supplement}){4}");
74 assertEquals("^(?:(\\p{InBasicLatin}|\\p{InLatin-1Supplement}){4})$", fixedUnicodeScriptPattern);
75 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
77 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsArmenian}){2,4}");
78 assertEquals("^(?:(\\p{IsLatin}|\\p{IsArmenian}){2,4})$", fixedUnicodeScriptPattern);
79 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
81 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsLatin}|\\p{IsBasicLatin}){2,}");
82 assertEquals("^(?:(\\p{IsLatin}|\\p{InBasicLatin}){2,})$", fixedUnicodeScriptPattern);
83 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
85 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\p{IsBasicLatin}|\\p{IsLatin})*?");
86 assertEquals("^(?:(\\p{InBasicLatin}|\\p{IsLatin})*?)$", fixedUnicodeScriptPattern);
87 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
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));
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));
99 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\p{IsBasicLatin})*+");
100 assertEquals("^(?:(\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
101 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
103 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\p{IsBasicLatin})*+");
104 assertEquals("^(?:(\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
105 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
107 fixedUnicodeScriptPattern = RegexUtils.getJavaRegexFromXSD("(\\\\\\\\\\\\\\p{IsBasicLatin})*+");
108 assertEquals("^(?:(\\\\\\\\\\\\\\p{InBasicLatin})*+)$", fixedUnicodeScriptPattern);
109 assertNotNull(Pattern.compile(fixedUnicodeScriptPattern));
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));
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));
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));
139 public void testCorrectBranches() {
140 String str = RegexUtils.getJavaRegexFromXSD("a|bb");
141 assertEquals("^(?:a|bb)$", str);
142 Predicate<String> pred = Pattern.compile(str).asPredicate();
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"));