OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'util.dart'; |
| 6 import 'package:expect/expect.dart'; |
| 7 |
| 8 void main() { |
| 9 description( |
| 10 'Test regular expression processing with alternatives that match consuming no
characters' |
| 11 ); |
| 12 |
| 13 var emptyStr = ""; |
| 14 var s1 = "xxxx"; |
| 15 var s2 = "aaaa"; |
| 16 var s3 = "aax"; |
| 17 var s4 = "abab"; |
| 18 var s5 = "ab"; |
| 19 var s6 = "xabx"; |
| 20 var s7 = "g0"; |
| 21 |
| 22 // Non-capturing empty first alternative greedy '*' |
| 23 var re1 = new RegExp(r"(?:|a|z)*"); |
| 24 shouldBe(firstMatch(emptyStr, re1), [""]); |
| 25 shouldBe(firstMatch(s1, re1), [""]); |
| 26 shouldBe(firstMatch(s2, re1), ["aaaa"]); |
| 27 shouldBe(firstMatch(s3, re1), ["aa"]); |
| 28 |
| 29 // Non-capturing empty middle alternative greedy '*' |
| 30 var re2 = new RegExp(r"(?:a||z)*"); |
| 31 shouldBe(firstMatch(emptyStr, re2), [""]); |
| 32 shouldBe(firstMatch(s1, re2), [""]); |
| 33 shouldBe(firstMatch(s2, re2), ["aaaa"]); |
| 34 shouldBe(firstMatch(s3, re2), ["aa"]); |
| 35 |
| 36 // Non-capturing empty last alternative greedy '*' |
| 37 var re3 = new RegExp(r"(?:a|z|)*"); |
| 38 shouldBe(firstMatch(emptyStr, re3), [""]); |
| 39 shouldBe(firstMatch(s1, re3), [""]); |
| 40 shouldBe(firstMatch(s2, re3), ["aaaa"]); |
| 41 shouldBe(firstMatch(s3, re3), ["aa"]); |
| 42 |
| 43 // Capturing empty first alternative greedy '*' |
| 44 var re4 = new RegExp(r"(|a|z)*"); |
| 45 shouldBe(firstMatch(emptyStr, re4), ["", null]); |
| 46 shouldBe(firstMatch(s1, re4), ["", null]); |
| 47 shouldBe(firstMatch(s2, re4), ["aaaa", "a"]); |
| 48 shouldBe(firstMatch(s3, re4), ["aa", "a"]); |
| 49 |
| 50 // Capturing empty middle alternative greedy '*' |
| 51 var re5 = new RegExp(r"(a||z)*"); |
| 52 shouldBe(firstMatch(emptyStr, re5), ["", null]); |
| 53 shouldBe(firstMatch(s1, re5), ["", null]); |
| 54 shouldBe(firstMatch(s2, re5), ["aaaa", "a"]); |
| 55 shouldBe(firstMatch(s3, re5), ["aa", "a"]); |
| 56 |
| 57 // Capturing empty last alternative greedy '*' |
| 58 var re6 = new RegExp(r"(a|z|)*"); |
| 59 shouldBe(firstMatch(emptyStr, re6), ["", null]); |
| 60 shouldBe(firstMatch(s1, re6), ["", null]); |
| 61 shouldBe(firstMatch(s2, re6), ["aaaa", "a"]); |
| 62 shouldBe(firstMatch(s3, re6), ["aa", "a"]); |
| 63 |
| 64 // Non-capturing empty first alternative fixed-count |
| 65 var re7 = new RegExp(r"(?:|a|z){2,5}"); |
| 66 shouldBe(firstMatch(emptyStr, re7), [""]); |
| 67 shouldBe(firstMatch(s1, re7), [""]); |
| 68 shouldBe(firstMatch(s2, re7), ["aaa"]); |
| 69 shouldBe(firstMatch(s3, re7), ["aa"]); |
| 70 |
| 71 // Non-capturing empty middle alternative fixed-count |
| 72 var re8 = new RegExp(r"(?:a||z){2,5}"); |
| 73 shouldBe(firstMatch(emptyStr, re8), [""]); |
| 74 shouldBe(firstMatch(s1, re8), [""]); |
| 75 shouldBe(firstMatch(s2, re8), ["aaaa"]); |
| 76 shouldBe(firstMatch(s3, re8), ["aa"]); |
| 77 |
| 78 // Non-capturing empty last alternative fixed-count |
| 79 var re9 = new RegExp(r"(?:a|z|){2,5}"); |
| 80 shouldBe(firstMatch(emptyStr, re9), [""]); |
| 81 shouldBe(firstMatch(s1, re9), [""]); |
| 82 shouldBe(firstMatch(s2, re9), ["aaaa"]); |
| 83 shouldBe(firstMatch(s3, re9), ["aa"]); |
| 84 |
| 85 // Non-capturing empty first alternative non-greedy '*' |
| 86 var re10 = new RegExp(r"(?:|a|z)*?"); |
| 87 shouldBe(firstMatch(emptyStr, re10), [""]); |
| 88 shouldBe(firstMatch(s1, re10), [""]); |
| 89 shouldBe(firstMatch(s2, re10), [""]); |
| 90 shouldBe(firstMatch(s3, re10), [""]); |
| 91 |
| 92 // Non-capturing empty middle alternative non-greedy '*' |
| 93 var re11 = new RegExp(r"(?:a||z)*?"); |
| 94 shouldBe(firstMatch(emptyStr, re11), [""]); |
| 95 shouldBe(firstMatch(s1, re11), [""]); |
| 96 shouldBe(firstMatch(s2, re11), [""]); |
| 97 shouldBe(firstMatch(s3, re11), [""]); |
| 98 |
| 99 // Non-capturing empty last alternative non-greedy '*' |
| 100 var re12 = new RegExp(r"(?:a|z|)*?"); |
| 101 shouldBe(firstMatch(emptyStr, re12), [""]); |
| 102 shouldBe(firstMatch(s1, re12), [""]); |
| 103 shouldBe(firstMatch(s2, re12), [""]); |
| 104 shouldBe(firstMatch(s3, re12), [""]); |
| 105 |
| 106 // Capturing empty first alternative non-greedy '*' |
| 107 var re13 = new RegExp(r"(|a|z)*?"); |
| 108 shouldBe(firstMatch(emptyStr, re13), ["", null]); |
| 109 shouldBe(firstMatch(s1, re13), ["", null]); |
| 110 shouldBe(firstMatch(s2, re13), ["", null]); |
| 111 shouldBe(firstMatch(s3, re13), ["", null]); |
| 112 |
| 113 // Capturing empty middle alternative non-greedy '*' |
| 114 var re14 = new RegExp(r"(a||z)*?"); |
| 115 shouldBe(firstMatch(emptyStr, re14), ["", null]); |
| 116 shouldBe(firstMatch(s1, re14), ["", null]); |
| 117 shouldBe(firstMatch(s2, re14), ["", null]); |
| 118 shouldBe(firstMatch(s3, re14), ["", null]); |
| 119 |
| 120 // Capturing empty last alternative non-greedy '*' |
| 121 var re15 = new RegExp(r"(a|z|)*?"); |
| 122 shouldBe(firstMatch(emptyStr, re15), ["", null]); |
| 123 shouldBe(firstMatch(s1, re15), ["", null]); |
| 124 shouldBe(firstMatch(s2, re15), ["", null]); |
| 125 shouldBe(firstMatch(s3, re15), ["", null]); |
| 126 |
| 127 // Non-capturing empty first alternative greedy '?' |
| 128 var re16 = new RegExp(r"(?:|a|z)?"); |
| 129 shouldBe(firstMatch(emptyStr, re16), [""]); |
| 130 shouldBe(firstMatch(s1, re16), [""]); |
| 131 shouldBe(firstMatch(s2, re16), ["a"]); |
| 132 shouldBe(firstMatch(s3, re16), ["a"]); |
| 133 |
| 134 // Non-capturing empty middle alternative greedy '?' |
| 135 var re17 = new RegExp(r"(?:a||z)?"); |
| 136 shouldBe(firstMatch(emptyStr, re17), [""]); |
| 137 shouldBe(firstMatch(s1, re17), [""]); |
| 138 shouldBe(firstMatch(s2, re17), ["a"]); |
| 139 shouldBe(firstMatch(s3, re17), ["a"]); |
| 140 |
| 141 // Non-capturing empty last alternative greedy '?' |
| 142 var re18 = new RegExp(r"(?:a|z|)?"); |
| 143 shouldBe(firstMatch(emptyStr, re18), [""]); |
| 144 shouldBe(firstMatch(s1, re18), [""]); |
| 145 shouldBe(firstMatch(s2, re18), ["a"]); |
| 146 shouldBe(firstMatch(s3, re18), ["a"]); |
| 147 |
| 148 // Capturing empty first alternative greedy '?' |
| 149 var re19 = new RegExp(r"(|a|z)?"); |
| 150 shouldBe(firstMatch(emptyStr, re19), ["", null]); |
| 151 shouldBe(firstMatch(s1, re19), ["", null]); |
| 152 shouldBe(firstMatch(s2, re19), ["a", "a"]); |
| 153 shouldBe(firstMatch(s3, re19), ["a", "a"]); |
| 154 |
| 155 // Capturing empty middle alternative greedy '?' |
| 156 var re20 = new RegExp(r"(a||z)?"); |
| 157 shouldBe(firstMatch(emptyStr, re20), ["", null]); |
| 158 shouldBe(firstMatch(s1, re20), ["", null]); |
| 159 shouldBe(firstMatch(s2, re20), ["a", "a"]); |
| 160 shouldBe(firstMatch(s3, re20), ["a", "a"]); |
| 161 |
| 162 // Capturing empty last alternative greedy '?' |
| 163 var re21 = new RegExp(r"(a|z|)?"); |
| 164 shouldBe(firstMatch(emptyStr, re21), ["", null]); |
| 165 shouldBe(firstMatch(s1, re21), ["", null]); |
| 166 shouldBe(firstMatch(s2, re21), ["a", "a"]); |
| 167 shouldBe(firstMatch(s3, re21), ["a", "a"]); |
| 168 |
| 169 // Non-capturing empty first alternative non-greedy '?' |
| 170 var re22 = new RegExp(r"(?:|a|z)??"); |
| 171 shouldBe(firstMatch(emptyStr, re22), [""]); |
| 172 shouldBe(firstMatch(s1, re22), [""]); |
| 173 shouldBe(firstMatch(s2, re22), [""]); |
| 174 shouldBe(firstMatch(s3, re22), [""]); |
| 175 |
| 176 // Non-capturing empty middle alternative non-greedy '?' |
| 177 var re23 = new RegExp(r"(?:a||z)??"); |
| 178 shouldBe(firstMatch(emptyStr, re23), [""]); |
| 179 shouldBe(firstMatch(s1, re23), [""]); |
| 180 shouldBe(firstMatch(s2, re23), [""]); |
| 181 shouldBe(firstMatch(s3, re23), [""]); |
| 182 |
| 183 // Non-capturing empty last alternative non-greedy '?' |
| 184 var re24 = new RegExp(r"(?:a|z|)??"); |
| 185 shouldBe(firstMatch(emptyStr, re24), [""]); |
| 186 shouldBe(firstMatch(s1, re24), [""]); |
| 187 shouldBe(firstMatch(s2, re24), [""]); |
| 188 shouldBe(firstMatch(s3, re24), [""]); |
| 189 |
| 190 // Capturing empty first alternative non-greedy '?' |
| 191 var re25 = new RegExp(r"(|a|z)??"); |
| 192 shouldBe(firstMatch(emptyStr, re25), ["", null]); |
| 193 shouldBe(firstMatch(s1, re25), ["", null]); |
| 194 shouldBe(firstMatch(s2, re25), ["", null]); |
| 195 shouldBe(firstMatch(s3, re25), ["", null]); |
| 196 |
| 197 // Capturing empty middle alternative non-greedy '?' |
| 198 var re26 = new RegExp(r"(a||z)??"); |
| 199 shouldBe(firstMatch(emptyStr, re26), ["", null]); |
| 200 shouldBe(firstMatch(s1, re26), ["", null]); |
| 201 shouldBe(firstMatch(s2, re26), ["", null]); |
| 202 shouldBe(firstMatch(s3, re26), ["", null]); |
| 203 |
| 204 // Capturing empty last alternative non-greedy '?' |
| 205 var re27 = new RegExp(r"(a|z|)??"); |
| 206 shouldBe(firstMatch(emptyStr, re27), ["", null]); |
| 207 shouldBe(firstMatch(s1, re27), ["", null]); |
| 208 shouldBe(firstMatch(s2, re27), ["", null]); |
| 209 shouldBe(firstMatch(s3, re27), ["", null]); |
| 210 |
| 211 // Non-capturing empty first alternative greedy '*' non-terminal |
| 212 var re28 = new RegExp(r"(?:|a|z)*x"); |
| 213 shouldBe(firstMatch(emptyStr, re28), null); |
| 214 shouldBe(firstMatch(s1, re28), ["x"]); |
| 215 shouldBe(firstMatch(s2, re28), null); |
| 216 shouldBe(firstMatch(s3, re28), ["aax"]); |
| 217 |
| 218 // Non-capturing empty middle alternative greedy '*' non-terminal |
| 219 var re29 = new RegExp(r"(?:a||z)*x"); |
| 220 shouldBe(firstMatch(emptyStr, re29), null); |
| 221 shouldBe(firstMatch(s1, re29), ["x"]); |
| 222 shouldBe(firstMatch(s2, re29), null); |
| 223 shouldBe(firstMatch(s3, re29), ["aax"]); |
| 224 |
| 225 // Non-capturing empty last alternative greedy '*' non-terminal |
| 226 var re30 = new RegExp(r"(?:a|z|)*x"); |
| 227 shouldBe(firstMatch(emptyStr, re30), null); |
| 228 shouldBe(firstMatch(s1, re30), ["x"]); |
| 229 shouldBe(firstMatch(s2, re30), null); |
| 230 shouldBe(firstMatch(s3, re30), ["aax"]); |
| 231 |
| 232 // Non-capturing two possibly empty alternatives greedy '*' |
| 233 var re31 = new RegExp(r"(?:a*|b*)*"); |
| 234 shouldBe(firstMatch(emptyStr, re31), [""]); |
| 235 shouldBe(firstMatch(s1, re31), [""]); |
| 236 shouldBe(firstMatch(s3, re31), ["aa"]); |
| 237 shouldBe(firstMatch(s4, re31), ["abab"]); |
| 238 |
| 239 // Non-capturing two possibly empty non-greedy alternatives non-greedy '*' |
| 240 var re32 = new RegExp(r"(?:a*?|b*?)*"); |
| 241 shouldBe(firstMatch(emptyStr, re32), [""]); |
| 242 shouldBe(firstMatch(s1, re32), [""]); |
| 243 shouldBe(firstMatch(s2, re32), ["aaaa"]); |
| 244 shouldBe(firstMatch(s4, re32), ["abab"]); |
| 245 shouldBe(firstMatch(s5, re32), ["ab"]); |
| 246 shouldBe(firstMatch(s6, re32), [""]); |
| 247 |
| 248 // Three possibly empty alternatives with greedy + |
| 249 var re33 = new RegExp(r"(?:(?:(?!))|g?|0*\*?)+"); |
| 250 shouldBe(firstMatch(emptyStr, re33), [""]); |
| 251 shouldBe(firstMatch(s1, re33), [""]); |
| 252 shouldBe(firstMatch(s7, re33), ["g0"]); |
| 253 |
| 254 // first alternative zero length fixed count |
| 255 var re34 = new RegExp(r"(?:|a)"); |
| 256 shouldBe(firstMatch(emptyStr, re34), [""]); |
| 257 shouldBe(firstMatch(s1, re34), [""]); |
| 258 shouldBe(firstMatch(s2, re34), [""]); |
| 259 shouldBe(firstMatch(s3, re34), [""]); |
| 260 } |
OLD | NEW |