提交 6960d3b7 编写于 作者: M mahaifeng

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

上级 5720bdd5
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
const map1 : Map<string, string> = new Map();
map1.set('a', 'alpha'); map1.set('a', 'alpha');
map1.set('b', 'beta'); map1.set('b', 'beta');
map1.set('g', 'gamma'); map1.set('g', 'gamma');
console.log(map1.size);
// expected output: 3
// #END
expect(map1.size).toEqual(3); expect(map1.size).toEqual(3);
map1.clear() map1.clear()
expect(map1.size).toEqual(0); expect(map1.size).toEqual(0);
}) })
test('clear', () => { test('clear', () => {
const map1 = new Map<string,string>(); // #TEST Map.clear
const map1 = new Map<string, string>();
map1.set('bar', 'baz'); map1.set('bar', 'baz');
map1.set("1", 'foo'); map1.set("1", 'foo');
expect(map1.size).toEqual(2);
map1.clear(); map1.clear();
console.log(map1.size);
// expected output: 0
// #END
expect(map1.size).toEqual(0); expect(map1.size).toEqual(0);
}) })
test('delete', () => { test('delete', () => {
const map1 = new Map<string,string>(); // #TEST Map.delete
const map1 = new Map<string, string>();
map1.set('bar', 'foo'); map1.set('bar', 'foo');
expect(map1.delete('bar')).toEqual(true); let ret1 = map1.delete('bar')
console.log(ret1);
// expected result: true
// (true indicates successful removal)
console.log(map1.has('bar'));
// expected result: false
// #END
expect(ret1).toEqual(true);
expect(map1.has('bar')).toEqual(false); expect(map1.has('bar')).toEqual(false);
}) })
test('get', () => { test('get', () => {
const map1 = new Map<string,string>(); // #TEST Map.get
const map1 = new Map<string, string>();
map1.set('bar', 'foo'); map1.set('bar', 'foo');
console.log(map1.get('bar'));
// expected output: "foo"
// #END
expect(map1.get('bar')).toEqual("foo"); expect(map1.get('bar')).toEqual("foo");
// js端输出undefined需要抹平差异 // js端输出undefined需要抹平差异
expect(map1.get('baz')).toEqual(null); expect(map1.get('baz')).toEqual(null);
}) })
test('has', () => { test('has', () => {
const map1 = new Map<string,string>(); // #TEST Map.has
const map1 = new Map<string, string>();
map1.set('bar', 'foo'); map1.set('bar', 'foo');
console.log(map1.has('bar'));
// expected output: true
console.log(map1.has('baz'));
// expected output: false
// #END
expect(map1.has('bar')).toEqual(true); expect(map1.has('bar')).toEqual(true);
expect(map1.has('baz')).toEqual(false); expect(map1.has('baz')).toEqual(false);
}) })
test('set', () => { test('set', () => {
const map1 = new Map<string,string>(); // #TEST Map.set
let map1 = new Map<string, string>();
map1.set('bar', 'foo'); map1.set('bar', 'foo');
console.log(map1.get('bar'));
// expected output: "foo"
console.log(map1.get('baz'));
// expected output: null
// #END
expect(map1.get('bar')).toEqual("foo"); expect(map1.get('bar')).toEqual("foo");
const map2 = new Map<string,string>(); const map2 = new Map<string, string>();
// ios平台不支持any作为key // ios平台不支持any作为key
map2.set('bar', 'foo'); map2.set('bar', 'foo');
expect(map2.get('bar')).toEqual('foo'); expect(map2.get('bar')).toEqual('foo');
map2.set('bar', 'baz'); map2.set('bar', 'baz');
expect(map2.get('bar')).toEqual('baz'); expect(map2.get('bar')).toEqual('baz');
const map3 = new Map<number,number>(); const map3 = new Map<number, number>();
map3.set(111, 111); map3.set(111, 111);
map3.set(222, 222); map3.set(222, 222);
map3.set(333, 333); map3.set(333, 333);
...@@ -65,16 +104,31 @@ export function testMap(): Result { ...@@ -65,16 +104,31 @@ export function testMap(): Result {
// map4.set(key2, '2') // map4.set(key2, '2')
// expect(map4.get(key1)).toEqual('1'); // expect(map4.get(key1)).toEqual('1');
// expect(map4.get(key2)).toEqual('2'); // expect(map4.get(key2)).toEqual('2');
const map5 = new Map<string,string>([['key1', 'value1'], ['key2', 'value2']]); const map5 = new Map<string, string>([['key1', 'value1'], ['key2', 'value2']]);
expect(map5.get('key1')).toEqual('value1'); expect(map5.get('key1')).toEqual('value1');
expect(map5.get('key2')).toEqual('value2'); expect(map5.get('key2')).toEqual('value2');
// #TEST Map.set_1
map1 = new Map(); //定义一个map,key为string类型,value也是string类型
map1.set('key1', "abc");
map1.set('key1', "def");
console.log(map1.get('key1')) //返回 def
// #END
}) })
test('forEach', () => { test('forEach', () => {
const map1 = new Map<string,string>(); // #TEST Map.forEach,Map.forEach_1,Map.forEach_2
const map1 = new Map<string, string>();
map1.set('key1', 'value1'); map1.set('key1', 'value1');
map1.set('key2', 'value2'); map1.set('key2', 'value2');
map1.set('key3', 'value3'); map1.set('key3', 'value3');
map1.forEach((value:string, key:string, map: Map<string,string>) => { 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)!); expect(value).toEqual(map.get(key)!);
}) })
map1.forEach((value, key) => { map1.forEach((value, key) => {
...@@ -115,5 +169,46 @@ export function testMap(): Result { ...@@ -115,5 +169,46 @@ export function testMap(): Result {
// expect(mapIter.next().value).toEqual("baz"); // expect(mapIter.next().value).toEqual("baz");
// expect(mapIter.next().done).toEqual(true); // 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.
先完成此消息的编辑!
想要评论请 注册