|
26 | 26 | */
|
27 | 27 | class PatternMatchUtilsTests {
|
28 | 28 |
|
| 29 | + @Test |
| 30 | + void nullAndEmptyValues() { |
| 31 | + assertDoesNotMatch((String) null, null); |
| 32 | + assertDoesNotMatch((String) null, ""); |
| 33 | + assertDoesNotMatch("123", null); |
| 34 | + |
| 35 | + assertDoesNotMatch((String[]) null, null); |
| 36 | + assertDoesNotMatch((String[]) null, ""); |
| 37 | + assertDoesNotMatch(new String[] {}, null); |
| 38 | + } |
| 39 | + |
29 | 40 | @Test
|
30 | 41 | void trivial() {
|
31 | 42 | assertThat(PatternMatchUtils.simpleMatch((String) null, "")).isFalse();
|
32 | 43 | assertThat(PatternMatchUtils.simpleMatch("1", null)).isFalse();
|
33 | 44 | doTest("*", "123", true);
|
34 | 45 | doTest("123", "123", true);
|
| 46 | + testMixedCaseMatch("abC", "Abc"); |
| 47 | + assertMatches("", ""); |
| 48 | + assertMatches("123", "123"); |
| 49 | + assertMatches("*", "123"); |
| 50 | + |
| 51 | + assertMatches(new String[] { "" }, ""); |
| 52 | + assertMatches(new String[] { "123" }, "123"); |
| 53 | + assertMatches(new String[] { "*" }, "123"); |
| 54 | + |
| 55 | + assertMatches(new String[] { null, "" }, ""); |
| 56 | + assertMatches(new String[] { null, "123" }, "123"); |
| 57 | + assertMatches(new String[] { null, "*" }, "123"); |
| 58 | + |
| 59 | + testMixedCaseMatch("abC", "Abc"); |
35 | 60 | }
|
36 | 61 |
|
37 | 62 | @Test
|
38 | 63 | void startsWith() {
|
39 |
| - doTest("get*", "getMe", true); |
40 |
| - doTest("get*", "setMe", false); |
| 64 | + assertMatches("get*", "getMe"); |
| 65 | + assertDoesNotMatch("get*", "setMe"); |
| 66 | + testMixedCaseMatch("geT*", "GetMe"); |
41 | 67 | }
|
42 | 68 |
|
43 | 69 | @Test
|
44 | 70 | void endsWith() {
|
45 |
| - doTest("*Test", "getMeTest", true); |
46 |
| - doTest("*Test", "setMe", false); |
| 71 | + assertMatches("*Test", "getMeTest"); |
| 72 | + assertDoesNotMatch("*Test", "setMe"); |
| 73 | + testMixedCaseMatch("*TeSt", "getMeTesT"); |
47 | 74 | }
|
48 | 75 |
|
49 | 76 | @Test
|
50 | 77 | void between() {
|
51 |
| - doTest("*stuff*", "getMeTest", false); |
52 |
| - doTest("*stuff*", "getstuffTest", true); |
53 |
| - doTest("*stuff*", "stuffTest", true); |
54 |
| - doTest("*stuff*", "getstuff", true); |
55 |
| - doTest("*stuff*", "stuff", true); |
| 78 | + assertDoesNotMatch("*stuff*", "getMeTest"); |
| 79 | + assertMatches("*stuff*", "getstuffTest"); |
| 80 | + assertMatches("*stuff*", "stuffTest"); |
| 81 | + assertMatches("*stuff*", "getstuff"); |
| 82 | + assertMatches("*stuff*", "stuff"); |
| 83 | + testMixedCaseMatch("*stuff*", "getStuffTest"); |
| 84 | + testMixedCaseMatch("*stuff*", "StuffTest"); |
| 85 | + testMixedCaseMatch("*stuff*", "getStuff"); |
| 86 | + testMixedCaseMatch("*stuff*", "Stuff"); |
56 | 87 | }
|
57 | 88 |
|
58 | 89 | @Test
|
59 | 90 | void startsEnds() {
|
60 |
| - doTest("on*Event", "onMyEvent", true); |
61 |
| - doTest("on*Event", "onEvent", true); |
62 |
| - doTest("3*3", "3", false); |
63 |
| - doTest("3*3", "33", true); |
| 91 | + assertMatches("on*Event", "onMyEvent"); |
| 92 | + assertMatches("on*Event", "onEvent"); |
| 93 | + assertDoesNotMatch("3*3", "3"); |
| 94 | + assertMatches("3*3", "33"); |
| 95 | + testMixedCaseMatch("on*Event", "OnMyEvenT"); |
| 96 | + testMixedCaseMatch("on*Event", "OnEvenT"); |
64 | 97 | }
|
65 | 98 |
|
66 | 99 | @Test
|
67 | 100 | void startsEndsBetween() {
|
68 |
| - doTest("12*45*78", "12345678", true); |
69 |
| - doTest("12*45*78", "123456789", false); |
70 |
| - doTest("12*45*78", "012345678", false); |
71 |
| - doTest("12*45*78", "124578", true); |
72 |
| - doTest("12*45*78", "1245457878", true); |
73 |
| - doTest("3*3*3", "33", false); |
74 |
| - doTest("3*3*3", "333", true); |
| 101 | + assertMatches("12*45*78", "12345678"); |
| 102 | + assertDoesNotMatch("12*45*78", "123456789"); |
| 103 | + assertDoesNotMatch("12*45*78", "012345678"); |
| 104 | + assertMatches("12*45*78", "124578"); |
| 105 | + assertMatches("12*45*78", "1245457878"); |
| 106 | + assertDoesNotMatch("3*3*3", "33"); |
| 107 | + assertMatches("3*3*3", "333"); |
75 | 108 | }
|
76 | 109 |
|
77 | 110 | @Test
|
78 | 111 | void ridiculous() {
|
79 |
| - doTest("*1*2*3*", "0011002001010030020201030", true); |
80 |
| - doTest("1*2*3*4", "10300204", false); |
81 |
| - doTest("1*2*3*3", "10300203", false); |
82 |
| - doTest("*1*2*3*", "123", true); |
83 |
| - doTest("*1*2*3*", "132", false); |
| 112 | + assertMatches("*1*2*3*", "0011002001010030020201030"); |
| 113 | + assertDoesNotMatch("1*2*3*4", "10300204"); |
| 114 | + assertDoesNotMatch("1*2*3*3", "10300203"); |
| 115 | + assertMatches("*1*2*3*", "123"); |
| 116 | + assertDoesNotMatch("*1*2*3*", "132"); |
84 | 117 | }
|
85 | 118 |
|
86 | 119 | @Test
|
87 | 120 | void patternVariants() {
|
88 |
| - doTest("*a", "*", false); |
89 |
| - doTest("*a", "a", true); |
90 |
| - doTest("*a", "b", false); |
91 |
| - doTest("*a", "aa", true); |
92 |
| - doTest("*a", "ba", true); |
93 |
| - doTest("*a", "ab", false); |
94 |
| - doTest("**a", "*", false); |
95 |
| - doTest("**a", "a", true); |
96 |
| - doTest("**a", "b", false); |
97 |
| - doTest("**a", "aa", true); |
98 |
| - doTest("**a", "ba", true); |
99 |
| - doTest("**a", "ab", false); |
| 121 | + assertDoesNotMatch("*a", "*"); |
| 122 | + assertMatches("*a", "a"); |
| 123 | + assertDoesNotMatch("*a", "b"); |
| 124 | + assertMatches("*a", "aa"); |
| 125 | + assertMatches("*a", "ba"); |
| 126 | + assertDoesNotMatch("*a", "ab"); |
| 127 | + assertDoesNotMatch("**a", "*"); |
| 128 | + assertMatches("**a", "a"); |
| 129 | + assertDoesNotMatch("**a", "b"); |
| 130 | + assertMatches("**a", "aa"); |
| 131 | + assertMatches("**a", "ba"); |
| 132 | + assertDoesNotMatch("**a", "ab"); |
| 133 | + } |
| 134 | + |
| 135 | + private void assertMatches(String pattern, String str) { |
| 136 | + assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isTrue(); |
| 137 | + assertThat(PatternMatchUtils.simpleMatchIgnoreCase(pattern, str)).isTrue(); |
| 138 | + } |
| 139 | + |
| 140 | + private void assertDoesNotMatch(String pattern, String str) { |
| 141 | + assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isFalse(); |
| 142 | + assertThat(PatternMatchUtils.simpleMatchIgnoreCase(pattern, str)).isFalse(); |
| 143 | + } |
| 144 | + |
| 145 | + private void testMixedCaseMatch(String pattern, String str) { |
| 146 | + assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isFalse(); |
| 147 | + assertThat(PatternMatchUtils.simpleMatchIgnoreCase(pattern, str)).isTrue(); |
| 148 | + } |
| 149 | + |
| 150 | + private void assertMatches(String[] patterns, String str) { |
| 151 | + assertThat(PatternMatchUtils.simpleMatch(patterns, str)).isTrue(); |
| 152 | + assertThat(PatternMatchUtils.simpleMatchIgnoreCase(patterns, str)).isTrue(); |
100 | 153 | }
|
101 | 154 |
|
102 |
| - private void doTest(String pattern, String str, boolean shouldMatch) { |
103 |
| - assertThat(PatternMatchUtils.simpleMatch(pattern, str)).isEqualTo(shouldMatch); |
| 155 | + private void assertDoesNotMatch(String[] patterns, String str) { |
| 156 | + assertThat(PatternMatchUtils.simpleMatch(patterns, str)).isFalse(); |
| 157 | + assertThat(PatternMatchUtils.simpleMatchIgnoreCase(patterns, str)).isFalse(); |
104 | 158 | }
|
105 | 159 |
|
106 | 160 | }
|
0 commit comments