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