提交 5ab9fa38 编写于 作者: M mahaifeng

[array]自动生成文档代码

上级 471b5cad
......@@ -2,34 +2,38 @@ import { describe, test, expect, Result } from './tests.uts'
export function testArray(): Result {
export function testArray() : Result {
return describe("Array", () => {
test('constructor', () => {
// 构造器测试
let a1 = [1,2,3]
expect(a1).toEqual([1,2,3]);
let a2 = [1,'2',3]
expect(a2).toEqual([1,'2',3]);
let a3 = new Array(1,2,3);
// #TEST Array.Constructor
let a1 = [1, 2, 3]
let a2 = [1, '2', 3]
console.log(a1) //[1, 2, 3]
console.log(a2) // [1, '2', 3]
// #END
expect(a1).toEqual([1, 2, 3]);
expect(a2).toEqual([1, '2', 3]);
let a3 = new Array(1, 2, 3);
// swift 中字面量创建数组,仅支持同一类型的元素
// #ifndef APP-IOS
expect(a3).toEqual(new Array(1,2,3));
let a4 = new Array<any>(1,'2',3);
expect(a4).toEqual(new Array<any>(1,'2',3));
let a5 = Array(1,2,3);
expect(a5).toEqual(Array(1,2,3));
let a6 = Array<any>(1,'2','3')
expect(a6).toEqual(Array<any>(1,'2','3'));
expect(a3).toEqual(new Array(1, 2, 3));
let a4 = new Array<any>(1, '2', 3);
expect(a4).toEqual(new Array<any>(1, '2', 3));
let a5 = Array(1, 2, 3);
expect(a5).toEqual(Array(1, 2, 3));
let a6 = Array<any>(1, '2', '3')
expect(a6).toEqual(Array<any>(1, '2', '3'));
// #endif
})
test('equals', () => {
// 构造器测试
let a1 = [1,2,3]
let a2 = [1,2,3]
let a1 = [1, 2, 3]
let a2 = [1, 2, 3]
let equalsRet = (a1 == a2)
console.log(equalsRet)
// #ifndef APP-IOS
......@@ -44,7 +48,7 @@ export function testArray(): Result {
test('convert-native', () => {
// #ifdef APP-ANDROID
let utsArray = ["1",2,3.0]
let utsArray = ["1", 2, 3.0]
let javaArray = utsArray.toTypedArray();
let kotlinArray = utsArray.toKotlinList()
......@@ -57,12 +61,14 @@ export function testArray(): Result {
})
test('length', () => {
// #TEST Array.length
const arr = ['shoes', 'shirts', 'socks', 'sweaters'];
expect(arr.length).toEqual(4);
expect(arr[0]).toEqual('shoes');
expect(arr[1]).toEqual('shirts');
console.log(arr.length)//4
console.log(arr[1])//'shoes'
console.log(arr[1])//'shirts'
// #END
// expect(arr[4]).toEqual(null);
const numbers: number[] = [1, 2, 3, 4, 5];
const numbers : number[] = [1, 2, 3, 4, 5];
if (numbers.length > 3) {
numbers.length = 3;
}
......@@ -96,34 +102,34 @@ export function testArray(): Result {
expect(arr2.copyWithin(-2, -3, -1)).toEqual([1, 2, 3, 3, 1]);
})
test("every", () => {
const isBelowThreshold = (currentValue:number):boolean=> currentValue < 40;
const array1: number[] = [1, 30, 39, 29, 10, 13];
const array2: number[] = [1, 30, 39, 29, 10, 13, 41];
const isBelowThreshold = (currentValue : number) : boolean => currentValue < 40;
const array1 : number[] = [1, 30, 39, 29, 10, 13];
const array2 : number[] = [1, 30, 39, 29, 10, 13, 41];
expect(array1.every(isBelowThreshold)).toEqual(true);
expect(array2.every(isBelowThreshold)).toEqual(false);
const array3: number[] = [1, 2, 3];
array3.every((element:number, index:number, array:number[]):boolean => {
const array3 : number[] = [1, 2, 3];
array3.every((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element);
return true;
})
})
test("fill", () => {
const array1: number[] = [1, 2, 3, 4];
const array1 : number[] = [1, 2, 3, 4];
expect(array1.fill(0, 2, 4)).toEqual([1, 2, 0, 0]);
expect(array1.fill(5, 1)).toEqual([1, 5, 5, 5]);
expect(array1.fill(6)).toEqual([6, 6, 6, 6]);
const array2: number[]= [1, 2, 3]
const array2 : number[] = [1, 2, 3]
expect(array2.fill(4)).toEqual([4, 4, 4]);
const array3: number[]= [0, 0]
const array3 : number[] = [0, 0]
expect(array3.fill(1, null)).toEqual([1, 1]);
expect(array3.fill(1, 0, 1.5)).toEqual([1, 1]);
})
test("filter", () => {
const words: string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter((word:string):boolean => word.length > 6);
const words : string[] = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
const result = words.filter((word : string) : boolean => word.length > 6);
expect(result).toEqual(["exuberant", "destruction", "present"]);
const array1: number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const isPrime = array1.filter((num:number):boolean => {
const array1 : number[] = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];
const isPrime = array1.filter((num : number) : boolean => {
for (let i = 2; num > i; i++) {
// swift里,基础类型暂不支持!==,===对比
if (num % i == 0) {
......@@ -133,64 +139,78 @@ export function testArray(): Result {
return num > 1;
})
expect(isPrime).toEqual([2, 3, 5, 7, 11, 13]);
const array2: number[] = [1, 2, 3];
array2.filter((element:number, index:number, array:number[]):boolean => {
const array2 : number[] = [1, 2, 3];
array2.filter((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element);
return true;
})
})
test("find", () => {
const array1: number[] = [5, 12, 8, 130, 44];
const found1 = array1.find((element:number):boolean => element > 10);
// #TEST Array.find_1
const array1 : number[] = [5, 12, 8, 130, 44];
const found1 = array1.find((element : number) : boolean => element > 10);
console.log(found1) //12
// #END
expect(found1).toEqual(12);
const found2 = array1.find((element:number):boolean => element < 5);
// #TEST Array.find_1
const array3 : number[] = [5, 12, 8, 130, 44];
const found2 = array3.find((element : number, index : number) : boolean => element < 5);
console.log(found2) // null
// #END
expect(found2).toEqual(null);
const array2: number[] = [1, 2, 3];
array2.find((element:number, index:number, array:number[]):boolean => {
// #TEST Array.find
let array2 : number[] = [1, 2, 3];
array2.find((element : number, index : number, array : number[]) : boolean => {
console.log(array[index]) //1=>2=>3
return true;
})
// #END
array2 = [1, 2, 3];
array2.find((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element);
return true;
})
})
test("findIndex", () => {
const array1: number[] = [5, 12, 8, 130, 44];
const isLargeNumber = (element:number):boolean => element > 13;
const array1 : number[] = [5, 12, 8, 130, 44];
const isLargeNumber = (element : number) : boolean => element > 13;
expect(array1.findIndex(isLargeNumber)).toEqual(3);
const array2: number[] = [10, 11, 12];
const array2 : number[] = [10, 11, 12];
expect(array2.findIndex(isLargeNumber)).toEqual(-1);
const array3: number[] = [1, 2, 3];
array3.findIndex((element:number, index:number, array:number[]):boolean => {
const array3 : number[] = [1, 2, 3];
array3.findIndex((element : number, index : number, array : number[]) : boolean => {
expect(array[index]).toEqual(element);
return true;
})
})
test("flat", () => {
const arr1: any[] = [0, 1, 2, [3, 4]];
const arr1 : any[] = [0, 1, 2, [3, 4]];
expect(arr1.flat()).toEqual([0, 1, 2, 3, 4]);
const arr2: any[] = [0, 1, 2, [[[3, 4]]]];
const arr2 : any[] = [0, 1, 2, [[[3, 4]]]];
expect(arr2.flat(2)).toEqual([0, 1, 2, [3, 4]]);
const arr3: any[] = [1, 2, [3, 4, [5, 6]]];
const arr3 : any[] = [1, 2, [3, 4, [5, 6]]];
expect(arr3.flat(2)).toEqual([1, 2, 3, 4, 5, 6]);
})
test("forEach", () => {
const array1: string[] = ['a', 'b', 'c'];
array1.forEach((element:string, index:number) => {
const array1 : string[] = ['a', 'b', 'c'];
array1.forEach((element : string, index : number) => {
expect(array1[index]).toEqual(element)
});
const items: string[] = ['item1', 'item2', 'item3'];
const copyItems: string[] = [];
items.forEach((item:string) => {
const items : string[] = ['item1', 'item2', 'item3'];
const copyItems : string[] = [];
items.forEach((item : string) => {
copyItems.push(item);
});
expect(copyItems).toEqual(items)
})
test("includes", () => {
const array1: number[] = [1, 2, 3];
const array1 : number[] = [1, 2, 3];
expect(array1.includes(2)).toEqual(true);
const pets: string[] = ['cat', 'dog', 'bat'];
const pets : string[] = ['cat', 'dog', 'bat'];
expect(pets.includes('cat')).toEqual(true);
expect(pets.includes('at')).toEqual(false);
const array2: string[] = ['a', 'b', 'c'];
const array2 : string[] = ['a', 'b', 'c'];
expect(array2.includes('c', 3)).toEqual(false);
expect(array2.includes('c', 100)).toEqual(false);
......@@ -208,7 +228,7 @@ export function testArray(): Result {
// #ifdef APP-IOS
const s = JSON.parse<P[]>(JSON.stringify([{ x: 0, y: 0 }])!) as P[]
s[0].x += 0;
const clearList = s.map((v : P, index: number, _a) : number => v.x)
const clearList = s.map((v : P, index : number, _a) : number => v.x)
expect(clearList.includes(0)).toEqual(true);
// #endif
......@@ -222,13 +242,13 @@ export function testArray(): Result {
arr.push(raw);
expect(arr.indexOf(raw)).toEqual(2);
const beasts: string[] = ['ant', 'bison', 'camel', 'duck', 'bison'];
const beasts : string[] = ['ant', 'bison', 'camel', 'duck', 'bison'];
expect(beasts.indexOf('bison')).toEqual(1);
expect(beasts.indexOf('bison', 2)).toEqual(4);
expect(beasts.indexOf('giraffe')).toEqual(-1);
const indices: number[] = [];
const array: string[] = ['a', 'b', 'a', 'c', 'a', 'd'];
const indices : number[] = [];
const array : string[] = ['a', 'b', 'a', 'c', 'a', 'd'];
const element = 'a';
let idx = array.indexOf(element);
// swift里,基础类型暂不支持!==,===对比
......@@ -239,7 +259,7 @@ export function testArray(): Result {
expect(indices).toEqual([0, 2, 4]);
})
test("join", () => {
const elements: string[] = ['Fire', 'Air', 'Water'];
const elements : string[] = ['Fire', 'Air', 'Water'];
expect(elements.join()).toEqual("Fire,Air,Water");
expect(elements.join('')).toEqual("FireAirWater");
expect(elements.join('-')).toEqual("Fire-Air-Water");
......@@ -254,10 +274,10 @@ export function testArray(): Result {
expect(arr.lastIndexOf(raw)).toEqual(2);
const animals: string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
const animals : string[] = ['Dodo', 'Tiger', 'Penguin', 'Dodo'];
expect(animals.lastIndexOf('Dodo')).toEqual(3);
expect(animals.lastIndexOf('Tiger')).toEqual(1);
const array: number[] = [2, 5, 9, 2];
const array : number[] = [2, 5, 9, 2];
let index = array.lastIndexOf(2);
expect(index).toEqual(3);
index = array.lastIndexOf(7);
......@@ -272,29 +292,29 @@ export function testArray(): Result {
})
test("map", () => {
const array1: number[] = [1, 4, 9, 16];
const map1 = array1.map((x:number):number => x * 2);
const array1 : number[] = [1, 4, 9, 16];
const map1 = array1.map((x : number) : number => x * 2);
expect(map1).toEqual([2, 8, 18, 32]);
const numbers: number[] = [1, 4, 9];
const roots = numbers.map((num:number):number => num + 1);
const numbers : number[] = [1, 4, 9];
const roots = numbers.map((num : number) : number => num + 1);
expect(numbers).toEqual([1, 4, 9]);
expect(roots).toEqual([2, 5, 10]);
const array2: number[] = [1, 2, 3];
array2.map((element:number, index:number, array:number[]) => {
const array2 : number[] = [1, 2, 3];
array2.map((element : number, index : number, array : number[]) => {
expect(array[index]).toEqual(element);
})
})
test("pop", () => {
const plants: string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
const plants : string[] = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
expect(plants.pop()).toEqual("tomato");
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage", "kale"]);
plants.pop();
expect(plants).toEqual(["broccoli", "cauliflower", "cabbage"]);
})
test("push", () => {
const animals: string[] = ['pigs', 'goats', 'sheep'];
const animals : string[] = ['pigs', 'goats', 'sheep'];
const count = animals.push('cows');
expect(count).toEqual(4);
expect(animals).toEqual(['pigs', 'goats', 'sheep', 'cows']);
......@@ -302,22 +322,22 @@ export function testArray(): Result {
expect(animals).toEqual(["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]);
})
test("reduce", () => {
const array1: number[] = [1, 2, 3, 4];
const initialValue:number = 0;
const array1 : number[] = [1, 2, 3, 4];
const initialValue : number = 0;
const sumWithInitial = array1.reduce(
(previousValue:number, currentValue:number):number => previousValue + currentValue,
(previousValue : number, currentValue : number) : number => previousValue + currentValue,
initialValue
);
expect(sumWithInitial).toEqual(10);
})
test("shift", () => {
const array1: number[] = [1, 2, 3];
const array1 : number[] = [1, 2, 3];
const firstElement = array1.shift();
expect(firstElement).toEqual(1);
expect(array1).toEqual([2, 3]);
})
test("slice", () => {
const animals: string[] = ['ant', 'bison', 'camel', 'duck', 'elephant'];
const animals : string[] = ['ant', 'bison', 'camel', 'duck', 'elephant'];
expect(animals.slice(2)).toEqual(["camel", "duck", "elephant"]);
expect(animals.slice(2, 4)).toEqual(["camel", "duck"]);
expect(animals.slice(1, 5)).toEqual(["bison", "camel", "duck", "elephant"]);
......@@ -326,15 +346,15 @@ export function testArray(): Result {
expect(animals.slice()).toEqual(["ant", "bison", "camel", "duck", "elephant"]);
})
test("some", () => {
const array: number[] = [1, 2, 3, 4, 5];
const even = (element:number):boolean=> element % 2 == 0;
const array : number[] = [1, 2, 3, 4, 5];
const even = (element : number) : boolean => element % 2 == 0;
expect(array.some(even)).toEqual(true);
const isBiggerThan10 = (element:number):boolean=> element > 10;
const isBiggerThan10 = (element : number) : boolean => element > 10;
expect([2, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(false);
expect([12, 5, 8, 1, 4].some(isBiggerThan10)).toEqual(true);
})
test("splice", () => {
const months: string[] = ['Jan', 'March', 'April', 'June'];
const months : string[] = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
expect(months).toEqual(["Jan", "Feb", "March", "April", "June"]);
months.splice(4, 1, 'May');
......@@ -350,7 +370,7 @@ export function testArray(): Result {
expect(array1).toEqual([1, 100000, 21, 30, 4]);
const array2 = [5, 1, 4, 2, 3];
array2.sort((a, b):number => a - b);
array2.sort((a, b) : number => a - b);
expect(array2).toEqual([1, 2, 3, 4, 5]);
// const array3 = [5, "banana", 4, "apple", 3, "cherry", 2, "date", 1];
......@@ -364,14 +384,14 @@ export function testArray(): Result {
{ name: "Alice", age: 21 }
];
// 先强转类型,解决编译报错
array4.sort((a, b):number => (a['age'] as number) - (b['age'] as number));
array4.sort((a, b) : number => (a['age'] as number) - (b['age'] as number));
// #ifndef APP-IOS
expect(array4).toEqual([{ name: "Sarah", age: 19 }, { name: "Alice", age: 21 }, { name: "John", age: 24 }, { name: "Bob", age: 27 }]);
// #endif
// #ifdef APP-IOS
const arr = array4.map((value: UTSJSONObject): number => { return value["age"] as number })
const arr = array4.map((value : UTSJSONObject) : number => { return value["age"] as number })
expect(arr).toEqual([19, 21, 24, 27])
// #endif
......@@ -380,12 +400,15 @@ export function testArray(): Result {
})
test("unshift", () => {
const array1: number[] = [1, 2, 3];
const array1 : number[] = [1, 2, 3];
expect(array1.unshift(4, 5)).toEqual(5);
expect(array1).toEqual([4, 5, 1, 2, 3]);
})
test("toString", () => {
const array1: number[] = [1, 2, 3];
// #TEST Array.length
const array1 : number[] = [1, 2, 3];
console.log(array1.toString()) //"1,2,3"
// #END
expect(array1.toString()).toEqual("1,2,3");
const array2 = new Array<string>()
array2.push("a")
......@@ -405,27 +428,27 @@ export function testArray(): Result {
// expect(array2).toEqual([5, 4, 3, 2, 1]);
})
test("reduceRight", () => {
const array1: number[][] = [[0, 1], [2, 3], [4, 5]];
const result1 = array1.reduceRight((accumulator: number[], currentValue: number[]): number[] => accumulator.concat(currentValue));
const array1 : number[][] = [[0, 1], [2, 3], [4, 5]];
const result1 = array1.reduceRight((accumulator : number[], currentValue : number[]) : number[] => accumulator.concat(currentValue));
expect(result1).toEqual([4, 5, 2, 3, 0, 1]);
const array2: number[] = [1, 2, 3, 4];
const result2 = array2.reduceRight((acc: number, cur: number, index: number, array: number[]): number => {
const array2 : number[] = [1, 2, 3, 4];
const result2 = array2.reduceRight((acc : number, cur : number, index : number, array : number[]) : number => {
expect(array[index]).toEqual(cur);
return acc + cur;
});
expect(result2).toEqual(10);
const result3 = array2.reduceRight((acc: number, cur: number): number => acc + cur, 5);
const result3 = array2.reduceRight((acc : number, cur : number) : number => acc + cur, 5);
expect(result3).toEqual(15);
})
test("flatMap", () => {
const arr: number[] = [1, 2, 3];
const result = arr.flatMap((x: number):number[] => [x, x * 2]);
const arr : number[] = [1, 2, 3];
const result = arr.flatMap((x : number) : number[] => [x, x * 2]);
expect(result).toEqual([1, 2, 2, 4, 3, 6]);
const arr1: number[] = [1, 2, 3, 4];
const result1 = arr1.flatMap((num: number, index: number, array: number[]): number[] => {
const arr1 : number[] = [1, 2, 3, 4];
const result1 = arr1.flatMap((num : number, index : number, array : number[]) : number[] => {
expect(array[index]).toEqual(num);
if (num % 2 == 0) {
return [num * 2];
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册