Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
Hello UTS
提交
1cc52c77
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看板
提交
1cc52c77
编写于
9月 05, 2024
作者:
M
mahaifeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[map]去除文档中手动生成的代码,添加注释
上级
bdee5dcf
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
211 addition
and
116 deletion
+211
-116
uni_modules/uts-tests/utssdk/Map.uts
uni_modules/uts-tests/utssdk/Map.uts
+211
-116
未找到文件。
uni_modules/uts-tests/utssdk/Map.uts
浏览文件 @
1cc52c77
import { describe, test, expect, Result } from './tests.uts'
import { describe, test, expect, Result } from './tests.uts'
export function testMap(): Result {
export function testMap() : Result {
return describe("Map", () => {
return describe("Map", () => {
test('size', () => {
test('size', () => {
const map1: Map<string,string> = new Map();
// #TEST Map.size
map1.set('a', 'alpha');
const map1 : Map<string, string> = new Map();
map1.set('b', 'beta');
map1.set('a', 'alpha');
map1.set('g', 'gamma');
map1.set('b', 'beta');
expect(map1.size).toEqual(3);
map1.set('g', 'gamma');
map1.clear()
console.log(map1.size);
expect(map1.size).toEqual(0);
// expected output: 3
})
// #END
test('clear', () => {
const map1 = new Map<string,string>();
expect(map1.size).toEqual(3);
map1.set('bar', 'baz');
map1.clear()
map1.set("1", 'foo');
expect(map1.size).toEqual(0);
expect(map1.size).toEqual(2);
})
map1.clear();
test('clear', () => {
expect(map1.size).toEqual(0);
// #TEST Map.clear
})
const map1 = new Map<string, string>();
test('delete', () => {
map1.set('bar', 'baz');
const map1 = new Map<string,string>();
map1.set("1", 'foo');
map1.set('bar', 'foo');
map1.clear();
expect(map1.delete('bar')).toEqual(true);
console.log(map1.size);
expect(map1.has('bar')).toEqual(false);
// expected output: 0
})
// #END
test('get', () => {
const map1 = new Map<string,string>();
expect(map1.size).toEqual(0);
map1.set('bar', 'foo');
})
expect(map1.get('bar')).toEqual("foo");
test('delete', () => {
// js端输出undefined需要抹平差异
// #TEST Map.delete
expect(map1.get('baz')).toEqual(null);
const map1 = new Map<string, string>();
})
map1.set('bar', 'foo');
test('has', () => {
let ret1 = map1.delete('bar')
const map1 = new Map<string,string>();
console.log(ret1);
map1.set('bar', 'foo');
// expected result: true
expect(map1.has('bar')).toEqual(true);
// (true indicates successful removal)
expect(map1.has('baz')).toEqual(false);
console.log(map1.has('bar'));
})
// expected result: false
test('set', () => {
// #END
const map1 = new Map<string,string>();
map1.set('bar', 'foo');
expect(ret1).toEqual(true);
expect(map1.get('bar')).toEqual("foo");
expect(map1.has('bar')).toEqual(false);
})
const map2 = new Map<string,string>();
test('get', () => {
// ios平台不支持any作为key
// #TEST Map.get
map2.set('bar', 'foo');
const map1 = new Map<string, string>();
expect(map2.get('bar')).toEqual('foo');
map1.set('bar', 'foo');
map2.set('bar', 'baz');
console.log(map1.get('bar'));
expect(map2.get('bar')).toEqual('baz');
// expected output: "foo"
const map3 = new Map<number,number>();
// #END
map3.set(111, 111);
map3.set(222, 222);
expect(map1.get('bar')).toEqual("foo");
map3.set(333, 333);
// js端输出undefined需要抹平差异
expect(map3.get(111)).toEqual(111);
expect(map1.get('baz')).toEqual(null);
expect(map3.get(222)).toEqual(222);
})
expect(map3.get(333)).toEqual(333);
test('has', () => {
// ios不支持链式调用
// #TEST Map.has
// myMap.set(1, 'foobar').set(2, 'baz');
const map1 = new Map<string, string>();
// const map4 = new Map<UTSJSONObject,string>();
map1.set('bar', 'foo');
// key 不支持对象
console.log(map1.has('bar'));
// const key1 = {}, key2 = {};
// expected output: true
// map4.set(key1, '1')
// map4.set(key2, '2')
console.log(map1.has('baz'));
// expect(map4.get(key1)).toEqual('1');
// expected output: false
// expect(map4.get(key2)).toEqual('2');
// #END
const map5 = new Map<string,string>([['key1', 'value1'], ['key2', 'value2']]);
expect(map5.get('key1')).toEqual('value1');
expect(map1.has('bar')).toEqual(true);
expect(map5.get('key2')).toEqual('value2');
expect(map1.has('baz')).toEqual(false);
})
})
test('forEach', () => {
test('set', () => {
const map1 = new Map<string,string>();
// #TEST Map.set
map1.set('key1', 'value1');
let map1 = new Map<string, string>();
map1.set('key2', 'value2');
map1.set('bar', 'foo');
map1.set('key3', 'value3');
console.log(map1.get('bar'));
map1.forEach((value:string, key:string, map: Map<string,string>) => {
// expected output: "foo"
expect(value).toEqual(map.get(key)!);
})
console.log(map1.get('baz'));
map1.forEach((value, key) => {
// expected output: null
expect(value).toEqual(map1.get(key)!);
// #END
})
})
expect(map1.get('bar')).toEqual("foo");
test("entries", () => {
const map2 = new Map<string, string>();
// const myMap = new Map<string, string>();
// ios平台不支持any作为key
// myMap.set("0", "foo");
map2.set('bar', 'foo');
// myMap.set("1", "bar");
expect(map2.get('bar')).toEqual('foo');
// myMap.set("2", "baz");
map2.set('bar', 'baz');
// const mapIter = myMap.entries();
expect(map2.get('bar')).toEqual('baz');
// expect(mapIter.next().value).toEqual(["0", "foo"]);
const map3 = new Map<number, number>();
// expect(mapIter.next().value).toEqual(["1", "bar"]);
map3.set(111, 111);
// expect(mapIter.next().value).toEqual(["2", "baz"]);
map3.set(222, 222);
// expect(mapIter.next().done).toEqual(true);
map3.set(333, 333);
})
expect(map3.get(111)).toEqual(111);
test("keys", () => {
expect(map3.get(222)).toEqual(222);
// const myMap = new Map<string, string>();
expect(map3.get(333)).toEqual(333);
// myMap.set("0", "foo");
// ios不支持链式调用
// myMap.set("1", "bar");
// myMap.set(1, 'foobar').set(2, 'baz');
// myMap.set("2", "baz");
// const map4 = new Map<UTSJSONObject,string>();
// const mapIter = myMap.keys();
// key 不支持对象
// expect(mapIter.next().value).toEqual("0");
// const key1 = {}, key2 = {};
// expect(mapIter.next().value).toEqual("1");
// map4.set(key1, '1')
// expect(mapIter.next().value).toEqual("2");
// map4.set(key2, '2')
// expect(mapIter.next().done).toEqual(true);
// expect(map4.get(key1)).toEqual('1');
})
// expect(map4.get(key2)).toEqual('2');
test("values", () => {
const map5 = new Map<string, string>([['key1', 'value1'], ['key2', 'value2']]);
// const myMap = new Map<string, string>();
expect(map5.get('key1')).toEqual('value1');
// myMap.set("0", "foo");
expect(map5.get('key2')).toEqual('value2');
// myMap.set("1", "bar");
// myMap.set("2", "baz");
// #TEST Map.set_1
// const mapIter = myMap.values();
map1 = new Map(); //定义一个map,key为string类型,value也是string类型
// expect(mapIter.next().value).toEqual("foo");
map1.set('key1', "abc");
// expect(mapIter.next().value).toEqual("bar");
map1.set('key1', "def");
// expect(mapIter.next().value).toEqual("baz");
console.log(map1.get('key1')) //返回 def
// expect(mapIter.next().done).toEqual(true);
// #END
})
})
test('forEach', () => {
// #TEST Map.forEach,Map.forEach_1,Map.forEach_2
const map1 = new Map<string, string>();
map1.set('key1', 'value1');
map1.set('key2', 'value2');
map1.set('key3', 'value3');
map1.forEach((value : string, key : string, map : Map<string, string>) => {
console.log(key)
console.log(value)
})
// #END
map1.forEach((value : string, key : string, map : Map<string, string>) => {
expect(value).toEqual(map.get(key)!);
})
map1.forEach((value, key) => {
expect(value).toEqual(map1.get(key)!);
})
})
test("entries", () => {
// const myMap = new Map<string, string>();
// myMap.set("0", "foo");
// myMap.set("1", "bar");
// myMap.set("2", "baz");
// const mapIter = myMap.entries();
// expect(mapIter.next().value).toEqual(["0", "foo"]);
// expect(mapIter.next().value).toEqual(["1", "bar"]);
// expect(mapIter.next().value).toEqual(["2", "baz"]);
// expect(mapIter.next().done).toEqual(true);
})
test("keys", () => {
// const myMap = new Map<string, string>();
// myMap.set("0", "foo");
// myMap.set("1", "bar");
// myMap.set("2", "baz");
// const mapIter = myMap.keys();
// expect(mapIter.next().value).toEqual("0");
// expect(mapIter.next().value).toEqual("1");
// expect(mapIter.next().value).toEqual("2");
// expect(mapIter.next().done).toEqual(true);
})
test("values", () => {
// const myMap = new Map<string, string>();
// myMap.set("0", "foo");
// myMap.set("1", "bar");
// myMap.set("2", "baz");
// const mapIter = myMap.values();
// expect(mapIter.next().value).toEqual("foo");
// expect(mapIter.next().value).toEqual("bar");
// expect(mapIter.next().value).toEqual("baz");
// expect(mapIter.next().done).toEqual(true);
})
test("sample", () => {
// #TEST Map.sample_create
let map = new Map<string, any>()
map.set("name", "zhangsan")
map.set("age", 12)
//Map(2) {"name":"zhangsan","age":12}
console.log(map)
// #END
// #TEST Map.sample_visit
let map1 = new Map<string, any>()
map1.set("name", "zhangsan")
map1.set("age", 12)
let nameVal = map1.get('name')
//zhangsan
console.log(nameVal)
// #END
// #TEST Map.sample_forEach
let map2 = new Map<string, any | null>()
map2.set("name", "zhangsan")
map2.set("age", 12)
// 遍历函数 1
map2.forEach(function (value : any | null) {
console.log(value)
})
// 遍历函数 2
map2.forEach(function (value : any | null, key : string) {
console.log(key)
console.log(value)
})
// 遍历函数 3
map2.forEach(function (value : any | null, key : string, map : Map<string, any | null>) {
console.log(value)
console.log(key)
console.log(map)
})
// #END
})
})
}
})
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录