提交 4f7af32c 编写于 作者: M mahaifeng

[regexp]去除文档中手动生成的代码,添加注释

上级 5d05f53b
import { describe, test, expect, Result } from './tests.uts' import { describe, test, expect, Result } from './tests.uts'
export function testRegExp(): Result { export function testRegExp() : Result {
return describe("RegExp", () => { return describe("RegExp", () => {
test("constructor", () => { test("constructor", () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
...@@ -20,85 +20,189 @@ export function testRegExp(): Result { ...@@ -20,85 +20,189 @@ export function testRegExp(): Result {
}) })
test("dotAll", () => { test("dotAll", () => {
// #TEST RegExp.dotAll
const regex1 = new RegExp('foo', 's'); const regex1 = new RegExp('foo', 's');
expect(regex1.dotAll).toEqual(true); console.log(regex1.dotAll);
// expected output: true
const regex2 = new RegExp('bar'); const regex2 = new RegExp('bar');
console.log(regex2.dotAll);
// expected output: false
// #END
expect(regex1.dotAll).toEqual(true);
expect(regex2.dotAll).toEqual(false); expect(regex2.dotAll).toEqual(false);
}) });
test("flags", () => { test("flags", () => {
// #TEST RegExp.flags
const regex1 = new RegExp('foo', 'ig'); const regex1 = new RegExp('foo', 'ig');
expect(regex1.flags).toEqual("gi"); console.log(regex1.flags);
// expected output: "gi"
const regex2 = new RegExp('bar', 'myu'); const regex2 = new RegExp('bar', 'myu');
expect(regex2.flags).toEqual("muy"); console.log(regex2.flags);
// expected output: "muy"
const regex3 = new RegExp('bar'); 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(""); expect(regex3.flags).toEqual("");
}) });
test("global", () => { test("global", () => {
// #TEST RegExp.global
const regex1 = new RegExp('foo', 'g'); const regex1 = new RegExp('foo', 'g');
expect(regex1.global).toEqual(true); console.log(regex1.global);
// expected output: true
const regex2 = new RegExp('bar'); const regex2 = new RegExp('bar');
console.log(regex2.global);
// expected output: false
// #END
expect(regex1.global).toEqual(true);
expect(regex2.global).toEqual(false); expect(regex2.global).toEqual(false);
}) });
test("hasIndices", () => { test("hasIndices", () => {
// #TEST RegExp.hasIndices
const regex1 = new RegExp('foo', 'd'); const regex1 = new RegExp('foo', 'd');
expect(regex1.hasIndices).toEqual(true); console.log(regex1.hasIndices);
// expected output: true
const regex2 = new RegExp('bar'); const regex2 = new RegExp('bar');
console.log(regex2.hasIndices);
// expected output: false
// #END
expect(regex1.hasIndices).toEqual(true);
expect(regex2.hasIndices).toEqual(false); expect(regex2.hasIndices).toEqual(false);
}) });
test("ignoreCase", () => { test("ignoreCase", () => {
// #TEST RegExp.ignoreCase
const regex1 = new RegExp('foo', 'i'); const regex1 = new RegExp('foo', 'i');
expect(regex1.ignoreCase).toEqual(true); console.log(regex1.ignoreCase);
// expected output: true
const regex2 = new RegExp('bar'); const regex2 = new RegExp('bar');
console.log(regex2.ignoreCase);
// expected output: false
// #END
expect(regex1.ignoreCase).toEqual(true);
expect(regex2.ignoreCase).toEqual(false); expect(regex2.ignoreCase).toEqual(false);
}) });
test("RegExpExecArray", () => { test("RegExpExecArray", () => {
// #ifdef APP-ANDROID // #ifdef APP-ANDROID
const regex1 = RegExp('foo*', 'g'); const regex1 = RegExp('foo*', 'g');
const str1 = 'table football, foosball'; const str1 = 'table football, foosball';
let array1:RegExpExecArray = regex1.exec(str1)!; let array1 : RegExpExecArray = regex1.exec(str1)!;
console.log(array1[0] == 'foo') console.log(array1[0] == 'foo')
expect(array1[0] == 'foo').toEqual(true); expect(array1[0] == 'foo').toEqual(true);
// #endif // #endif
}) })
test("lastIndex", () => { test("lastIndex", () => {
// #TEST RegExp.lastIndex
const regex = /ab/g; const regex = /ab/g;
const str = 'ab ab ab'; const str = 'ab ab ab';
const result1 = regex.exec(str)!; const result1 = regex.exec(str)!;
console.log(result1.index);
console.log(regex.lastIndex);
// expected output:
// result1.index: 0
// regex.lastIndex: 2
const result2 = regex.exec(str)!;
console.log(result2.index);
console.log(regex.lastIndex);
// expected output:
// result2.index: 3
// regex.lastIndex: 5
const result3 = regex.exec(str)!;
console.log(result3.index);
console.log(regex.lastIndex);
// expected output:
// result3.index: 6
// regex.lastIndex: 8
// #END
expect(result1.index).toEqual(0); expect(result1.index).toEqual(0);
expect(regex.lastIndex).toEqual(2); expect(regex.lastIndex).toEqual(2);
const result2 = regex.exec(str)!;
expect(result2.index).toEqual(3); expect(result2.index).toEqual(3);
expect(regex.lastIndex).toEqual(5); expect(regex.lastIndex).toEqual(5);
const result3 = regex.exec(str)!;
expect(result3.index).toEqual(6); expect(result3.index).toEqual(6);
expect(regex.lastIndex).toEqual(8); expect(regex.lastIndex).toEqual(8);
}) });
test("multiline", () => { test("multiline", () => {
// #TEST RegExp.multiline
const regex1 = new RegExp('foo', 'm'); const regex1 = new RegExp('foo', 'm');
expect(regex1.multiline).toEqual(true); console.log(regex1.multiline);
// expected output: true
const regex2 = new RegExp('bar'); const regex2 = new RegExp('bar');
console.log(regex2.multiline);
// expected output: false
// #END
expect(regex1.multiline).toEqual(true);
expect(regex2.multiline).toEqual(false); expect(regex2.multiline).toEqual(false);
}) });
test("source", () => { test("source", () => {
// #TEST RegExp.source
const regex1 = new RegExp('foo', 'ig'); const regex1 = new RegExp('foo', 'ig');
expect(regex1.source).toEqual("foo"); console.log(regex1.source);
// expected output: "foo"
const regex2 = new RegExp('bar'); const regex2 = new RegExp('bar');
expect(regex2.source).toEqual("bar"); console.log(regex2.source);
// expected output: "bar"
const regex3 = /\w+/gi; 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(regex3.source).toEqual("\\w+");
const regex4 = new RegExp('\\d+', 'ig');;
expect(regex4.source).toEqual("\\d+"); expect(regex4.source).toEqual("\\d+");
}) });
test("sticky", () => { test("sticky", () => {
// #TEST RegExp.sticky
const str1 = 'table football'; const str1 = 'table football';
const regex1 = new RegExp('foo', 'y'); const regex1 = new RegExp('foo', 'y');
regex1.lastIndex = 6; regex1.lastIndex = 6;
console.log(regex1.sticky);
// expected output: true
console.log(regex1.test(str1));
// expected output: true (the match starts at index 6)
// Reset the lastIndex to test non-match
regex1.lastIndex = 0;
console.log(regex1.test(str1));
// expected output: false (subsequent tests should fail since the lastIndex is at 0)
// #END
expect(regex1.sticky).toEqual(true); expect(regex1.sticky).toEqual(true);
expect(regex1.test(str1)).toEqual(true); expect(regex1.test(str1)).toEqual(true);
expect(regex1.test(str1)).toEqual(false); expect(regex1.test(str1)).toEqual(false);
}) });
// android 暂时不支持 unicode // android 暂时不支持 unicode
// test("unicode", () => { // test("unicode", () => {
// const regex1 = new RegExp('foo', 'u'); // const regex1 = new RegExp('foo', 'u');
...@@ -108,9 +212,28 @@ export function testRegExp(): Result { ...@@ -108,9 +212,28 @@ export function testRegExp(): Result {
// }) // })
test('test', () => { test('test', () => {
// #TEST RegExp.test
const str = 'table football'; const str = 'table football';
const regex = new RegExp('foo*'); const regex = new RegExp('foo*');
const globalRegex = new RegExp('foo*', 'g'); 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(regex.test(str)).toEqual(true);
expect(globalRegex.lastIndex).toEqual(0); expect(globalRegex.lastIndex).toEqual(0);
expect(globalRegex.test(str)).toEqual(true); expect(globalRegex.test(str)).toEqual(true);
...@@ -178,7 +301,7 @@ export function testRegExp(): Result { ...@@ -178,7 +301,7 @@ export function testRegExp(): Result {
// const re11 = /(?=a)?b/; // const re11 = /(?=a)?b/;
// expect(re11.test("b")).toEqual(true); // expect(re11.test("b")).toEqual(true);
const isValidIdentifierName = (str: string): boolean => { const isValidIdentifierName = (str : string) : boolean => {
const reg = const reg =
/^(?!(?:break|case|catch)$)[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u; /^(?!(?:break|case|catch)$)[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*$/u;
return reg.test(str); return reg.test(str);
...@@ -187,7 +310,7 @@ export function testRegExp(): Result { ...@@ -187,7 +310,7 @@ export function testRegExp(): Result {
expect(isValidIdentifierName("foo")).toEqual(true); expect(isValidIdentifierName("foo")).toEqual(true);
expect(isValidIdentifierName("cases")).toEqual(true); expect(isValidIdentifierName("cases")).toEqual(true);
const isASCIIIDPart = (char: string): boolean => { const isASCIIIDPart = (char : string) : boolean => {
return /^(?=\p{ASCII}$)\p{ID_Start}$/u.test(char); return /^(?=\p{ASCII}$)\p{ID_Start}$/u.test(char);
} }
expect(isASCIIIDPart("a")).toEqual(true); expect(isASCIIIDPart("a")).toEqual(true);
...@@ -199,8 +322,13 @@ export function testRegExp(): Result { ...@@ -199,8 +322,13 @@ export function testRegExp(): Result {
const result = pattern.exec('hi there'); const result = pattern.exec('hi there');
expect(result).toEqual(null); expect(result).toEqual(null);
// #TEST RegExp.exec
const pattern1 = new RegExp('hello'); const pattern1 = new RegExp('hello');
const result1 = pattern1.exec('hello world')!; const result1 = pattern1.exec('hello world')!;
console.log(result1[0]) //'hello'
console.log(result1.index) //0
// #END
expect(result1[0]).toEqual('hello'); expect(result1[0]).toEqual('hello');
expect(result1.index).toEqual(0); expect(result1.index).toEqual(0);
expect(result1.input).toEqual("hello world"); expect(result1.input).toEqual("hello world");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册