RegExp.uts 12.7 KB
Newer Older
Y
yurj26 已提交
1 2
import { describe, test, expect, Result } from './tests.uts'

3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
export function testRegExp() : Result {
  return describe("RegExp", () => {
    test("constructor", () => {
      // #ifdef APP-ANDROID
      const str = 'table football';
      const regex = new RegExp('foo*');
      const globalRegex = new RegExp();

      expect(regex.test(str)).toEqual(true);
      expect(globalRegex.test(str)).toEqual(true);

      let r2 = new RegExp(/ab+c/);
      expect(r2.test(str)).toEqual(false);

      let r3 = new RegExp(/foo/);
      expect(r3.test(str)).toEqual(true);
      // #endif
    })

    test("dotAll", () => {
      // #TEST RegExp.dotAll
      const regex1 = new RegExp('foo', 's');
      console.log(regex1.dotAll);
      // expected output: true

      const regex2 = new RegExp('bar');
      console.log(regex2.dotAll);
      // expected output: false
      // #END
      expect(regex1.dotAll).toEqual(true);
      expect(regex2.dotAll).toEqual(false);
    });

    test("flags", () => {
      // #TEST RegExp.flags
      const regex1 = new RegExp('foo', 'ig');
      console.log(regex1.flags);
      // expected output: "gi"

      const regex2 = new RegExp('bar', 'myu');
      console.log(regex2.flags);
      // expected output: "muy"

      const regex3 = new RegExp('bar');
      console.log(regex3.flags);
      // expected output: ""
      // #END
      expect(regex1.flags).toEqual("gi");
      expect(regex2.flags).toEqual("muy");
      expect(regex3.flags).toEqual("");
    });

    test("global", () => {
      // #TEST RegExp.global
      const regex1 = new RegExp('foo', 'g');
      console.log(regex1.global);
      // expected output: true

      const regex2 = new RegExp('bar');
      console.log(regex2.global);
      // expected output: false
      // #END
      expect(regex1.global).toEqual(true);
      expect(regex2.global).toEqual(false);
    });

    test("hasIndices", () => {
      // #TEST RegExp.hasIndices
      const regex1 = new RegExp('foo', 'd');
      console.log(regex1.hasIndices);
      // expected output: true

      const regex2 = new RegExp('bar');
      console.log(regex2.hasIndices);
      // expected output: false
      // #END
      expect(regex1.hasIndices).toEqual(true);
      expect(regex2.hasIndices).toEqual(false);
    });

    test("ignoreCase", () => {
      // #TEST RegExp.ignoreCase
      const regex1 = new RegExp('foo', 'i');
      console.log(regex1.ignoreCase);
      // expected output: true

      const regex2 = new RegExp('bar');
      console.log(regex2.ignoreCase);
      // expected output: false
      // #END
      expect(regex1.ignoreCase).toEqual(true);
      expect(regex2.ignoreCase).toEqual(false);
    });

    test("RegExpExecArray", () => {
      // #ifdef APP-ANDROID
      const regex1 = RegExp('foo*', 'g');
      const str1 = 'table football, foosball';
      let array1 : RegExpExecArray = regex1.exec(str1)!;
      console.log(array1[0] == 'foo')
      expect(array1[0] == 'foo').toEqual(true);
      // #endif
    })
    test("lastIndex", () => {
      // #TEST RegExp.lastIndex
      const regex = /ab/g;
      const str = 'ab ab ab';

      const result1 = regex.exec(str)!;
      console.log(result1.index);
113 114
      const ret1 = regex.lastIndex
      console.log(ret1);
115 116 117 118 119 120
      // expected output: 
      // result1.index: 0
      // regex.lastIndex: 2

      const result2 = regex.exec(str)!;
      console.log(result2.index);
121 122
      let ret2 = regex.lastIndex
      console.log(ret2);
123 124 125 126 127 128
      // expected output: 
      // result2.index: 3
      // regex.lastIndex: 5

      const result3 = regex.exec(str)!;
      console.log(result3.index);
129 130
      let ret3 = regex.lastIndex
      console.log(ret3);
131 132 133 134 135 136
      // expected output: 
      // result3.index: 6
      // regex.lastIndex: 8

      // #END
      expect(result1.index).toEqual(0);
137
      expect(ret1).toEqual(2);
138 139

      expect(result2.index).toEqual(3);
140
      expect(ret2).toEqual(5);
141 142

      expect(result3.index).toEqual(6);
143
      expect(ret3).toEqual(8);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    });

    test("multiline", () => {
      // #TEST RegExp.multiline
      const regex1 = new RegExp('foo', 'm');
      console.log(regex1.multiline);
      // expected output: true

      const regex2 = new RegExp('bar');
      console.log(regex2.multiline);
      // expected output: false
      // #END

      expect(regex1.multiline).toEqual(true);
      expect(regex2.multiline).toEqual(false);
    });

    test("source", () => {
      // #TEST RegExp.source
      const regex1 = new RegExp('foo', 'ig');
      console.log(regex1.source);
      // expected output: "foo"

      const regex2 = new RegExp('bar');
      console.log(regex2.source);
      // expected output: "bar"

      const regex3 = /\w+/gi;
      console.log(regex3.source);
      // expected output: "\\w+"

      const regex4 = new RegExp('\\d+', 'ig');
      console.log(regex4.source);
      // expected output: "\\d+"
      // #END

      expect(regex1.source).toEqual("foo");
      expect(regex2.source).toEqual("bar");
      expect(regex3.source).toEqual("\\w+");
      expect(regex4.source).toEqual("\\d+");
    });

    test("sticky", () => {
      // #TEST RegExp.sticky
      const str1 = 'table football';
      const regex1 = new RegExp('foo', 'y');
      regex1.lastIndex = 6;

      console.log(regex1.sticky);
      // expected output: true
194 195 196
      let ret = regex1.test(str1)
      console.log(ret);
      // expected output: true 
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

      regex1.lastIndex = 0;
      console.log(regex1.test(str1));
      // #END

      expect(regex1.sticky).toEqual(true);
      expect(regex1.test(str1)).toEqual(false);
    });

    // android 暂时不支持 unicode
    // test("unicode", () => {
    //     const regex1 = new RegExp('foo', 'u');
    //     expect(regex1.unicode).toEqual(true);
    //     const regex2 = new RegExp('bar');
    //     expect(regex2.unicode).toEqual(false);
    // })

    test('test', () => {
      // #TEST RegExp.test
      const str = 'table football';
      const regex = new RegExp('foo*');
      const globalRegex = new RegExp('foo*', 'g');

      console.log(regex.test(str));
      // expected output: true

      console.log(globalRegex.lastIndex);
      // expected output: 0

      console.log(globalRegex.test(str));
      // expected output: true

      console.log(globalRegex.lastIndex);
      // expected output: 9

      console.log(globalRegex.test(str));
      // expected output: false

      // #END

      expect(regex.test(str)).toEqual(true);
      expect(globalRegex.lastIndex).toEqual(0);
      expect(globalRegex.test(str)).toEqual(true);
      expect(globalRegex.lastIndex).toEqual(9);
      expect(globalRegex.test(str)).toEqual(false);

      const re = new RegExp('hello', 'g');
      const str1 = 'hello world';
      const str2 = 'hello there';
      expect(re.test(str1)).toEqual(true);
      expect(re.lastIndex).toEqual(5);
      expect(re.test(str2)).toEqual(false);
      expect(re.lastIndex).toEqual(0);

      const re1 = new RegExp('(\\d{3})-(\\d{2})-(\\d{4})');
      expect(re1.test('123-45-6789')).toEqual(true);
      expect(re1.test('123-45-678')).toEqual(false);

      const re2 = /^(http|https):\/\/[\w\-]+(\.[\w\-]+)*(:\d+)?(\/[\w\-./?%&=]*)?$/;
      expect(re2.test('https://www.example.com')).toEqual(true);
      expect(re2.test('http://127.0.0.1:8080')).toEqual(true);
      expect(re2.test('www.example.com')).toEqual(false);
      expect(re2.test('https:/www.example.com')).toEqual(false);
      expect(re2.test('https:/www.example.com@foo')).toEqual(false);
      expect(re2.test('123')).toEqual(false);

      const re3 = /^1[3-9]\d{9}$/;
      expect(re3.test('13912345678')).toEqual(true);
      expect(re3.test('15812345678')).toEqual(true);
      expect(re3.test('12912345678')).toEqual(false);
      expect(re3.test('1391234')).toEqual(false);
      expect(re3.test('1581234567a')).toEqual(false);

      const re4 = /[\u4E00-\u9FA5]/;
      expect(re4.test('测试')).toEqual(true);
      expect(re4.test('123')).toEqual(false);
      expect(re4.test('abc')).toEqual(false);

      const re5 = /^#([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$/;
      expect(re5.test('#b8b8b8')).toEqual(true);
      expect(re5.test('#b8b')).toEqual(true);
      expect(re5.test('#b8b8b81')).toEqual(false);
      expect(re5.test('#b8b8b')).toEqual(false);
      expect(re5.test('b8b8b')).toEqual(false);

      const re6 = new RegExp('hello', 'i');
      expect(re6.test('Hello')).toEqual(true);

      const re7 = new RegExp('^Hello', 'm');
      expect(re7.test('Hello\nWorld')).toEqual(true);

      // const re8 = new RegExp('\\u{1F602}', 'u');
      // expect(re8.test('😂')).toEqual(true);
      // expect(re8.test('😊')).toEqual(false);

      const re9 = new RegExp('a\\d', 'y');
      expect(re9.test('a1a2a3')).toEqual(true);
      expect(re9.test('a1a2a3')).toEqual(true);
      expect(re9.test('a1a2a3')).toEqual(true);
      expect(re9.test('a1a2a3')).toEqual(false);

      const re10 = new RegExp('^foo.bar$', 's');
      expect(re10.test('foo\nbar')).toEqual(true);

      // const re11 = /(?=a)?b/;
      // expect(re11.test("b")).toEqual(true);

      const isValidIdentifierName = (str : string) : boolean => {
        const reg =
          /^(?!(?:break|case|catch)$)[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u;
        return reg.test(str);
      }
      expect(isValidIdentifierName("break")).toEqual(false);
      expect(isValidIdentifierName("foo")).toEqual(true);
      expect(isValidIdentifierName("cases")).toEqual(true);

      const isASCIIIDPart = (char : string) : boolean => {
        return /^(?=\p{ASCII}$)\p{ID_Start}$/u.test(char);
      }
      expect(isASCIIIDPart("a")).toEqual(true);
      expect(isASCIIIDPart("α")).toEqual(false);
      expect(isASCIIIDPart(":")).toEqual(false);
    })
    test('exec', () => {
      const pattern = new RegExp('hello');
      const result = pattern.exec('hi there');
      expect(result).toEqual(null);

      // #TEST RegExp.exec
      const pattern1 = new RegExp('hello');
      const result1 = pattern1.exec('hello world')!;
      console.log(result1[0]) //'hello'
      console.log(result1.index) //0
      // #END

      expect(result1[0]).toEqual('hello');
      expect(result1.index).toEqual(0);
      expect(result1.input).toEqual("hello world");

      const pattern2 = /quick\s(?<color>brown).+?(jumps)/igd;
      const result2 = pattern2.exec('The Quick Brown Fox Jumps Over The Lazy Dog')!;
      expect(result2[0]).toEqual("Quick Brown Fox Jumps");
      expect(result2[1]).toEqual("Brown");
      expect(result2[2]).toEqual("Jumps");
      expect(result2.index).toEqual(4);
      expect(result2.input).toEqual("The Quick Brown Fox Jumps Over The Lazy Dog");
      // expect(result2.groups).toEqual({color: "Brown"});

      const pattern3 = /(\d{4})-(\d{2})-(\d{2})/g;
      const result3 = pattern3.exec("2023-02-01")!;
      expect(result3[0]).toEqual("2023-02-01");
      expect(result3[1]).toEqual("2023");
      expect(result3[2]).toEqual("02");
      expect(result3[3]).toEqual("01");
      expect(result3.index).toEqual(0);
      expect(result3.input).toEqual("2023-02-01");

      const pattern4 = /(?=(a+))a*b\1/;
      const result4 = pattern4.exec("baabac")!;
      expect(result4[0]).toEqual("aba");
      expect(result4[1]).toEqual("a");
      expect(result4.index).toEqual(2);
      expect(result4.input).toEqual("baabac");

      const pattern5 = /(.*?)a(?!(a+)b\1c)\1(.*)/;
      const result5 = pattern5.exec("baaabaac")!;
      expect(result5[0]).toEqual("baaabaac");
      expect(result5[1]).toEqual("baa");
      expect(result5[3]).toEqual("c");
      expect(result5.index).toEqual(0);

      const pattern6 = /^.*?(?=[,.])/;
      const result6 = pattern6.exec("Hello, world!")!;
      expect(result6[0]).toEqual("Hello");

      const pattern7 = /\d{4}-(?=\d{2}-\d{2})\d{2}-\d{2}/;
      const result7 = pattern7.exec('2023/05-26');
      expect(result7).toEqual(null);


      const pattern8 = /https:\/\/(?!\w+\.(com|net))\w+\.\w+/;
      const result8 = pattern8.exec('https://example.org')!;
      expect(result8[0]).toEqual('https://example.org');

      // #ifndef WEB
      // 目前的测试环境safari版本为15,不支持?<
      const pattern9 = /(?<=@)\w+/;
      const result9 = pattern9.exec('Email: john@example.com')!;
      expect(result9[0]).toEqual('example');

      const pattern10 = /(?<!@)\w+/;
      const result10 = pattern10.exec('Username: john')!;
      expect(result10[0]).toEqual('Username');
      // #endif


      const CHUNK_REGEXP =
        /^(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?\s*(\S*)?\s*(\d*\.?\d+(?:ms|s)?)?$/
      const match2 = CHUNK_REGEXP.exec('none')
      expect(match2![0]).toEqual("none");
      expect(match2![1]).toEqual("none");
      // expect(JSON.stringify(match2![2])).toEqual("null");
      // expect(JSON.stringify(match2![3])).toEqual("null");
      // expect(JSON.stringify(match2![4])).toEqual("null");

Y
yurj26 已提交
402
    })
403 404
  })
}