Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
Hello UTS
提交
e3a4d4f4
H
Hello UTS
项目概览
DCloud
/
Hello UTS
通知
1595
Star
27
Fork
9
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
2
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
Hello UTS
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
2
Issue
2
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
e3a4d4f4
编写于
9月 05, 2024
作者:
M
mahaifeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[string]去除文档中手动生成的代码,添加注释
上级
585ce1ac
变更
1
显示空白变更内容
内联
并排
Showing
1 changed file
with
603 addition
and
409 deletion
+603
-409
uni_modules/uts-tests/utssdk/String.uts
uni_modules/uts-tests/utssdk/String.uts
+603
-409
未找到文件。
uni_modules/uts-tests/utssdk/String.uts
浏览文件 @
e3a4d4f4
import { describe, test, expect, Result } from './tests.uts'
export function testString(): Result {
export function testString()
: Result {
return describe("String", () => {
test('length', () => {
// #TEST String.length
const x = "Mozilla";
const e = "";
console.log("Mozilla is " + x.length + " code units long");
/* "Mozilla is 7 code units long" */
console.log("The empty string is has a length of " + e.length);
/* "The e string is has a length of 0" */
// #END
expect(x.length).toEqual(7);
// expect(x[0]).toEqual('M');
const empty = "";
...
...
@@ -24,8 +34,17 @@ export function testString(): Result {
// expect(str.length).toEqual(11);
})
test('at', () => {
// #TEST String.at
const sentence = 'The quick brown fox jumps over the lazy dog.';
let index = 5;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of 5 the character returned is u"
index = -4;
console.log(`Using an index of ${index} the character returned is ${sentence.at(index)}`);
// expected output: "Using an index of -4 the character returned is d"
// #END
index = 5;
expect(sentence.at(index)).toEqual("u");
index = -4;
expect(sentence.at(index)).toEqual("d");
...
...
@@ -41,7 +60,24 @@ export function testString(): Result {
expect(empty.at(0)).toEqual(null);
})
test('charAt', () => {
// #TEST String.charAt
const anyString = "Brave new world";
console.log("The character at index 0 is '" + anyString.charAt(0) + "'");
// The character at index 0 is 'B'
console.log("The character at index 1 is '" + anyString.charAt(1) + "'");
// The character at index 1 is 'r'
console.log("The character at index 2 is '" + anyString.charAt(2) + "'");
// The character at index 2 is 'a'
console.log("The character at index 3 is '" + anyString.charAt(3) + "'");
// The character at index 3 is 'v'
console.log("The character at index 4 is '" + anyString.charAt(4) + "'");
// The character at index 4 is 'e'
console.log("The character at index 999 is '" + anyString.charAt(999) + "'");
// The character at index 999 is ''
// #END
expect(anyString.charAt(0)).toEqual("B");
expect(anyString.charAt(1)).toEqual("r");
expect(anyString.charAt(2)).toEqual("a");
...
...
@@ -57,7 +93,12 @@ export function testString(): Result {
})
test('toWellFormed', () => {
// #ifdef APP-ANDROID
expect("ab\uD800".toWellFormed()).toEqual("ab\uFFFD");
// #TEST String.toWellFormed
let ret = "ab\uD800".toWellFormed()
console.log(ret) //"ab\uFFFD"
// #END
expect(ret).toEqual("ab\uFFFD");
expect("ab\uD800c".toWellFormed()).toEqual("ab\uFFFDc");
expect("\uDFFFab".toWellFormed()).toEqual("\uFFFDab");
expect("c\uDFFFab".toWellFormed()).toEqual("c\uFFFDab");
...
...
@@ -69,7 +110,12 @@ export function testString(): Result {
})
test('isWellFormed', () => {
// #ifdef APP-ANDROID
expect("ab\uD800".isWellFormed()).toEqual(false);
// #TEST String.isWellFormed
let ret = "ab\uD800".isWellFormed()
console.log(ret) //false
// #END
expect(ret).toEqual(false);
expect("ab\uD800c".isWellFormed()).toEqual(false);
expect("\uDFFFab".isWellFormed()).toEqual(false);
expect("c\uDFFFab".isWellFormed()).toEqual(false);
...
...
@@ -80,8 +126,13 @@ export function testString(): Result {
// #endif
})
test('charCodeAt', () => {
// #TEST String.charCodeAt
const sentence = 'The quick brown fox jumps over the lazy dog.';
const index = 4;
console.log(`The character code ${sentence.charCodeAt(index)} is equal to ${sentence.charAt(index)}`);
// expected output: "The character code 113 is equal to q"
// #END
expect(sentence.charCodeAt(index)).toEqual(113);
expect("ABC".charCodeAt(0)).toEqual(65);
...
...
@@ -93,27 +144,53 @@ export function testString(): Result {
expect(empty.charCodeAt(0)).toEqual(NaN);
})
test('fromCharCode', () => {
// #TEST String.fromCharCode
console.log(String.fromCharCode(65, 66, 67));
// expected output: "ABC"
console.log(String.fromCharCode(0x12014));
// expected output: "𝌆a𝌇"
// #END
expect(String.fromCharCode(65, 66, 67)).toEqual("ABC");
expect(String.fromCharCode(0x12014)).toEqual("—");
expect(String.fromCharCode(0xd834, 0xdf06, 0x61, 0xd834, 0xdf07)).toEqual("𝌆a𝌇");
})
test('concat', () => {
// #TEST String.concat
let hello = 'Hello, '
expect(hello.concat('Kevin', '. Have a nice day.')).toEqual("Hello, Kevin. Have a nice day.");
let ret1 = hello.concat('Kevin', '. Have a nice day.')
console.log(ret1)
// Hello, Kevin. Have a nice day.
// #END
expect(ret1).toEqual("Hello, Kevin. Have a nice day.");
expect("".concat('abc')).toEqual("abc");
})
test('endsWith', () => {
// #TEST String.endsWith
const str1 = 'Cats are the best!';
console.log(str1.endsWith('best!'));
// expected output: true
console.log(str1.endsWith('best', 17));
// expected output: true
const str2 = 'Is this a question?';
console.log(str2.endsWith('question'));
// expected output: false
// #END
expect(str1.endsWith('best!')).toEqual(true);
expect(str1.endsWith('best', 17)).toEqual(true);
const str2 = 'Is this a question?';
expect(str2.endsWith('question')).toEqual(false);
expect("".includes("test")).toEqual(false);
})
test('includes', () => {
// #TEST String.includes
const sentence = 'The quick brown fox jumps over the lazy dog.';
const word = 'fox';
console.log(sentence.includes(word)) // true
// #END
expect(sentence.includes(word)).toEqual(true);
expect("Blue Whale".includes("blue")).toEqual(false);
...
...
@@ -122,9 +199,17 @@ export function testString(): Result {
expect("".includes("test")).toEqual(false);
})
test('indexOf', () => {
// #TEST String.indexOf
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const searchTerm = 'dog';
const indexOfFirst = paragraph.indexOf(searchTerm);
console.log(`The index of the first "${searchTerm}" from the beginning is ${indexOfFirst}`);
// expected output: "The index of the first "dog" from the beginning is 40"
console.log(`The index of the 2nd "${searchTerm}" is ${paragraph.indexOf(searchTerm, (indexOfFirst + 1))}`);
// expected output: "The index of the 2nd "dog" is 52"
// #END
expect(indexOfFirst).toEqual(40);
expect(paragraph.indexOf(searchTerm, (indexOfFirst + 1))).toEqual(52);
...
...
@@ -137,8 +222,11 @@ export function testString(): Result {
expect("".indexOf("test")).toEqual(-1);
})
test('match', () => {
// #TEST String.match
const str = 'The quick brown fox jumps over the lazy dog. It barked.';
const result = str.match(new RegExp('[A-Z]', 'g'));
console.log(result![0])//"T"
// #END
// expect(result!.length).toEqual(2);
expect(result![0]).toEqual("T");
expect(result![1]).toEqual("I");
...
...
@@ -171,10 +259,19 @@ export function testString(): Result {
})
test('padEnd', () => {
// #TEST String.padEnd
const str1 = 'Breaded Mushrooms';
expect(str1.padEnd(25, '.')).toEqual("Breaded Mushrooms........");
let ret1= str1.padEnd(25, '.')
console.log(ret1);
// expected output: "Breaded Mushrooms........"
const str2 = '200';
expect(str2.padEnd(5)).toEqual("200 ");
let ret2=str2.padEnd(5)
console.log(ret2);
// expected output: "200 "
// #END
expect(str1.padEnd(25, '.')).toEqual("Breaded Mushrooms........");
expect(ret2).toEqual("200 ");
expect('abc'.padEnd(10)).toEqual("abc ");
expect('abc'.padEnd(10, "foo")).toEqual("abcfoofoof");
...
...
@@ -182,8 +279,14 @@ export function testString(): Result {
expect('abc'.padEnd(1)).toEqual("abc");
})
test('padStart', () => {
// #TEST String.padStart
const str1 = '5';
expect(str1.padStart(2, '0')).toEqual("05");
let ret = str1.padStart(2, '0')
console.log(ret);
// expected output: "05"
// #END
expect(ret).toEqual("05");
expect('abc'.padStart(10)).toEqual(" abc");
expect('abc'.padStart(10, "foo")).toEqual("foofoofabc");
...
...
@@ -193,6 +296,12 @@ export function testString(): Result {
})
test('repeat', () => {
const str1 = 'abc';
// #TEST String.repeat
"abc".repeat(0) // ""
"abc".repeat(1) // "abc"
"abc".repeat(2) // "abcabc"
"abc".repeat(3.5) // "abcabcabc" 参数 count 将会被自动转换成整数。
// #END
expect(str1.repeat(0)).toEqual("");
expect(str1.repeat(1)).toEqual("abc");
expect(str1.repeat(2)).toEqual("abcabc");
...
...
@@ -201,12 +310,22 @@ export function testString(): Result {
expect("".repeat(1)).toEqual("");
})
test('replace', () => {
// #TEST String.replace
const p = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
expect(p.replace('dog', 'monkey')).toEqual("The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?");
let ret1 = p.replace('dog', 'monkey')
console.log(ret1);
// expected output: "The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?"
const regex = /Dog/i;
expect(p.replace(regex, 'ferret')).toEqual("The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?");
let ret2 = p.replace(regex, 'ferret')
console.log(ret2);
// expected output: "The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?"
// #END
expect(ret1).toEqual("The quick brown fox jumps over the lazy monkey. If the dog reacted, was it really lazy?");
expect(ret2).toEqual("The quick brown fox jumps over the lazy ferret. If the dog reacted, was it really lazy?");
const str = 'abc12345#$*%';
const replacer = (match:string, p: string[], offset:number, string:string)
: string => {
const replacer = (match : string, p : string[], offset : number, string : string)
: string => {
// p1 is nondigits, p2 digits, and p3 non-alphanumerics
expect(offset).toEqual(0);
expect(match).toEqual(str);
...
...
@@ -214,6 +333,30 @@ export function testString(): Result {
return p.join(' - ');
}
// #TEST String.replace_1
// 不包含捕捉组的示例
let a = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
let b = a.replace(RegExp("fox"), function (match : string, offset : number, string : string) : string {
console.log("match", match)
console.log("offset", offset)
console.log("string", string)
return "cat"
})
console.log("b:", b)
// 包含一个捕获组的示例。注意,目前android仅支持最多五个捕获组
let a1 = "The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?"
let b1 = a1.replace(RegExp("(fox)"), function (match : string, p1 : string, offset : number, string : string) : string {
console.log("match", match)
console.log("p1", p1)
console.log("offset", offset)
console.log("string", string)
return "cat"
})
console.log("b1", b1)
// #END
// const REGEX_FORMAT = /[YMDHhms]o|\[([^\]]+)]|Y{1,4}|M{1,4}|D{1,2}|d{1,4}|H{1,2}|h{1,2}|a{1,2}|A{1,2}|m{1,2}|s{1,2}|Z{1,2}|SSS/g
// const formatStr = 'This is a [sample] text with [another] capture group.'
// const nextStr = formatStr.replace(REGEX_FORMAT, (match:string, p1: string|null, offset: number, string: string):string =>{
...
...
@@ -228,8 +371,15 @@ export function testString(): Result {
// expect(newString1).toEqual('hello, JavaScript (7)');
})
test('search', () => {
// #TEST String.search
const paragraph = 'The quick brown fox jumps over the lazy dog. If the dog barked, was it really lazy?';
const regex = /[^\w\s]/g;
console.log(paragraph.search(regex));
// expected output: 43
console.log(paragraph[paragraph.search(regex)]);
// expected output: "."
// #END
expect(paragraph.search(regex)).toEqual(43);
var str = "hey JudE";
...
...
@@ -240,8 +390,16 @@ export function testString(): Result {
expect("".search(re2)).toEqual(-1);
})
test('slice', () => {
// #TEST String.slice
const str = 'The quick brown fox jumps over the lazy dog.';
expect(str.slice(31)).toEqual("the lazy dog.");
let ret = str.slice(31)
console.log(ret);
// expected output: "the lazy dog."
console.log(str.slice(4, 19));
// expected output: "quick brown fox"
// #END
expect(ret).toEqual("the lazy dog.");
let str1 = 'The morning is upon us.', // str1 的长��length ��23��
str2 = str1.slice(1, 8),
...
...
@@ -258,14 +416,22 @@ export function testString(): Result {
expect("".slice()).toEqual("");
expect("abcdefg".slice(-1)).toEqual("g");
expect("abcdefg".slice(-1,
-2)).toEqual("");
expect("abcdefg".slice(-1,
-2)).toEqual("");
})
test('split', () => {
// #TEST String.split
const str = 'The quick brown fox jumps over the lazy dog.';
const words = str.split(' ');
expect(words[3]).toEqual("fox");
let ret1 = words[3]
console.log(ret1);
// expected output: "fox"
const chars = str.split('');
console.log(chars[8]);
// expected output: "k"
// #END
expect(ret1).toEqual("fox");
expect(chars[8]).toEqual("k");
var myString = "Hello World. How are you doing?";
...
...
@@ -280,7 +446,7 @@ export function testString(): Result {
var str1 = "Harry Trump ;Fred Barney; Helen Rigby ; Bill Abel ;Chris Hand ";
var re1 = /\s*(?:;|$)\s*/;
expect(str1.split(re1)).toEqual([ "Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", ""
])
expect(str1.split(re1)).toEqual(["Harry Trump", "Fred Barney", "Helen Rigby", "Bill Abel", "Chris Hand", ""
])
var str2 = "a, b, c, d, e";
var re2 = /,\s*/g;
...
...
@@ -295,17 +461,33 @@ export function testString(): Result {
expect(str4.split(re4)).toEqual(["a", " b", " {c, d, e}", " f", " g", " h"]);
})
test('toLowerCase', () => {
const str1 = '中文����zh-CN || zh-Hans';
expect(str1.toLowerCase()).toEqual("中文����zh-cn || zh-hans")
;
// #TEST String.toLowerCase
const str1 = '中文简体 zh-CN || zh-Hans'
;
const str2 = 'ALPHABET';
console.log('str1'.toLowerCase());
// 中文简体 zh-cn || zh-hans
console.log(str2.toLowerCase());
// "alphabet"
// #END
expect(str1.toLowerCase()).toEqual("中文简体 zh-cn || zh-hans");
expect(str2.toLowerCase()).toEqual("alphabet");
})
test('toUpperCase', () => {
// #TEST String.toUpperCase
const sentence = 'The quick brown fox jumps over the lazy dog.';
console.log(sentence.toUpperCase());
// expected output: "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG."
// #END
expect(sentence.toUpperCase()).toEqual("THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.");
})
test("lastIndexOf", () => {
// #TEST String.lastIndexOf
console.log('canal'.lastIndexOf('a'))//3
// #END
expect('canal'.lastIndexOf('a')).toEqual(3);
expect('canal'.lastIndexOf('a', 2)).toEqual(1);
expect('canal'.lastIndexOf('a', 0)).toEqual(-1);
...
...
@@ -315,8 +497,13 @@ export function testString(): Result {
expect("Blue Whale, Killer Whale".lastIndexOf("blue")).toEqual(-1);
})
test("substring", () => {
// #TEST String.substr
var str1 = "Mozilla";
expect(str1.substring(0, 3)).toEqual("Moz");
let ret = str1.substring(0, 3)
console.log(ret)//"Moz"
// #END
expect(ret).toEqual("Moz");
expect(str1.substring(3, 0)).toEqual("Moz");
expect(str1.substring(3, -3)).toEqual("Moz");
...
...
@@ -334,10 +521,13 @@ export function testString(): Result {
expect(str3.substring(10, 1)).toEqual("aa");
})
test("trim", () => {
const greeting = ' Hello world! ';
expect(greeting).toEqual(" Hello world! ");
expect(greeting.trim()).toEqual("Hello world!");
const orig:string = ' foo ';
// #TEST String.trim
let greeting = ' Hello world! ';
let ret = greeting.trim()
console.log(ret) //Hello world!
// #END
expect(ret).toEqual("Hello world!");
const orig : string = ' foo ';
expect(orig.trim()).toEqual("foo");
const str = '\t\t\tworld\t\t\t';
expect(str.trim()).toEqual('world');
...
...
@@ -363,13 +553,17 @@ export function testString(): Result {
expect(str2.trimEnd()).toEqual('');
})
test("startsWith", () => {
// #TEST String.startsWith
const str = 'hello world';
console.log(str.startsWith('hello'))//true
// #END
expect(str.startsWith('hello')).toEqual(true);
expect(str.startsWith('h')).toEqual(true);
expect(str.startsWith('HELLO')).toEqual(false);
expect(str.startsWith('o')).toEqual(false);
const str1:
string = "To be, or not to be, that is the question.";
const str1 :
string = "To be, or not to be, that is the question.";
expect(str1.startsWith("To be")).toEqual(true);
expect(str1.startsWith("not to be")).toEqual(false);
expect(str1.startsWith("not to be", 10)).toEqual(true);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录