diff --git a/test_framework/harness/assert.ts b/test_framework/harness/assert.ts new file mode 100644 index 0000000000000000000000000000000000000000..fbeede1f3117cab593daa38b6859c6befd1599b3 --- /dev/null +++ b/test_framework/harness/assert.ts @@ -0,0 +1,47 @@ +class Assert { + private static defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + + } + static equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect)); + } + } + static notEqual(actual: any, expect: any, msg?: string) { + if (actual == expect) { + throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect, false)); + } + } + static isTrue(actual: any, msg?: string) { + this.equal(actual, true, msg); + } + static isFalse(flag: any, msg?: string) { + this.equal(flag, false, msg); + } + static isNumber(x: any, msg?: string) { + this.equal(typeof x, "number", msg); + } + static isString(s: any, msg?: string) { + this.equal(typeof s, "string", msg); + } + static isBoolean(s: any, msg?: string) { + this.equal(typeof s, "boolean", msg); + } + static isSymbol(actual: any, msg?: string) { + this.equal(typeof actual, "symbol", msg); + } + static isFunction(fun: any, msg?: string) { + this.equal(typeof fun, "function", msg); + } + static notNULL(v: any, msg?: string) { + this.notEqual(v, null, msg); + } + static isUndefined(actual: any, msg?: string) { + this.equal(actual, undefined, msg); + } +} \ No newline at end of file diff --git a/test_framework/harness/assertionError.ts b/test_framework/harness/assertionError.ts new file mode 100644 index 0000000000000000000000000000000000000000..bc066b9730486bc9f4ef135ae2783f58f866c89c --- /dev/null +++ b/test_framework/harness/assertionError.ts @@ -0,0 +1,8 @@ +class AssertionError extends Error { + protected msg: string | undefined = ""; + + constructor(msg: string | undefined) { + super(msg) + this.name = "AssertionError"; + } +} diff --git a/test_framework/harness/console.ts b/test_framework/harness/console.ts new file mode 100644 index 0000000000000000000000000000000000000000..353834523f22e1dcb652a5723afca09ccacb0383 --- /dev/null +++ b/test_framework/harness/console.ts @@ -0,0 +1 @@ +console.log("TESTCASE SUCCESS"); \ No newline at end of file diff --git a/test_framework/harness/print.ts b/test_framework/harness/print.ts new file mode 100644 index 0000000000000000000000000000000000000000..0819fde9a673a17f6857093c732c2139a076f55b --- /dev/null +++ b/test_framework/harness/print.ts @@ -0,0 +1 @@ +print("TESTCASE SUCCESS"); \ No newline at end of file diff --git a/test_framework/suite/assert.ts b/test_framework/suite/assert.ts new file mode 100644 index 0000000000000000000000000000000000000000..9bfabf63a714b31feb6b1652ef04be5775e90377 --- /dev/null +++ b/test_framework/suite/assert.ts @@ -0,0 +1,52 @@ +import { AssertionError } from "./assertionError" + +export class Assert { + private static defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + + } + static equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect)); + } + } + static notEqual(actual: any, expect: any, msg?: string) { + if (actual == expect) { + throw new AssertionError(msg ? msg : this.defaultMessage(actual, expect, false)); + } + } + static isTrue(actual: any, msg?: string) { + this.equal(actual, true, msg); + } + static isFalse(flag: any, msg?: string) { + this.equal(flag, false, msg); + } + static isNumber(x: any, msg?: string) { + this.equal(typeof x, "number", msg); + } + static isString(s: any, msg?: string) { + this.equal(typeof s, "string", msg); + } + static isBoolean(s: any, msg?: string) { + this.equal(typeof s, "boolean", msg); + } + static isSymbol(actual: any, msg?: string) { + this.equal(typeof actual, "symbol", msg); + } + static isFunction(fun: any, msg?: string) { + this.equal(typeof fun, "function", msg); + } + static notNULL(v: any, msg?: string) { + this.notEqual(v, null, msg); + } + static isUndefined(actual: any, msg?: string) { + this.equal(actual, undefined, msg); + } + static isObject(obj: any, msg?: string) { + this.equal(typeof obj, "object", msg); + } +} \ No newline at end of file diff --git a/test_framework/suite/assertionError.ts b/test_framework/suite/assertionError.ts new file mode 100644 index 0000000000000000000000000000000000000000..da59b29ce6f4f816334ca705ee7f71a84411d3fa --- /dev/null +++ b/test_framework/suite/assertionError.ts @@ -0,0 +1,8 @@ +export class AssertionError extends Error { + protected msg: string | undefined = ""; + + constructor(msg: string | undefined) { + super(); + this.msg = msg; + } +} diff --git a/test_framework/test/2.0/abstract_properties_and_accessors/abstract_properties_and_accessors.ts b/test_framework/test/2.0/abstract_properties_and_accessors/abstract_properties_and_accessors.ts new file mode 100644 index 0000000000000000000000000000000000000000..a68b7fde72c17a6778ab6f66de956a31821de026 --- /dev/null +++ b/test_framework/test/2.0/abstract_properties_and_accessors/abstract_properties_and_accessors.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + An abstract class can declare abstract properties and/or accessors. Any sub class will need to declare the abstract properties or be marked as abstract. Abstract properties cannot have an initializer. Abstract accessors cannot have bodies. + ---*/ + +abstract class Base { + abstract name: string; + abstract get value(); + abstract set value(v: number); +} +class Derived extends Base { + name = "derived"; + value = 1; +} +let d = new Derived(); +Assert.equal(d.name, "derived"); +Assert.equal(d.value, 1); + diff --git a/test_framework/test/2.0/allow_duplicate_identifiers_across_declarations/allow_duplicate_identifiers_across_declarations_1.ts b/test_framework/test/2.0/allow_duplicate_identifiers_across_declarations/allow_duplicate_identifiers_across_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6fe04808e6ed45952dad299ebb69eaadc6cbbadb --- /dev/null +++ b/test_framework/test/2.0/allow_duplicate_identifiers_across_declarations/allow_duplicate_identifiers_across_declarations_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.0 relaxes this constraint and allows duplicate identifiers across blocks, as long as they have identical types. +---*/ + + +interface ErrorA { + stack?: string; +} + +interface ErrorB { + code?: string; + path?: string; + stack?: string; // OK +} + +let sa: ErrorA = { + stack: "123456" +}; + +let sb: ErrorB = { + code: "29189", + path: "/path/ts/doc", + stack: "65867967", +}; + +Assert.equal(sa.stack, "123456"); +Assert.equal(sb.stack, "65867967"); \ No newline at end of file diff --git a/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis1.ts b/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b82c042e6c7c7a78809dd83801189e4a350b6fc6 --- /dev/null +++ b/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis1.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type checker analyses all possible flows of control in statements and expressions to produce the most specific type possible (the narrowed type) at any given location for a local variable or parameter that is declared to have a union type. +---*/ + + +function foo(x: string | number | boolean) { + if (typeof x === "string") { + x; // type of x is string here + Assert.equal(typeof x, "string") + x = 1; + // type of x is number here + x; + } + // type of x is number | boolean here + x; +} + +function bar(x: string | number) { + if (typeof x === "number") { + Assert.equal(typeof x, "number") + return; + } + // type of x is string here + x; +} + +foo("hello") +bar(50) \ No newline at end of file diff --git a/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis2.ts b/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis2.ts new file mode 100644 index 0000000000000000000000000000000000000000..336e6332a29def5bbac0dcd60427d60336276415 --- /dev/null +++ b/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Control flow based type analysis is particularly relevant in strictNullChecks mode because nullable types are represented using union types. +---*/ + + +function test(x: string | null) { + if (x === null) { + Assert.equal(typeof x, "object") + return; + } + // type of x is string in remainder of function + x; +} + +test(null) \ No newline at end of file diff --git a/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis3.ts b/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis3.ts new file mode 100644 index 0000000000000000000000000000000000000000..46ef0fea0f7ccaf0c4a7dc33e513d045054f46ff --- /dev/null +++ b/test_framework/test/2.0/control_flow_based_type_analysis/control_flow_based_type_analysis3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In strictNullChecks mode, control flow based type analysis includes definite assignment analysis for local variables of types that don’t permit the value undefined. +---*/ + + +// Compiled with --strictNullChecks +function mumble(check: boolean) { + // Type doesn't permit undefined + let x: number; + if (check) { + x = 1; + Assert.equal(x, 1); + } + x = 2; + Assert.equal(x, 2); +} + +mumble(true); \ No newline at end of file diff --git a/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_1.ts b/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0a1a01fcd89a1da44b6c016a0a3fb492ae0b57d9 --- /dev/null +++ b/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_1.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Type guards now support checking “dotted names” consisting of a variable or parameter name followed one or more property accesses. +---*/ + + +interface Options { + location?: { + x?: number; + y?: number; + }; +} + +function foo(options?: Options) { + if (options && options.location && options.location.x) { + const x = options.location.x; + Assert.equal(typeof x, "number"); + } + + if (options && options.location && options.location.y) { + const x = options.location.y; + Assert.equal(typeof x, "number"); + } +} + +let options: Options = { + location: { + x: 10, + y: 20, + } +}; +foo(options); \ No newline at end of file diff --git a/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_2.ts b/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..dcb62d697d860d92551cabfe5fc00a91747cd34e --- /dev/null +++ b/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_2.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Type guards for dotted names also work with user defined type guard functions and the instanceof operators and do not depend on the strictNullChecks compiler option. +---*/ + + +interface Base { + name: string; +} + +class A implements Base { + a: string; + name: string; + constructor(name: string, a: string) { + this.name = name; + this.a = a; + } +} + +class B implements Base { + b: number; + name: string; + constructor(name: string, b: number) { + this.name = name; + this.b = b; + } +} + +// user defined type guard functions +function doStuff(arg: A | B) { + if (arg instanceof A) { + Assert.equal(typeof arg.a, "string"); + } else { + Assert.equal(typeof arg.b, "number"); + } +} + +const a: A = new A("aa", "Ril"); +const b: B = new B("bb", 100); +doStuff(a); +doStuff(b); \ No newline at end of file diff --git a/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_3.ts b/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..9be8a6d3ecb051761531cbadb5a05672b036eb40 --- /dev/null +++ b/test_framework/test/2.0/dotted_names_in_type_guards/dotted_names_in_type_guards_3.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Type guards for dotted names also work with user defined type guard functions and the typeof operators and do not depend on the strictNullChecks compiler option. +---*/ + + +interface IPerson { + name: string; + age: number; +} +let person: IPerson = { + name: 'Anna', + age: 18 +}; + +type Person = typeof person; + +let p: Person = { + name: 'Jack', + age: 20 +} + +// user defined type guard functions +function myinfo(parmas: { name: string, age: number }) { + let name: typeof p.name; + let age: typeof p.age; + Assert.equal(typeof parmas.name, "string"); + Assert.equal(typeof parmas.age, "number"); +} + +myinfo(p); +myinfo(person); \ No newline at end of file diff --git a/test_framework/test/2.0/expression_operators/expression_operators_1.ts b/test_framework/test/2.0/expression_operators/expression_operators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b26c88d71a1cefa731197db0bb9e78e904fe7684 --- /dev/null +++ b/test_framework/test/2.0/expression_operators/expression_operators_1.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Expression operators permit operand types to include null and/or undefined but always produce values of non-null and non-undefined types. + ---*/ + + +function sum(a: number, b: number) { + return a + b; +} +sum(3, 5); + +interface Entity { + name: string; +} + +function testEntityFunc(e: Entity) { + return e.name; +} +Assert.equal(testEntityFunc({ name: "caihua" }), "caihua"); + +let x = testEntityFunc; +let s = x; +let y = x || { name: "test" }; diff --git a/test_framework/test/2.0/expression_operators/expression_operators_2.ts b/test_framework/test/2.0/expression_operators/expression_operators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d197091d0cffac457ec5c0d9874ae0c3aa1859ec --- /dev/null +++ b/test_framework/test/2.0/expression_operators/expression_operators_2.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The && operator adds null and/or undefined to the type of the right operand depending on which are present in the type of the left operand, and the || operator removes both null and undefined from the type of the left operand in the resulting union type. + ---*/ + + +interface Entity { + name: string; +} +// Compiled with --strictNullChecks +function testEntityFunc(e: Entity) { + return e.name; +} +Assert.equal(testEntityFunc({ name: "caihua" }), "caihua"); + +let x = testEntityFunc; +let s = x; +let y = x || { name: "test" }; + +Assert.equal(x.name, "testEntityFunc") \ No newline at end of file diff --git a/test_framework/test/2.0/implicit_index_signatures/implicit_index_signatures.ts b/test_framework/test/2.0/implicit_index_signatures/implicit_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..2175c835bc63cf79a601312858c51ad6472df34f --- /dev/null +++ b/test_framework/test/2.0/implicit_index_signatures/implicit_index_signatures.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description:An object literal type is now assignable to a type with an index signature if all known properties in the object literal are assignable to that index signature. This makes it possible to pass a variable that was initialized with an object literal as parameter to a function that expects a map or dictionary: + ---*/ + + +function httpService(path: string, headers: { [x: string]: string }) { + return JSON.stringify(headers); +} +const headers = { + "Content-Type": "application/x-www-form-urlencoded", +}; +let s1: string = httpService("", { "Content-Type": "application/x-www-form-urlencoded" }); +Assert.equal(s1, '{"Content-Type":"application/x-www-form-urlencoded"}'); + +// Now ok, previously wasn't +let s2: string = httpService("", headers); +Assert.equal(s2, '{"Content-Type":"application/x-www-form-urlencoded"}'); + diff --git a/test_framework/test/2.0/module_identifiers_allow_for_js_extension/module_identifiers_allow_for_js_extension_1.ts b/test_framework/test/2.0/module_identifiers_allow_for_js_extension/module_identifiers_allow_for_js_extension_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e58e61f508b00727de7df0089f2f3cca8c0752dd --- /dev/null +++ b/test_framework/test/2.0/module_identifiers_allow_for_js_extension/module_identifiers_allow_for_js_extension_1.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: With TypeScript 2.0, the compiler will look up definition of "moduleA.js" in ./moduleA.ts or ./moduleA.d.t. +module: ESNext +isCurrent: true +---*/ + + +import { HelloWorld } from "./test_js_extension.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +let world = new HelloWorld("Tom"); +equal(world.name, "Tom"); \ No newline at end of file diff --git a/test_framework/test/2.0/module_identifiers_allow_for_js_extension/test_js_extension.ts b/test_framework/test/2.0/module_identifiers_allow_for_js_extension/test_js_extension.ts new file mode 100644 index 0000000000000000000000000000000000000000..afd6af92ec36997d504fae97268339e66eab22d0 --- /dev/null +++ b/test_framework/test/2.0/module_identifiers_allow_for_js_extension/test_js_extension.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export class HelloWorld { + name: string; + constructor(name: string){ + this.name = name; + } +} \ No newline at end of file diff --git a/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_1.ts b/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b580bdf6693e19bd7e39cf357c81cb7a402b9fa --- /dev/null +++ b/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + test base url import,the tsc command needs to be executed in the 2.0 file directory. + tsconfig.json file url: testing-framework\test\2.0\tsconfig.json. +module: ESNext +isCurrent: true +---*/ + + +import { ADD, flag } from "../test_base_url.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(ADD(2, 7), 9); +equal(flag, "baseurl"); \ No newline at end of file diff --git a/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_2.ts b/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0e2c2b04e5dbbf9f511bb4a74733152acbba70bf --- /dev/null +++ b/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_2.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The TypeScript compiler supports the declaration of such mappings using paths property in tsconfig.json files, + Path mapping. + The tsc command needs to be executed in the 2.0 file directory. + tsconfig.json file url: testing-framework\test\2.0\tsconfig.json. +module: ESNext +isCurrent: true +---*/ + + +import { Directions } from '../test_virtual_directories_with_rootDirs/view2.js'; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(Directions.Up, 0); \ No newline at end of file diff --git a/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_3.ts b/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..74c0d2cdb05291b6f0d0324a5ee9715aaa644e89 --- /dev/null +++ b/test_framework/test/2.0/module_resolution_enhancements/module_resolution_enhancements_3.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Using ‘rootDirs’, you can inform the compiler of the roots making up this “virtual” directory; + and thus the compiler can resolve relative modules imports within these “virtual” directories + as if they were merged together in one directory. + The tsc command needs to be executed in the 2.0 file directory. + tsconfig.json file url: testing-framework\test\2.0\tsconfig.json. +module: ESNext +isCurrent: true +---*/ + + +import './view2'; +import { Directions } from '../test_virtual_directories_with_rootDirs/view2'; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(Directions.Left, 2); \ No newline at end of file diff --git a/test_framework/test/2.0/optional_class_properties/optional_class_properties.ts b/test_framework/test/2.0/optional_class_properties/optional_class_properties.ts new file mode 100644 index 0000000000000000000000000000000000000000..7ad0358ed00a82e81124b50df5e04fa68e2539b4 --- /dev/null +++ b/test_framework/test/2.0/optional_class_properties/optional_class_properties.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description:Optional properties and methods can now be declared in classes, similar to what is already permitted in interfaces. + ---*/ + + +class Bar { + a: number = 0; + b?: number; + f() { + return this.a; + } + // Body of optional method can be omitted + g?(): number; + h?() { + return this.b; + } +} +let bar = new Bar(); +bar.a = 1024; +bar.b = 1408; +Assert.equal(bar.f(), 1024); + +if (bar.h) { + Assert.equal(bar.h(), 1408); +} + +bar.g = () => { + if (bar.b !== undefined) { + return bar.a + bar.b; + } else { + return bar.a; + } +} +Assert.equal(bar.g(), 2432); + + + diff --git a/test_framework/test/2.0/private_and_protected_constructor/private_and_protected_constructor.ts b/test_framework/test/2.0/private_and_protected_constructor/private_and_protected_constructor.ts new file mode 100644 index 0000000000000000000000000000000000000000..9693c364a728f36c1ed6099f437b18cab76a14e1 --- /dev/null +++ b/test_framework/test/2.0/private_and_protected_constructor/private_and_protected_constructor.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description:A class constructor may be marked private or protected. A class with private constructor cannot be instantiated outside the class body, and cannot be extended. A class with protected constructor cannot be instantiated outside the class body, but can be extended. + ---*/ + + +class Singleton { + [x: string]: any; + private static instance: Singleton = { + cname: "Singleton", + }; + private constructor() { } + static getInstance() { + if (!Singleton.instance) { + Singleton.instance = new Singleton(); + } + return Singleton.instance; + } +} +let v = Singleton.getInstance(); +Assert.equal(v.cname, "Singleton"); \ No newline at end of file diff --git a/test_framework/test/2.0/read-only_properties_and_index_signatures/read-only_properties_and_index_signatures.ts b/test_framework/test/2.0/read-only_properties_and_index_signatures/read-only_properties_and_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..b99fd0fe087a99f4eeba43bec00211db87c5eaeb --- /dev/null +++ b/test_framework/test/2.0/read-only_properties_and_index_signatures/read-only_properties_and_index_signatures.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A property or index signature can now be declared with the readonly modifier is considered read-only. + Read-only properties may have initializers and may be assigned to in constructors within the same class declaration, but otherwise assignments to read-only properties are disallowed + ---*/ + + +interface Point { + readonly x: number; + readonly y: number; +} +var p1: Point = { x: 10, y: 20 }; +var p2 = { x: 1, y: 1 }; +// Ok, read-only alias for p2 +var p3: Point = p2; +Assert.equal(JSON.stringify(p3), '{"x":1,"y":1}'); +// Ok, but also changes p3.x because of aliasing +p2.x = 5; + +class Foo { + readonly a = 1; + readonly b: string; + constructor() { + // Assignment permitted in constructor + this.b = "hello"; + } +} + +let a: Array = [0, 1, 2, 3, 4]; +let b: ReadonlyArray = a; +Assert.equal(JSON.stringify(b), '[0,1,2,3,4]'); diff --git a/test_framework/test/2.0/shorthand_ambient_module_declarations/declarations.d.ts b/test_framework/test/2.0/shorthand_ambient_module_declarations/declarations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..8ad161eb359bd599b391d10cf3038710779741d4 --- /dev/null +++ b/test_framework/test/2.0/shorthand_ambient_module_declarations/declarations.d.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare module 'hot-new-moduled'; \ No newline at end of file diff --git a/test_framework/test/2.0/shorthand_ambient_module_declarations/hot-new-module.ts b/test_framework/test/2.0/shorthand_ambient_module_declarations/hot-new-module.ts new file mode 100644 index 0000000000000000000000000000000000000000..bf3818895d5431a79f80ecc19c93ea1c5f71c1cf --- /dev/null +++ b/test_framework/test/2.0/shorthand_ambient_module_declarations/hot-new-module.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default function x(a: number) { + return a + 15; +} + +export const y = 10; \ No newline at end of file diff --git a/test_framework/test/2.0/shorthand_ambient_module_declarations/shorthand_ambient_module_declarations_1.ts b/test_framework/test/2.0/shorthand_ambient_module_declarations/shorthand_ambient_module_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b2d44d16e909ecb97f8c7268206f7a401365d6d9 --- /dev/null +++ b/test_framework/test/2.0/shorthand_ambient_module_declarations/shorthand_ambient_module_declarations_1.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + If you don’t want to take the time to write out declarations before using a new module, + you can now just use a shorthand declaration to get started quickly. +module: ESNext +isCurrent: true +---*/ + + +// All imports from a shorthand module will have the any type. +import x, { y } from "./hot-new-module.js"; + +x(y); +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(typeof x, "function", "msg"); \ No newline at end of file diff --git a/test_framework/test/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_1.ts b/test_framework/test/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..bbf0b4538b138bd02372163007496ecc6b3b5909 --- /dev/null +++ b/test_framework/test/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_1.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: You can provide an explicit this parameter. this parameters are fake parameters that come first in the parameter list of a function. +module: ESNext +isCurrent: true +---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +function f(this: void) { + // make sure `this` is unusable in this standalone function + equal(this, undefined); +} + +f(); \ No newline at end of file diff --git a/test_framework/test/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_2.ts b/test_framework/test/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d2ea4c6d486ceb2f955d08af6f92ef7f71ccbbc --- /dev/null +++ b/test_framework/test/2.0/specifying_the_type_of_this_for_functions/specifying_the_type_of_this_for_functions_2.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + this parameters in callbacks.Libraries can also use this parameters to declare how callbacks will be invoked. + this: void means that addClickListener expects onclick to be a function that does not require a this type. +module: ESNext +isCurrent: true +---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +interface UIElement { + addClickListener(onclick: (this: void, e: Event) => void): void; +} + +class Handler { + info: string; + constructor(info: string) { + this.info = info; + } + onClickGood(this: void, e: Event) { + // can't use this here because it's of type void! + equal(this, undefined); + } +} + +let uiElement: UIElement = { + addClickListener(onclick: (this: void, e: Event) => void) { } +}; +let h = new Handler("hahaha"); +uiElement.addClickListener(h.onClickGood); \ No newline at end of file diff --git a/test_framework/test/2.0/support_for_UMD_module_definitions/math-lib.d.ts b/test_framework/test/2.0/support_for_UMD_module_definitions/math-lib.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..9263d384129e61568b484377d3de3ca0d125b9e1 --- /dev/null +++ b/test_framework/test/2.0/support_for_UMD_module_definitions/math-lib.d.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export function isPrime(x: number): boolean; +export as namespace mathLib; \ No newline at end of file diff --git a/test_framework/test/2.0/support_for_UMD_module_definitions/math-lib.ts b/test_framework/test/2.0/support_for_UMD_module_definitions/math-lib.ts new file mode 100644 index 0000000000000000000000000000000000000000..77b23255e258c669f34428910f1eb803bed35aff --- /dev/null +++ b/test_framework/test/2.0/support_for_UMD_module_definitions/math-lib.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export function isPrime(x: number): boolean { + if (x < 2) { + return false; + } + for (let i = 2; i <= x / 2; i++) { + if (x % i == 0) { + return false; + } + } + return true; +} \ No newline at end of file diff --git a/test_framework/test/2.0/support_for_UMD_module_definitions/support_for_UMD_module_definitions_1.ts b/test_framework/test/2.0/support_for_UMD_module_definitions/support_for_UMD_module_definitions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d9f958d7edd21292021071b9894bec670aec453 --- /dev/null +++ b/test_framework/test/2.0/support_for_UMD_module_definitions/support_for_UMD_module_definitions_1.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + These UMD libraries can be accessed through either an import. +module: ESNext +isCurrent: true +---*/ + + +import { isPrime } from "./math-lib.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(isPrime(2), true, "this is prime"); \ No newline at end of file diff --git a/test_framework/test/2.0/tagged_union_types/tagged_union_types_1.ts b/test_framework/test/2.0/tagged_union_types/tagged_union_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4b358b3dcfc5c3bc1ac016da30b9c77aecac4a72 --- /dev/null +++ b/test_framework/test/2.0/tagged_union_types/tagged_union_types_1.ts @@ -0,0 +1,115 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: The TS compiler now support type guards that narrow union types based on tests of a discriminant property and furthermore extend that capability to switch statements. +module: ESNext +isCurrent: true +---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +interface Square { + kind: "square"; + size: number; +} + +interface Rectangle { + kind: "rectangle"; + width: number; + height: number; +} + +interface Circle { + kind: "circle"; + radius: number; +} + +type Shape = Square | Rectangle | Circle; + +function area(s: Shape) { + // In the following switch statement, the type of s is narrowed in each case clause + // according to the value of the discriminant property, thus allowing the other properties + // of that variant to be accessed without a type assertion. + switch (s.kind) { + case "square": + return s.size * s.size; + case "rectangle": + return s.width * s.height; + case "circle": + return Math.PI * s.radius * s.radius; + } +} + +function test1(s: Shape) { + if (s.kind === "rectangle") { + // Rectangle + equal(typeof s.height, "number"); + equal(typeof s.width, "number"); + } + else { + // Square | Circle + s; + } +} + +function test2(s: Shape) { + if (s.kind === "square" || s.kind === "rectangle") { + return; + } + // Circle + equal(typeof s.radius, "number"); +} + +let square: Square = { + kind: "square", + size: 5 +}; + +let rectangle: Rectangle = { + kind: "rectangle", + width: 5, + height: 8 +}; + +let circle: Circle = { + kind: "circle", + radius: 10 +}; + +equal(area(square), 25); +test1(rectangle); +test2(circle); \ No newline at end of file diff --git a/test_framework/test/2.0/test_base_url.ts b/test_framework/test/2.0/test_base_url.ts new file mode 100644 index 0000000000000000000000000000000000000000..0fbd59e58afc12a4899bfdcdadda27c8b1ae5c58 --- /dev/null +++ b/test_framework/test/2.0/test_base_url.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function add(a: number, b: number): number { + return a + b; +} +let flag: string = "baseurl"; +export { add as ADD, flag } \ No newline at end of file diff --git a/test_framework/test/2.0/test_virtual_directories_with_rootDirs/view1.ts b/test_framework/test/2.0/test_virtual_directories_with_rootDirs/view1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7466ebdbcba95fea2fd2473b441c172640fceb52 --- /dev/null +++ b/test_framework/test/2.0/test_virtual_directories_with_rootDirs/view1.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +import './module_resolution_enhancements_3' \ No newline at end of file diff --git a/test_framework/test/2.0/test_virtual_directories_with_rootDirs/view2.ts b/test_framework/test/2.0/test_virtual_directories_with_rootDirs/view2.ts new file mode 100644 index 0000000000000000000000000000000000000000..58d9a6f69e993091f72f243032bbf94aea350cfe --- /dev/null +++ b/test_framework/test/2.0/test_virtual_directories_with_rootDirs/view2.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export enum Directions { + Up, + Down, + Left, + Right +}; \ No newline at end of file diff --git a/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_1.ts b/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8272b06d6249350517072197474297b3986c1a6 --- /dev/null +++ b/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The type inferred for a const variable or readonly property without a type annotation is the type of the literal initializer. + ---*/ + + +// const variable without a type annotation +const a = 1; +let aa: typeof a = 1; +const b = false; +let bb: typeof b = false; +const c = "hello"; +let cc: typeof c = "hello"; + +// readonly property without a type annotation +class Test { + static readonly age = 12; + static readonly isJob = false; + static readonly nickName = "wangcai"; +} + +let age: typeof Test.age = 12; +Assert.equal(typeof age, "number"); +let isJob: typeof Test.isJob = false; +Assert.equal(typeof isJob, "boolean"); +let nickName: typeof Test.nickName = "wangcai"; +Assert.equal(typeof nickName, "string"); \ No newline at end of file diff --git a/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_2.ts b/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..36321f52c9f5457c1c3e0f55ed9bb972b819e595 --- /dev/null +++ b/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_2.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The type inferred for a let variable, var variable, parameter, or non-readonly property with an initializer + and no type annotation is the widened literal type of the initializer. + where the widened type for a string literal type is string, number for numeric literal types, boolean for true or false + and the containing enum for enum literal types. + ---*/ + + +// let number +let a = 1; +let aa: typeof a = 12; +Assert.isNumber(aa); +// var number +var b = 1; +var bb: typeof b = 12; +Assert.isNumber(bb); +// parameter boolean +function test(a = true) { + return a; +} +let tt: ReturnType = false; +Assert.isBoolean(tt); +// non-readonly property +class Test { + static job = "coder"; +} +// string +let cc: typeof Test.job = "driver"; +Assert.isString(cc); \ No newline at end of file diff --git a/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_3.ts b/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1754b02955018327e61bf2531f9f0d5b7ccd2df6 --- /dev/null +++ b/test_framework/test/2.1/better_inference_for_literal_types/better_inference_for_literal_types_3.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Literal type widening can be controlled through explicit type annotations. + ---*/ + + +const c1 = "hello"; +// Type string +let v1 = c1; +v1 = "world"; +const c2: "hello" = "hello"; +let v2 = c2; +Assert.equal(v2, "hello"); + +const c3 = 1; +let c4 = c3; +c4 = 10; +Assert.equal(c4, 10); +const c5: 1 = 1; +let c6 = c5; +c6 = 1; +Assert.equal(c6, 1); + +const c7 = false; +let c8 = c7; +c8 = true; +Assert.equal(c8, true); +const c9: false = false; +let c10 = c9; +c10 = false; +Assert.equal(c10, false); \ No newline at end of file diff --git a/test_framework/test/2.1/downlevel_async_functions/downlevel_async_functions_1.ts b/test_framework/test/2.1/downlevel_async_functions/downlevel_async_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a0c160f3936c2c691b2ccfc825693b874a3cde8c --- /dev/null +++ b/test_framework/test/2.1/downlevel_async_functions/downlevel_async_functions_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + This feature was supported before TypeScript 2.1, + but only when targeting ES6/ES2015. + TypeScript 2.1 brings the capability to ES3 and ES5 run-times. +options: + lib:dom,es5,es2015.promise + ---*/ + + +function delay(milliseconds: number) { + return new Promise((resolve) => { + setTimeout(resolve, milliseconds); + }); +} +async function dramaticWelcome(a: number, b: string) { + Assert.equal(a, 12); + for (let i = 0; i < 3; i++) { + await delay(500); + Assert.equal(b, "hello"); + } + Assert.equal(b, "hello"); +} +dramaticWelcome(12, "hello"); diff --git a/test_framework/test/2.1/improved_any_inference/improved_any_inference_1.ts b/test_framework/test/2.1/improved_any_inference/improved_any_inference_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ef547a12a8798f7ba2440b41e00a1e58db5d1b3e --- /dev/null +++ b/test_framework/test/2.1/improved_any_inference/improved_any_inference_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript2.1 will infer types based on what you end up assigning later on. + This is only enabled if noImplicitAny is set. + ---*/ + + +let x; +x = (a: number) => a + 1; +// function type +let y = x; +Assert.equal(y(12), 13); +// string +x = "abc"; +Assert.isString(x); +Assert.equal(x.toUpperCase(), "ABC"); diff --git a/test_framework/test/2.1/improved_any_inference/improved_any_inference_2.ts b/test_framework/test/2.1/improved_any_inference/improved_any_inference_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0404b733b742f6eabf34a1c7ed59682fd1a02644 --- /dev/null +++ b/test_framework/test/2.1/improved_any_inference/improved_any_inference_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + A variable declared with no type annotation and an initial value of [] is considered an implicit any[] variable. + However, each subsequent x.push(value), x.unshift(value) or x[n] = value operation evolves the type of the variable + in accordance with what elements are added to it. + This is only enabled if noImplicitAny is set. + ---*/ + + +function func() { + let arr = []; + arr[0] = 0; + arr.push("true"); + arr.unshift("abc"); + return arr; +} +// (string | number)[] +type c = ReturnType; + +let cc: c = ["hello", 12]; diff --git a/test_framework/test/2.1/keyof_and_lookup_types/keyof_and_lookup_types_1.ts b/test_framework/test/2.1/keyof_and_lookup_types/keyof_and_lookup_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..aeb8ed524f2e0b6e74899e945f76e5869d235039 --- /dev/null +++ b/test_framework/test/2.1/keyof_and_lookup_types/keyof_and_lookup_types_1.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description:> + An indexed type query keyof T yields the type of permitted property names for T. + A keyof T type is considered a subtype of string. + ---*/ + + +class Person { + name: string; + age: number; + // not permitted property + private job: string; + // must initialize, or cannot pass python script + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + this.job = job; + } +} +type TestName = Pick; +let nn: TestName = { + name: "nn", +}; +Assert.notEqual(nn.name, undefined); + +type TestAge = Pick; +let aa: TestAge = { + age: 20, +}; +Assert.notEqual(aa.age, undefined); + +type Test = Pick; +let cc: Test = { + name: "cc", + age: 15, +}; +Assert.notEqual(cc.name, undefined); +Assert.notEqual(cc.age, undefined); + +type TestString = keyof { [x: string]: number }; + +let str: TestString = "abc"; +Assert.isString(str); diff --git a/test_framework/test/2.1/keyof_and_lookup_types/keyof_and_lookup_types_2.ts b/test_framework/test/2.1/keyof_and_lookup_types/keyof_and_lookup_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..04d6981fa9cee9139c8a4d3110a5589773bcf22a --- /dev/null +++ b/test_framework/test/2.1/keyof_and_lookup_types/keyof_and_lookup_types_2.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description:indexed access types, also called lookup types. + ---*/ + + +class Person { + name: string; + age: number; + // must initialize, or cannot pass python script + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + } +} + +type TestName = Person["name"]; +let tt1: TestName = ""; +Assert.equal(typeof tt1, "string"); +Assert.notEqual(typeof tt1, "number"); +Assert.notEqual(typeof tt1, "boolean"); + +type TestAge = Person["age"]; +let tt2: TestAge = 0; +Assert.equal(typeof tt2, "number"); +Assert.notEqual(typeof tt2, "string"); +Assert.notEqual(typeof tt2, "boolean"); diff --git a/test_framework/test/2.1/mapped_types/mapped_types_1.ts b/test_framework/test/2.1/mapped_types/mapped_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..81216063e57ae2d26371fa51c4c89de350834400 --- /dev/null +++ b/test_framework/test/2.1/mapped_types/mapped_types_1.ts @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Partial + Constructs a type with all properties of Type set to optional. + This utility will return a type that represents all subsets of a given type. + ---*/ + + +interface Person { + name: string; + age: number; + location: string; +} +type PartialPerson = Partial; + +let cc: Person = { + name: "caihua", + age: 20, + location: "earth", +}; + +let dd: PartialPerson; +dd = {}; +Assert.equal(dd.name, undefined); +Assert.equal(dd.age, undefined); +Assert.equal(dd.location, undefined); +dd = { name: "caihua" }; +Assert.equal(dd.name, "caihua"); +Assert.equal(dd.age, undefined); +Assert.equal(dd.location, undefined); +dd = { age: 20 }; +Assert.equal(dd.name, undefined); +Assert.equal(dd.age, 20); +Assert.equal(dd.location, undefined); +dd = { location: "earth" }; +Assert.equal(dd.name, undefined); +Assert.equal(dd.age, undefined); +Assert.equal(dd.location, "earth"); +dd = { name: "caihua", age: 20 }; +Assert.equal(dd.name, "caihua"); +Assert.equal(dd.age, 20); +Assert.equal(dd.location, undefined); +dd = { name: "caihua", location: "earth" }; +dd = { age: 20, location: "earth" }; +dd = { name: "caihua", age: 20, location: "earth" }; +Assert.equal(dd.name, "caihua"); +Assert.equal(dd.age, 20); +Assert.equal(dd.location, "earth"); \ No newline at end of file diff --git a/test_framework/test/2.1/mapped_types/mapped_types_2.ts b/test_framework/test/2.1/mapped_types/mapped_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5c5231295873a15b6b2cf01c02b36672f7f3cf6 --- /dev/null +++ b/test_framework/test/2.1/mapped_types/mapped_types_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Readonly + Constructs a type with all properties of Type set to readonly, + meaning the properties of the constructed type cannot be reassigned. + ---*/ + + +interface Person { + name: string; + age: number; + location: string; +} +type ReadonlyPerson = Readonly; + +let cc: Person = { + name: "caihua", + age: 20, + location: "earth", +}; +cc.name = "caihua1"; +Assert.notEqual(cc.name, "caihua"); +cc.age = 15; +Assert.notEqual(cc.age, 20); +cc.location = "Mars"; +Assert.notEqual(cc.location, "earth"); + +let dd: ReadonlyPerson = { + name: "caihua", + age: 20, + location: "earth", +}; diff --git a/test_framework/test/2.1/mapped_types/mapped_types_3.ts b/test_framework/test/2.1/mapped_types/mapped_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..52cd43cabeeaa6c4a28becae88cc96c26e5517ae --- /dev/null +++ b/test_framework/test/2.1/mapped_types/mapped_types_3.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Record + Constructs an object type whose property keys are Keys and whose property values are Type. + This utility can be used to map the properties of a type to another type. + ---*/ + + +interface CatInfo { + age: number; + breed: string; +} + +type CatName = "miffy" | "boris" | "mordred"; + +const cats: Record = { + miffy: { age: 10, breed: "Persian" }, + boris: { age: 5, breed: "Maine Coon" }, + mordred: { age: 16, breed: "British Shorthair" }, +}; +Assert.equal(cats.miffy.age, 10); +Assert.equal(cats.boris.age, 5); +Assert.equal(cats.mordred.age, 16); +Assert.equal(cats.miffy.breed, "Persian"); +Assert.equal(cats.boris.breed, "Maine Coon"); +Assert.equal(cats.mordred.breed, "British Shorthair"); diff --git a/test_framework/test/2.1/mapped_types/mapped_types_4.ts b/test_framework/test/2.1/mapped_types/mapped_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..e959ceebd1e191a25deb9c6ee8907a98f7c994e2 --- /dev/null +++ b/test_framework/test/2.1/mapped_types/mapped_types_4.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Pick + Constructs a type by picking the set of properties Keys (string literal or union of string literals) from Type. + ---*/ + + + interface Todo { + title: string; + description: string; + completed: boolean; +} + +type TestTitle = Pick; +type TestTitleOrCompleted = Pick; +type TestAll = Pick; + +const title: TestTitle = { + title: "Clean room", +}; +Assert.equal("title" in title, true); +Assert.equal("description" in title, false); +Assert.equal("completed" in title, false); + +const titleAndCompleted: TestTitleOrCompleted = { + title: "Clean room", + completed: false, +}; +Assert.equal("title" in titleAndCompleted, true); +Assert.equal("completed" in titleAndCompleted, true); +Assert.equal("description" in titleAndCompleted, false); + +const testAll: TestAll = { + title: "Clean room", + description: "clean the whole room", + completed: false, +}; +Assert.equal("title" in testAll, true); +Assert.equal("description" in testAll, true); +Assert.equal("completed" in testAll, true); diff --git a/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_1.ts b/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1c268d56deebedbfd95283f7924eaf7d5ebe583 --- /dev/null +++ b/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_1.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: spreading an object can be handy to get a shallow copy. + ---*/ + + +let person = { + name: "caihua", + age: 20, +}; +let job = { job: "stu" }; +let loc = { + location: "mars", + pets: { + type: "dog", + name: "ahuang", + }, +}; + +let copy = { ...person, ...job, ...loc }; + +Assert.equal(copy.name, person.name); +Assert.equal(copy.age, person.age); +Assert.equal(copy.job, job.job); +Assert.equal(copy.location, loc.location); +Assert.equal(copy.pets.name, loc.pets.name); +Assert.equal(copy.pets.type, loc.pets.type); +// shallow copy +Assert.equal(copy.name == person.name, true); +Assert.equal(copy.age == person.age, true); +Assert.equal(copy.job == job.job, true); +Assert.equal(copy.location == loc.location, true); +Assert.equal(copy.pets == loc.pets, true); +Assert.equal(copy.pets.type == loc.pets.type, true); +Assert.equal(copy.pets.name == loc.pets.name, true); diff --git a/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_2.ts b/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..33ce9c977d4f830a0a4fbcd919590e5587569542 --- /dev/null +++ b/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + override existing properties and add new ones + The order of specifying spread operations determines what properties end up in the resulting object; + properties in later spreads "win out" over previously created properties. + ---*/ + + +let person = { + name: "caihua", + age: 20, +}; + +let copy = { ...person, name: "huahua", job: "teacher" }; + +Assert.equal(copy.age, person.age); +Assert.equal(copy.age == person.age, true); +Assert.equal(copy.name != person.name, true); +// later properties +Assert.equal(copy.job != undefined, true); diff --git a/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_3.ts b/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..585d1f8316ba8cd963f05c5e6c75b15100e02bbe --- /dev/null +++ b/test_framework/test/2.1/object_spread_and_rest/object_spread_and_rest_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + override existing properties and add new ones + The order of specifying spread operations determines what properties end up in the resulting object; + properties in later spreads "win out" over previously created properties. + ---*/ + + +let person = { + name: "caihua", + age: 20, +}; + +let copy = { ...person, name: "huahua", job: "teacher" }; + +let { job, ...restProperties } = copy; +Assert.equal(job, copy.job); +Assert.equal(restProperties.age, copy.age); +Assert.equal(restProperties.name, copy.name); diff --git a/test_framework/test/2.1/use_returned_values_from_super_call_as_this/use_returned_values_from_super_call_as_this_1.ts b/test_framework/test/2.1/use_returned_values_from_super_call_as_this/use_returned_values_from_super_call_as_this_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c94b72daae73c140fc9cf8e459f595c2a26b53a --- /dev/null +++ b/test_framework/test/2.1/use_returned_values_from_super_call_as_this/use_returned_values_from_super_call_as_this_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In ES2015, constructors which return an object implicitly substitute the value of this for any callers of super(). + it is necessary to capture any potential return value of super() and replace it with this. + ---*/ + + +class Base { + x: number; + constructor(x: number) { + this.x = x; + return { + x: 1, + }; + } +} +class Derived extends Base { + constructor(x: number) { + super(x); + } +} +let cc = new Derived(12); +Assert.equal(cc.x, 1); +Assert.notEqual(cc.x, 12); diff --git a/test_framework/test/2.2/dotted_property_for_types_with_string_index_signatures/dotted_property_for_types_with_string_index_signatures.ts b/test_framework/test/2.2/dotted_property_for_types_with_string_index_signatures/dotted_property_for_types_with_string_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..27f48ddf73bd53523fe7e204cf85b848325440dd --- /dev/null +++ b/test_framework/test/2.2/dotted_property_for_types_with_string_index_signatures/dotted_property_for_types_with_string_index_signatures.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + types with a string index signature can be indexed using the "[]" notation, but were not allowed to use the ".". Starting with TypeScript 2.2 using either should be allowed. + this only apply to types with an explicit string index signature. It is still an error to access unknown properties on a type using "." notation. + ---*/ + + +interface StringMap { + [x: string]: T; +} +var smap: StringMap = {}; +smap["ATP"] = 0x00; +smap.NARC = 0x01; +Assert.equal(smap.ATP, 0x00); +Assert.equal(smap["NARC"], 0x01); diff --git a/test_framework/test/2.2/object_type/object_type.ts b/test_framework/test/2.2/object_type/object_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..7d1d7cb86521b552dd1f298f144a4f0c9ef13aa9 --- /dev/null +++ b/test_framework/test/2.2/object_type/object_type.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + typeScript did not have a type that represents the non-primitive type, i.e. any thing that is not number, string, boolean, symbol, null, or undefined. + ---*/ + + +var obj1 = { num: 0, str: "string", bool: false, null: null, undefined: undefined }; +Assert.isNumber(obj1.num); +Assert.isString(obj1.str); +Assert.isBoolean(obj1.bool); +Assert.equal(obj1.null, null); +Assert.equal(obj1.undefined, undefined); \ No newline at end of file diff --git a/test_framework/test/2.2/support_for_mix_in_classes/support_for_mix_in_classes_1.ts b/test_framework/test/2.2/support_for_mix_in_classes/support_for_mix_in_classes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f51c9d56cb0959995e3851b4c86f69b0f36f476b --- /dev/null +++ b/test_framework/test/2.2/support_for_mix_in_classes/support_for_mix_in_classes_1.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + typeScript 2.2 adds support for the ECMAScript 2015 mixin class pattern as well as rules for combining mixin construct signatures with regular construct signatures in intersection types. + a mixin constructor type refers to a type that has a single construct signature with a single rest argument of type any[] and an object-like return type. + a mixin class is a class declaration or expression that extends an expression of a type parameter type. + ---*/ + + +class PointXY { + public x: number; + public y: number; + constructor(x: number = 0, y: number = 0) { + this.x = x; + this.y = y; + } + toString() { + return `(${this.x}, ${this.y})`; + } +} +type Constructor = new (...args: any[]) => T; +function mixC>(Base: T) { + return class extends Base { + constructor(...args: any[]) { + super(...args); + } + public pname: string = ""; + setPname(pname: string) { + this.pname = pname; + } + getPname() { return this.pname; } + } +} +const PointMix = mixC(PointXY); +var a = new PointMix(4, 4); +var b = new PointMix(); +a.setPname("A"); +b.setPname("B"); +Assert.equal(a.toString(), "(4, 4)"); +Assert.equal(b.getPname(), "B"); + +class PointMixCopy extends mixC(PointXY) { + public occupied: boolean = false; + setOccupied(occupied: boolean) { this.occupied = occupied; } + getOccupied() { return this.occupied; } +} +var c = new PointMixCopy(5, 5); +c.setPname("C"); +c.setOccupied(true); +Assert.equal(c.toString(), "(5, 5)"); +Assert.equal(c.getPname(), "C"); +Assert.equal(c.getOccupied(), true); diff --git a/test_framework/test/2.2/support_for_mix_in_classes/support_for_mix_in_classes_2.ts b/test_framework/test/2.2/support_for_mix_in_classes/support_for_mix_in_classes_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..67b1d645e3296efacad328dabd15446386f29603 --- /dev/null +++ b/test_framework/test/2.2/support_for_mix_in_classes/support_for_mix_in_classes_2.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + mixin classes can constrain the types of classes they can mix into by specifying a construct signature return type in the constraint for the type parameter. + ---*/ + + +class Color { + public Red: number; + public Green: number; + public Blue: number; + constructor(red: number = 0x00, green: number = 0x00, blue: number = 0x00) { + this.Red = red; + this.Green = green; + this.Blue = blue; + } + toString() { + return `Color(${this.Red}, ${this.Green}, ${this.Blue})`; + } +} +type ConstructorCopy = new (...args: any[]) => T; +function ColorClassMix>(Base: T) { + return class extends Base { + getColors(): [number, number, number] { + return [this.Red, this.Green, this.Blue]; + } + } +} + +const ColorMix = ColorClassMix(Color); +var color = new ColorMix(255, 255, 255); +Assert.equal(color.toString(), "Color(255, 255, 255)"); +Assert.equal(color.getColors()[0], 255); + diff --git a/test_framework/test/2.2/support_for_new.target/support_for_new.target_1.ts b/test_framework/test/2.2/support_for_new.target/support_for_new.target_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1a13403ec7f50f3add6519e2d2d0e9396e8ccb7d --- /dev/null +++ b/test_framework/test/2.2/support_for_new.target/support_for_new.target_1.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the new.target meta-property is new syntax introduced in ES2015. When an instance of a constructor is created via new, the value of new.target is set to be a reference to the constructor function initially used to allocate the instance. + if a function is called rather than constructed via new, new.target is set to undefined. +options: + lib: es2015 +---*/ + + +class A { + public cname: string; + constructor() { + this.cname = new.target.name; + } +} + +class B extends A { constructor() { super(); } } + +var na = new A(); +Assert.equal(na.cname, "A"); +var nb = new B(); +Assert.equal(nb.cname, "B"); + +class C { + public data: any; + constructor() { + this.data = new.target; + } +} +class D extends C { constructor() { super(); } } + +var nc = new C(); +Assert.equal(nc.data.name, "C"); +var nd = new D(); +Assert.equal(nd.data.name, "D"); \ No newline at end of file diff --git a/test_framework/test/2.2/support_for_new.target/support_for_new.target_2.ts b/test_framework/test/2.2/support_for_new.target/support_for_new.target_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7cce41638164b1687321190c6f3d409bdfaee7e7 --- /dev/null +++ b/test_framework/test/2.2/support_for_new.target/support_for_new.target_2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the new.target meta-property is new syntax introduced in ES2015. When an instance of a constructor is created via new, the value of new.target is set to be a reference to the constructor function initially used to allocate the instance. + if a function is called rather than constructed via new, new.target is set to undefined. +options: + lib: es2015 +---*/ + + +var gstr: string = ""; +function newTarget(): void { + if (new.target === undefined) { + gstr = "The function is called without \"new\""; + } else { + gstr = "The function is called with \"new\""; + } +} +newTarget(); +var ntf1: string = gstr; +Assert.equal(ntf1, "The function is called without \"new\""); + +new (newTarget as any)(); +var ntf2: string = gstr; +Assert.equal(ntf2, "The function is called with \"new\""); \ No newline at end of file diff --git a/test_framework/test/2.3/async_iteration/async_generators/async_generators.ts b/test_framework/test/2.3/async_iteration/async_generators/async_generators.ts new file mode 100644 index 0000000000000000000000000000000000000000..8ba136d01f5a89c54208520cbf989ab307ed230f --- /dev/null +++ b/test_framework/test/2.3/async_iteration/async_generators/async_generators.ts @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Async Iteration proposal introduces "Async Generators", which are async functions that also can be used to yield partial computation results. Async Generators can also delegate calls via yield* to either an iterable or async iterable. + as with Generators, Async Generators can only be function declarations, function expressions, or methods of classes or object literals. Arrow functions cannot be Async Generators. Async Generators require a valid, global Promise implementation (either native or an ES2015-compatible polyfill), in addition to a valid Symbol.asyncIterator reference. + options: + lib: es2018 + ---*/ + + +async function* ag() { + let i = 0; + let c = 0; + yield* [0, 0, 0, 0]; + while (true) { + if (i % 3 == 0) { + yield c -= 1; + } else { + yield c += 1; + } + i++; + if (i > 30) { + break; + } + } +} +function newArray() { + let arr: number[] = [0, 0, 0, 0]; + let i = 0; + let c = 0; + while (true) { + if (i % 3 == 0) { + c -= 1; + arr[i + 4] = c; + } else { + c += 1; + arr[i + 4] = c; + } + i++; + if (i > 30) { + break; + } + } + return arr; +} +let arr = ag(); +async function showAsyncGenerator(arr: AsyncGenerator) { + let i = 0; + for await (let x of arr) { + Assert.equal(x as number, arr2[i++]); + } +} +let arr2 = newArray(); +showAsyncGenerator(arr); \ No newline at end of file diff --git a/test_framework/test/2.3/async_iteration/async_iterators/async_iterators_1.ts b/test_framework/test/2.3/async_iteration/async_iterators/async_iterators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a1f590fafad8e41ca43e7eebb963ecc4e7f17096 --- /dev/null +++ b/test_framework/test/2.3/async_iteration/async_iterators/async_iterators_1.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Async Iteration introduces an AsyncIterator, which is similar to Iterator. The difference lies in the fact that the next, return, and throw methods of an AsyncIterator return a Promise for the iteration result, rather than the result itself. + this allows the caller to enlist in an asynchronous notification for the time at which the AsyncIterator has advanced to the point of yielding a value. + options: + lib: es2015 + ---*/ + + +interface AsyncIterator { + next(value?: any): Promise>; + return?(value?: any): Promise>; + throw?(e?: any): Promise>; +} +function createAsyncInterator(arr: any[]): AsyncIterator { + let index = 0; + let len = arr.length; + return { + async next() { + return new Promise((resolve, reject) => { + if (index < len) { + resolve({ value: arr[index++], done: false }); + } else { + resolve({ value: undefined, done: true }); + } + }); + }, + }; +} + +let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]; + +async function exp(arr: any[]) { + let asy = createAsyncInterator(arr); + let i = 0; + let fg; + while (true) { + if (fg == true) { + break; + } + await asy.next().then((v) => { + if (v.done == true) { + fg = true; + } + Assert.equal(v.value, arr[i++]); + return; + }); + } +} +exp(arr); diff --git a/test_framework/test/2.3/async_iteration/async_iterators/async_iterators_2.ts b/test_framework/test/2.3/async_iteration/async_iterators/async_iterators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2af47c73053c41c53602d6595914b799788d0db7 --- /dev/null +++ b/test_framework/test/2.3/async_iteration/async_iterators/async_iterators_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an object that supports async iteration is said to be "iterable" if it has a Symbol.asyncIterator method that returns an AsyncIterator object. + options: + lib: es2018 + ---*/ + + +async function asc(arr: any[]) { + let i = 0; + for await (let x of arr) { + Assert.equal(x, arr[i]); + i++ + } +} +let arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]; +asc(arr); diff --git a/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_1.ts b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..68da16685dd48e879d61de7bf22feee67dd431d9 --- /dev/null +++ b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + es2015 also introduced "Generators", which are functions that can be used to yield partial computation results via the Iterator interface and the yield keyword. + options: + lib: es2015 + ---*/ + + +function* generators() { + var i = 0; + while (true) { + yield i += 3; + if (i > 33) { + break; + } + } +} +let gen = generators(); +let c = 0; +while (true) { + let next = gen.next(); + if (next.done == true) { + break; + } + Assert.equal(next.value, c += 3); +} diff --git a/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_2.ts b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d8d04351313d1cd637c737877dd39858e1526edc --- /dev/null +++ b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/generators/generators_2.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + generators can also internally delegate calls to another iterable through yield *. + options: + lib: es2015 + ---*/ + + +let arr2 = ["A", "B", "C", "D", "E", "F"]; +function* generators2() { + yield* arr2; + let s = arr2[arr2.length - 1].charCodeAt(0); + for (let i = 0; i < 20; i++) { + s++; + yield String.fromCharCode(s); + } +} +let gen2 = generators2(); +let c = 65; +while (true) { + let next = gen2.next(); + if (next.done == true) { + break; + } + Assert.equal(next.value, String.fromCharCode(c++)); +} diff --git a/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_1.ts b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..282746ffd9dab29fd0a11dd718373c73b0bb5a8e --- /dev/null +++ b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_1.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + es2015 introduced Iterator, which is an object that exposes three methods, next, return, and throw. + options: + lib: es2015 + ---*/ + + +interface Iterator { + next(value?: any): IteratorResult; + return?(value?: any): IteratorResult; + throw?(e?: any): IteratorResult; +} +function createInterator(arr: any[]): Iterator { + let index = 0; + let len = arr.length; + return { + next() { + return index < len + ? { value: arr[index++], done: false } + : { value: undefined, done: true } + } + } +} +var arr = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29]; +var iterator = createInterator(arr); +let i = 0; +while (true) { + let n = iterator.next(); + if (n.done == true) { + break; + } + Assert.equal(n.value, arr[i++]); +} diff --git a/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_2.ts b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b753f2813735de68f71edf82bb77c5016fac43f --- /dev/null +++ b/test_framework/test/2.3/generators_and_iteration_for_ES5,ES3/iterators/iterators_2.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + es2015 introduced Iterator, which is an object that exposes three methods, next, return, and throw. + options: + lib: es2015 + ---*/ + + +let arr: string[] = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]; +let iter = arr[Symbol.iterator](); +let i = 0; +while (true) { + let n = iter.next(); + if (n.done == true) { + break; + } + Assert.equal(n.value, arr[i++]); +} +i = 0; +for (let x of arr) { + Assert.equal(x, arr[i++]); +} diff --git a/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_1.ts b/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..93add8c17526d7eb28e2aa02e11dcba89582d9fd --- /dev/null +++ b/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 2.3 adds support for declaring defaults for generic type parameters. + a type parameter is deemed optional if it has a default.Required type parameters must not follow optional type parameters. + when specifying type arguments, you are only required to specify type arguments for the required type parameters. Unspecified type parameters will resolve to their default types.If a default type is specified and inference cannot choose a candidate, the default type is inferred. + ---*/ + + +type ALL = string | number | boolean | object; +function fun1(v: V, t: T) { + return JSON.stringify({ v, t }); +} +let f1 = fun1(1, [1, 0]); +Assert.equal(f1, "{\"v\":1,\"t\":[1,0]}") + +let f2 = fun1("A", 0); +Assert.equal(f2, "{\"v\":\"A\",\"t\":0}"); + +let f3 = fun1("A", [1, "A"]); +Assert.equal(f3, "{\"v\":\"A\",\"t\":[1,\"A\"]}"); diff --git a/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_2.ts b/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..157c624268e92a307839d24865c0ff5d1fbfabc8 --- /dev/null +++ b/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_2.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 2.3 adds support for declaring defaults for generic type parameters. + a class or interface declaration that merges with an existing class or interface declaration may introduce a default for an existing type parameter. + a class or interface declaration that merges with an existing class or interface declaration may introduce a new type parameter as long as it specifies a default. + ---*/ + + +class CA{ + ca0: V; + ca1: T; + ca2: U; + constructor(ca0: V, ca1: T, ca2: U) { this.ca0 = ca0; this.ca1 = ca1; this.ca2 = ca2; } + creatOBJ() { + let a = this.ca0; + let b = this.ca1; + let c = this.ca2; + return { a, b, c }; + } + toJSON() { + return JSON.stringify(this.creatOBJ()); + } +} +class CCA extends CA{ } +let cca1 = new CCA(0, [0, 0], "[0]"); +Assert.equal(cca1.toJSON(), "{\"a\":0,\"b\":[0,0],\"c\":\"[0]\"}"); +let cca2 = new CCA("A", ["A"], [["A"], ["B"]]); +Assert.equal(cca2.toJSON(), "{\"a\":\"A\",\"b\":[\"A\"],\"c\":[[\"A\"],[\"B\"]]}"); + +interface IB { + ia: V; + ib: T; + ic: U; +} +interface CIB extends IB { } +let cib1: CIB = { ia: 0, ib: [0], ic: [[0], [1, 2]] }; +Assert.equal(JSON.stringify(cib1), "{\"ia\":0,\"ib\":[0],\"ic\":[[0],[1,2]]}"); +let cib2: CIB = { ia: "A", ib: ["A", "B"], ic: [["A"], ["B", "C"]] }; +Assert.equal(JSON.stringify(cib2), "{\"ia\":\"A\",\"ib\":[\"A\",\"B\"],\"ic\":[[\"A\"],[\"B\",\"C\"]]}"); diff --git a/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_3.ts b/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..eb875ac3fa81212600766960de514b981171d6e3 --- /dev/null +++ b/test_framework/test/2.3/generic_parameter_defaults/generic_parameter_defaults_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 2.3 adds support for declaring defaults for generic type parameters. + default types for a type parameter must satisfy the constraint for the type parameter, if it exists. + ---*/ + + +interface Length { + length: number; +} +type STRS = string | string[] +function fun3(s: T) { + return s.length; +} +Assert.equal(fun3("ABC"), 3); +Assert.equal(fun3([1, 2, 3, 4]), 4); diff --git a/test_framework/test/2.4/1_dynamic_import_expressions/1_dynamic_import_expressions.ts b/test_framework/test/2.4/1_dynamic_import_expressions/1_dynamic_import_expressions.ts new file mode 100644 index 0000000000000000000000000000000000000000..f143b63daf6b2027641d1490b5e4d958a5ee77d9 --- /dev/null +++ b/test_framework/test/2.4/1_dynamic_import_expressions/1_dynamic_import_expressions.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Dynamic import expressions are a new feature and part of ECMAScript that allows users to asynchronously request a module at any arbitrary point in your program. +module: ESNext +isCurrent: true +---*/ + + +async function getSumAsyn(x: number, y: number): Promise { + const add = await import("./lib.js"); + const sum = add.add(x, y); + return sum; +} + +getSumAsyn(10, 20).then((sum) => { + equal(30, sum); +}); + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} \ No newline at end of file diff --git a/test_framework/test/2.4/1_dynamic_import_expressions/lib.ts b/test_framework/test/2.4/1_dynamic_import_expressions/lib.ts new file mode 100644 index 0000000000000000000000000000000000000000..b54b6f553cf7aa65bb412799710bc173c4df8590 --- /dev/null +++ b/test_framework/test/2.4/1_dynamic_import_expressions/lib.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export function add(x: number, y: number) { + return x + y; +} \ No newline at end of file diff --git a/test_framework/test/2.4/2_string_enums/string_enums_1.ts b/test_framework/test/2.4/2_string_enums/string_enums_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b69e4e1619dffdc26f1cc573526962836e12b787 --- /dev/null +++ b/test_framework/test/2.4/2_string_enums/string_enums_1.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript 2.4 now allows enum members to contain string initializers. + ---*/ + + +enum Colors { + Red = "RED", + Green = "GREEN", + Blue = "BLUE" +} + +Assert.equal(Colors.Red, "RED", "Colors.Red is not RED") \ No newline at end of file diff --git a/test_framework/test/2.4/3_improved_inference_for_generics/3_improved_inference_for_generics.ts b/test_framework/test/2.4/3_improved_inference_for_generics/3_improved_inference_for_generics.ts new file mode 100644 index 0000000000000000000000000000000000000000..af7ff77b7d1810066a6ce184bee5a4384de143ce --- /dev/null +++ b/test_framework/test/2.4/3_improved_inference_for_generics/3_improved_inference_for_generics.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.4 introduces a few wonderful changes around the way generics are inferred. +---*/ + + +// For one, TypeScript can now make inferences for the return type of a call. This can improve your experience and catch errors. Something that now works: +function arrayMap(f: (x: T) => U): (a: T[]) => U[] { + return a => a.map(f); +} + +const lengths: (a: string[]) => number[] = arrayMap(s => s.length); +Assert.equal(1, lengths(["s", "ss", "sss"])[0]); +Assert.equal(2, lengths(["s", "ss", "sss"])[1]); +Assert.equal(3, lengths(["s", "ss", "sss"])[2]); + +// TypeScript now tries to unify type parameters when comparing two single-signature types. +type A = (x: T, y: U) => [T, U]; +type B = (x: S, y: S) => [S, S]; + +function f(a: A, b: B) { + b = a; + Assert.isTrue(true); +} + +let a: A = function funA(x: T, y: U): [T, U] { + return [x, y]; +} +let b: B = function funB(x: S, y: S): [S, S] { + return [x, x]; +} +f(a, b); \ No newline at end of file diff --git a/test_framework/test/2.4/4_strict_contravariance_for_callback_parameters/4_strict_contravariance_for_callback_parameters.ts b/test_framework/test/2.4/4_strict_contravariance_for_callback_parameters/4_strict_contravariance_for_callback_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f1b775f522638e075e81846877e29769300d536 --- /dev/null +++ b/test_framework/test/2.4/4_strict_contravariance_for_callback_parameters/4_strict_contravariance_for_callback_parameters.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript 2.4 introduces tightens this up when relating two callback types. + ---*/ + + +interface MyInteface { + add_10(t: T): T; +} + +class myA implements MyInteface { + add_10(n: number) { + return n += 10; + } +} + +let a: MyInteface; +let b: MyInteface; + +a = new myA(); + +b = a; + +// can to here +Assert.equal(20, b.add_10(10)); \ No newline at end of file diff --git a/test_framework/test/2.4/5_weak_type_detection/5_weak_type_detection.ts b/test_framework/test/2.4/5_weak_type_detection/5_weak_type_detection.ts new file mode 100644 index 0000000000000000000000000000000000000000..b4bf4134339a450f83517348ee614be4f8175fb9 --- /dev/null +++ b/test_framework/test/2.4/5_weak_type_detection/5_weak_type_detection.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.4 introduces the concept of "weak types". Any type that contains nothing but a set of all-optional properties is considered to be weak. +---*/ + + +interface Options { + data?: string; + timeout?: number; + maxRetries?: number; +} + +function sendMessage(options: Options) { + Assert.isTrue(true); +} + +const opts = { + payload: "hello world!", + retryOnFail: true +}; + +// Declare the properties if they really do exist. +const opts2 = { + data: "hello world!" +}; +sendMessage(opts2); + +// Add an index signature to the weak type (i.e. [propName: string]: {}). +const opts3: { + [index: string]: { data: string }; +} = {}; + +sendMessage(opts3); + +// Use a type assertion (i.e. opts as Options). +const opts4 = { + payload: "hello world!", + retryOnFail: true +} as Options; + +sendMessage(opts4); \ No newline at end of file diff --git a/test_framework/test/2.5/1_optional_catch_clause_variables.ts b/test_framework/test/2.5/1_optional_catch_clause_variables.ts new file mode 100644 index 0000000000000000000000000000000000000000..f279512c8a9c27b500f8d468c7b3938b42a24cb6 --- /dev/null +++ b/test_framework/test/2.5/1_optional_catch_clause_variables.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript 2.5 implements a new ECMAScript feature that allows users to omit the variable in catch clauses. + ---*/ + + +let input = "have a exception!"; +try { + JSON.parse(input); +} catch { + // ^ Notice that our `catch` clause doesn't declare a variable. + Assert.equal("have a exception!", input); +} \ No newline at end of file diff --git a/test_framework/test/2.6/suppress_errors_in_ts_files_using_ts_ignore_comments/suppress_errors_in_ts_files_using_ts_ignore_comments.ts b/test_framework/test/2.6/suppress_errors_in_ts_files_using_ts_ignore_comments/suppress_errors_in_ts_files_using_ts_ignore_comments.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d903edc6a098e7e43a6a94f5a9cecfc060e7d82 --- /dev/null +++ b/test_framework/test/2.6/suppress_errors_in_ts_files_using_ts_ignore_comments/suppress_errors_in_ts_files_using_ts_ignore_comments.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A // @ts-ignore comment suppresses all errors that originate on the following line. + It is recommended practice to have the remainder of the comment following @ts-ignore explain which error is being suppressed. + Please note that this comment only suppresses the error reporting, and we recommend you use this comments very sparingly. + ---*/ + + +function addString(str: string, str2: string): string { + str += ""; + str2 += ""; + return str + str2; +} +// @ts-ignore +let n: number = addString(114, 514); +Assert.isString(n); +Assert.equal(n, "114514"); diff --git a/test_framework/test/2.7/10_numeric_separators/10_numeric_separators.ts b/test_framework/test/2.7/10_numeric_separators/10_numeric_separators.ts new file mode 100644 index 0000000000000000000000000000000000000000..932aa114a500701d70f89afb9304c66b9b41f0a4 --- /dev/null +++ b/test_framework/test/2.7/10_numeric_separators/10_numeric_separators.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.7 brings support for ES Numeric Separators. Numeric literals can now be separated into segments using _. + ---*/ + + +const million = 1_000_000; +const phone = 555_734_2231; +const bytes = 0xff_0c_00_ff; +const word = 0b1100_0011_1101_0001; + +Assert.equal(1000000, million); +Assert.equal(5557342231, phone); +Assert.equal(4278976767, bytes); +Assert.equal(50129, word); \ No newline at end of file diff --git a/test_framework/test/2.7/1_constant_named_properties/1_constant_named_properties.ts b/test_framework/test/2.7/1_constant_named_properties/1_constant_named_properties.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d7c395e57a08157d8d5b9164bd2b3ace5c52066 --- /dev/null +++ b/test_framework/test/2.7/1_constant_named_properties/1_constant_named_properties.ts @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.7 adds support for declaring const-named properties on types including ECMAScript symbols. +module: ESNext +isCurrent: true +---*/ + + +import { SERIALIZE, Serializable } from "./lib.js"; + +// consumer +class JSONSerializableItem implements Serializable { + [SERIALIZE](obj: {}) { + return JSON.stringify(obj); + } +} +var obj = new JSONSerializableItem(); +equal('"test open harmony"', obj[SERIALIZE]("test open harmony")); +equal(123456, obj[SERIALIZE](123456)); + + +// This also applies to numeric and string literals. +const Foo = "Foo"; +const Bar = "Bar"; + +let x = { + [Foo]: 100, + [Bar]: "hello" +}; + +// has type 'number' +let a = x[Foo]; +// has type 'string' +let b = x[Bar]; +equal(100, a); +equal("hello", b); + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} \ No newline at end of file diff --git a/test_framework/test/2.7/1_constant_named_properties/lib.ts b/test_framework/test/2.7/1_constant_named_properties/lib.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b5ff502f0b99b33d68e5f7576a2d3d76a5dfdfd --- /dev/null +++ b/test_framework/test/2.7/1_constant_named_properties/lib.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +// Lib +export const SERIALIZE = Symbol("serialize-method-key"); +export interface Serializable { + [SERIALIZE](obj: {}): string; +} \ No newline at end of file diff --git a/test_framework/test/2.7/2_unique_symbol/2_unique_symbol.ts b/test_framework/test/2.7/2_unique_symbol/2_unique_symbol.ts new file mode 100644 index 0000000000000000000000000000000000000000..beef5339c5e171a70d16dbf5f4b703f0bfc914c1 --- /dev/null +++ b/test_framework/test/2.7/2_unique_symbol/2_unique_symbol.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: To enable treating symbols as unique literals a new type unique symbol is available. +---*/ + + +// Works +declare const Foo: unique symbol; + +// unique symbol is a subtype of symbol, and are produced only from calling Symbol() or Symbol.for(), or from explicit type annotations. +const Bar: unique symbol = Symbol(); +const Bar2: unique symbol = Symbol.for("Bar2"); + +// Works - refers to a unique symbol, but its identity is tied to 'Foo'. +// in order to reference a specific unique symbol, you’ll have to use the typeof operator. +let Baz: typeof Bar = Bar; +let Baz2: typeof Bar2 = Bar2; + +// Also works. +class C { + static readonly StaticSymbol: unique symbol = Symbol(); +} + +Assert.isTrue('symbol' === typeof Baz); +Assert.isTrue('symbol' === typeof Baz2); +Assert.isTrue('symbol' === typeof C.StaticSymbol); \ No newline at end of file diff --git a/test_framework/test/2.7/3_strict_class_initialization/3_strict_class_initialization.ts b/test_framework/test/2.7/3_strict_class_initialization/3_strict_class_initialization.ts new file mode 100644 index 0000000000000000000000000000000000000000..ef79d18d0c153b949ee1cafa9f44de0396fdc45a --- /dev/null +++ b/test_framework/test/2.7/3_strict_class_initialization/3_strict_class_initialization.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.7 introduces a new flag called strictPropertyInitialization. This flag performs checks to ensure that each instance property of a class gets initialized in the constructor body, or by a property initializer. +---*/ + + +class C { + foo: number = 1; + foo2!: number; + bar = "hello"; + baz: boolean = true; + + constructor() { + this.initialize(); + } + + initialize() { + this.foo2 = 2; + } +} + +let c = new C(); +Assert.equal(1, c.foo); +Assert.equal(2, c.foo2); +Assert.equal('hello', c.bar); +Assert.isTrue(c.baz); \ No newline at end of file diff --git a/test_framework/test/2.7/4_definite_assignment_assertions/4_definite_assignment_assertions.ts b/test_framework/test/2.7/4_definite_assignment_assertions/4_definite_assignment_assertions.ts new file mode 100644 index 0000000000000000000000000000000000000000..8c8902c9c332a8e2eab905cbe33182c8083cdf57 --- /dev/null +++ b/test_framework/test/2.7/4_definite_assignment_assertions/4_definite_assignment_assertions.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: The definite assignment assertion is a feature that allows a ! to be placed after instance property and variable declarations to relay to TypeScript that a variable is indeed assigned for all intents and purposes, even if TypeScript's analyses cannot detect so. +---*/ + + +// With definite assignment assertions, we can assert that x is really assigned by appending an ! to its declaration +let x!: number; + +// In a sense, the definite assignment assertion operator is the dual of the non-null assertion operator (in which expressions are post-fixed with a !) +let y = x! + x!; + +Assert.isTrue(Number.isNaN(y)); +initialize(); + +// No error! +let z = x + x; +function initialize() { + x = 10; +} + +Assert.equal(20, z); \ No newline at end of file diff --git a/test_framework/test/2.7/5_fixed_length_tuples/5_fixed_length_tuples.ts b/test_framework/test/2.7/5_fixed_length_tuples/5_fixed_length_tuples.ts new file mode 100644 index 0000000000000000000000000000000000000000..1835eec6f739157e6fae9dcf3d38d7f99790d591 --- /dev/null +++ b/test_framework/test/2.7/5_fixed_length_tuples/5_fixed_length_tuples.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: tuple types now encode their arity into the type of their respective length property. This is accomplished by leveraging numeric literal types, which now allow tuples to be distinct from tuples of different arities. +---*/ + + +interface NumStrTuple extends Array { + 0: number; + 1: string; + // using the numeric literal type '2' + length: 2; +} + +const myNumStrTuple: NumStrTuple = [1, "this is a string"]; + +// If you need to resort to the original behavior in which tuples only enforce a minimum length, you can use a similar declaration that does not explicitly define a length property +interface MinimumNumStrTuple extends Array { + 0: number; + 1: string; +} + +const myMinimumNumStrTuple: MinimumNumStrTuple = [2, "this is a string", 3, "two string"]; + + +Assert.equal(1, myNumStrTuple[0]); +Assert.equal("this is a string", myNumStrTuple[1]); +Assert.equal(2, myMinimumNumStrTuple[0]); +Assert.equal("this is a string", myMinimumNumStrTuple[1]); +Assert.equal(3, myMinimumNumStrTuple[2]); +Assert.equal("two string", myMinimumNumStrTuple[3]); \ No newline at end of file diff --git a/test_framework/test/2.7/6_improved_type_inference_for_object_literals/6_improved_type_inference_for_object_literals.ts b/test_framework/test/2.7/6_improved_type_inference_for_object_literals/6_improved_type_inference_for_object_literals.ts new file mode 100644 index 0000000000000000000000000000000000000000..2595c85f70c7b937ad29e8ddd24ee15f613c0e7d --- /dev/null +++ b/test_framework/test/2.7/6_improved_type_inference_for_object_literals/6_improved_type_inference_for_object_literals.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.7 improves type inference for multiple object literals occurring in the same context. When multiple object literal types contribute to a union type, we now normalize the object literal types such that all properties are present in each constituent of the union type. +---*/ + + +let obj = [{ a: 1, b: 2 }, { a: "abc" }, {}][0]; + +// Multiple object literal type inferences for the same type parameter are similarly collapsed into a single normalized union type +function f(...items: T[]): T { + return items[1]; +}; +let obj2 = f({ a: 1, b: 2 }, { a: "abc", b: "cde" }, {}); + +Assert.equal(1, obj.a); +Assert.equal(2, obj.b); +Assert.equal("abc", obj2.a); +Assert.equal("cde", obj2.b); \ No newline at end of file diff --git a/test_framework/test/2.7/7_improved_handling/7_improved_handling.ts b/test_framework/test/2.7/7_improved_handling/7_improved_handling.ts new file mode 100644 index 0000000000000000000000000000000000000000..dc8ae2ff2659918ee1fdf4e2857dbd11b9749add --- /dev/null +++ b/test_framework/test/2.7/7_improved_handling/7_improved_handling.ts @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript 2.7 improves the handling of structurally identical classes in union types and instanceof expressions: + Structurally identical, but distinct, class types are now preserved in union types (instead of eliminating all but one). + Union type subtype reduction only removes a class type if it is a subclass of and derives from another class type in the union. + Type checking of the instanceof operator is now based on whether the type of the left operand derives from the type indicated by the right operand (as opposed to a structural subtype check). +---*/ + + +class A { } +class B extends A { } +class C extends A { } +class D extends A { } +class E extends D { } + +// A +let x1 = !true ? new A() : new B(); +// B | C (previously B) +let x2 = !true ? new B() : new C(); +// C | D (previously C) +let x3 = !true ? new C() : new D(); +Assert.isTrue(x1 instanceof B); +Assert.isTrue(x2 instanceof C); +Assert.isTrue(x3 instanceof D); + +// A[] +let a1 = [new A(), new B(), new C(), new D(), new E()]; +// (B | C | D)[] (previously B[]) +let a2 = [new B(), new C(), new D(), new E()]; + +Assert.isTrue(a1[0] instanceof A); +Assert.isTrue(a1[1] instanceof B); +Assert.isTrue(a1[2] instanceof C); +Assert.isTrue(a1[3] instanceof D); +Assert.isTrue(a1[4] instanceof E); + + +Assert.isTrue(a2[0] instanceof B); +Assert.isTrue(a2[1] instanceof C); +Assert.isTrue(a2[2] instanceof D); +Assert.isTrue(a2[3] instanceof E); + + +function f1(x: B | C | D) { + if (x instanceof B) { + Assert.isTrue(x instanceof B); + } else if (x instanceof C) { + Assert.isTrue(x instanceof C); + } else { + Assert.isFalse(x instanceof B); + Assert.isFalse(x instanceof C); + } +} + +f1(new A()); +f1(new B()); +f1(new C()); +f1(new D()); \ No newline at end of file diff --git a/test_framework/test/2.7/8_Type_guards_inferred_from_in_operator/8_Type_guards_inferred_from_in_operator.ts b/test_framework/test/2.7/8_Type_guards_inferred_from_in_operator/8_Type_guards_inferred_from_in_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..8acc0e41e143f7d8d5a2bed435f0fd5bae240436 --- /dev/null +++ b/test_framework/test/2.7/8_Type_guards_inferred_from_in_operator/8_Type_guards_inferred_from_in_operator.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The in operator now acts as a narrowing expression for types. + For a n in x expression, where n is a string literal or string literal type and x is a union type, the “true” branch narrows to types which have an optional or required property n, and the “false” branch narrows to types which have an optional or missing property n. +---*/ + + +interface A { + a: number; +} +interface B { + b: string; +} + +function foo(x: A | B) { + if ("a" in x) { + return x.a; + } + return x.b; +} + +let a1: A = { + a: 1 +} + +let b1: B = { + b: 'this is a string' +} + +Assert.equal(1, foo(a1)); +Assert.equal('this is a string', foo(b1)); \ No newline at end of file diff --git a/test_framework/test/2.8/conditional_types.ts b/test_framework/test/2.8/conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa5cf3c583a3e815252604bbcf65d437f313bcfb --- /dev/null +++ b/test_framework/test/2.8/conditional_types.ts @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Conditional Types. +module: ESNext +isCurrent: true + ---*/ + + +type TypeName = + T extends string ? string : + T extends number ? number : + T extends boolean ? boolean : + T extends undefined ? undefined : + T extends Function ? Function : + object; + +type T0 = TypeName; +type T1 = TypeName<"a">; +type T2 = TypeName; +type T3 = TypeName<() => void>; +type T4 = TypeName; + + +let a:T0 = "string"; +let b:T1 = 'string'; +let c:T2 = true; +let d:T3 = (() => {}); +let e:T4 = {}; + +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + + +equal(typeof a,'string'); +equal(typeof b,'string'); +equal(typeof c,'boolean'); +equal(typeof d,'function'); +equal(typeof e,'object'); + + +export default TypeName \ No newline at end of file diff --git a/test_framework/test/2.8/distributive_conditional_types.ts b/test_framework/test/2.8/distributive_conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..b0712210ffcb1102e569b3df3b1c1e6a2ae63430 --- /dev/null +++ b/test_framework/test/2.8/distributive_conditional_types.ts @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Distributive conditional types +module: ESNext +isCurrent: true +---*/ + + +import TypeName from './conditional_types'; + + +type T0 = TypeName void)>; +type T2 = TypeName; +type T1 = TypeName; + +type BoxedValue = { value: T }; +type BoxedArray = { array: T[] }; +type Boxed = T extends any[] ? BoxedArray : BoxedValue; + +type T20 = Boxed; +type T21 = Boxed; +type T22 = Boxed; + +let a: T0 = 's'; +let b: T0 = (() => { +}); +let c: T2 = 's'; +let d: T2 = ['s']; +let e: T2 = undefined; +let f: T1 = ['s']; +let g: T1 = [1]; +let h: T20 = {value: "s"}; +let i: T21 = {array: [1]}; +let j: T22 = {value: "s"}; +let k: T22 = {array: [1]}; + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(typeof a, 'string'); +equal(typeof b, 'function'); +equal(typeof c, 'string'); +equal(typeof d, 'object'); +equal(typeof e, 'undefined'); +equal(typeof f, 'object'); +equal(typeof g, 'object'); +equal(typeof h, 'object'); +equal(typeof i, 'object'); +equal(typeof j, 'object'); +equal(typeof k, 'object'); + + + diff --git a/test_framework/test/2.8/filter_union_types.ts b/test_framework/test/2.8/filter_union_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..df4ca0083ba37854d0344df0343e411abfa164f6 --- /dev/null +++ b/test_framework/test/2.8/filter_union_types.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The distributive property of conditional types can conveniently be used to filter union types + ---*/ + + +{ + type Diff = T extends U ? never : T; + type Filter = T extends U ? T : never; + + type T0 = Diff; + type T1 = Filter; + + let a: T0 = 'b'; + let b: T0 = true; + let c: T1 = 1; + + Assert.equal(typeof a, 'string'); + Assert.equal(typeof b, 'boolean'); + Assert.equal(typeof c, 'number'); +} \ No newline at end of file diff --git a/test_framework/test/2.8/improved_control_over_mapped_type_modifiers.ts b/test_framework/test/2.8/improved_control_over_mapped_type_modifiers.ts new file mode 100644 index 0000000000000000000000000000000000000000..089b17496732961cae50444aac360cb335355cbf --- /dev/null +++ b/test_framework/test/2.8/improved_control_over_mapped_type_modifiers.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Improved control over mapped type modifiers + ---*/ + + +{ + type A = { a: string }; + type B = { b: string }; + + // "a" | "b" + type T1 = keyof (A & B); + // keyof T | "b" + type T2 = keyof (T & B); + // "a" | keyof U + type T3 = keyof (A & U); + // keyof T | keyof U + type T4 = keyof (T & U); + // "a" | "b" + type T5 = T2; + // "a" | "b" + type T6 = T3; + // "a" | "b" + type T7 = T4; + + let a: T1 = 'a'; + let b: T1 = "b"; + let c: T5 = "a"; + let d: T6 = "a"; + let e: T7 = "a"; + + Assert.equal(a, 'a'); + Assert.equal(b, 'b'); + Assert.equal(c, 'a'); + Assert.equal(d, 'a'); + Assert.equal(e, 'a'); +} + diff --git a/test_framework/test/2.8/predefined_conditional_types.ts b/test_framework/test/2.8/predefined_conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..834a1cc88d87e417ce051f1e3fe90b1c5922eb65 --- /dev/null +++ b/test_framework/test/2.8/predefined_conditional_types.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Predefined conditional types + ---*/ + + +{ + // "b" | "d" + type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">; + // "a" | "c" + type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">; + + // string | number + type T02 = Exclude void), Function>; + // () => void + type T03 = Extract void), Function>; + + function f1(s: string) { + return { a: 1, b: s }; + } + + class C { + x = 0; + y = 0; + } + + // { a: number, b: string } + type T14 = ReturnType; + // C + type T20 = InstanceType; + + let a: T00 = "b"; + let b: T00 = "d"; + let c: T01 = "a"; + let d: T01 = "c"; + let e: T02 = "c"; + let f: T03 = ((): string => { return 's' }); + let x: T14 = { a: 1, b: 's' } + let y: T20 = { x: 1, y: 2 } + + Assert.equal(a, 'b'); + Assert.equal(b, 'd'); + Assert.equal(c, 'a'); + Assert.equal(d, 'c'); + Assert.equal(e, 'c'); + Assert.equal(typeof f, "function"); + Assert.equal(x.a, 1) + Assert.equal(y.x, 1) +} diff --git a/test_framework/test/2.8/type_inference.ts b/test_framework/test/2.8/type_inference.ts new file mode 100644 index 0000000000000000000000000000000000000000..e76eb45e1574c16d388c7dee66f919367f6c976d --- /dev/null +++ b/test_framework/test/2.8/type_inference.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type inference in conditional types + ---*/ + + +{ + type Unpacked = + T extends (infer U)[] ? U : + T extends (...args: any[]) => infer U ? U : + T extends Promise ? U : + T; + + // string + type T0 = Unpacked; + // string + type T1 = Unpacked; + // string + type T2 = Unpacked<() => string>; + // string + type T3 = Unpacked>; + // string + type T4 = Unpacked[]>>; + + let a: T0 = 's'; + let b: T1 = 's'; + let c: T2 = 's'; + let d: T3 = 's'; + let e: T4 = 's'; + + Assert.equal(typeof a, 'string'); + Assert.equal(typeof b, 'string'); + Assert.equal(typeof c, 'string'); + Assert.equal(typeof d, 'string'); + Assert.equal(typeof e, 'string'); + + type Foo = T extends { a: infer U, b: infer U } ? U : never; + // string + type T10 = Foo<{ a: string, b: string }>; + // string | number + type T11 = Foo<{ a: string, b: number }>; + + let f: T10 = 's'; + let g: T11 = 's'; + let h: T11 = 1; + + Assert.equal(typeof f, 'string'); + Assert.equal(typeof g, 'string'); + Assert.equal(typeof h, 'number'); +} \ No newline at end of file diff --git a/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_1.ts b/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ba2c41f221cd04065629112b0269e646a2e7495e --- /dev/null +++ b/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_1.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.9 adds support for number and symbol named properties in index types and mapped types. +---*/ + + +const c = "c"; +const d = 10; +const e = Symbol(); +const f = true; + +const enum E1 { A, B, C } +const enum E2 { A = "A", B = "B", C = "C" } + +// An index type keyof T for some type T is a subtype of string | number | symbol. +type Foo = { + a: string; + 5: string; + // A mapped type { [P in K]: XXX } permits any K assignable to string | number | symbol. + [c]: string; + [d]: string; + [e]: string; + // f is boolean, so it can not be used here, like: [f]: string; + [E1.A]: string; + [E2.A]: string; +} + +// In a for...in statement for an object of a generic type T, the inferred type of the iteration variable was previously keyof T but is now Extract. (In other words, the subset of keyof T that includes only string-like values.) +type K1 = keyof Foo; +type K2 = Extract; +type K3 = Extract; +type K4 = Extract; + + +let k1: K1 = 'c'; +let k2: K2 = E2.A; +let k3: K3 = 10; +Assert.equal("c", k1); +Assert.equal(E2.A, k2); +Assert.equal(10, k3); \ No newline at end of file diff --git a/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_2.ts b/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..515167924b79e07f29e663b135708b538f26d5f1 --- /dev/null +++ b/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_2.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Since keyof now reflects the presence of a numeric index signature by including type number in the key type, mapped types such as Partial and Readonly work correctly when applied to object types with numeric index signatures. +---*/ + + +type Arrayish = { + length: number; + [x: number]: T; +} + +type ReadonlyArrayish = Readonly>; + +let map: ReadonlyArrayish = { + [1]: "b", + [2]: "a", + length: 2, +}; + + +let n = map.length; +let x = map[1]; +Assert.equal(2, n); +Assert.equal("b", x); \ No newline at end of file diff --git a/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_3.ts b/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..bd533e84b9d0578359c159a51f5ec5835886a7ff --- /dev/null +++ b/test_framework/test/2.9/1_support_number_and_symbol_nam/1_support_number_and_symbol_nam_3.ts @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: with the keyof operator's support for number and symbol named keys, it is now possible to abstract over access to properties of objects that are indexed by numeric literals (such as numeric enum types) and unique symbols. +---*/ + + +const enum Enum { + A, + B, + C, +} + +const enumToStringMap = { + [Enum.A]: "Name A", + [Enum.B]: "Name B", + [Enum.C]: "Name C", +}; + +const sym1 = Symbol(); +const sym2 = Symbol(); +const sym3 = Symbol(); + +const symbolToNumberMap = { + [sym1]: 1, + [sym2]: 2, + [sym3]: 3, +}; + +type KE = keyof typeof enumToStringMap; +type KS = keyof typeof symbolToNumberMap; + +function getValue(obj: T, key: K): T[K] { + return obj[key]; +} + +let x1 = getValue(enumToStringMap, Enum.C); +let x2 = getValue(symbolToNumberMap, sym3); +Assert.equal("Name C", x1); +Assert.equal(3, x2); + + +// If your functions are only able to handle string named property keys, use Extract in the declaration +function useKey1>(o: T, k: K) { + var name: string = k; + return name; +} + +const stringMap = { + a: "mystring" +}; +let x3 = useKey1(stringMap, "a"); +Assert.equal("a", x3); + +// your functions are open to handling all property keys, then the changes should be done down-stream +function useKey2(o: T, k: K) { + var name: string | number | symbol = k; + return name; +} +const uniteMap = { + a: "mystring", + b: 1 +}; +let x4 = useKey2(uniteMap, "b"); +Assert.equal("b", x4); \ No newline at end of file diff --git a/test_framework/test/2.9/3_generic_type_arguments_in_tt.ts b/test_framework/test/2.9/3_generic_type_arguments_in_tt.ts new file mode 100644 index 0000000000000000000000000000000000000000..958024bce5b01c22ace09f832749f4144823d450 --- /dev/null +++ b/test_framework/test/2.9/3_generic_type_arguments_in_tt.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 2.9 allows passing generic type arguments to tagged template strings. +---*/ + + +let dessert = 'cake' + +function kitchen(strings: TemplateStringsArray, value: string) { + Assert.equal(2, strings.length); +} + +let breakfast = kitchen`今天的早餐是${dessert}!`; + + + +function tag(strs: TemplateStringsArray, args: T): T { + Assert.equal(2, strs.length); + return args; +}; + + +let a = tag `今天的午餐是${dessert}!`; +Assert.equal("cake", a); + +let b = tag `今天的午餐是${"dessert"}!`; + +Assert.equal("dessert", b); diff --git a/test_framework/test/2.9/4_import_types.ts b/test_framework/test/2.9/4_import_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b70c5b1dc6689c6cc941454f86c8dd3e8b0bf0c --- /dev/null +++ b/test_framework/test/2.9/4_import_types.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Modules can import types declared in other modules. But non-module global scripts cannot access types declared in modules. Enter import types. + Using import("mod") in a type annotation allows for reaching in a module and accessing its exported declaration without importing it. +---*/ + + +function adopt(p: import("./module").Pet) { + Assert.equal('puppy', `${p.name}`); + Assert.equal('puppy', p.name); +} + +let p: import("./module").Pet = { + name: "puppy", +}; +adopt(p); \ No newline at end of file diff --git a/test_framework/test/2.9/5_relaxing_declaration.ts b/test_framework/test/2.9/5_relaxing_declaration.ts new file mode 100644 index 0000000000000000000000000000000000000000..fdd5ac8678ec53c4d7468f320d48c8af53e6b5c9 --- /dev/null +++ b/test_framework/test/2.9/5_relaxing_declaration.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + With import types available, many of the visibility errors reported during declaration file generation can be handled by the compiler without the need to change the input. + need install module: npm install @types/node + module: ESNext + isCurrent: true +---*/ + + +import { createHash } from "crypto"; + +export const hash = createHash("md5"); + +hash.update("a"); + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal("DMF1ucDxtqgxw5niaXcmYQ==", hash.digest('base64')); + diff --git a/test_framework/test/2.9/6_support_for_import_meta.ts b/test_framework/test/2.9/6_support_for_import_meta.ts new file mode 100644 index 0000000000000000000000000000000000000000..448ecba2d23979f6723f66a6ce822f6459ff4293 --- /dev/null +++ b/test_framework/test/2.9/6_support_for_import_meta.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript 2.9 introduces support for import.meta, a new meta-property as described by the current TC39 proposal. +module: ESNext +isCurrent: true +---*/ + + +let url = import.meta.url; + +let contains = url.endsWith('6_support_for_import_meta.js'); + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(true, contains); + diff --git a/test_framework/test/2.9/module.d.ts b/test_framework/test/2.9/module.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..eb412c345f652abd615180b34d6b60dfbd6d3028 --- /dev/null +++ b/test_framework/test/2.9/module.d.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export declare class Pet { + name: string; +} \ No newline at end of file diff --git a/test_framework/test/3.0/new_unknown_top_type/new_unknown_top_type.ts b/test_framework/test/3.0/new_unknown_top_type/new_unknown_top_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..725f46f9cdc1709440d6b08c387cb49ef6fa4a88 --- /dev/null +++ b/test_framework/test/3.0/new_unknown_top_type/new_unknown_top_type.ts @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 3.0 introduces a new top type unknown. unknown is the type-safe counterpart of any. + anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. + likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type. + ---*/ + + +type T00 = unknown & null; +type T01 = unknown & undefined; +type T02 = unknown & null & undefined; +type T03 = unknown & string; +type T04 = unknown & string[]; +type T05 = unknown & unknown; +type T06 = unknown & any; + +type T10 = unknown | null; +type T11 = unknown | undefined; +type T12 = unknown | null | undefined; +type T13 = unknown | string; +type T14 = unknown | string[]; +type T15 = unknown | unknown; +type T16 = unknown | any; + +type T20 = T & {}; +type T21 = T | {}; +type T22 = T & unknown; +type T23 = T | unknown; + +type T30 = unknown extends T ? true : false; +type T31 = T extends unknown ? true : false; +type T32 = never extends T ? true : false; +type T33 = T extends never ? true : false; + +type T40 = keyof any; +type T41 = keyof unknown; + +function f10(x: unknown): unknown { + x = 10; + return x; +} +Assert.equal(f10(0), 10); + +declare function isFunction(x: unknown): x is Function; + +function f20(x: unknown) { + return typeof x; +} +Assert.equal(f20("N"), "string"); + +type T50 = { [P in keyof T]: number }; +type T51 = T50; +type T52 = T50; + +function f21(pAny: any, pundefined: undefined, pT: T) { + let x: unknown; + x = 123; + Assert.isNumber(x); + x = "hello"; + Assert.isString(x); + x = [1, 2, 3]; + x = new Error(); + x = x; + x = pAny; + x = pundefined; + x = pT; +} +f21(1024, undefined, "A"); + +function f22(x: unknown) { + let v1: any = x; + let v2: unknown = x; +} +f22(1024); + +function f24(x: { [x: string]: unknown }) { + x = {}; + x = { a: 5 }; +} + +function f25() { + let x: unknown; + let y = x; +} + +function f27(): unknown { + let x: unknown; + return x; +} + +class C1 { + a: string = ""; + b: unknown; + c: any; +} + +let x: number = 1024; +(x as unknown as string).length; diff --git a/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/generic_rest_parameters/generic_rest_parameters.ts b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/generic_rest_parameters/generic_rest_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..d0bc3cd399ee4715977c9a01f346658db391be0c --- /dev/null +++ b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/generic_rest_parameters/generic_rest_parameters.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a rest parameter is permitted to have a generic type that is constrained to an array type, and type inference can infer tuple types for such generic rest parameters. + ---*/ + + +function bind(f: (x: T, ...args: U) => V): (...args: U) => V { + function frm(...args: U) { let json = JSON.stringify(args); return json as unknown as V; } + return frm; +}; +function gf(x: number, y: string, z: boolean): string { + let a: any[] = [x, y, z]; + let json = JSON.stringify(a); + return json; +} +let a = gf(5, "A", true); +let gf1 = bind(gf); +let b = gf1("B", true); +let gf2 = bind(gf1); +let c = gf2(true); +let gf3 = bind(gf2); +let d = gf3(); +Assert.equal(a, "[5,\"A\",true]"); +Assert.equal(b, "[\"B\",true]"); +Assert.equal(c, "[true]"); +Assert.equal(d, "[]"); diff --git a/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..8efa8ce566436fa7657ac68f9f990d043144bfe7 --- /dev/null +++ b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/optional_elements_in_tuple_types/optional_elements_in_tuple_types.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + tuple types now permit a ? postfix on element types to indicate that the element is optional.In strictNullChecks mode, a ? modifier automatically includes undefined in the element type, similar to optional parameters. + a tuple type permits an element to be omitted if it has a postfix ? modifier on its type and all elements to the right of it also have ? modifiers. + The length property of a tuple type with optional elements is a union of numeric literal types representing the possible lengths. For example, the type of the length property in the tuple type [number, string?, boolean?] is 1 | 2 | 3. + ---*/ + + +let t: [number, string?, boolean?]; +t = [42, "hello", true]; +Assert.equal(JSON.stringify(t), "[42,\"hello\",true]"); +Assert.equal(t.length, 3); +t = [42, "hello"]; +Assert.equal(JSON.stringify(t), "[42,\"hello\"]"); +Assert.equal(t.length, 2); +t = [42]; +Assert.equal(JSON.stringify(t), "[42]"); +Assert.equal(t.length, 1); \ No newline at end of file diff --git a/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_elements_in_tuple_types/rest_elements_in_tuple_types.ts b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_elements_in_tuple_types/rest_elements_in_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..966812b108b4f23ed746247c5a5300b9f18c2668 --- /dev/null +++ b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_elements_in_tuple_types/rest_elements_in_tuple_types.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the last element of a tuple type can be a rest element of the form ...X, where X is an array type. A rest element indicates that the tuple type is open-ended and may have zero or more additional elements of the array element type. + the type of the length property of a tuple type with a rest element is number. + ---*/ + + +function tuple(...args: T): T { + return args; +} +function getArrayOfNumbers0_10() { + let arr: number[] = []; + for (let i = 0; i <= 10; i++) { + arr.push(i); + } + return arr; +} +const numbers: number[] = getArrayOfNumbers0_10(); +const t1 = tuple("foo", 1, true); +const t2 = tuple("bar", ...numbers); +const t3 = ["A", true, ...numbers, false]; +Assert.equal(JSON.stringify(t1), "[\"foo\",1,true]"); +Assert.equal(JSON.stringify(t2), "[\"bar\",0,1,2,3,4,5,6,7,8,9,10]"); +Assert.equal(JSON.stringify(t3), "[\"A\",true,0,1,2,3,4,5,6,7,8,9,10,false]"); +Assert.isNumber(t1.length); \ No newline at end of file diff --git a/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..af1fe485d86ac4efd845b8ce3d2671a5b8f1f1ff --- /dev/null +++ b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/rest_parameters_with_tuple_types/rest_parameters_with_tuple_types.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when a rest parameter has a tuple type, the tuple type is expanded into a sequence of discrete parameters. + ---*/ + + +function rfun1(...v: [number, string, boolean]) { + return { number: v[0], string: v[1], boolean: v[2] }; +} +function rfun2(v1: number, v2: string, v3: boolean) { + return { number: v1, string: v2, boolean: v3 }; +} +var r1 = rfun1(1, "rfun", true); +var r2 = rfun2(1, "rfun", true); +var jr1 = JSON.stringify(r1); +var jr2 = JSON.stringify(r2); +Assert.equal(jr1, jr2); \ No newline at end of file diff --git a/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/spread_expressions_with_tuple_types/spread_expressions_with_tuple_types.ts b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/spread_expressions_with_tuple_types/spread_expressions_with_tuple_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..a2c6f21b61aa2f0062544e048e4e95752570ede6 --- /dev/null +++ b/test_framework/test/3.0/tuples_in_rest_parameters_and_spread_expressions/spread_expressions_with_tuple_types/spread_expressions_with_tuple_types.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when a function call includes a spread expression of a tuple type as the last argument, the spread expression corresponds to a sequence of discrete arguments of the tuple element types. + ---*/ + + +function sfun(...v: [number, string, boolean]) { + return { number: v[0], string: v[1], boolean: v[2] }; +} +const args: [number, string, boolean] = [42, "hello", true]; +var v1 = sfun(...args); +var v2 = sfun(args[0], args[1], args[2]); +var v3 = sfun(42, "hello", true); +var jv1 = JSON.stringify(v1); +var jv2 = JSON.stringify(v2); +var jv3 = JSON.stringify(v3); +var flag = false; +if (jv1 === jv2 && jv2 === jv3 && jv3 === jv1) { + flag = true; +} +Assert.isTrue(flag); diff --git a/test_framework/test/3.1/mapped_types_on_tuples_and_arrays/mapped_types_on_tuples_and_arrays_1.ts b/test_framework/test/3.1/mapped_types_on_tuples_and_arrays/mapped_types_on_tuples_and_arrays_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..915d0064922a026389436175536d876048f6de4f --- /dev/null +++ b/test_framework/test/3.1/mapped_types_on_tuples_and_arrays/mapped_types_on_tuples_and_arrays_1.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In TypeScript 3.1, mapped object types over tuples and arrays now produce new tuples/arrays, + rather than creating a new type where members like push(), pop(), and length are converted. +---*/ + + +type Test = { + [k in keyof T]: Array; +}; +type tupleTest = [number, string, boolean]; +type tt = Test; +let cc: tt = [ + [0, 1, 2, 3], + ["a", "b", "c", "d"], + [false, true], +]; +Assert.equal(Array.isArray(cc), true); +Assert.equal(cc.length, 3); +cc.push([1, 2, 3]); +cc.push([false, false]); +cc.push(["hello", "world"]); +Assert.equal(cc.length, 6); +// pop +let vv: any = cc.pop(); +Assert.equal(vv[0], "hello"); +Assert.equal(vv[1], "world"); +Assert.equal(cc.length, 5); diff --git a/test_framework/test/3.1/properties_declarations_on_functions/properties_declarations_on_functions_1.ts b/test_framework/test/3.1/properties_declarations_on_functions/properties_declarations_on_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..51a75c9fb14fcf63d4923f24229b985c59a9ad21 --- /dev/null +++ b/test_framework/test/3.1/properties_declarations_on_functions/properties_declarations_on_functions_1.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript 3.1 brings the ability to define properties on function declarations and const-declared functions, + simply by assigning to properties on these functions in the same scope. + ---*/ + + +function add(a: T, b: K) { + if (typeof a == "number" && typeof b == "number") { + return a + b; + } else { + return `${a}${b}`; + } +} + +add.description = "this"; +add.aa = 0; +add.bb = false; + +Assert.equal(add.description, "this"); +Assert.equal(add.aa, 0); +Assert.equal(add.bb, false); + +add.showInfo = () => { + return 1; +}; +Assert.equal(add.showInfo(), 1); + + +const link = () => { + return 11; +}; + +link.aa = 0; +link.bb = false; +link.cc = "this"; +link.showInfo = () => { + return 1; +}; +Assert.equal(link.cc, "this"); +Assert.equal(link.aa, 0); +Assert.equal(link.bb, false); +Assert.equal(link.showInfo(), 1); diff --git a/test_framework/test/3.2/bigint/bigint_1.ts b/test_framework/test/3.2/bigint/bigint_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..215b6511ebf3252d2906224aa34d4616287dec13 --- /dev/null +++ b/test_framework/test/3.2/bigint/bigint_1.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + BigInts are part of an upcoming proposal in ECMAScript that allow us to model theoretically arbitrarily large integers. + Brings type-checking for BigInts, as well as support for emitting BigInt literals when targeting esnext. + BigInt support in TypeScript introduces a new primitive type called the 'bigint' (all lowercase). + You can get a bigint by calling the BigInt() function or by writing out a BigInt literal by adding an n to the end of any integer numeric litera. + options: + target: es2020 + ---*/ + + +let foo: bigint = BigInt(100); +Assert.equal(foo, 100n) + +let bar: bigint = 100n; +Assert.equal(bar, 100n) + +function fibonacci(n: bigint) { + let result = 1n; + for (let last = 0n, i = 0n; i < n; i++) { + const current = result; + result += last; + last = current; + } + return result; +} + +var a = fibonacci(100n); +Assert.equal(a, 573147844013817084101n) \ No newline at end of file diff --git a/test_framework/test/3.2/bigint/bigint_2.ts b/test_framework/test/3.2/bigint/bigint_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d3ff044e4aa95c56dc9f26924c8af4b4c6ce0f50 --- /dev/null +++ b/test_framework/test/3.2/bigint/bigint_2.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + As specified in ECMAScript, mixing numbers and bigints in arithmetic operations is an error. + You'll have to explicitly convert values to BigInts. + options: + target: es2020 + ---*/ + + +var foo: number = 10 +var bar: bigint = 100n + +var mult = BigInt(foo) * bar +Assert.equal(mult, 1000n) \ No newline at end of file diff --git a/test_framework/test/3.2/bigint/bigint_3.ts b/test_framework/test/3.2/bigint/bigint_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7614a619356ee696dd4c2d27bdf0e96252d1fc75 --- /dev/null +++ b/test_framework/test/3.2/bigint/bigint_3.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + bigints produce a new string when using the typeof operator: the string "bigint". + Thus, TypeScript correctly narrows using typeof as you’d expect. + options: + target: es2020 + ---*/ + + +function whatKindOfNumberIsIt(x: number | bigint) { + if (typeof x === "bigint") { + return x + } + else { + return x + 1 + } +} +var a = whatKindOfNumberIsIt(10) +var b = whatKindOfNumberIsIt(10n) +Assert.equal(a, 11) +Assert.equal(b, 10n) \ No newline at end of file diff --git a/test_framework/test/3.2/generic_object_rest_variables_and_parameters/generic_object_rest_variables_and_parameters.ts b/test_framework/test/3.2/generic_object_rest_variables_and_parameters/generic_object_rest_variables_and_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..180ce4b1ac7f75d4b8b700cdd930b97a85c0ad8e --- /dev/null +++ b/test_framework/test/3.2/generic_object_rest_variables_and_parameters/generic_object_rest_variables_and_parameters.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Allows destructuring a rest binding from a generic variable. + This is achieved by using the predefined Pick and Exclude helper types from lib.d.ts, + and using the generic type in question as well as the names of the other bindings in the destructuring pattern. + ---*/ + + +function excludeTag(obj: T) { + let { tag, ...rest } = obj + return rest +} + +const taggedPoint = { x: 10, y: 20, tag: "point" } +const point = excludeTag(taggedPoint) +Assert.equal(point, '[object Object]') \ No newline at end of file diff --git a/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_1.ts b/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..06581a372dd2cd434201065e64c2041bd60659e7 --- /dev/null +++ b/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Object literals now allow generic spread expressions which now produce intersection types, + similar to the Object.assign function and JSX literals. + ---*/ + + +function taggedObject(obj: T, tag: U) { + return { ...obj, tag } +} + +let x = taggedObject({ x: 10, y: 20 }, "point") +Assert.equal(x, '[object Object]') \ No newline at end of file diff --git a/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_2.ts b/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..96dd1b0d2bcc624d6eb75f2ee626d8a59eab295b --- /dev/null +++ b/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_2.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Property assignments and non-generic spread expressions are merged to the greatest extent possible on either side of a generic spread expression. + ---*/ + + +function foo(t: T, obj1: { a: string }, obj2: { b: string }) { + var arr = { ...obj1, x: 1, ...t, ...obj2, y: 2 } + return arr +} +let o1 = { + a: 'a' +} +let o2 = { + b: 'b' +} +var f = foo('s', o1, o2) +Assert.equal(f, '[object Object]') \ No newline at end of file diff --git a/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_3.ts b/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f74972e21cd1797311c4a27ca820e3e6531eadf --- /dev/null +++ b/test_framework/test/3.2/generic_spread_expressions_in_object_literals/generic_spread_expressions_in_object_literals_3.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Non-generic spread expressions continue to be processed as before: Call and construct signatures are stripped, + only non-method properties are preserved, and for properties with the same name, the type of the rightmost property is used. + This contrasts with intersection types which concatenate call and construct signatures, + preserve all properties, and intersect the types of properties with the same name. + Thus, spreads of the same types may produce different results when they are created through instantiation of generic types + ---*/ + + +function spread(t: T, u: U) { + return { ...t, ...u } +} + +let x: { a: string, b: number } = { a: 'a', b: 1 } +let y: { b: string, c: boolean } = { b: 'b', c: true } + +let s1 = { ...x, ...y } +Assert.equal(typeof s1, 'object') +let s2 = spread(x, y) +Assert.equal(typeof s2, 'object') +let b1 = s1.b +Assert.isString(b1) +let b2 = s2.b +Assert.isString(b2) \ No newline at end of file diff --git a/test_framework/test/3.2/non-unit_types_as_union_discriminants/non-unit_types_as_union_discriminants.ts b/test_framework/test/3.2/non-unit_types_as_union_discriminants/non-unit_types_as_union_discriminants.ts new file mode 100644 index 0000000000000000000000000000000000000000..0100cb934f5697490344af27411e4c29b4b1d692 --- /dev/null +++ b/test_framework/test/3.2/non-unit_types_as_union_discriminants/non-unit_types_as_union_discriminants.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Common properties of unions are now considered discriminants as long as they contain some singleton type, + and they contain no generics. + ---*/ + + +type Reault = { error: Error; data: null } | { error: null; data: T } + +function unwarp(result: Reault) { + if (result.error) { + throw result.error + } + return result.data +} + +var a = { + error: null, + data: null +} +var b = unwarp(a) +let f = false +if (b == null) { + f = true +} +Assert.isTrue(f) + +var c = { + error: null, + data: 10 +} +var d = unwarp(c) +Assert.equal(d, '10') \ No newline at end of file diff --git a/test_framework/test/3.3/improved_behavior_for_calling_union_types.ts b/test_framework/test/3.3/improved_behavior_for_calling_union_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..dddce5fb0cb50961136aa76b90d27e9fd88476c8 --- /dev/null +++ b/test_framework/test/3.3/improved_behavior_for_calling_union_types.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Improved behavior for calling union types. + ---*/ + + +{ + type Fruit = "apple" | "orange"; + type Color = "red" | "orange"; + // eats and ranks the fruit + type FruitEater = (fruit: Fruit) => number; + // eats and ranks the fruit + let s: FruitEater = (fruit: Fruit) => 1; + // consumes and describes the colors + type ColorConsumer = (color: Color) => string; + // consumes and describes the colors + let sp: ColorConsumer = (color: Color) => 'good'; + + Assert.equal(s("apple"), 1) + Assert.equal(sp("orange"), 'good') + + interface Dog { + kind: "dog"; + dogProp: any; + } + + interface Cat { + kind: "cat"; + catProp: any; + } + + const catOrDogArray: Dog[] | Cat[] = [{ kind: "dog", dogProp: 1 }, { kind: "dog", dogProp: 2 }]; + let ex: number[] = []; + catOrDogArray.forEach((animal: Dog | Cat) => { + if (animal.kind === "dog") { + ex.push(animal.dogProp); + } else if (animal.kind === "cat") { + ex.push(animal.catProp); + } + }); + + Assert.equal(ex[0], 1) + Assert.equal(ex[1], 2) +} diff --git a/test_framework/test/3.4/higher_order_type_inference.ts b/test_framework/test/3.4/higher_order_type_inference.ts new file mode 100644 index 0000000000000000000000000000000000000000..df8578d2c8e6893eaa806e9f0ce5f45602ddc530 --- /dev/null +++ b/test_framework/test/3.4/higher_order_type_inference.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Higher order type inference from generic functions. + ---*/ + + +function compose(f: (arg: A) => B, g: (arg: B) => C): (arg: A) => C { + return (x) => g(f(x)); +} + +interface Box { + value: T; +} + +function makeArray(x: T): T[] { + return [x]; +} + +function makeBox(value: U): Box { + return { value }; +} + +// has type '(arg: {}) => Box<{}[]>' +const makeBoxedArray = compose( + makeArray, + makeBox, +) + + +Assert.equal(makeBoxedArray("hello").value[0].toUpperCase(), 'HELLO') \ No newline at end of file diff --git a/test_framework/test/3.4/readonly_array.ts b/test_framework/test/3.4/readonly_array.ts new file mode 100644 index 0000000000000000000000000000000000000000..d45abe9e497d511892c088e085cb1e5b0c80964e --- /dev/null +++ b/test_framework/test/3.4/readonly_array.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Improvements for ReadonlyArray and readonly tuples. + const assertions. + ---*/ + + +{ + let arr: ReadonlyArray = ['1', '2']; + let sa: readonly string[] = ['1', '2']; + + Assert.equal(arr[1], '2') + Assert.equal(sa[1], '2') + + function getShapes() { + let result = [ + { kind: "circle", radius: 100 }, + { kind: "square", sideLength: 50 }, + ] as const; + return result; + } + for (const shape of getShapes()) { + // Narrows perfectly! + if (shape.kind === "circle") { + console.log("Circle radius", shape.radius); + Assert.equal(shape.radius, 100) + } else { + console.log("Square side length", shape.sideLength); + Assert.equal(shape.sideLength, 50) + } + } +} diff --git a/test_framework/test/3.5/higher_order_type_inference.ts b/test_framework/test/3.5/higher_order_type_inference.ts new file mode 100644 index 0000000000000000000000000000000000000000..515dba8e97bb309fdf71577e9a97b60f3057f9a2 --- /dev/null +++ b/test_framework/test/3.5/higher_order_type_inference.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In TypeScript 3.4, we improved inference for when generic functions that return functions. + ---*/ + + +{ + class Box { + value: T; + + constructor(value: T) { + this.value = value; + } + } + + class Bag { + value: U; + + constructor(value: U) { + this.value = value; + } + } + + function composeCtor( + F: new (x: T) => U, + G: new (y: U) => V + ): (x: T) => V { + return x => new G(new F(x)); + } + + // has type '(x: T) => Bag>' + let f = composeCtor(Box, Bag); + // has type 'Bag>' + let a = f(1024); + + Assert.equal(a.value.value, 1024) +} \ No newline at end of file diff --git a/test_framework/test/3.5/omit_helper_type.ts b/test_framework/test/3.5/omit_helper_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..e7551d253de1108be3d2f79d101a8a8ef964320f --- /dev/null +++ b/test_framework/test/3.5/omit_helper_type.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 3.5 introduces the new Omit helper type, which creates a new type with some properties dropped from the original. + ---*/ + + +{ + type Person = { + name: string; + age: number; + location: string; + }; + type QuantumPersons = Omit; + let s: QuantumPersons = { + name: 'string', + age: 1, + } + let s1: Person = { + name: 'string', + age: 1, + location: 'string' + } + Assert.isFalse("location" in s) + Assert.isTrue("location" in s1) +} \ No newline at end of file diff --git a/test_framework/test/3.5/smarter_union_type_checking.ts b/test_framework/test/3.5/smarter_union_type_checking.ts new file mode 100644 index 0000000000000000000000000000000000000000..1ae7eb45b531f1a3ddc77e7e9eee0721050bd125 --- /dev/null +++ b/test_framework/test/3.5/smarter_union_type_checking.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Smarter union type checking. + ---*/ + + +{ + type Point = { + x: number; + y: number; + name: string; + }; + + type Label = { + name: string; + }; + + const thing: Point | Label = { + x: 0, + y: 0, + name: 'name' + } + Assert.equal(thing.name, 'name') +} \ No newline at end of file diff --git a/test_framework/test/3.6/better_unicode_support_for_identifiers/better_unicode_support_for_identifiers_1.ts b/test_framework/test/3.6/better_unicode_support_for_identifiers/better_unicode_support_for_identifiers_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..eba4358dd6e63d95025d07644ae2e99071ae70a4 --- /dev/null +++ b/test_framework/test/3.6/better_unicode_support_for_identifiers/better_unicode_support_for_identifiers_1.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 3.6 contains better support for Unicode characters in identifiers when emitting to ES2015 and later targets. +options: + target:es2015 + ---*/ + + +const 𝓱𝓮𝓵𝓵𝓸 = "world"; +Assert.equal(𝓱𝓮𝓵𝓵𝓸, "world"); diff --git a/test_framework/test/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/get_and_set_accessors_are_allowed_in_ambient_contexts_1.ts b/test_framework/test/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/get_and_set_accessors_are_allowed_in_ambient_contexts_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1734f7e5dffd42d3b2c6be3aaae491a84f8f3b54 --- /dev/null +++ b/test_framework/test/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/get_and_set_accessors_are_allowed_in_ambient_contexts_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: users can write getters and setters in ambient contexts in TypeScript 3.6 and later. +module: ESNext +isCurrent: true + ---*/ + + +import { Foo } from "./test.js"; + +let foo: Foo = { x: 1 }; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(1, foo.x); + + diff --git a/test_framework/test/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/test.d.ts b/test_framework/test/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/test.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..fac1296325365eefff1513f0d1d2ef38524d72fa --- /dev/null +++ b/test_framework/test/3.6/get_and_set_accessors_are_allowed_in_ambient_contexts/test.d.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare class Foo { + // Allowed in 3.6+. + get x(): number; + set x(val: number); +} +export { Foo }; diff --git a/test_framework/test/3.6/more_accurate_array_spread/more_accurate_array_spread_1.ts b/test_framework/test/3.6/more_accurate_array_spread/more_accurate_array_spread_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..080099ef4dc136e56104c174d9e611270b4f1a49 --- /dev/null +++ b/test_framework/test/3.6/more_accurate_array_spread/more_accurate_array_spread_1.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: More Accurate Array Spread. + ---*/ + + +const person = { name: "John", age: 30 }; +const skills = ["javascript", "typescript", "react"]; +const personWithSkills = { ...person, skills }; +Assert.equal("name" in personWithSkills, true); +Assert.equal("age" in personWithSkills, true); +Assert.equal("skills" in personWithSkills, true); +Assert.equal("isJob" in personWithSkills, false); diff --git a/test_framework/test/3.6/stricter_generators/stricter_generators_1.ts b/test_framework/test/3.6/stricter_generators/stricter_generators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..544a4ef9e2b85f34a86628185f293bd91d85f9d3 --- /dev/null +++ b/test_framework/test/3.6/stricter_generators/stricter_generators_1.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: In TypeScript 3.6, the checker now knows that the correct type for iter.next().value +options: + lib:es2015 +---*/ + + +function* test() { + yield 100; + yield "Finished!"; + return false; +} +let iter = test(); +let cc = iter.next(); +Assert.isNumber(cc.value); +cc = iter.next(); +Assert.isString(cc.value); +cc = iter.next(); +Assert.isBoolean(cc.value); +cc = iter.next(); +Assert.equal(cc.done, true); \ No newline at end of file diff --git a/test_framework/test/3.6/stricter_generators/stricter_generators_2.ts b/test_framework/test/3.6/stricter_generators/stricter_generators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1ae7be0e8ee864b0d9e6b8d454273b8966b157e --- /dev/null +++ b/test_framework/test/3.6/stricter_generators/stricter_generators_2.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The Iterator type now allows users to specify the yielded type, the returned type, and the type that next can accept. + To allow differentiation between returned values and yielded values, + TypeScript 3.6 converts the IteratorResult type to a discriminated union type. + ---*/ + + +class Person { + name: string; + age: number; + isJob: boolean; + constructor(name: string, age: number, isJob: boolean) { + this.name = name; + this.age = age; + this.isJob = isJob; + } +} + +let cc = new Person("caihua", 12, false); + +const keys = Object.keys(cc); +var nextIndex = 0; +let iteratorPerson: Iterator = { + next() { + return nextIndex < keys.length + ? { + value: keys[nextIndex++], + done: false, + } + : { value: 0, done: true }; + }, +}; +// next type +let dd = iteratorPerson.next(); +Assert.isString(dd.value); +Assert.isBoolean(dd.done); +Assert.equal(dd.value, "name"); +Assert.equal(dd.done, false); +iteratorPerson.next(); +iteratorPerson.next(); +// return type +dd = iteratorPerson.next(); +Assert.isNumber(dd.value); +Assert.isBoolean(dd.done); +Assert.equal(dd.value, 0); +Assert.equal(dd.done, true); diff --git a/test_framework/test/3.6/stricter_generators/stricter_generators_3.ts b/test_framework/test/3.6/stricter_generators/stricter_generators_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..3565ff12b9d3a2d5d4c20cf51d90374cc9d42797 --- /dev/null +++ b/test_framework/test/3.6/stricter_generators/stricter_generators_3.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: the new Generator type is an Iterator that always has both the return and throw methods present, and is also iterable. +options: + lib:es2017 + ---*/ + + +function* test(): Generator { + let i = 0; + while (true) { + if (yield i++) { + break; + } + } + return "done!"; +} + +let cc = test(); +let ccItem = cc.next(); +while (!ccItem.done) { + Assert.equal(typeof ccItem.value, "number"); + ccItem = cc.next(typeof ccItem.value == "number"); +} diff --git a/test_framework/test/3.6/stricter_generators/stricter_generators_4.ts b/test_framework/test/3.6/stricter_generators/stricter_generators_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..e9101876ec5f7291a9ff58be96e95dfd860dd115 --- /dev/null +++ b/test_framework/test/3.6/stricter_generators/stricter_generators_4.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + To correctly represent the types that can be passed in to a generator from calls to next(), + TypeScript 3.6 also infers certain uses of yield within the body of a generator function. +options: + lib:es2015 + ---*/ + + +function* foo() { + let x: number = yield; + Assert.isNumber(x); +} +let x = foo(); +x.next(); +x.next(1); + diff --git a/test_framework/test/3.6/stricter_generators/stricter_generators_5.ts b/test_framework/test/3.6/stricter_generators/stricter_generators_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..2ef94b291d9c35fbded9608099f5f5df916d0fa7 --- /dev/null +++ b/test_framework/test/3.6/stricter_generators/stricter_generators_5.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + To correctly represent the types that can be passed in to a generator from calls to next(), + TypeScript 3.6 also infers certain uses of yield within the body of a generator function. +options: + lib:es2015 + ---*/ + + +function* counter(): Generator { + let i = 0; + while (true) { + let cc = yield i++; + Assert.isBoolean(cc); + if (cc) { + break; + } + } + return "done!"; +} + +var iter = counter(); +var curr = iter.next(); +while (!curr.done) { + curr = iter.next(curr.value === 5); +} diff --git a/test_framework/test/3.7/assertion_functions/assertion_functions_1.ts b/test_framework/test/3.7/assertion_functions/assertion_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..50cdb81d820a2f6720430ef5858f80622f694b13 --- /dev/null +++ b/test_framework/test/3.7/assertion_functions/assertion_functions_1.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Assertions in JavaScript are often used to guard against improper types being passed in. +module: ESNext +isCurrent: true +---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +function multiply(x: any, y: any) { + equal(typeof x, "number"); + equal(typeof y, "number"); + + return x * y; +} +equal(typeof multiply(6, 8), "number"); \ No newline at end of file diff --git a/test_framework/test/3.7/better_support_for_never-Returning_functions/better_support_for_never-Returning_functions_1.ts b/test_framework/test/3.7/better_support_for_never-Returning_functions/better_support_for_never-Returning_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c1dca5c6c37e35b8d2185aa4907e743753956883 --- /dev/null +++ b/test_framework/test/3.7/better_support_for_never-Returning_functions/better_support_for_never-Returning_functions_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + when these never-returning functions are called, TypeScript recognizes that they affect the control flow graph and accounts for them. + Need to execute the command: npm i -- save dev @ types/node +---*/ + + +function doThingWithString(x: string) { + return x; +} + +function doThingWithNumber(x: number) { + return x; +} + +function dispatch(x: string | number): any { + if (typeof x === "string") { + return doThingWithString(x); + } + else if (typeof x === "number") { + return doThingWithNumber(x); + } + process.exit(1); +} + +Assert.equal(typeof dispatch(10), "number"); +Assert.equal(typeof dispatch("hello"), "string"); \ No newline at end of file diff --git a/test_framework/test/3.7/nullish_coalescing/nullish_coalescing_1.ts b/test_framework/test/3.7/nullish_coalescing/nullish_coalescing_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c51565e17c4627097b6de4aac33208a982ba8d8 --- /dev/null +++ b/test_framework/test/3.7/nullish_coalescing/nullish_coalescing_1.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + We can think of this feature - the ?? operator - as a way to "fall back" to a default value when dealing with null or undefined. +module: ESNext +isCurrent: true +---*/ + + +let data: number | null; +data = 10000; +let bookName: string = "Pride and Prejudice"; +let x = data ?? bookName; + +data = null; +let y = data ?? bookName; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(typeof x, "number"); +equal(typeof y, "string"); \ No newline at end of file diff --git a/test_framework/test/3.7/nullish_coalescing/nullish_coalescing_2.ts b/test_framework/test/3.7/nullish_coalescing/nullish_coalescing_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..010d5bc0a8060c23e725c7fed1b6ac328aa12818 --- /dev/null +++ b/test_framework/test/3.7/nullish_coalescing/nullish_coalescing_2.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The ?? operator can replace uses of || when trying to use a default value. +module: ESNext +isCurrent: true +---*/ + + +let variable = 0; +function initializeAudio() { + variable = variable ?? 0.5; + return variable; +} + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(initializeAudio(), 0); \ No newline at end of file diff --git a/test_framework/test/3.7/optional_chaining/optional_chaining_1.ts b/test_framework/test/3.7/optional_chaining/optional_chaining_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e2cebb90a11d436b4530b7429c3b199ff273af50 --- /dev/null +++ b/test_framework/test/3.7/optional_chaining/optional_chaining_1.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The star of the show in optional chaining is the new ?. operator for optional property accesses. +module: ESNext +isCurrent: true +---*/ + + +const fooOp = { + bar: { + baz: 'hello' + } +}; + +let xOp = fooOp?.bar.baz; +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(xOp, "hello"); \ No newline at end of file diff --git a/test_framework/test/3.7/optional_chaining/optional_chaining_2.ts b/test_framework/test/3.7/optional_chaining/optional_chaining_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..647e92c89badca26071305ae656e524363a588e1 --- /dev/null +++ b/test_framework/test/3.7/optional_chaining/optional_chaining_2.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + You might find yourself using ?. to replace a lot of code that performs repetitive nullish checks using the && operator. +module: ESNext +isCurrent: true +---*/ + + +const vehicle = { + car: { + BYD: 'e2' + } +}; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +if (vehicle?.car?.BYD) { + equal(vehicle?.car?.BYD, "e2", "true"); +} \ No newline at end of file diff --git a/test_framework/test/3.7/optional_chaining/optional_chaining_3.ts b/test_framework/test/3.7/optional_chaining/optional_chaining_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ec1f1558faa7a3031346dec8dbcae91d25cfcc9c --- /dev/null +++ b/test_framework/test/3.7/optional_chaining/optional_chaining_3.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + One use of optional chain is optional element access, which acts similarly to optional property accesses, but allows us to access non-identifier properties (e.g. arbitrary strings, numbers). +module: ESNext +isCurrent: true +---*/ + + +function tryGetFirstElement(arr?: T[]) { + return arr?.[0]; +} + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +const arr: number[] = [1, 25, 8]; +equal(tryGetFirstElement(arr), 1, "true"); + +const arrStr: string[] = ["flower", "xian"]; +equal(tryGetFirstElement(arrStr), "flower", "true"); \ No newline at end of file diff --git a/test_framework/test/3.7/optional_chaining/optional_chaining_4.ts b/test_framework/test/3.7/optional_chaining/optional_chaining_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a7abb39f51a0ff24d72b2ec404ffd394c4cfd5b --- /dev/null +++ b/test_framework/test/3.7/optional_chaining/optional_chaining_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + There's also optional call, which allows us to conditionally call expressions if they’re not null or undefined. +---*/ + + +async function makeRequest(url: string, log?: (msg: string) => void) { + log?.(`Request started at ${new Date().toISOString()}`); + const result = ((await fetch(url)).json()); + log?.(`Request finished at ${new Date().toISOString()}`); + return result; +} +makeRequest("http://127.0.0.1:8081/test.txt").then(res => { + Assert.equal(typeof res, "object"); +}).catch(err => { +}); \ No newline at end of file diff --git a/test_framework/test/3.7/optional_chaining/optional_chaining_5.ts b/test_framework/test/3.7/optional_chaining/optional_chaining_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..9afaf7957d938370a56f3f78b927c10f3b797a3c --- /dev/null +++ b/test_framework/test/3.7/optional_chaining/optional_chaining_5.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The "short-circuiting" behavior that optional chains have is limited property accesses, calls, element accesses - it doesn't expand any further out from these expressions. +---*/ + + +const nums = { + num1: { + num2: 10 + } +}; +function someComputation(divisor: number) { + return divisor + 4; +} +let result = nums?.num1?.num2 / someComputation(1); +Assert.equal(typeof result, "number"); \ No newline at end of file diff --git a/test_framework/test/3.7/recursive_type_aliases/recursive_type_aliases_1.ts b/test_framework/test/3.7/recursive_type_aliases/recursive_type_aliases_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e64ede25a0e113f3d8ff003b4dc948a648638b9f --- /dev/null +++ b/test_framework/test/3.7/recursive_type_aliases/recursive_type_aliases_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + That's exactly what TypeScript 3.7 introduces. At the "top level" of a type alias, TypeScript will defer resolving type arguments to permit these patterns. +---*/ + + +type Json = + | string + | number + | boolean + | null + | JsonObject + | JsonArray; + +interface JsonObject { + [property: string]: Json; +} + +interface JsonArray extends Array { } +const myJson: Json = ["a", 2, true, null, { + "dataId": 123, + "dataType": "mysql", + "resultData": [{ + "binlog": "mysql_binlog.000", + "column": [{ + "columnname": "single_cloum0", + "columntype": "varchar(10)", + "index": 0, + "modified": false, + "pk": false, + "sqlType": 0, + "value": "7" + }] + }] +}, []]; + +Assert.equal(typeof myJson, "object"); \ No newline at end of file diff --git a/test_framework/test/3.8/1_type_only_imports_and_export.ts b/test_framework/test/3.8/1_type_only_imports_and_export.ts new file mode 100644 index 0000000000000000000000000000000000000000..62c3997e1ce2147c3a8ef89d0e44bb6b36256eb4 --- /dev/null +++ b/test_framework/test/3.8/1_type_only_imports_and_export.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 3.8 adds a new syntax for type-only imports and exports. +module: ESNext +isCurrent: true +---*/ + + +import type { SomeThing } from "./some_module"; + +export type { SomeThing }; + +let sth: SomeThing = { legs: 5 }; + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(5, sth.legs); + + + diff --git a/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_1.ts b/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e3affb5353fc00952ea651f282783664ea22449 --- /dev/null +++ b/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 3.8 brings support for ECMAScript’s private fields +---*/ + + +class Person { + #name: string; + + constructor(name: string) { + this.#name = name; + } + + greet(): string { + return this.#name; + } +} + +let jeremy = new Person("Jeremy Bearimy"); + +Assert.equal("Jeremy Bearimy", jeremy.greet()); + +// jeremy.#name; +// Property '#name' is not accessible outside class 'Person' +// because it has a private identifier. \ No newline at end of file diff --git a/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_2.ts b/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..14769afef41e71506227aeef07ca72b672f5448a --- /dev/null +++ b/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_2.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: With private fields, each field name is unique to the containing class. +---*/ + + +class C { + #foo = 10; + + cHelper() { + return this.#foo; + } +} + +class D extends C { + #foo = 20; + + dHelper() { + return this.#foo; + } +} + +let instance = new D(); + +// 'this.#foo' refers to a different field within each class. +// prints '10' +Assert.equal(10, instance.cHelper()); + +// prints '20' +Assert.equal(20, instance.dHelper()); \ No newline at end of file diff --git a/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_3.ts b/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..db4e252fd68678c96cf4cb53a5c86437c7e3c3ef --- /dev/null +++ b/test_framework/test/3.8/2_ecmsscript_private_fields/2_ecmsscript_private_fields_3.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Accessing a private field on any other type will result in a TypeError! +---*/ + + +class Square { + #sideLength: number; + + constructor(sideLength: number) { + this.#sideLength = sideLength; + } + + equals(other: any) { + return this.#sideLength === other.#sideLength; + } +} + +const a = new Square(100); +const b = { sideLength: 100 }; +const c = new Square(100); + + +Assert.isTrue(a.equals(c)); \ No newline at end of file diff --git a/test_framework/test/3.8/3_which_should_i_use.ts b/test_framework/test/3.8/3_which_should_i_use.ts new file mode 100644 index 0000000000000000000000000000000000000000..0deebb46c47733ea0e7d48c9b87f82e301f5c8ca --- /dev/null +++ b/test_framework/test/3.8/3_which_should_i_use.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: the difference between private and # +---*/ + + +class C { + private foo = 10; +} + + +class D { + #foo = 10; +} + +Assert.equal(10, new C()["foo"]); diff --git a/test_framework/test/3.8/4_export_star_as_ns_syntax.ts b/test_framework/test/3.8/4_export_star_as_ns_syntax.ts new file mode 100644 index 0000000000000000000000000000000000000000..1df0224fecdff7e4f307f309d4af46678fef360a --- /dev/null +++ b/test_framework/test/3.8/4_export_star_as_ns_syntax.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: ECMAScript 2020 recently added a new syntax to support this pattern! +module: ESNext +isCurrent: true +---*/ + + +import { myModule } from "./export_star.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(8888, myModule.x); +equal("my string", myModule.getX("my string")); + diff --git a/test_framework/test/3.8/5_top_level_await.ts b/test_framework/test/3.8/5_top_level_await.ts new file mode 100644 index 0000000000000000000000000000000000000000..24b9d320131e0483abf297285586f582575e5482 --- /dev/null +++ b/test_framework/test/3.8/5_top_level_await.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: ECMAScript 2020 recently added a new syntax to support this pattern! +module: ESNext +isCurrent: true +---*/ + + +import { greeting } from "./await_module.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal("mytext", greeting); diff --git a/test_framework/test/3.8/await_module.ts b/test_framework/test/3.8/await_module.ts new file mode 100644 index 0000000000000000000000000000000000000000..08b15e6d1cf85176b2c88bf02f07e33b9ced2818 --- /dev/null +++ b/test_framework/test/3.8/await_module.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +const response = await getSomeThine(); +const greeting = await response.text; + +async function getSomeThine() { + await delay(1000); + return { + text:"mytext" + }; +} + +function delay(ms: number) { + return new Promise( resolve => setTimeout(resolve, ms) ); +} + +// Make sure we're a module +export {greeting}; \ No newline at end of file diff --git a/test_framework/test/3.8/export_star.ts b/test_framework/test/3.8/export_star.ts new file mode 100644 index 0000000000000000000000000000000000000000..afc745e53c526f09dcf1b68fb727565e2feeb332 --- /dev/null +++ b/test_framework/test/3.8/export_star.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export * as myModule from "./my_module.js"; + diff --git a/test_framework/test/3.8/my_module.ts b/test_framework/test/3.8/my_module.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e92ddb54f3060edd7990647610cb9ef481d6f76 --- /dev/null +++ b/test_framework/test/3.8/my_module.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export let x = 8888; +export function getX (x : string){return x;}; \ No newline at end of file diff --git a/test_framework/test/3.8/some_module.ts b/test_framework/test/3.8/some_module.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f32b745bc0403af33d1383e202603e13abde429 --- /dev/null +++ b/test_framework/test/3.8/some_module.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +const cat: any = { legs: 4 }; + +export type SomeThing = typeof cat; \ No newline at end of file diff --git a/test_framework/test/3.9/improvements_in_inference_and_Promise.all/improvements_in_inference_and_Promise.all.ts b/test_framework/test/3.9/improvements_in_inference_and_Promise.all/improvements_in_inference_and_Promise.all.ts new file mode 100644 index 0000000000000000000000000000000000000000..787bdea6dd4a29f2a8be71653f2761a6586ef52e --- /dev/null +++ b/test_framework/test/3.9/improvements_in_inference_and_Promise.all/improvements_in_inference_and_Promise.all.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + recrent versions of TypeScript (around 3.7) have had updates to the declarations of functions like Promise.all and Promise.race. + unfortunately, that introduced a few regressions, especially when mixing in values with null or undefined. + this issue has now been fixed. + options: + lib: es2015 + ---*/ + + +interface Lion { + roar(): void; +} + +interface Seal { + singKissFromARose(): void; +} + +async function visitZoo( + lionExhibit: Promise, + sealExhibit: Promise +) { + let [lion, seal] = await Promise.all([lionExhibit, sealExhibit]); + lion.roar(); +} +Assert.isString("The \"lion.roar()\" in the above example was incorrectly reported as having the value \"undefined\" and has now been fixed.") \ No newline at end of file diff --git a/test_framework/test/3.9/ts-expect-error_comments/ts-expect-error_comments.ts b/test_framework/test/3.9/ts-expect-error_comments/ts-expect-error_comments.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1900640073ca0ab6aababbaced17ed9ec4cb119 --- /dev/null +++ b/test_framework/test/3.9/ts-expect-error_comments/ts-expect-error_comments.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when a line is preceded by a // @ts-expect-error comment, TypeScript will suppress that error from being reported; but if there’s no error, TypeScript will report that // @ts-expect-error wasn’t necessary. + ---*/ + + +function toASCIICode(s: string) { + s += ""; + let arr: number[] = []; + let char: string[] = s.split(''); + for (let i = 0; i < s.length; i++) { + arr[i] = char[i].charCodeAt(0); + } + return arr; +} +// @ts-expect-error +let json = JSON.stringify(toASCIICode(123)); + +Assert.equal(json, "[49,50,51]"); diff --git a/test_framework/test/4.0/class_property_Inference_from_constructors/class_property_Inference_from_constructors_1.ts b/test_framework/test/4.0/class_property_Inference_from_constructors/class_property_Inference_from_constructors_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7309e07a4269d08c33dfbe49b463c45126ccea2 --- /dev/null +++ b/test_framework/test/4.0/class_property_Inference_from_constructors/class_property_Inference_from_constructors_1.ts @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript 4.0 can now use control flow analysis to determine the types of properties in classes when noImplicitAny is enabled. + ---*/ + + +class personW { + name: string; + age: number | undefined; + constructor(name: string) { + this.name = name; + } + setAge(age: number) { + if (age >= 0) { + this.age = age; + } + } +} +const W = new personW("tom"); +Assert.equal(W.name, "tom"); + +class squareD { + area: number | undefined; + + sideLength: number | undefined; + constructor(sideLength: number) { + this.sideLength = sideLength; + this.area = sideLength * 2; + } +} +const D = new squareD(6); +Assert.equal(D.area, 12); + +class Square { + sideLength!: number; + constructor(sideLength: number) { + this.initialize(sideLength); + } + + initialize(sideLength: number) { + this.sideLength = sideLength; + } + + get area() { + return this.sideLength ** 2; + } +} +const b = new Square(8); +Assert.equal(b.area, 64); diff --git a/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_1.ts b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..50e87573811bcb043cc308db9deac14950c7a04e --- /dev/null +++ b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_1.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + function foo(...args: [string, number]): void { + // ...} + ---*/ + + +function fooA(...args: [string, number]): void { + const newArr = [args[0].toUpperCase(), args[1] * 2]; + Assert.equal(newArr[0], "HELLO"); + Assert.equal(newArr[1], 10); +} +fooA("hello", 5); +function fooB(arg0: string, arg1: number): void { + const Arrb = [arg0.toUpperCase(), arg1 * 2]; + Assert.equal(Arrb[0], "HELLO"); + Assert.equal(Arrb[1], 10); +} +fooB("hello", 5); diff --git a/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_2.ts b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e890f54929c3cb8b26b90f4b8a501468fa04dfb --- /dev/null +++ b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_2.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + To deepen the connection between parameter lists and tuple types, + the syntax for rest elements and optional elements mirrors the syntax for parameter lists. + ---*/ + + +type Foo = [first: number, second?: string, ...rest: any[]]; +let myArr: Foo = [42, "Hello", true, { foo: "bar" }]; + +Assert.equal(myArr[0], 42); +Assert.equal(myArr[1], "Hello"); +Assert.equal(myArr[2], true); diff --git a/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_3.ts b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..c61fecd124a8a48cdfa6d91b51289ebe96000346 --- /dev/null +++ b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_3.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + There are a few rules when using labeled tuples. + For one, when labeling a tuple element, all other elements in the tuple must also be labeled. + ---*/ + + +type PersonA = [name: string, age: number]; +type PartialPersonA = [name?: string, age?: number]; + +const myTuple: [string, number, boolean] = ["hello", 123, true]; +Assert.equal(myTuple[0], "hello"); +Assert.equal(myTuple[1], 123); +Assert.equal(myTuple[2], true); diff --git a/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_4.ts b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..a9e1a691c4c9e3da9f6adbd2cf9315ecccfed5be --- /dev/null +++ b/test_framework/test/4.0/labeled_tuple_elements/labeled_tuple_elements_4.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + It’s worth noting - labels don’t require us to name our variables differently when destructuring. + They’re purely there for documentation and tooling. + ---*/ + + +function foo(x: [first: string, second: number]) { + const [a, b] = x; + Assert.equal(a, "hello"); + Assert.equal(b, 42); +} +foo(["hello", 42]); diff --git a/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_1.ts b/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e2c0750536bce9243454a905d3c58e242c8d132 --- /dev/null +++ b/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_1.ts @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + JavaScript, and a lot of other languages, support a set of operators called compound assignment operators. + Compound assignment operators apply an operator to two arguments, + and then assign the result to the left side. You may have seen these before: + ---*/ + + +let a: number = 1; +let bb: number = 2; +a += bb; +Assert.equal(a, 3); + +let c: number = 4; +let d: number = 3; +c -= d; +Assert.equal(c, 1); + +let e: number = 5; +let f: number = 6; +e *= f; +Assert.equal(e, 30); + +let g: number = 7; +let h: number = 8; +g /= h; +Assert.equal(g, 0.875); + +let j: number = 2; +let k: number = 3; +j **= k; +Assert.equal(j, 8); + +let m: number = 20; +let n: number = 30; +m <<= n; +Assert.equal(m, 0); + +let o: number = 4; +let p: number = 5; +o = o && p; +Assert.equal(o, 5); + +let s: number = 7; +let t: number = 8; +s = s || t; +Assert.equal(s, 7); + +let u: number = 10; +let v: number = 20; +u = u ?? v; +Assert.equal(u, 10); diff --git a/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_2.ts b/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b23753fb344ae8a2a063b73af97513ac79fef00 --- /dev/null +++ b/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_2.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + There are even some patterns we’ve seen (or, uh, written ourselves) to lazily initialize values, only if they’ll be needed. + ---*/ + + +let values: string[]; + +(values ??= []).push("hello"); +(values ??= []).push("world"); + +Assert.equal(values[0], "hello"); +Assert.equal(values[1], "world"); diff --git a/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_3.ts b/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ff65b19a3500491da7d3423c78d6d6883b1d8fb0 --- /dev/null +++ b/test_framework/test/4.0/short_circuiting_assignment_operators/short_circuiting_assignment_operators_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + On the rare case that you use getters or setters with side-effects, it’s worth noting that these operators only perform assignments if necessary. + In that sense, not only is the right side of the operator “short-circuited” - the assignment itself is too. + ---*/ + + +const obj = { + get prop() { + return true; + }, + set prop(_val: boolean) { }, +}; +function fooW() { + return true; +} +obj.prop = obj.prop || fooW(); +Assert.equal(obj.prop, true); diff --git a/test_framework/test/4.0/unknow_on_catch_clause_bindings/unknow_on_catch_clause_bindings_1.ts b/test_framework/test/4.0/unknow_on_catch_clause_bindings/unknow_on_catch_clause_bindings_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5febd8165a2343b820eadccaf733142b9437110e --- /dev/null +++ b/test_framework/test/4.0/unknow_on_catch_clause_bindings/unknow_on_catch_clause_bindings_1.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Since the beginning days of TypeScript, catch clause variables have always been typed as any. + This meant that TypeScript allowed you to do anything you wanted with them. + ---*/ + + +function throwError(): never { + throw new Error("An error occurred"); +} +try { + throwError(); +} catch (error: any) { + Assert.equal(error.message, "An error occurred"); +} diff --git a/test_framework/test/4.0/unknow_on_catch_clause_bindings/unknow_on_catch_clause_bindings_2.ts b/test_framework/test/4.0/unknow_on_catch_clause_bindings/unknow_on_catch_clause_bindings_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..bd8e0273f7e89de0497c72a47c25bdbe01fbd3f5 --- /dev/null +++ b/test_framework/test/4.0/unknow_on_catch_clause_bindings/unknow_on_catch_clause_bindings_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The above has some undesirable behavior if we’re trying to prevent more errors from happening in our error-handling code! + Because these variables have the type any by default, they lack any type-safety which could have errored on invalid operations. + That’s why TypeScript 4.0 now lets you specify the type of catch clause variables as unknown instead. + unknown is safer than any because it reminds us that we need to perform some sorts of type-checks before operating on our values. + ---*/ + + +function throwError(): never { + throw new Error("An error occurred"); +} +try { + throwError(); +} catch (error: unknown) { + if (error instanceof Error) { + Assert.equal(error.message, "An error occurred"); + } else { + let errString = "An unknow error occurred"; + } +} diff --git a/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_1.ts b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..2c4300385999e8ba7121086a7dc4c278a59ae04f --- /dev/null +++ b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_1.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Consider a function in JavaScript called concat that takes two array or tuple types and concatenates them together to make a new array. + ---*/ + + +function concat(arr1: any, arr2: any) { + return [...arr1, ...arr2]; +} +const arrA: any = [1, 2, 3]; +const arrB: any = [4, 5, 6]; +let judgelength = concat(arrA, arrB).length; +Assert.equal(judgelength, 6); + +function tail(arg: any) { + const [_, ...result] = arg; + return result; +} + +const myArray: any = [1, 2, 3, 4, 5]; +let taillength = tail(myArray).length; +Assert.equal(taillength, 4); diff --git a/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_2.ts b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..954f0ce461a265c4895d0d79e2a3c708b0efc0b6 --- /dev/null +++ b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_2.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + How would we type either of these in TypeScript? + For concat, the only valid thing we could do in older versions of the language was to try and write some overloads. + function concat(arr1: [A, B, C, D, E, F], arr2: []): [A, B, C, D, E, F]; + ---*/ + + +function concatA(arr1: [T1, T2], arr2: []): [T1, T2]; +function concatA(arr1: T[], arr2: T[]): T[] { + return arr1.concat(arr2); +} +const tuple: [] = []; +const combinedTuple: [number, string] = concatA([1, "a"], tuple); +Assert.equal(combinedTuple[0], 1); +Assert.equal(combinedTuple[1], "a"); + +const arr1: [number, string, boolean, number[], object, string] = [ + 1, + "hello", + true, + [2, 3], + { name: "john" }, + "world", +]; +const arr2: [] = []; +function concat( + arr1: [A, B, C, D, E, F], + arr2: [] +): [A, B, C, D, E, F] { + return [...arr1, ...arr2]; +} +const result = concat(arr1, arr2); +Assert.equal(result.length, 6); diff --git a/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_3.ts b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..abde645aad6bb8f08a2548b3f1e554ae001ed9d9 --- /dev/null +++ b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + This is another case of what we like to call “death by a thousand overloads”, and it doesn’t even solve the problem generally. + It only gives correct types for as many overloads as we care to write. + If we wanted to make a catch-all case, we’d need an overload like the following: + ---*/ + + +function concat(arr1: T[], arr2: U[]): Array{ + return [...arr1,...arr2] +} +const arr1: string[] = ["hello", "world"]; +const arr2: number[] = [1, 2, 3]; +const result:Array = concat(arr1, arr2); +function tail(arg: any) { + const [_, ...result] = arg; + return result; +} +const myTuple: any = [1, 2, 3, 4] as const; +const newArr: any = ["hello", "world"]; +const r1 = tail(myTuple); +const r2 = tail([...myTuple, ...newArr] as const); +const Arrlength = r2.length; +Assert.equal(Arrlength, 5); diff --git a/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_4.ts b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..16292261ef34397bd61d1aa77a1cde090a075c6a --- /dev/null +++ b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_4.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Uh…okay, that’s…seven overloads for when the second array is always empty. Let’s add some for when arr2 has one argument. + function concat(arr1: [A1, B1, C1, D1, E1, F1], arr2: [A2]): [A1, B1, C1, D1, E1, F1, A2]; + ---*/ + + +function concat( + arr1: [A1, B1, C1, D1, E1, F1, A2], + arr2: [A3] +): [A1, B1, C1, D1, E1, F1, A2, A3] { + return [...arr1, ...arr2]; +} +const arr1: [string, number, boolean, number, null, undefined, string] = [ + "hello", + 12, + true, + 45, + null, + undefined, + "tom", +]; +const arr2: [string] = ["world"]; +const result = concat(arr1, arr2); +Assert.equal(result.length, 8); diff --git a/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_5.ts b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..f87fe73a725aac496575f45e16ab085d18894e9a --- /dev/null +++ b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_5.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The first change is that spreads in tuple type syntax can now be generic. + This means that we can represent higher-order operations on tuples and arrays even when we don’t know the actual types we’re operating over. + When generic spreads are instantiated (or, replaced with a real type) in these tuple types, they can produce other sets of array and tuple types + ---*/ + + +function tail(arr: readonly [any, ...T]) { + const [_ignored, ...rest] = arr; + return rest; +} +const myTuple = [1, 2, 3, 4] as const; +const myArray = ["hello", "world"]; +const r1 = tail(myTuple); +const r2 = tail([...myTuple, ...myArray] as const); +Assert.equal(r2.length, 5); +Assert.equal(r2[0], 2); +Assert.equal(r2[1], 3); +Assert.equal(r2[2], 4); +Assert.equal(r2[3], "hello"); +Assert.equal(r2[4], "world"); diff --git a/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_6.ts b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f797666879b4f5dfd542fb947af7d3cb37832d0 --- /dev/null +++ b/test_framework/test/4.0/variadic_tuple_types/variadic_tuple_types_6.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The second change is that rest elements can occur anywhere in a tuple - not just at the end! + TypeScript 4.0 improves the inference process for rest parameters and rest tuple elements so that we can type this and have it “just work”. + ---*/ + + +type Strings = [string, string]; +type Numbers = number[]; +type Unbounded = [...Strings, ...Numbers, boolean]; +const myVariable: Unbounded = ["hello", "world", 1, 2, true]; +Assert.equal(myVariable.length, 5); +type Arr = readonly any[]; +function concat(arr1: T, arr2: U): [...T, ...U] { + return [...arr1, ...arr2]; +} +const arr1 = [1, 2, 3]; +const arr2 = ["a", "b", "c"]; +const arr3 = concat(arr1, arr2); +Assert.equal(arr3.length, 6); + +// add +type Arry = readonly unknown[]; + +function partialCall( + f: (...args: [...T, ...U]) => R, + ...headArgs: T +) { + return (...tailArgs: U) => f(...headArgs, ...tailArgs); +} +function foo(a: number, b: number, c: number): number { + return a + b + c; +} +const add2And3 = partialCall(foo, 2, 3); +const result = add2And3(4); +Assert.equal(result, 9); diff --git a/test_framework/test/4.1/abstract_members_can_not_be_marked_async/abstract_members_can_not_be_marked_async.ts b/test_framework/test/4.1/abstract_members_can_not_be_marked_async/abstract_members_can_not_be_marked_async.ts new file mode 100644 index 0000000000000000000000000000000000000000..6768ea290d4dd12c1871587d389d1f23c934cd9a --- /dev/null +++ b/test_framework/test/4.1/abstract_members_can_not_be_marked_async/abstract_members_can_not_be_marked_async.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Members marked as abstract can no longer be marked as async. + ---*/ + + +abstract class Animal { + abstract name: string; + abstract eat(): number; + run(): void { } +} +class Dog extends Animal { + name: string = "dog"; + gender: string; + constructor(gender: string) { + super(); + this.gender = gender; + } + eat(): number { + return 1; + } +} +let dog = new Dog("male"); +Assert.equal(dog.eat(), 1); +Assert.equal(dog.name, "dog"); diff --git a/test_framework/test/4.1/any_or_unknown_are_propagated_in_falsy_positions/any_or_unknown_are_propagated_in_falsy_positions.ts b/test_framework/test/4.1/any_or_unknown_are_propagated_in_falsy_positions/any_or_unknown_are_propagated_in_falsy_positions.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4c753350416703c9745be929587dc3f8538d273 --- /dev/null +++ b/test_framework/test/4.1/any_or_unknown_are_propagated_in_falsy_positions/any_or_unknown_are_propagated_in_falsy_positions.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Since nothing is known about the type on the left side of the &&, + we propagate any and unknown outward instead of the type on the right side. + ---*/ + + +let foo: unknown; +declare let somethingElse: { someProp: string }; +let anyx = foo && somethingElse; +function isThing(x: any): boolean { + return x && typeof x === "object" && x.blah === "foo"; +} +let y = isThing(anyx); +Assert.isUndefined(y); diff --git a/test_framework/test/4.1/conditional_spreads_create_optional_properties/conditional_spreads_create_optional_properties_1.ts b/test_framework/test/4.1/conditional_spreads_create_optional_properties/conditional_spreads_create_optional_properties_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..fa9a72ab46961e4874681e2e63ca66389d367906 --- /dev/null +++ b/test_framework/test/4.1/conditional_spreads_create_optional_properties/conditional_spreads_create_optional_properties_1.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the returned type sometimes uses all-optional properties. + ---*/ + + +interface Person { + name: string; + age: number; + location: string; +} +interface Animal { + name: string; + owner: Person; +} +function copyOwner(pet?: Animal) { + return { + ...(pet && pet.owner), + otherStuff: 123, + }; +} +let pet: Animal = { + name: "qiqi", + owner: { + name: "owner", + age: 11, + location: "qingdao", + }, +}; +let pet2 = copyOwner(pet); +Assert.equal(pet2.age, 11); +Assert.equal(pet2.name, "owner"); +Assert.equal(pet2.location, "qingdao"); +Assert.equal(pet2.otherStuff, 123); diff --git a/test_framework/test/4.1/key_remapping_in_mapped_types/key_remapping_in_mapped_types_1.ts b/test_framework/test/4.1/key_remapping_in_mapped_types/key_remapping_in_mapped_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..58ada5cbfadb3a3f33edefeaf981498816731ed6 --- /dev/null +++ b/test_framework/test/4.1/key_remapping_in_mapped_types/key_remapping_in_mapped_types_1.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + allows you to re-map keys in mapped types with a new as clause. + ---*/ + + +type Getters = { + [K in keyof T as `get${Capitalize}`]: () => T[K]; +}; +interface Person { + name: string; + age: number; + location: string; +} +type LazyPerson = Getters; +let k1: LazyPerson = { + getName() { + return "honny"; + }, + getAge() { + return 22; + }, + getLocation() { + return "qingdao"; + }, +}; +Assert.equal(k1.getName(), "honny"); +Assert.equal(k1.getAge(), 22); +Assert.equal(k1.getLocation(), "qingdao"); diff --git a/test_framework/test/4.1/key_remapping_in_mapped_types/key_remapping_in_mapped_types_2.ts b/test_framework/test/4.1/key_remapping_in_mapped_types/key_remapping_in_mapped_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0adbddff08f6a06d2d19c1b4e4d3694693e7798d --- /dev/null +++ b/test_framework/test/4.1/key_remapping_in_mapped_types/key_remapping_in_mapped_types_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + you can even filter out keys by producing never + ---*/ + + +type RemoveKindField = { + [K in keyof T as Exclude]: T[K]; +}; +interface Circle { + kind: "circle"; + radius: number; +} +type KindlessCircle = RemoveKindField; +let kindless: KindlessCircle = { radius: 10 }; +Assert.equal(kindless.radius, 10); diff --git a/test_framework/test/4.1/recursive_conditional_types/recursive_conditional_types.ts b/test_framework/test/4.1/recursive_conditional_types/recursive_conditional_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2f617d0717b94a089e46f2bcd3f702468a9b1ab --- /dev/null +++ b/test_framework/test/4.1/recursive_conditional_types/recursive_conditional_types.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + conditional types can now immediately reference themselves within their branches + ---*/ + + +type ElementType = T extends ReadonlyArray ? ElementType : T; + +function deepFlatten(x: T): ElementType[] { + return []; +} +let deepx = deepFlatten([1, 2, 3]); +type typeofx = typeof deepx; +let nx: typeofx = [1, 2, 3]; +Assert.isNumber(nx[0]); +let deepx2 = deepFlatten([[1], [2, 3]]); +type typeofx2 = typeof deepx2; +let nx2: typeofx2 = [4, 5, 6]; +Assert.isNumber(nx2[0]); +let deepx3 = deepFlatten([[1], [[2]], [[[3]]]]); +type typeofx3 = typeof deepx3; +let nx3: typeofx3 = [7, 8, 9]; +Assert.isNumber(nx3[0]); \ No newline at end of file diff --git "a/test_framework/test/4.1/resolver's_parameters_are_no_longer_optional_in_promises/resolve\342\200\231s_parameters_are_no_longer_optional_in_promises.ts" "b/test_framework/test/4.1/resolver's_parameters_are_no_longer_optional_in_promises/resolve\342\200\231s_parameters_are_no_longer_optional_in_promises.ts" new file mode 100644 index 0000000000000000000000000000000000000000..2ba2807abc3dc34c2cb83ac8bbe2955bd5085a88 --- /dev/null +++ "b/test_framework/test/4.1/resolver's_parameters_are_no_longer_optional_in_promises/resolve\342\200\231s_parameters_are_no_longer_optional_in_promises.ts" @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + resolve’s Parameters Are No Longer Optional in Promises + ---*/ + + +var p = Promise.resolve(); +var count: number = 1; +p.then((resolve) => { }).catch((reject) => { }); +setTimeout(function () { + count++; +}, 0); +Promise.resolve().then(function () { + count++; +}); +Assert.equal(count, 1); diff --git a/test_framework/test/4.1/template_literal_types/template_literal_types_1.ts b/test_framework/test/4.1/template_literal_types/template_literal_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f0fbbd96e50be3268fdf5301eca463b4b8f1ccea --- /dev/null +++ b/test_framework/test/4.1/template_literal_types/template_literal_types_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + String literal types in TypeScript allow us to model functions and APIs that expect a set of specific strings. + ---*/ + + +function getnum(location: "top" | "middle" | "bottom") { + if (location == "top") return 1; + else if (location == "middle") return 2; + else if (location == "bottom") return 3; +} +let num1 = getnum("top"); +let num2 = getnum("middle"); +let num3 = getnum("bottom"); +Assert.equal(num1, 1); +Assert.equal(num2, 2); +Assert.equal(num3, 3); diff --git a/test_framework/test/4.1/template_literal_types/template_literal_types_2.ts b/test_framework/test/4.1/template_literal_types/template_literal_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0a14eee615f8ec8e766a8b8b4bfbea07b21f62ff --- /dev/null +++ b/test_framework/test/4.1/template_literal_types/template_literal_types_2.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + template literal string type has the same syntax as template literal strings in JavaScript, but is used in type positions. + When you use it with concrete literal types, it produces a new string literal type by concatenating the contents. + ---*/ + + +type World = "world"; +type Greeting = `hello ${World}`; +function helloworld(w: World | Greeting) { + if (w == "world") return 1; + else if (w == "hello world") return 2; +} +let saying1 = helloworld("world"); +Assert.equal(saying1, 1); +let saying2 = helloworld("hello world"); +Assert.equal(saying2, 2); +type Color = "red" | "blue"; +type Quantity = "one" | "two"; +type SeussFish = `${Quantity | Color} fish`; +function getfish(fish: SeussFish) { + if (fish == "one fish") return 1; + else if (fish == "two fish") return 2; + else if (fish == "red fish") return "red"; + else if (fish == "blue fish") return "blue"; +} +let fish1 = getfish("one fish"); +Assert.equal(fish1, 1); +let fish2 = getfish("two fish"); +Assert.equal(fish2, 2); +let fish3 = getfish("red fish"); +Assert.equal(fish3, "red"); +let fish4 = getfish("blue fish"); +Assert.equal(fish4, "blue"); diff --git a/test_framework/test/4.1/template_literal_types/template_literal_types_3.ts b/test_framework/test/4.1/template_literal_types/template_literal_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1164613e0120d0a36662e6a892d8755909a58838 --- /dev/null +++ b/test_framework/test/4.1/template_literal_types/template_literal_types_3.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The new type aliases are Uppercase, Lowercase, Capitalize and Uncapitalize. + ---*/ + + +type UpperHello = `${Uppercase}`; +type LowerHello = `${Lowercase}`; +type CapHello = `${Capitalize}`; +type UncapHello = `${Uncapitalize}`; +type HELLO = UpperHello<"hello">; +type hello = LowerHello<"HEllO">; +type Hello = CapHello<"hello">; +type hELLO = UncapHello<"HELLO">; +function gethello(hi: HELLO | hello | Hello | hELLO) { + if (hi == "HELLO") return 1; + else if (hi == "hello") return 2; + else if (hi == "Hello") return 3; + else if (hi == "hELLO") return 4; +} +let hi1 = gethello("HELLO"); +Assert.equal(hi1, 1); +let hi2 = gethello("hello"); +Assert.equal(hi2, 2); +let hi3 = gethello("Hello"); +Assert.equal(hi3, 3); +let hi4 = gethello("hELLO"); +Assert.equal(hi4, 4); diff --git a/test_framework/test/4.2/improved_uncalled_function_checks_in_logical_expressions/improved_uncalled_function_checks_in_logical_expressions.ts b/test_framework/test/4.2/improved_uncalled_function_checks_in_logical_expressions/improved_uncalled_function_checks_in_logical_expressions.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc3c3166a3f57165a8daccdd5585156b8bee8782 --- /dev/null +++ b/test_framework/test/4.2/improved_uncalled_function_checks_in_logical_expressions/improved_uncalled_function_checks_in_logical_expressions.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript’s uncalled function checks now apply within && and || expressions. +---*/ + + +class FUN { + add2?(a: number, b: number): number; + add3(a: number, b: number, c: number) { + return a + b + c; + } +} +let fff = new FUN(); +Assert.isUndefined(fff.add2); +Assert.isFunction(fff.add3); +Assert.isUndefined(fff.add2 && fff.add3); +Assert.isFunction(fff.add2 || fff.add3); + + + + + diff --git a/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types1.ts b/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types1.ts new file mode 100644 index 0000000000000000000000000000000000000000..60ff5002b502cfeb97e5ea02961f0edcd00487e8 --- /dev/null +++ b/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In TypeScript, tuple types are meant to model arrays with specific lengths and element types. +---*/ + + +// A tuple that stores a pair of numbers +let a: [number, number] = [1, 2]; +// A tuple that stores a string, a number, and a boolean +let b: [string, number, boolean] = ["hello", 42, true]; + +Assert.equal(JSON.stringify(a), "[1,2]") +Assert.equal(JSON.stringify(b), "[\"hello\",42,true]") \ No newline at end of file diff --git a/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types2.ts b/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types2.ts new file mode 100644 index 0000000000000000000000000000000000000000..63abe28e95a11e437bdeac866e2e48d809934456 --- /dev/null +++ b/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + they can have optional elements and rest elements, and can even have labels for tooling and readability. + ---*/ + + +// A tuple that has either one or two strings. +let c: [string, string?] = ["hello"]; +c = ["hello", "world"]; + +// A labeled tuple that has either one or two strings. +let d: [first: string, second?: string] = ["hello"]; +d = ["hello", "world"]; + +// A tuple with a *rest element* - holds at least 2 strings at the front. +// and any number of booleans at the back. +let e: [string, string, ...boolean[]]; + +e = ["hello", "world"]; +e = ["hello", "world", false]; +e = ["hello", "world", true, false, true]; + +Assert.equal(JSON.stringify(c), "[\"hello\",\"world\"]") +Assert.equal(JSON.stringify(e), "[\"hello\",\"world\",true,false,true]") \ No newline at end of file diff --git a/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types3.ts b/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types3.ts new file mode 100644 index 0000000000000000000000000000000000000000..57ad21b08d49a44672af92ba91c2e59b9f35f133 --- /dev/null +++ b/test_framework/test/4.2/leading_middle_rest_elements_in_tuple_types/leading_middle_rest_elements_in_tuple_types3.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In prior versions, TypeScript only allowed ...rest elements at the very last position of a tuple type.However, now rest elements can occur anywhere within a tuple - with only a few restrictions. +---*/ + + +let foo: [...string[], number]; + +foo = [123]; +foo = ['hello', 123]; +foo = ['hello!', 'hello!', 'hello!', 123]; + +let bar: [boolean, ...string[], boolean]; + +bar = [true, false]; +bar = [true, 'some text', false]; +bar = [true, 'some', 'separated', 'text', false]; + +Assert.equal(JSON.stringify(foo), "[\"hello!\",\"hello!\",\"hello!\",123]") +Assert.equal(JSON.stringify(bar), "[true,\"some\",\"separated\",\"text\",false]") + diff --git a/test_framework/test/4.2/smarter_type_alias_preservation/smarter_type_alias_preservation1.ts b/test_framework/test/4.2/smarter_type_alias_preservation/smarter_type_alias_preservation1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2bf5b2b68f9387d03b3c3169e14f1e94acb9a1f --- /dev/null +++ b/test_framework/test/4.2/smarter_type_alias_preservation/smarter_type_alias_preservation1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript has always used a set of rules and guesses for when to reuse type aliases when printing out types. +---*/ + + +type BasicPrimitive = number | string | boolean; + +function doStuff(value: BasicPrimitive) { + let x = value; + Assert.equal(typeof x, "string") + return x; +} + +let arr = [1, "hello", false]; +Assert.equal(typeof arr[0], "number") \ No newline at end of file diff --git a/test_framework/test/4.2/smarter_type_alias_preservation/smarter_type_alias_preservation2.ts b/test_framework/test/4.2/smarter_type_alias_preservation/smarter_type_alias_preservation2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a4694b2f03e3d5037e12024ccb3481349562c587 --- /dev/null +++ b/test_framework/test/4.2/smarter_type_alias_preservation/smarter_type_alias_preservation2.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + If we hover our mouse over x in an editor like Visual Studio, Visual Studio Code, or the TypeScript Playground, we’ll get a quick info panel that shows the type BasicPrimitive. Likewise, if we get the declaration file output (.d.ts output) for this file, TypeScript will say that doStuff returns BasicPrimitive. +---*/ + + +type BasicPrimitive = number | string | boolean; + +function doStuff(value: BasicPrimitive) { + if (Math.random() < 0.5) { + Assert.equal(Math.random(), 0.3) + return undefined; + } + + return value; +} + +let arr = [10, "hello", false]; +Assert.equal(typeof arr[0], "number") \ No newline at end of file diff --git a/test_framework/test/4.3/always-truthy_promise_checks/always-truthy_promise_checks.ts b/test_framework/test/4.3/always-truthy_promise_checks/always-truthy_promise_checks.ts new file mode 100644 index 0000000000000000000000000000000000000000..cd71f01ad497372cf338d0e34f06b06161daad47 --- /dev/null +++ b/test_framework/test/4.3/always-truthy_promise_checks/always-truthy_promise_checks.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Under strictNullChecks, checking whether a Promise is 'truthy' in a conditional will trigger an error. + options: + lib: es2015 + ---*/ + + +async function foo(): Promise { + return false; +} + +async function bar(): Promise { + if (!foo()) { + return 'false'; + } + return 'true'; +} + +var b = bar() +Assert.equal(b, '[object Promise]') \ No newline at end of file diff --git a/test_framework/test/4.3/constructorParameters_works_on_abstract_class/constructorParameters_works_on_abstract_class.ts b/test_framework/test/4.3/constructorParameters_works_on_abstract_class/constructorParameters_works_on_abstract_class.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5ca1dc71108561fd2dfc06dea80ea641cdfba56 --- /dev/null +++ b/test_framework/test/4.3/constructorParameters_works_on_abstract_class/constructorParameters_works_on_abstract_class.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The ConstructorParameters type helper now works on abstract classes. + ---*/ + + +abstract class C { + a: string + b: number + constructor(a: string, b: number) { + this.a = a + this.b = b + } +} + +// Has the type '[a: string, b: number]' +type CParams = ConstructorParameters +var c: CParams = ['s', 10] +Assert.equal(c, 's,10') \ No newline at end of file diff --git a/test_framework/test/4.3/contextual_narrowing_for_generics/contextual_narrowing_for_generics.ts b/test_framework/test/4.3/contextual_narrowing_for_generics/contextual_narrowing_for_generics.ts new file mode 100644 index 0000000000000000000000000000000000000000..e3a103303737ce73211710b2050c4f4551ab9426 --- /dev/null +++ b/test_framework/test/4.3/contextual_narrowing_for_generics/contextual_narrowing_for_generics.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.3 now includes some slightly smarter type-narrowing logic on generic values. + This allows TypeScript to accept more patterns, and sometimes even catch mistakes. + ---*/ + + +// the entire follwoing example compiles with no type-checking errors. +function makeUnique | T[]>( + collection: C, + comparer: (x: T, y: T) => number +): C { + if (collection instanceof Set) { + return collection; + } + collection.sort(comparer); + for (let i = 0; i < collection.length; i++) { + let j = i; + while ( + j < collection.length && + comparer(collection[i], collection[j + 1]) === 0 + ) { + j++; + } + collection.splice(i + 1, j - i); + } + return collection; +} + +let num: number[] = [1, 3, 2, 2, 4] +function func(a: number, b: number) { + if (a == b) { + return 0 + } + return 1 +} +Assert.equal(makeUnique(num, func), '1,3,2,4') \ No newline at end of file diff --git a/test_framework/test/4.3/ecmascript_#private_class_elements/ecmascript_#private_class_elements.ts b/test_framework/test/4.3/ecmascript_#private_class_elements/ecmascript_#private_class_elements.ts new file mode 100644 index 0000000000000000000000000000000000000000..3e4edf825650cd990f69c9267935099e886614a7 --- /dev/null +++ b/test_framework/test/4.3/ecmascript_#private_class_elements/ecmascript_#private_class_elements.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.3 expands which elements in a class can be given #private #names to make them truly private at run-time. + In addition to properties, methods and accessors can also be given private names. + Even more broadly, static members can now also have private names. + options: + target: es2015 + ---*/ + + +class Foo { + #someMethod() { + return 1 + } + test1() { + return this.#someMethod() + } + static #something(num: number) { + return num + } + test2() { + return Foo.#something(10) + } + get #someValue() { + return 100 + } + publicMethod() { + this.#someMethod() + return this.#someValue + } +} + +let foo = new Foo() +Assert.equal(foo.test1(), 1) +Assert.equal(foo.test2(), 10) +Assert.equal(foo.publicMethod(), 100); \ No newline at end of file diff --git a/test_framework/test/4.3/separate_write_types_on_properties/separate_write_types_on_properties_1.ts b/test_framework/test/4.3/separate_write_types_on_properties/separate_write_types_on_properties_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..56f1661fba631b1f1f562f4aab345a851496f6a2 --- /dev/null +++ b/test_framework/test/4.3/separate_write_types_on_properties/separate_write_types_on_properties_1.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.3 allows to specify types for reading and writing to properties. + When considering how two properties with the same name relate to each other, TypeScript will only use the "reading" type. + "Writing" types are only considered when directly writing to a property. + options: + target: es2015 + lib: es2015 + ---*/ + + +class Thing { + #size = 0 + get size(): number { + return this.#size + } + set size(value: string | number | boolean) { + let num = Number(value) + if (!Number.isFinite(num)) { + this.#size = 0 + return + } + this.#size = num + } +} + +let thing = new Thing() + +thing.size = 'hello' +thing.size = 42 +thing.size = true + +// Reading `thing.size` always produces a number! +let mySize: number = thing.size +Assert.equal(typeof mySize, 'number') \ No newline at end of file diff --git a/test_framework/test/4.3/separate_write_types_on_properties/separate_write_types_on_properties_2.ts b/test_framework/test/4.3/separate_write_types_on_properties/separate_write_types_on_properties_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1eadc10e48e22353a4578f553165186589915b25 --- /dev/null +++ b/test_framework/test/4.3/separate_write_types_on_properties/separate_write_types_on_properties_2.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Be able to write getters and setters with different types in object literals. + options: + lib: es2015 + ---*/ + + +interface Thing { + get size(): number + set size(value: number | string | boolean) +} + +function makeThing(): Thing { + let size = 0 + return { + get size(): number { + return size + }, + set size(value: string | number | boolean) { + let num = Number(value) + + if (!Number.isFinite(num)) { + size = 0 + return + } + size = num + } + } +} + +let myThing = makeThing() +Assert.equal(myThing.size, 0) \ No newline at end of file diff --git a/test_framework/test/4.3/static_index_signatures/static_index_signatures.ts b/test_framework/test/4.3/static_index_signatures/static_index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..ce8a6cf6f9bedfcd082c48304fef7d778f072b7e --- /dev/null +++ b/test_framework/test/4.3/static_index_signatures/static_index_signatures.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Index signatures allow us set more properties on a value than a type explicitly declares, + And index signatures can now be declared as static. + ---*/ + + +class Foo { + hello = "hello"; + world = "1213"; + [propName: string]: string | number | undefined +} + +let instance = new Foo() +instance['whatever'] = 42 +Assert.equal(instance['whatever'], 42) + +// Has type 'string | number | undefined' +let x = instance['something'] +x = undefined +Assert.isUndefined(x) +x = 20 +Assert.equal(x, 20) +x = 'x' +Assert.equal(x, 'x') + +class Bar { + static hello = 'hello'; + static world = 1342; + static [propName: string]: string | number | undefined +} + +Bar['whatever'] = 42 +Assert.equal(Bar['whatever'], 42) + +// Has type 'string | number | undefined' +let y = Bar['something'] +y = 'y' +Assert.equal(y, 'y') +y = 10 +Assert.equal(y, 10) +y = undefined +Assert.isUndefined(y) \ No newline at end of file diff --git a/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_1.ts b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a827be4c0f759c8de0f078525f591020829d607 --- /dev/null +++ b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Template string types are types that either construct new string-like types by concatenating, + or match patterns of other string-like types. + ---*/ + + +type Color = "red" | "blue" +type Quantity = "one" | "two" + +// SeussFish1 and SeussFish2 are of the same type +type SeussFish1 = `${Quantity | Color} fish` +type SeussFish2 = "one fish" | "two fish" | "red fish" | "blue fish" +var fish1: SeussFish1 = "blue fish" +var fish2: SeussFish2 = fish1 +fish1 = "one fish" +fish2 = fish1 +fish1 = "red fish" +fish2 = fish1 +fish1 = "two fish" +fish2 = fish1 + +let s1: `${number}-${number}-${number}` +let s2: `1-2-3` = `1-2-3` +s1 = s2 +Assert.equal(s1, '1-2-3') \ No newline at end of file diff --git a/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_2.ts b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..75cfe8fc0d1c0a8efcaf4350a51c1911771338d6 --- /dev/null +++ b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_2.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a template string is contextually typed by a string-literal-like type it will try to give that expression a template type. + This also kicks in when inferring types, and the type parameter extends string. + ---*/ + + +function bar(s: string): `hello ${string}` { + // Previously an error, now works + return `hello ${s}` +} +var b = bar('s') +Assert.equal(b, 'hello s') + +let s: string = 's' +function f(x: T): T { + return x +} +let x: `hello ${string}` = f(`hello ${s}`) +Assert.equal(x, 'hello s') \ No newline at end of file diff --git a/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_3.ts b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..f73e408265d741579dd11521256ff00ede059002 --- /dev/null +++ b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript can now better-relate, and infer between, different template string types. + ---*/ + + +let s1: `${number}-${number}-${number}` +let s2: `1-2-3` = `1-2-3` +let s3: `${number}-2-3` = `${4}-2-3` +let s4: `1-${number}-3` = `1-${5}-3` +let s5: `1-2-${number}` = `1-2-${6}` +let s6: `${number}-2-${number}` = `${7}-2-${8}` + +// Now *all of these* work +s1 = s2 +Assert.equal(s1, "1-2-3"); +s1 = s3 +Assert.equal(s1, "4-2-3"); +s1 = s4 +Assert.equal(s1, "1-5-3"); +s1 = s5 +Assert.equal(s1, "1-2-6"); +s1 = s6 +Assert.equal(s1, "7-2-8"); \ No newline at end of file diff --git a/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_4.ts b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..c8d9fba348777891c095cb3d92518ea214d768e0 --- /dev/null +++ b/test_framework/test/4.3/template_string_type_improvements/template_string_type_improvements_4.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript add better inference capabilities + ---*/ + + +function foo(arg: `*${V}*`): `*${V}*` { + return arg +} +function test(s: string, n: number, b: boolean, t: T) { + // "hello" + let x1 = foo("*hello*"); + Assert.equal(x1, '*hello*') + + // "*hello*" + let x2 = foo("**hello**"); + Assert.equal(x2, '**hello**') + + // string + let x3 = foo(`*${s}*` as const); + Assert.equal(x3, '*s*') + + // `${number}` + let x4 = foo(`*${n}*` as const); + Assert.equal(x4, '*5*') + + // "true" | "false" + let x5 = foo(`*${b}*` as const); + Assert.equal(x5, '*true*') + + // `${T}` + let x6 = foo(`*${t}*` as const); + Assert.equal(x6, '*t*') + + // `*${string}*` + let x7 = foo(`**${s}**` as const); + Assert.equal(x7, '**s**') +} + +test('s', 5, true, 't') \ No newline at end of file diff --git a/test_framework/test/4.3/union_enums_cannot_be_compared_to_arbitrary_numbers/union_enums_cannot_be_compared_to_arbitrary_numbers.ts b/test_framework/test/4.3/union_enums_cannot_be_compared_to_arbitrary_numbers/union_enums_cannot_be_compared_to_arbitrary_numbers.ts new file mode 100644 index 0000000000000000000000000000000000000000..128e66b09b0a82cc307d0e3156c44d9f1cc7dc47 --- /dev/null +++ b/test_framework/test/4.3/union_enums_cannot_be_compared_to_arbitrary_numbers/union_enums_cannot_be_compared_to_arbitrary_numbers.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If a value with a union enum type is compared with a numeric literal that it could never be equal to, + then the type-checker will issue an error. + As a workaround, you can re-write an annotation to include the appropriate literal type. + You can also use a type-assertion on the value. + Alternatively, you can re-declare your enum to have a non-trivial initializer so that any number is both assignable and comparable to that enum. + ---*/ + + +enum E { + A = 0, + B = 1, +} + +// include the appropriate literal type +// Include -1 in the type +function doSomething1(x: E | -1) { + if (x === -1) { + return -1 + } + return 1 +} +Assert.equal(doSomething1(-1), -1) +Assert.equal(doSomething1(E.A), 1) + +// use a type-assertion on the value +function doSomething2(x: E) { + // Use a type asertion on 'x' + if ((x as number) === -1) { + return x + } + return x + 1 +} +Assert.equal(doSomething2(E.A), 1) + +// enum have a non-trivial initializer +enum e { + A = +0, + B = 1 +} +function doSomething3(x: e) { + if (x === 1) { + return x + } + return x + 2 +} + +Assert.equal(doSomething3(e.A), 2) +Assert.equal(doSomething3(e.B), 1) \ No newline at end of file diff --git a/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_1.ts b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..953e6055d7f80bcdf22b668cd475656e3c39dce6 --- /dev/null +++ b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_1.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description:> + When TypeScript sees that we are testing a constant value, it will do a little bit of extra work to see if it contains a type guard. + If that type guard operates on a const, a readonly property, or an un-modified parameter, + then TypeScript is able to narrow that value appropriately. + ---*/ + + +// const property +function foo(arg: unknown) { + const argIsString = typeof arg === "string"; + if (argIsString) { + Assert.equal(arg.toUpperCase(), "C"); + } +} +foo("c"); +// readonly property +interface Test { + readonly name: "caihua"; +} +function test() { + let tt: Test = { name: "caihua" }; + let res = tt.name === "caihua"; + if (res) { + Assert.isNumber(tt.name.length); + Assert.equal(tt.name[0], "c"); + } +} +test(); +// un-modified parameter +function func(a: number, b = "function" as const) { + let cc = typeof b === "function"; + if (cc) { + Assert.isNumber(b.length); + Assert.equal(b[0], "f"); + Assert.equal(cc, true); + } else { + Assert.equal(cc, false); + } +} +func(12); diff --git a/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_2.ts b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..833ddbc96e34cbdd0238e24c6f0d4e192c99554a --- /dev/null +++ b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_2.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description:Different sorts of type guard conditions are preserved. For example, checks on discriminated unions + ---*/ + + +type Circle = { kind: "circle"; radius: number }; +type Square = { kind: "square"; sideLength: number }; +type Shape = Circle | Square; + +let circle: Circle = { kind: "circle", radius: 2 }; +let square: Square = { kind: "square", sideLength: 2 }; + +function areaV1(shape: Shape): number { + const isCircle = shape.kind === "circle"; + if (isCircle) { + return Math.PI * shape.radius ** 2; + } else { + return shape.sideLength ** 2; + } +} +Assert.equal(Math.round(areaV1(circle)), 13); +Assert.equal(areaV1(square), 4); + +function areaV2(shape: Shape): number { + const { kind } = shape; + if (kind === "circle") { + return Math.PI * shape.radius ** 2; + } else { + return shape.sideLength ** 2; + } +} +Assert.equal(Math.round(areaV2(circle)), 13); +Assert.equal(areaV2(square), 4); diff --git a/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_3.ts b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7cdef63c6f7e300ff5bfbf6c655b8f3b9155569d --- /dev/null +++ b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_3.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description:> + Different sorts of type guard conditions are preserved. + As another example, here’s a function that checks whether two of its inputs have contents. + ---*/ + + +function test(a: string | undefined, b: number | undefined, c: boolean) { + const res = a && b && c; + if (res) { + Assert.equal(a.toLowerCase(), "abc"); + Assert.equal(b, 1); + Assert.equal(c, true); + return 1; + } else { + return 0; + } +} +Assert.equal(test("abc", 1, true), 1); +Assert.equal(test("abc", undefined, true), 0); diff --git a/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_4.ts b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..2c4986abd96d027cb6ca0e5a5521a8e1e75fb187 --- /dev/null +++ b/test_framework/test/4.4/control_flow_analysis_of_aliased_conditions_and_discriminants/control_flow_analysis_of_aliased_conditions_and_discriminants_4.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description:> + One neat feature here is that this analysis works transitively. + TypeScript will hop through constants to understand what sorts of checks you’ve already performed. + ---*/ + + +function isBoolean(x: string | number | boolean) { + const isString = typeof x === "string"; + const isNumber = typeof x === "number"; + const isStringOrNumber = isString || isNumber; + if (isStringOrNumber) { + return 0; + } else { + return 1; + } +} +Assert.equal(isBoolean(false), 1); +Assert.equal(isBoolean("false"), 0); +Assert.equal(isBoolean(12), 0); diff --git a/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_1.ts b/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..eea7bc017a8586c1af142aaa1d797e382c5d1c56 --- /dev/null +++ b/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_1.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript 4.4 introduces a new flag called useUnknownInCatchVariables. + This flag changes the default type of catch clause variables from any to unknown. + ---*/ + + +function test(a: any) { + if (typeof a === "number") { + } else { + throw new Error("type error"); + } +} + +try { + test("string"); +} catch (err) { + Assert.equal(err instanceof Error, true); + if (err instanceof Error) { + Assert.equal(err.message, "type error"); + } +} diff --git a/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_2.ts b/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5d0ef458c68d6e7810107e538825a3926d109872 --- /dev/null +++ b/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_2.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + useUnknownInCatchVariables flag is enabled under the strict family of options. + That means that if you check your code using strict, this option will automatically be turned on + ---*/ + + +function test(a: any) { + if (typeof a === "number") { + } else { + throw new Error("type error"); + } +} + +try { + test("string"); +} catch (err) { + Assert.equal(err instanceof Error, true); + if (err instanceof Error) { + Assert.equal(err.message, "type error"); + } +} diff --git a/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_3.ts b/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c186ae08708383f1e3eee77eef94011e8adb4c0 --- /dev/null +++ b/test_framework/test/4.4/defaulting_to_the_unknown_type_in_catch_variables/defaulting_to_the_unknown_type_in_catch_variables_3.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In cases where we don’t want to deal with an unknown variable in a catch clause, + we can always add an explicit : any annotation so that we can opt out of stricter types. + ---*/ + + +function test(a: any) { + if (typeof a === "number") { + } else { + throw new Error("type error"); + } +} + +try { + test("string"); +} catch (err: any) { + Assert.equal(err instanceof Error, true); + Assert.equal(err.message, "type error"); +} diff --git a/test_framework/test/4.4/exact_optional_property_types/exact_optional_property_types_1.ts b/test_framework/test/4.4/exact_optional_property_types/exact_optional_property_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a3368d6a458e50434e06d3e92740befebbd536a --- /dev/null +++ b/test_framework/test/4.4/exact_optional_property_types/exact_optional_property_types_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In TypeScript 4.4, the new flag exactOptionalPropertyTypes specifies that optional property types + should be interpreted exactly as written, meaning that | undefined is not added to the type + This flag needs to be turned on explicitly. It also requires strictNullChecks to be enabled as well. + ---*/ + + +interface Person { + name: string; + age?: number; +} +const p: Person = { + name: "Daniel", +}; +const keys = Object.keys(p); +Assert.equal(keys.indexOf("age"), -1); +Assert.equal(typeof p.age, "undefined"); +Assert.notEqual(typeof p.age, "number"); +// if exactOptionalPropertyTypes exists, only number type can be assigned to p.age +p.age = 12; +Assert.equal(typeof p.age, "number"); +Assert.notEqual(typeof p.age, "undefined"); diff --git a/test_framework/test/4.4/static_blocks_in_classes/static_blocks_in_classes_1.ts b/test_framework/test/4.4/static_blocks_in_classes/static_blocks_in_classes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..59fe8a193c43131015747f5a7ef5ea7dde662f4d --- /dev/null +++ b/test_framework/test/4.4/static_blocks_in_classes/static_blocks_in_classes_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + These static blocks allow you to write a sequence of statements with their own scope + that can access private fields within the containing class. + we can write initialization code with all the capabilities of writing statements, no leakage of variables, and full access to our class’s internals. +options: + target: es2015 +---*/ + + +class Test { + static #count = 0; + + get countValue() { + return Test.#count; + } + + static { + Test.#count++; + } +} +let tt = new Test(); +Assert.equal(tt.countValue, 1); diff --git a/test_framework/test/4.4/static_blocks_in_classes/static_blocks_in_classes_2.ts b/test_framework/test/4.4/static_blocks_in_classes/static_blocks_in_classes_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb0999c4067e990ccf454704f7d45d19705154fd --- /dev/null +++ b/test_framework/test/4.4/static_blocks_in_classes/static_blocks_in_classes_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: a class can have multiple static blocks, and they’re run in the same order in which they’re written. +---*/ + + +class Test { + static count = 0; + + get countValue() { + return Test.count; + } + static { + Test.count++; + Assert.equal(Test.count, 1); + } + static { + Test.count++; + Assert.equal(Test.count, 2); + } + static { + Test.count++; + Assert.equal(Test.count, 3); + } +} +let tt = new Test(); diff --git a/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_1.ts b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..821e5470530bccf30d7fbb2dfadb4e88a8d4651a --- /dev/null +++ b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript lets us describe objects where every property has to have a certain type using index signatures. + This allows us to use these objects as dictionary-like types, where we can use string keys to index into them with square brackets. + ---*/ + + +interface Test { + [key: string]: number | string; +} +let cc: Test = {}; +cc["1"] = 1; +cc["2"] = "1"; +const keys = Object.keys(cc); +keys.forEach((item) => { + Assert.isString(item); +}); diff --git a/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_2.ts b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..91d787a192f973203e3b231160bce2283a1c3b1a --- /dev/null +++ b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_2.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Array already defines a number index signature that lets us insert/retrieve values of type T + ---*/ + + +let arr: Array = []; +arr[0] = "a"; +Assert.equal(arr[0], "a"); +Assert.isString(arr[0]); +arr[1] = 12; +Assert.equal(arr[1], 12); +Assert.isNumber(arr[1]); diff --git a/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_3.ts b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..6fb1af952e39458515ba894d0de61af70a3e5782 --- /dev/null +++ b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_3.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript 4.4 allows index signatures for symbols and template string patterns. +options: + lib:es2015 + ---*/ + + +interface Color { + [keys: symbol]: number; +} +const red = Symbol("red"); +const green = Symbol("green"); +const blue = Symbol("blue"); + +let colors: Color = {}; +colors[red] = 0xff0000; +colors[green] = 0x00ff00; +colors[blue] = 0x0000ff; +Assert.isNumber(colors[red]); + + diff --git a/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_4.ts b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..f02c0a1ddf3b8a54f29ac0bbddb62c6c4c4bd201 --- /dev/null +++ b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_4.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: we can write an index signature with template string pattern type + ---*/ + + +interface PersonInfo { + name: string; + age: number; +} +interface PersonInfoWithExtraProperties extends PersonInfo { + [optName: `personal-${string}`]: unknown; +} +let a: PersonInfoWithExtraProperties = { + name: "wangcai", + age: 20, + "personal-specialHobby": "eat", +}; +Assert.isString(a.name); +Assert.isNumber(a.age); +Assert.equal(a["personal-specialHobby"], "eat"); diff --git a/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_5.ts b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..f09e03646adf38fc395035598f7e33cc617e84f9 --- /dev/null +++ b/test_framework/test/4.4/symbol_and_templates_string_pattern_index_signatures/symbol_and_templates_string_pattern_index_signatures_5.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + index signatures now permit union types,as long as they’re a union of infinite-domain primitive types + specifically:string,number,symbol,template string patterns + An index signature whose argument is a union of these types will de-sugar into several different index signatures. +options: + lib:es2015 + ---*/ + + +interface DataItem1 { + [key: string | number | symbol | `info-${string}`]: any; +} +interface DataItem2 { + [key: string]: any; + [key: symbol]: any; + [key: number]: any; +} +interface DataItem3 extends DataItem2 { + [key: `info-${string}`]: unknown; +} +const one = Symbol(1); +let a: DataItem1 = {}; +let b: DataItem3 = {}; +b[0] = "string"; +b[one] = 1; +b["string"] = "string"; +b[`info-hobby`] = "run"; +// Successful assignment +a = b; +Assert.equal(a[0], "string"); +Assert.equal(a[one], 1); +Assert.equal(a["string"], "string"); +Assert.equal(a["info-hobby"], "run"); diff --git a/test_framework/test/4.5/disabling_import_elision/animal.ts b/test_framework/test/4.5/disabling_import_elision/animal.ts new file mode 100644 index 0000000000000000000000000000000000000000..8da9358069b8dad8f2a91ed0194c54d5b29101b2 --- /dev/null +++ b/test_framework/test/4.5/disabling_import_elision/animal.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export class Animal { + color: string; + age: number; + gender: string; + constructor(color: string, age: number, gender: string) { + this.color = color; + this.age = age; + this.gender = gender; + } +} + +export { Animal as animal } \ No newline at end of file diff --git a/test_framework/test/4.5/disabling_import_elision/disabling_import_elision_1.ts b/test_framework/test/4.5/disabling_import_elision/disabling_import_elision_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..608e6ba8b3491184abb028afecff7a05363bc593 --- /dev/null +++ b/test_framework/test/4.5/disabling_import_elision/disabling_import_elision_1.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.5, you can enable a new flag called preserveValueImports to prevent TypeScript + from stripping out any imported values from your JavaScript outputs. + module: ESNext + isCurrent: true +---*/ + + +import { animal } from "./animal.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +function isFunction(fun: any, msg?: string) { + equal(typeof fun, "function", msg); +} + +isFunction(animal, "function"); \ No newline at end of file diff --git a/test_framework/test/4.5/private_field_presence_checks/private_field_presence_checks_1.ts b/test_framework/test/4.5/private_field_presence_checks/private_field_presence_checks_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..63ae5570d723cf55a5f3546923ff341229a73cc3 --- /dev/null +++ b/test_framework/test/4.5/private_field_presence_checks/private_field_presence_checks_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + You can now write a class with a #private field member and see whether another object has the same field by using the in operator. +---*/ + + +class Person { + #name: string; + constructor(name: string) { + this.#name = name; + } + + equals(other: unknown) { + return other && + typeof other === "object" && + #name in other && + this.#name === other.#name; + } +} + +const men = new Person('jack'); +const women = new Person('marry'); +Assert.equal(women.equals(men), false); \ No newline at end of file diff --git a/test_framework/test/4.5/tail_recursion_elimination_on_conditional_types/tail_recursion_elimination_on_conditional_types1.ts b/test_framework/test/4.5/tail_recursion_elimination_on_conditional_types/tail_recursion_elimination_on_conditional_types1.ts new file mode 100644 index 0000000000000000000000000000000000000000..85e4a9355019386176afd831e3a21c8245969c46 --- /dev/null +++ b/test_framework/test/4.5/tail_recursion_elimination_on_conditional_types/tail_recursion_elimination_on_conditional_types1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + As long as one branch of a conditional type is simply another conditional type, TypeScript can avoid intermediate instantiations. +---*/ + + +type TrimLeft = + T extends ` ${infer Rest}` + ? TrimLeft + : T; + +type Test = TrimLeft<" oops"> +var t: Test = 'oops' +Assert.equal(t, 'oops') \ No newline at end of file diff --git a/test_framework/test/4.5/tail_recursion_elimination_on_conditional_types/tail_recursion_elimination_on_conditional_types2.ts b/test_framework/test/4.5/tail_recursion_elimination_on_conditional_types/tail_recursion_elimination_on_conditional_types2.ts new file mode 100644 index 0000000000000000000000000000000000000000..dcbed26892a2fece841bf003310fc78e98d95ce8 --- /dev/null +++ b/test_framework/test/4.5/tail_recursion_elimination_on_conditional_types/tail_recursion_elimination_on_conditional_types2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The following type won't be optimized, since it uses the result of a conditional type by adding it to a union. + You can introduce a helper that takes an "accumulator" type parameter to make it tail-recursive +---*/ + + +type GetChars = + S extends `${infer Char}${infer Rest}` ? Char | GetChars : never; +type gc = GetChars<" getChar"> +var g1: gc = 'e' +Assert.isString(g1) + + +type GetChars1 = GetCharsHelper; +type GetCharsHelper = + S extends `${infer Char}${infer Rest}` ? GetCharsHelper : Acc; + +type gch = GetCharsHelper +var g2: gch = 10 +Assert.isNumber(g2) diff --git a/test_framework/test/4.5/template_string_types_as_discriminants/template_string_types_as_discriminants_1.ts b/test_framework/test/4.5/template_string_types_as_discriminants/template_string_types_as_discriminants_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c07a3ec1932f8b75f098270d28f114fa7b8ad4d --- /dev/null +++ b/test_framework/test/4.5/template_string_types_as_discriminants/template_string_types_as_discriminants_1.ts @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript 4.5 now can narrow values that have template string types, and also recognizes template string types as discriminants. +module: ESNext +isCurrent: true +---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +interface Message { + type: string; + url: string; +} + +interface SuccessMessage { + type: `${string}Success`; + body: string; +} + +interface ErrorMessage { + type: `${string}Error`; + message: string; +} + +function handler(r: SuccessMessage | ErrorMessage) { + if (r.type === "HttpSuccess") { + // 'r' 的类型为 'Success' + let token = r.body; + equal(r.type, "HttpSuccess"); + } else { + // 'r' 的类型为 'Error'; + equal(r.type, "HttpError"); + } +} + +const successMessage: SuccessMessage = { + type: "HttpSuccess", + body: "request success" +} + +const errorMessage: ErrorMessage = { + type: "HttpError", + message: "100-continue" +} + +handler(successMessage); +handler(errorMessage); \ No newline at end of file diff --git a/test_framework/test/4.5/the_awaited_type_and_promise_improvements/the_awaited_type_and_promise_improvements_1.ts b/test_framework/test/4.5/the_awaited_type_and_promise_improvements/the_awaited_type_and_promise_improvements_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c786d8604658651c121310b35c0c947c3fe03eb0 --- /dev/null +++ b/test_framework/test/4.5/the_awaited_type_and_promise_improvements/the_awaited_type_and_promise_improvements_1.ts @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.5 introduces a new utility type called the Awaited type. + This type is meant to model operations like await in async functions, + or the .then() method on Promises - specifically, the way that they recursively unwrap Promises; + some of the problems around inference with Promise.all served as motivations for Awaited. + module: ESNext + isCurrent: true +---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +function isObject(obj: any, msg?: string) { + equal(typeof obj, "object", msg); +} + +function MaybePromise(value: number): number | Promise | PromiseLike { + return new Promise((resolve, reject) => { + resolve(Math.random()) + }); +} + +async function doSomething(): Promise<[number, number]> { + const result = await Promise.all([MaybePromise(100), MaybePromise(200)]); + return result; +} + +let D = doSomething().then(res => { + isObject(res, "object"); +}); +// E = void; +type E = Awaited>; + +let e: E; +equal(e, undefined); \ No newline at end of file diff --git a/test_framework/test/4.5/type_modifiers_on_import_names/some-module.ts b/test_framework/test/4.5/type_modifiers_on_import_names/some-module.ts new file mode 100644 index 0000000000000000000000000000000000000000..69ba3fb8a2eba80b7a5650f19258050fa59bced0 --- /dev/null +++ b/test_framework/test/4.5/type_modifiers_on_import_names/some-module.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +let num = 5; +export let someFunc = function () { + return 10 + num +}; + +export class BaseType { +} \ No newline at end of file diff --git a/test_framework/test/4.5/type_modifiers_on_import_names/type_modifiers_on_import_names_1.ts b/test_framework/test/4.5/type_modifiers_on_import_names/type_modifiers_on_import_names_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f03ba770ecfd44628fc79c322a4a8448e67b1398 --- /dev/null +++ b/test_framework/test/4.5/type_modifiers_on_import_names/type_modifiers_on_import_names_1.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + That’s part of why TypeScript 4.5 allows a type modifier on individual named imports. + BaseType is always guaranteed to be erased and someFunc will be preserved under preserveValueImports. +module: ESNext +isCurrent: true +---*/ + + +import { someFunc, type BaseType } from "./some-module.js"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +function isFunction(fun: any, msg?: string) { + equal(typeof fun, "function", msg); +} + +function isObject(obj: any, msg?: string) { + equal(typeof obj, "object", msg); +} + +export class Thing implements BaseType { + someMethod() { + someFunc(); + } +} + +const thing = new Thing(); +isFunction(Thing, "function"); +isObject(thing, "object"); \ No newline at end of file diff --git a/test_framework/test/4.6/1_allowing_code_in_constructors.ts b/test_framework/test/4.6/1_allowing_code_in_constructors.ts new file mode 100644 index 0000000000000000000000000000000000000000..5c02827a9def78779a4eb6433b77468312522876 --- /dev/null +++ b/test_framework/test/4.6/1_allowing_code_in_constructors.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript 4.6 is now much more lenient in that check and permits other code to run before super()., all while still ensuring that super() occurs at the top-level before any references to this. +---*/ + + +class Base { } + +class Derived extends Base { + someProperty = true; + + constructor() { + let ret = doSomeStuff(); + super(); + this.someProperty = false; + } + + getSomeProperty(): boolean { + return this.someProperty; + } + + +} + +function doSomeStuff(): void { } + +let derived = new Derived(); + +Assert.equal(false, derived.getSomeProperty()); + diff --git a/test_framework/test/4.6/2_control_flow_analysis.ts b/test_framework/test/4.6/2_control_flow_analysis.ts new file mode 100644 index 0000000000000000000000000000000000000000..55937e7a7a28942f8d931a8505b9e49eeaa2c127 --- /dev/null +++ b/test_framework/test/4.6/2_control_flow_analysis.ts @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript is able to narrow types based on what’s called a discriminant property. For example, in the following code snippet, TypeScript is able to narrow the type of action based on every time we check against the value of kind. +---*/ + + +type Action = + | { kind: "NumberContents"; payload: number } + | { kind: "StringContents"; payload: string }; + +function processAction(action: Action) { + if (action.kind === "NumberContents") { + // `action.payload` is a number here. + let num = action.payload * 2; + return num; + } else if (action.kind === "StringContents") { + // `action.payload` is a string here. + const str = action.payload.trim(); + return str; + } +} + +let action1: Action = { + kind: "NumberContents", + payload: 1 +} + +let action2: Action = { + kind: "StringContents", + payload: " 1234 " +} + +Assert.equal(2, processAction(action1)); +Assert.equal("1234", processAction(action2)); + + +// you might have wanted to destructure kind and payload in the the example above. +type Action2 = + | { kind: "NumberContents"; payload: number } + | { kind: "StringContents"; payload: string }; + +function processAction2(action: Action2) { + const { kind, payload } = action; + if (kind === "NumberContents") { + let num = payload * 2; + return num; + } else if (kind === "StringContents") { + const str = payload.trim(); + return str; + } +} + + +let action3: Action = { + kind: "NumberContents", + payload: 2 +} + +let action4: Action = { + kind: "StringContents", + payload: " 5678 " +} + +Assert.equal(4, processAction2(action3)); +Assert.equal("5678", processAction2(action4)); \ No newline at end of file diff --git a/test_framework/test/4.6/3_improved_recursion_depth_checks.ts b/test_framework/test/4.6/3_improved_recursion_depth_checks.ts new file mode 100644 index 0000000000000000000000000000000000000000..142ede76c88f2f7cbca42e7f52f9a391f94d9be5 --- /dev/null +++ b/test_framework/test/4.6/3_improved_recursion_depth_checks.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: Improved Recursion Depth Checks +---*/ + + +// In a structural type system, object types are compatible based on the members they have. +interface Source { + prop: Source>; +} + +interface Target { + prop: Target>; +} + +function check(source: Source, target: Target) { + target = source; +} + + +interface Foo { + prop: T; +} + +declare let x: Foo>>>>>; +declare let y: Foo>>>>; + +// TypeScript 4.6 is now able to distinguish these cases, and correctly errors on the last example. +// x = y; this is error; + +Assert.equal(1, 1); \ No newline at end of file diff --git a/test_framework/test/4.6/4_indexed_access_inference_improvements.ts b/test_framework/test/4.6/4_indexed_access_inference_improvements.ts new file mode 100644 index 0000000000000000000000000000000000000000..ef151f3f5591eb844a4b6f93d8f2c9fc64f56619 --- /dev/null +++ b/test_framework/test/4.6/4_indexed_access_inference_improvements.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: TypeScript now can correctly infer to indexed access types which immediately index into a mapped object type. +---*/ + + +interface TypeMap { + number: number; + string: string; + boolean: boolean; +} + +type UnionRecord

= { + [K in P]: { + kind: K; + v: TypeMap[K]; + f: (p: TypeMap[K]) => void; + }; +}[P]; + +function processRecord(record: UnionRecord) { + record.f(record.v); +} + +// This call used to have issues - now works! +processRecord({ + kind: "string", + v: "hello!", + + // 'val' used to implicitly have the type 'string | number | boolean', + // but now is correctly inferred to just 'string'. + f: (val) => { + Assert.equal("HELLO!", val.toUpperCase()); + }, +}); \ No newline at end of file diff --git a/test_framework/test/4.6/5_control_flow_analysis_for_dependent_parameters.ts b/test_framework/test/4.6/5_control_flow_analysis_for_dependent_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..3fc5722975927ad238438a5ae8650853e8bbec11 --- /dev/null +++ b/test_framework/test/4.6/5_control_flow_analysis_for_dependent_parameters.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: A signature can be declared with a rest parameter whose type is a discriminated union of tuples. +---*/ + + +type Func = (...args: ["a", number] | ["b", string]) => void; + +const f1: Func = (kind, payload) => { + if (kind === "a") { + Assert.equal(42, payload.toFixed()); + } + if (kind === "b") { + Assert.equal("HELLO", payload.toUpperCase()); + } +}; + +f1("a", 42); +f1("b", "hello"); \ No newline at end of file diff --git a/test_framework/test/4.7/control-flow_analysis_for_bracketed_element_access/control-flow_analysis_for_bracketed_element_access.ts b/test_framework/test/4.7/control-flow_analysis_for_bracketed_element_access/control-flow_analysis_for_bracketed_element_access.ts new file mode 100644 index 0000000000000000000000000000000000000000..24ec48ca8cf64c8ba1952e8da417a7248cd5e13f --- /dev/null +++ b/test_framework/test/4.7/control-flow_analysis_for_bracketed_element_access/control-flow_analysis_for_bracketed_element_access.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.7 now narrows the types of element accesses when the indexed keys are literal types and unique symbols. + ---*/ + + +const key = Symbol() + +const numberOrString = Math.random() < 0.5 ? 42 : "hello" + +const obj = { + [key]: numberOrString, +} + +if (typeof obj[key] === "string") { + let str = obj[key].toUpperCase() + Assert.equal(str, 'HELLO') +} \ No newline at end of file diff --git a/test_framework/test/4.7/extends_constraints_on_infer_type_variables/extends_constraints_on_infer_type_variables.ts b/test_framework/test/4.7/extends_constraints_on_infer_type_variables/extends_constraints_on_infer_type_variables.ts new file mode 100644 index 0000000000000000000000000000000000000000..414165481aa8ffbcea268ad90a9a30fdbffc52c9 --- /dev/null +++ b/test_framework/test/4.7/extends_constraints_on_infer_type_variables/extends_constraints_on_infer_type_variables.ts @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Conditional types are a bit of a power-user feature. + They allow us to match and infer against the shape of types, and make decisions based on them. + To avoid that second level of nesting, TypeScript 4.7 now allows to place a constraint on any infer type. + ---*/ + + +// FirstIfString1 and FirstIfString2 are used in the same way +type FirstIfString1 = + T extends [infer S, ...unknown[]] + ? S extends string ? S : boolean + : boolean; + +type FirstIfString2 = + T extends [infer S extends string, ...unknown[]] + ? S + : boolean; + +// string +let A1: FirstIfString1<[string, number, number]> = 'A1' +let A2: FirstIfString2<[string, number, number]> = 'A2' +Assert.isString(A1) +Assert.isString(A2) + +// "hello" +let B1: FirstIfString1<["hello", number, number]> = 'hello' +let B2: FirstIfString2<["hello", number, number]> = 'hello' +Assert.equal(B1, 'hello') +Assert.equal(B2, 'hello') + +// "hello" | "world" +let C1: FirstIfString1<["hello" | "world", boolean]> = 'hello' +Assert.equal(C1, 'hello') +let C2: FirstIfString2<["hello" | "world", boolean]> = 'world' +Assert.equal(C2, 'world') +C1 = 'world' +Assert.equal(C1, 'world') +C2 = 'hello' +Assert.equal(C2, 'hello') + +let D1: FirstIfString1<[object, number, string]> = false +Assert.isBoolean(D1) +let D2: FirstIfString2<[object, number, string]> = false +Assert.isBoolean(D2) \ No newline at end of file diff --git a/test_framework/test/4.7/improved_function_inference_in_objects_and_methods/improved_function_inference_in_objects_and_methods.ts b/test_framework/test/4.7/improved_function_inference_in_objects_and_methods/improved_function_inference_in_objects_and_methods.ts new file mode 100644 index 0000000000000000000000000000000000000000..3047d3b7d1e17c1840e6bae9b6d127da93789e86 --- /dev/null +++ b/test_framework/test/4.7/improved_function_inference_in_objects_and_methods/improved_function_inference_in_objects_and_methods.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.7 can now perform more granular inferences from functions within objects and arrays. + This allows the types of these functions to consistently flow in a left-to-right manner just like for plain arguments. + ---*/ + + +function f(arg: { + produce: (n: string) => T, + consume: (x: T) => void +} +): void { } + +// Works +var a = f({ + produce: () => "hello", + consume: x => x.toLowerCase() +}); +Assert.isUndefined(a) +// Works +var b = f({ + produce: (n: string) => n, + consume: x => x.toLowerCase(), +}); +Assert.isUndefined(b) +// Was an error, now works. +var c = f({ + produce: n => n, + consume: x => x.toLowerCase(), +}); +Assert.isUndefined(c) +// Was an error, now works. +var d = f({ + produce: function () { return "hello"; }, + consume: x => x.toLowerCase(), +}); +Assert.isUndefined(d) +// Was an error, now works. +var e = f({ + produce() { return "hello" }, + consume: x => x.toLowerCase(), +}); +Assert.isUndefined(e) \ No newline at end of file diff --git a/test_framework/test/4.7/instantiation_expressions/instantiation_expressions.ts b/test_framework/test/4.7/instantiation_expressions/instantiation_expressions.ts new file mode 100644 index 0000000000000000000000000000000000000000..e12ecab99c98b77e5225100e173200dea962e65e --- /dev/null +++ b/test_framework/test/4.7/instantiation_expressions/instantiation_expressions.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript 4.7 allows exactly that we can now take functions and constructors and feed them type arguments directly. + ---*/ + + +interface Box { + value: T +} +interface Hammer { + name: string +} +interface Wrench { + use: string +} +function makeBox(value: T) { + return { value } +} + +const makeHammerBox = makeBox({ name: 'hammer' }) +Assert.equal(makeHammerBox, '[object Object]') +const makeWrenchBox = makeBox({ use: 'turn the screw' }) +Assert.equal(makeWrenchBox, '[object Object]') \ No newline at end of file diff --git a/test_framework/test/4.7/optional_variance_annotations_for_type_parameters/optional_variance_annotations_for_type_parameters.ts b/test_framework/test/4.7/optional_variance_annotations_for_type_parameters/optional_variance_annotations_for_type_parameters.ts new file mode 100644 index 0000000000000000000000000000000000000000..b55d1e1b4c88d385203c04d51996bb6c32fd790e --- /dev/null +++ b/test_framework/test/4.7/optional_variance_annotations_for_type_parameters/optional_variance_annotations_for_type_parameters.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + With TypeScript 4.7, we're now able to explicitly specify variance on type parameters. + ---*/ + + +interface Animal { + animalStuff: any +} +interface Dog extends Animal { + dogStugg: any +} + +// the 'out' make Getter is covariant on T +type Getter = () => T +let animal1: Getter = (): any => { } +let dog1: Getter = (): any => { } +animal1 = dog1 +Assert.isTrue(animal1 == dog1) + +// the 'in' make Setter is contravariant on T +type Setter = (value?: T) => void +let animal2: Setter = (): any => { } +let dog2: Setter = (): any => { } +dog2 = animal2 +Assert.isTrue(dog2 == animal2) \ No newline at end of file diff --git a/test_framework/test/4.7/readonly_tuples_have_a_readonly_length_property/readonly_tuples_have_a_readonly_length_property.ts b/test_framework/test/4.7/readonly_tuples_have_a_readonly_length_property/readonly_tuples_have_a_readonly_length_property.ts new file mode 100644 index 0000000000000000000000000000000000000000..3355701d040fe3807a111178457af262ab29436d --- /dev/null +++ b/test_framework/test/4.7/readonly_tuples_have_a_readonly_length_property/readonly_tuples_have_a_readonly_length_property.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A readonly tuple will now treat its length property as readonly. + ---*/ + + +function overwriteLength(tuple: readonly [string, string, string]) { + return tuple.length +} +var a = overwriteLength(['a', 'b', 'c']) +Assert.equal(a, 3) \ No newline at end of file diff --git a/test_framework/test/4.8/improved_inference_for_infer_types_in_template_string_types/improved_inference_for_infer_types_in_template_string_types.ts b/test_framework/test/4.8/improved_inference_for_infer_types_in_template_string_types/improved_inference_for_infer_types_in_template_string_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..b5d55c53798c47cfaa28719c63baec64ea4e42ad --- /dev/null +++ b/test_framework/test/4.8/improved_inference_for_infer_types_in_template_string_types/improved_inference_for_infer_types_in_template_string_types.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript recently introduced a way to add extends constraints to infer type variables in conditional types. + if these infer types appear in a template string type and are constrained to a primitive type, TypeScript will now try to parse out a literal type. + this can now better convey what a library will do at runtime, and give more precise types. + ---*/ + + +type TrySetVIfFirst = T extends [infer U extends V, ...unknown[]] ? U : never; +function setFirst(arr: any[], x: TrySetVIfFirst<[T, ...any], V>) { + arr[0] = x; + return arr; +} +let arr = ["A", "B", "C"]; +let first = setFirst(arr, 65); +Assert.equal(JSON.stringify(first), "[65,\"B\",\"C\"]"); + +type SomeNum = "100" extends `${infer U extends number}` ? U : never; +type SomeBigInt = "100" extends `${infer U extends bigint}` ? U : never; +type SomeBool = "true" extends `${infer U extends boolean}` ? U : never; +let sn: SomeNum = 100; +let sbi: SomeBigInt = 100n; +let sbool: SomeBool = true; +Assert.equal(sn, 100); +Assert.equal(sbi, 100n); +Assert.equal(sbool, true); \ No newline at end of file diff --git a/test_framework/test/4.8/improved_intersection_reduction_union_compatibility_and_narrowing/improved_intersection_reduction_union_compatibility_and_narrowing.ts b/test_framework/test/4.8/improved_intersection_reduction_union_compatibility_and_narrowing/improved_intersection_reduction_union_compatibility_and_narrowing.ts new file mode 100644 index 0000000000000000000000000000000000000000..784f881d8cafa90e9e155785c6114af8a485954e --- /dev/null +++ b/test_framework/test/4.8/improved_intersection_reduction_union_compatibility_and_narrowing/improved_intersection_reduction_union_compatibility_and_narrowing.ts @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 4.8 brings a series of correctness and consistency improvements under --strictNullChecks. These changes affect how intersection and union types work, and are leveraged in how TypeScript narrows types. + on their own, these changes may appear small - but they represent fixes for many many paper cuts that have been reported over several years. + ---*/ + + +function f(x: unknown, y: {} | null | undefined) { + x = y; + y = x; +} +f(0, {}); +f(undefined, {}); +f(null, {}); +f(0, null); +f(0, undefined); +f(undefined, null); +f(undefined, undefined); +f(null, undefined); +f(null, null); + +let n: NonNullable = "\"type NonNullable = T extends null | undefined ? never : T; \" Is reduced to \"type NonNullable = T & {};\"" +Assert.equal(n, "\"type NonNullable = T extends null | undefined ? never : T; \" Is reduced to \"type NonNullable = T & {};\""); + +function foo(x: NonNullable, y: NonNullable>) { + x = y; + y = x; + return "NonNullable> now simplifies at least to NonNullable, whereas it didn't before."; +} +let fo = foo("NonNullable", "NonNullable>"); +let s = "NonNullable> now simplifies at least to NonNullable, whereas it didn't before."; +Assert.equal(fo, s); + +function narrowUnknownishUnion(x: {} | null | undefined) { + if (x) { + x; + return "(parameter) x: {}"; + } + else { + x; + return "(parameter) x: {} | null | undefined"; + } +} +function narrowUnknown(x: unknown) { + if (x) { + x; + return "(parameter) x: {}"; + } + else { + x; + return "(parameter) x: unknown"; + } +} +let x1_1 = narrowUnknownishUnion(1); +let x1_2 = narrowUnknownishUnion(null); +let x2_1 = narrowUnknown(1); +let x2_2 = narrowUnknown(undefined); +let s1 = "(parameter) x: {}"; +let s2 = "(parameter) x: {} | null | undefined"; +let s3 = "(parameter) x: unknown"; +Assert.equal(x1_1, s1); +Assert.equal(x1_2, s2); +Assert.equal(x2_1, s1); +Assert.equal(x2_2, s3); + +function throwIfNullable(value: T): NonNullable | string { + if (value === undefined || value === null) { + return "Nullable value!" + } + return value; +} + +Assert.equal(throwIfNullable(null), "Nullable value!"); +Assert.equal(throwIfNullable(1024), 1024); \ No newline at end of file diff --git a/test_framework/test/4.9/auto_accessors_in_classes/auto_accessors_in_classes.ts b/test_framework/test/4.9/auto_accessors_in_classes/auto_accessors_in_classes.ts new file mode 100644 index 0000000000000000000000000000000000000000..31535a1aa506acd00cc1de63db2fb42b2ac753d6 --- /dev/null +++ b/test_framework/test/4.9/auto_accessors_in_classes/auto_accessors_in_classes.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 4.9 makes the in operator a little bit more powerful when narrowing types that don’t list the property at all. + options: + target: es2015 + ---*/ + + +class NormalClass { + #_str: string; + get str(): string { return this.#_str; } + set str(value: string) { this.#_str = value; } + constructor(str: string) { this.#_str = str; } +} +class AutoClass { + accessor str: string; + constructor(str: string) { + this.str = str; + } +} + +let c1 = new NormalClass("0"); +let c2 = new AutoClass("0"); +c1.str = "NormalClass"; +Assert.equal(c1.str, "NormalClass"); +c2.str = "AutoClass"; +Assert.equal(c2.str, "AutoClass"); diff --git a/test_framework/test/4.9/checks_for_equality_on_NaN/checks_for_equality_on_NaN.ts b/test_framework/test/4.9/checks_for_equality_on_NaN/checks_for_equality_on_NaN.ts new file mode 100644 index 0000000000000000000000000000000000000000..e880f087b7cd5c229f454eed76194cd30d241dff --- /dev/null +++ b/test_framework/test/4.9/checks_for_equality_on_NaN/checks_for_equality_on_NaN.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript now errors on direct comparisons against NaN, and will suggest using some variation of Number.isNaN instead. + options: + lib: es2015 + ---*/ + + +function notNaN(x: number) { + return !Number.isNaN(x); +} +Assert.isFalse(notNaN(NaN)); +Assert.isTrue(notNaN(0)); diff --git a/test_framework/test/4.9/the_satisfies_operator/the_satisfies_operator.ts b/test_framework/test/4.9/the_satisfies_operator/the_satisfies_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..70cee37738a86a72acb9dd6ef0f560f3bc153f23 --- /dev/null +++ b/test_framework/test/4.9/the_satisfies_operator/the_satisfies_operator.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the new satisfies operator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression. + ---*/ + + +type Colors = "Red" | "Green" | "Blue"; +type RGB = [Red: number, Green: number, Blue: number]; +const palette = { + Red: [255, 0, 0], + Green: "0x00ff00", + Blue: [0, 0, 255], +} satisfies Record; + +const favoriteColors = { + "Red": "yes", + "Green": false, + "Blue": "kinda", +} satisfies Record; + +const redComponent = palette.Red.length; +const greenNormalized = palette.Green.toUpperCase(); +const g: boolean = favoriteColors.Green; + +Assert.equal(redComponent, 3); +Assert.equal(greenNormalized, "0X00FF00"); +Assert.isFalse(g); diff --git a/test_framework/test/4.9/unlisted_property_narrowing_with_the_in_operator/unlisted_property_narrowing_with_the_in_operator.ts b/test_framework/test/4.9/unlisted_property_narrowing_with_the_in_operator/unlisted_property_narrowing_with_the_in_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..217de532e5806abd29ae44bd71975a7a2c2ed8be --- /dev/null +++ b/test_framework/test/4.9/unlisted_property_narrowing_with_the_in_operator/unlisted_property_narrowing_with_the_in_operator.ts @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + typescript 4.9 makes the in operator a little bit more powerful when narrowing types that don’t list the property at all. + ---*/ + + +interface RGBIN { + red: number; + green: number; + blue: number; +} + +interface HSV { + hue: number; + saturation: number; + value: number; +} + +function setColor(color: RGBIN | HSV) { + if ("hue" in color && "red" in color) { + return "RGBIN | HSV"; + } else if ("hue" in color) { + return "HSV"; + } else if ("red" in color) { + return "RGBIN"; + } +} +var c1: RGBIN = { red: 0, green: 0, blue: 0 }; +var c2: HSV = { hue: 0, saturation: 0, value: 0 }; +var c3: RGBIN | HSV = { hue: 0, saturation: 0, value: 0, red: 0, green: 0, blue: 0 }; + +Assert.equal(setColor(c1), "RGBIN"); +Assert.equal(setColor(c2), "HSV"); +Assert.equal(setColor(c3), "RGBIN | HSV"); + +interface Context { + packageJSON: unknown; +} + +function tryGetPackageName(context: Context): string | undefined { + const packageJSON = context.packageJSON; + if (packageJSON && typeof packageJSON === "object") { + if ("name" in packageJSON && typeof packageJSON.name === "string") { + return packageJSON.name; + } + } + return "no JSON"; +} +let pak: Context = { packageJSON: { name: "JSON" } }; +let none: Context = { packageJSON: "null" }; +let out1 = tryGetPackageName(pak); +let out2 = tryGetPackageName(none); +Assert.equal(out1, "JSON"); +Assert.equal(out2, "no JSON"); diff --git a/test_framework/test/declairation.yaml b/test_framework/test/declairation.yaml new file mode 100644 index 0000000000000000000000000000000000000000..adb9bfae3ab08dc8d06be385df4322ed18e4267c --- /dev/null +++ b/test_framework/test/declairation.yaml @@ -0,0 +1,12 @@ +description: #描述测试用例的测试内容,观点,一般是原文中对语法特性的描述" +options: #对应tsc的compiler option + strict: #true/false 是否采用strict模式,未设置时采用框架统一设定 + target: + lib: +module: ESNext #支持ESNext模块 +isCurrent: true #有些情况不支持默认的--outFile编译选项,这个时候设置isCurrent为true可以去掉--outFile +error: #异常系的声明 + code: #compiler异常时的错误code + message: #compiler异常时错误消息(目前未使用) + type: #对应es2abc场景时的ERRORType + phase: #异常出现的阶段 compiler/runtime 默认runtime diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_class_declarations/ambient_class_declarations.d.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_class_declarations/ambient_class_declarations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e13dc8ffbb85c4d1320f361b88a94ab1250920a0 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_class_declarations/ambient_class_declarations.d.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient class declaration declares a class type and a constructor function in the containing declaration space. + ---*/ + + +declare class ClassA { + constructor(cname?: string, cost?: number); + public cname: string; + public ccost: number; +} diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_enum_declarations/ambient_enum_declarations.d.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_enum_declarations/ambient_enum_declarations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..464a7c063db8f2436178ed1eef334b07f49f4662 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_enum_declarations/ambient_enum_declarations.d.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient enum declaration declares a class type and a constructor function in the containing declaration space. + ---*/ + + +declare enum ColorAED { + Red = 0xFF0000, + Green = 0x00FF00, + Blue = 0x0000FF, +} \ No newline at end of file diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.d.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..efe499bba820ef650e5d2f1e2a1996bf5d277d9c --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.d.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare type DF1 = () => void; +declare type DF2 = (a: string, b: string) => string; +declare type DF3 = (a: number, b?: number) => string; +declare function fun1(): void; +declare function fun2(a: number): string; \ No newline at end of file diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..ae58670ec3eedd41f5d6463081ac088a9fd50d2c --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_function_declarations/ambient_function_declarations.ts @@ -0,0 +1,42 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient function declaration introduces a function in the containing declaration space. + ambient functions may be overloaded by specifying multiple ambient function declarations with the same name, but it is an error to declare multiple overloads that are considered identical or differ only in their return types. + ambient function declarations cannot specify a function bodies and do not permit default parameter values. + ---*/ + + +const dFun1: DF1 = () => { Assert.isString("dFun1"); } + +const dFun2: DF2 = (a: string, b: string) => { Assert.isString("dFun2"); return a + b; } + +const dFun3: DF3 = (a: number, b?: number) => { + var c: any; + if (b != undefined) { + c = a + b; + } else { + c = a; + } + c = c.toString(); + Assert.isString("dFun3"); + return "$" + c; +} + +dFun1(); +Assert.equal(dFun2("A", "B"), 'AB'); +Assert.equal(dFun3(10, 20), "$30"); diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.d.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..c7e17ca08c9f73e5b719358fbbeb3c68d5448ad1 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.d.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare namespace AmbientNamespace { + var an1: string; + export var an2: string; + interface ColorInterface { + Red: number; + Green: number; + Blue: number; + } +} \ No newline at end of file diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b414fff290412b08f9d235599550e9d61bfa63e --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_namespace_declarations/ambient_namespace_declarations.ts @@ -0,0 +1,24 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient namespace declaration declares a namespace. + except for ImportAliasDeclarations, AmbientNamespaceElements always declare exported entities regardless of whether they include the optional export modifier. + ---*/ + + +var colorI: AmbientNamespace.ColorInterface = { Red: 0, Green: 1, Blue: 2 }; +Assert.equal(JSON.stringify(colorI), '{"Red":0,"Green":1,"Blue":2}'); diff --git a/test_framework/test/spec/ambients/ambient_declarations/ambient_variable_declarations/ambient_variable_declarations.d.ts b/test_framework/test/spec/ambients/ambient_declarations/ambient_variable_declarations/ambient_variable_declarations.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..e94cb2ed43a3cec4e593e761362a619e97a34c20 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_declarations/ambient_variable_declarations/ambient_variable_declarations.d.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ambient variable declaration introduces a variable in the containing declaration space. + an ambient variable declaration may optionally include a type annotation. If no type annotation is present, the variable is assumed to have type Any. + an ambient variable declaration does not permit an initializer expression to be present. + ---*/ + + +declare var varAny: any; +declare let letNum: number; +declare var varBool: boolean; diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.d.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..3b55130d4cd1f0914a86268cd05f0cca82ff8fee --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.d.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare module AMD1 { + export var a1: any; + export interface AMD1IF { + a1_1: any; + a1_2: number; + } +} diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ffdd0564e9426e2467ca431a5d69a0d45f0c295e --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_1.ts @@ -0,0 +1,23 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + ambient modules are "open-ended" and ambient module declarations with the same string literal name contribute to a single module. + ---*/ + + +var am2: AMD1.AMD1IF = { a1_1: "am2", a1_2: 123 }; +Assert.equal(JSON.stringify(am2), '{"a1_1":"am2","a1_2":123}') diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_2.d.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_2.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..82a58f67f7e53b0b65784db204996f90fedb279c --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_2.d.ts @@ -0,0 +1,35 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare module AMD2_1 { + interface AMD2_1IF { + a2_1: number; + a2_2: number; + } + var AMD2Var1: string; + export import X = AMD1; + export import Y = AMD2_2; +} +declare module AMD2_2 { + interface AMD2_2IF { + a2_1: string; + a2_2: string; + a2_3: string; + } + var AMD2Var2: number; +} + diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_2.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4336f334e74766616f4b42805c499348102c3d8d --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_2.ts @@ -0,0 +1,27 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an ImportRequireDeclaration in an AmbientModuleDeclaration may reference other modules only through top-level module names. Relative module names are not permitted. + ---*/ + + +var am2_1: AMD2_1.AMD2_1IF = { a2_1: 0, a2_2: 0 }; +var am2_2: AMD2_1.X.AMD1IF = { a1_1: { 0x00: 0x00 }, a1_2: 0xAC }; +var am2_3: AMD2_1.Y.AMD2_2IF = { a2_1: "A", a2_2: "B", a2_3: "C" }; +Assert.equal(JSON.stringify(am2_1), '{"a2_1":0,"a2_2":0}'); +Assert.equal(JSON.stringify(am2_2), '{"a1_1":{"0":0},"a1_2":172}'); +Assert.equal(JSON.stringify(am2_3), '{"a2_1":"A","a2_2":"B","a2_3":"C"}'); diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.d.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a8abaa62e9ebdaeb159cc37ae4478f23e3ea938 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.d.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export declare module AMD3_1 { + interface AMD3_1IF { + a3_1: boolean; + a3_2: number; + } + var AMD2Var1: string; +} +declare module AMD3_2 { + export interface AMD3_2IF { + a3_1: boolean; + a3_2: string; + } + export var AMD2Var1: string; +} + + diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..766ff115ec6dcdb915dcd21bb6e761eba53e34d2 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_3.ts @@ -0,0 +1,53 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if an ambient module declaration includes an export assignment, it is an error for any of the declarations within the module to specify an export modifier. + if an ambient module declaration contains no export assignment, entities declared in the module are exported regardless of whether their declarations include the optional export modifier. + module: ESNext + isCurrent: true + ---*/ + + +import { AMD3_1, AMD3_2 } from "./ambient_module_declarations_3.d"; + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +var am3_1: AMD3_1.AMD3_1IF = { a3_1: false, a3_2: 0 }; +var am3_2: AMD3_2.AMD3_2IF = { a3_1: true, a3_2: "T" }; +equal(JSON.stringify(am3_1), '{"a3_1":false,"a3_2":0}'); +equal(JSON.stringify(am3_2), '{"a3_1":true,"a3_2":"T"}'); diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_4.d.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_4.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..c7c6129a0e665f3dac478c86660f5e36da8772e9 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_4.d.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +declare module "dmname" { + interface AMD4_1IF { + a4_1: boolean; + a4_2: boolean; + } +} + +declare module "dmname" { + interface AMD4_2IF { + a4_1: number; + a4_2: number; + } +} + diff --git a/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_4.ts b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..2509005174ade17b85ccdc5ba94b63d650724b77 --- /dev/null +++ b/test_framework/test/spec/ambients/ambient_module_declarations/ambient_module_declarations_4.ts @@ -0,0 +1,52 @@ +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + ambient modules are "open-ended" and ambient module declarations with the same string literal name contribute to a single module. + module: ESNext + isCurrent: true + ---*/ + + +import * as dmname from "dmname" +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +var am4_1: dmname.AMD4_1IF = { a4_1: true, a4_2: false }; +equal(JSON.stringify(am4_1), '{"a4_1":true,"a4_2":false}') + +var am4_2: dmname.AMD4_2IF = { a4_1: 1, a4_2: 0 }; +equal(JSON.stringify(am4_2), '{"a4_1":1,"a4_2":0}') diff --git a/test_framework/test/spec/basic_concepts/declarations/declarations_1.ts b/test_framework/test/spec/basic_concepts/declarations/declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ff80f63d089095375109bf41249d1b1b6fc5a054 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/declarations/declarations_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + A name that denotes a value has an associated type and can be referenced in expressions. + A name that denotes a type can be used by itself in a type reference or on the right hand side of a dot in a type reference. + A name that denotes a namespace can be used on the left hand side of a dot in a type reference. + ---*/ + + +// name denotes a value +var X: string = "X is string"; +Assert.equal("X is string1", X + "1"); + +// name denotes a type +type X = String | Number; +var x: X = "xx"; +Assert.equal("xx", x); + +// name denotes a namespace +namespace X { + export type Y = string; +} +var y: X.Y = "ystr"; +Assert.equal("ystr", y); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/declarations/declarations_2.ts b/test_framework/test/spec/basic_concepts/declarations/declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..df5de8876e89c78606c74cbeafb0db6147975c37 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/declarations/declarations_2.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Declarations introduce the following meanings for the name they declare: + A variable, parameter, function, generator, member variable, member function, member accessor, or enum member declaration introduces a value meaning. + An interface, type alias, or type parameter declaration introduces a type meaning. + A class declaration introduces a value meaning (the constructor function) and a type meaning (the class type). + An enum declaration introduces a value meaning (the enum instance) and a type meaning (the enum type). + A namespace declaration introduces a namespace meaning (the type and namespace container) and, if the namespace is instantiated, a value meaning (the namespace instance). + An import or export declaration introduces the meaning(s) of the imported or exported entity. + ---*/ + + +// class declaration +class C { + x: string; + constructor(x: string) { + this.x = x; + } +} +var c: C = new C("x"); +Assert.equal(c.x, "x"); + +// enum declaration +enum WeekDay { + MON = 1, + TUE, + WEN, + THU, + FRI, + SAT, + SUN +} +type a = WeekDay; +var mon: a = WeekDay.MON; +Assert.equal(mon, 1); + +// namespace declaration +namespace N { + export let x: string = "x"; +} +Assert.equal(N.x, "x"); diff --git a/test_framework/test/spec/basic_concepts/names/computed_property_names/computed_property_names_1.ts b/test_framework/test/spec/basic_concepts/names/computed_property_names/computed_property_names_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a2c2bec8244103936326c7b03317f700a81f4897 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/computed_property_names/computed_property_names_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + ECMAScript 2015 permits object literals and classes to declare members with computed property names. + A computed property name specifies an expression that computes the actual property name at run-time. +options: + lib: es6 + ---*/ + + +class ComputedName { + // computed property name with a well-known symbol name have a simple literal type + aa: 1 | undefined; + // computed property name have a simple literal type + ["address"]: string; + constructor(x: string, y: 1) { + this.address = x; + if (y === 1) { + this.aa = y; + } else { + this.aa = undefined; + } + } +} +var c: ComputedName = new ComputedName("address No1", 1); +Assert.equal(1, c.aa); +Assert.equal("address No1", c.address); + +// computed property name with in object literal +var objectliteral = { ["xx" + "123".length]: 22, name: "string" }; +Assert.equal(22, objectliteral.xx3); diff --git a/test_framework/test/spec/basic_concepts/names/property_names/property_names_1.ts b/test_framework/test/spec/basic_concepts/names/property_names/property_names_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..642a5d1f14ddc881e12fd69e6e26771ea06fd93b --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/property_names/property_names_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + A property name can be any identifier (including a reserved word), a string literal, a numeric literal, + or a computed property name. String literals may be used to give properties names that are not valid identifiers, + such as names containing blanks. Numeric literal property names are equivalent to string literal property names + with the string representation of the numeric literal, as defined in the ECMAScript specification. + ---*/ + + +class Property { + // reserved word as property name + break: string; + // string literal as property name + "with blank": number; + // numeric literal as property name + 1: boolean; + constructor(x: string, y: number, z: boolean) { + this.break = x; + this["with blank"] = y; + this[1] = z; + } +} + +var p: Property = new Property("abc", 12, false); +Assert.equal("abc", p.break); +Assert.equal(12, p["with blank"]); +Assert.equal(false, p[1]); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/any_cannot_be_type_name_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/any_cannot_be_type_name_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7ef77f7e901dea0de46caa44290f42addd42da44 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/any_cannot_be_type_name_2.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: any cannot be type name, but are otherwise not restricted. + ---*/ + + +var any: string = "any"; +Assert.equal(any, "any"); diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/boolean_cannot_be_type_name_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/boolean_cannot_be_type_name_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..655446c820715a946bf3bca682c76a737044f640 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/boolean_cannot_be_type_name_2.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: boolean cannot be type name, but are otherwise not restricted. + ---*/ + + +var boolean: number = 0; +Assert.equal(boolean, 0); diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/number_cannot_be_type_name_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/number_cannot_be_type_name_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..75a8d48327e7ecd2de3072105a5c11f5f585461c --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/number_cannot_be_type_name_2.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: number cannot be type name, but are otherwise not restricted. + ---*/ + + +var number: string = "number"; +Assert.equal(number, "number"); diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/string_cannot_be_type_name_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/string_cannot_be_type_name_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f7e7efd9553c8e60dccb196e33c5a743474d9417 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/string_cannot_be_type_name_2.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: string cannot be type name, but are otherwise not restricted. + ---*/ + + +var string: number = 10; +Assert.equal(string, 10); diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/symbol_cannot_be_type_name_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/symbol_cannot_be_type_name_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3c92d4f0c6c11f56d2b1795509f9417d82127f0 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/cannot_be_type_name/symbol_cannot_be_type_name_2.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: symbol cannot be type name, but are otherwise not restricted. + ---*/ + + +var symbol: string = "any"; +Assert.equal(symbol, "any"); diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..43ff019524989e01b5059ce82a63e5013e33c5f3 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: abstract is valid identifier + ---*/ + + +var abstract: string = "abstract"; +Assert.equal(abstract, "abstract"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..37a9b7eb7d499f16cc7905b4866894b02b3333cf --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: abstract is valid identifier + ---*/ + + +function abstract(): string { + return "abstract"; +} +Assert.equal(abstract(), "abstract"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..a155e929588d2b142feda38395d0d374eff6a7b9 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: abstract is valid identifier + ---*/ + + +class abstract { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new abstract("abstract"); +Assert.equal(newobj.getName(), "abstract"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..06ec36f293059d66f2992b244d0a515830cb1059 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/abstract_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: abstract is valid identifier + ---*/ + + +interface abstruct { + name: string, + getName: () => string +} +var customer: abstruct = { + name: "abstruct", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "abstruct"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a01df49fd906fe88224e6354920e17e0bd753488 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: as is valid identifier + ---*/ + + +var as: string = "as"; +Assert.equal(as, "as"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..34f2b9d2f3611ee70c17cb8f7c305fd0dd83fa69 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: as is valid identifier + ---*/ + + +function as(): string { + return "as"; +} +Assert.equal(as(), "as"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7332d4a6e6e7a61561027f780b6869edbaa89817 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: as is valid identifier + ---*/ + + +class as { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new as("as"); +Assert.equal(newobj.getName(), "as"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f392a8ed75fb764c8b3fdca604b8e66fddc0e6f --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/as_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: as is valid identifier + ---*/ + + +interface as { + name: string, + getName: () => string +} +var customer: as = { + name: "as", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "as"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b33431cc8b2bccb0d9594361ee3429452a45103d --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: async is valid identifier + ---*/ + + +var async: string = "async"; +Assert.equal(async, "async"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..163539347884b97a1d6b5223bef4e4222f3b4ef5 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: async is valid identifier + ---*/ + + +function async(): string { + return "async"; +} +Assert.equal(async(), "async"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..f752057ea7abc11af03ec524e5aa069ffcce276d --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: async is valid identifier + ---*/ + + +class async { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new async("async"); +Assert.equal(newobj.getName(), "async"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..36c9b7762dda73828c2669488a6a491643313cc6 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/async_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: async is valid identifier + ---*/ + + +interface async { + name: string, + getName: () => string +} +var customer: async = { + name: "async", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "async"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0bd2a4c0c92d743b925f82a40464e04d8ea7cc29 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'constructor' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var constructor: string = "constructor"; +Assert.equal(constructor, "constructor"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3454abeb2d43d208083d556841faf0d4dfd82cd2 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'constructor' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function constructor(): string { + return "constructor"; +} +Assert.equal(constructor(), "constructor"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d269db624563ab49c7050cdddcdcd26987c806e5 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'constructor' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class constructor { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new constructor("constructor"); +Assert.equal(newobj.getName(), "constructor"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..7654d9df8dd8618812ae18b9c6c1116825c6b982 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/constructor_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'constructor' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface constructor { + name: string, + getName: () => string +} +var customer: constructor = { + name: "constructor", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "constructor"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c7b00bd0a0964d38d245be34d65214b87cdea3a --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'declare' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var declare: string = "declare"; +Assert.equal(declare, "declare"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f60e60db45d8bf76d891a78818c0eb0ebe6bbcf4 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'declare' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function declare(): string { + return "declare"; +} +Assert.equal(declare(), "declare"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..5dea3c81550162c7810354ff28555a3e8b9d4bdd --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'declare' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class declare { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new declare("declare"); +Assert.equal(newobj.getName(), "declare"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..98449729c8ad6e7e3419ddf86454f8a7cfba9d5f --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/declare_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'declare' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface declare { + name: string, + getName: () => string +} +var customer: declare = { + name: "declare", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "declare"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e8b808176075a4bab1bcbdb88b3d7238e4d8fba --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'from' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var from: string = "from"; +Assert.equal(from, "from"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..28905ed447d3d60e81869e581e59c0e380926fac --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'from' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function from(): string { + return "from"; +} +Assert.equal(from(), "from"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb506dbf3ac622bd28d978b568fec3df850f8d21 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'from' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class from { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new from("from"); +Assert.equal(newobj.getName(), "from"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..bef9ed8f90621b8c5b841392847ece25b47a7d50 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/from_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'from' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface from { + name: string, + getName: () => string +} +var customer: from = { + name: "from", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "from"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1b7225444af46bd4671865be1fefb0ca43a88d47 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'get' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var get: string = "get"; +Assert.equal(get, "get"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..429a35e8bdd13036e35f74a3dae08385e4382cd9 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'get' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function get(): string { + return "get"; +} +Assert.equal(get(), "get"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..ee02e5c74db3fe00fd2eed1cdc615f1352ea3293 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'get' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class get { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new get("get"); +Assert.equal(newobj.getName(), "get"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..2bd1f402f5e215fea4217344353d0c2cb2eea448 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/get_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'get' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface get { + name: string, + getName: () => string +} +var customer: get = { + name: "get", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "get"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..479fa70ae84c15fb4d0433f8f4774fc9f9ec9c55 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'is' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var is: string = "is"; +Assert.equal(is, "is"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..39458a83c74cc64ff490aad5e091ad068362fbc3 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'is' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function is(): string { + return "is"; +} +Assert.equal(is(), "is"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..dec5b21e11a7b1044dac378cb59ea32c9c6c27be --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'is' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class is { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new is("is"); +Assert.equal(newobj.getName(), "is"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..afbe8a57f425367bec70161e40a900843211abab --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/is_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'is' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface is { + name: string, + getName: () => string +} +var customer: is = { + name: "is", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "is"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b0ee8ec5de48c9cd867f543b4e3436ff761342ac --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'module' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var module2: string = "module"; +Assert.equal(module2, "module"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1177ea4d1b676e00f80abd46555fa12038d3cb91 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_2.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'module' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +{ + function module1(): string { + return "module"; + } + + Assert.equal(module1(), "module"); +} \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..38ac183fbd1dc3a5e4936da30a20af177329b3fe --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_3.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'module' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +{ + class module { + name: string; + + constructor(name: string) { + this.name = name; + } + + getName(): string { + return this.name; + } + } + + var newobj = new module("module"); + Assert.equal(newobj.getName(), "module"); +} \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a3508e197c53905ebb5ca648e5fc74fa2d151c5 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/module_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'module' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface module { + name: string, + getName: () => string +} +var customer: module = { + name: "module", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "module"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f6772029beec4c5b05d0e51944700775641b591 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'namespace' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var namespace: string = "namespace"; +Assert.equal(namespace, "namespace"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c5fb6830ccccb1317d4ec9fda01db303bd49a5c2 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'namespace' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function namespace(): string { + return "namespace"; +} +Assert.equal(namespace(), "namespace"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..5087ff40882cf8785eb8b552053cf2d50264a90a --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'namespace' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class namespace { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new namespace("namespace"); +Assert.equal(newobj.getName(), "namespace"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..a074742d974b17f2e526b676ca22f7926f659fa2 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/namespace_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'namespace' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface namespace { + name: string, + getName: () => string +} +var customer: namespace = { + name: "namespace", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "namespace"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f13db3a3f9ef250ead549247ca009c00d6bb228 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'of' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var of: string = "of"; +Assert.equal(of, "of"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1b5dbe681075a99428834119926aa5525cb3f63 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'of' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function of(): string { + return "of"; +} +Assert.equal(of(), "of"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1ad38559d14350ad844d135629de4773ddf5b4f --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'of' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class of { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new of("of"); +Assert.equal(newobj.getName(), "of"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..53e4e2098e203a12302f825f8eb6f7dd513e4e51 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/of_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'of' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface of { + name: string, + getName: () => string +} +var customer: of = { + name: "of", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "of"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/require_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/require_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5387f4cbfc42fd57d6df621527e128a9bc67e767 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/require_is_valid_identifier_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'require' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface require { + name: string, + getName: () => string +} +var customer: require = { + name: "require", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "require"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d556c2a3f6fd87f6f48186bd761e8f071d3c3a23 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'set' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var set: string = "set"; +Assert.equal(set, "set"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..184b76bb62c54c88e285bcc19cbb5469bde9073f --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'set' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function set(): string { + return "set"; +} +Assert.equal(set(), "set"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..acd08786ae54cbb1f0511eb51e83712e2122d72d --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'set' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class set { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new set("set"); +Assert.equal(newobj.getName(), "set"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..34798c3a0ecc866beacb627b3126051b63f49ffb --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/set_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'set' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface set { + name: string, + getName: () => string +} +var customer: set = { + name: "set", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "set"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_1.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9891f81cb2736c88f0cbf64646f7c89e2826a389 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'type' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +var type: string = "type"; +Assert.equal(type, "type"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_2.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f577b00436aa9d43f0a1391f95c43c490488846f --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_2.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'type' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +function type(): string { + return "type"; +} +Assert.equal(type(), "type"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_3.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..280aaa02a76c4e88785eb8d1d96e0d5fefb1ab73 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'type' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +class type { + name: string; + constructor(name: string) { + this.name = name; + } + getName(): string { + return this.name; + } +} +var newobj = new type("type"); +Assert.equal(newobj.getName(), "type"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_4.ts b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..3167afbd31d4ce665c7ea0d3bc627b861ac8766d --- /dev/null +++ b/test_framework/test/spec/basic_concepts/names/reserved_words/special_mean_but_valid_identifier/type_is_valid_identifier_4.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The keywords 'type' has special meaning in certain contexts, but is valid identifier. + ---*/ + + +interface type { + name: string, + getName: () => string +} +var customer: type = { + name: "type", + getName(): string { + return this.name; + } +} +Assert.equal(customer.name, "type"); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/scopes/let_scope.ts b/test_framework/test/spec/basic_concepts/scopes/let_scope.ts new file mode 100644 index 0000000000000000000000000000000000000000..3717f0993cda852d8a02211e270179236d2953d1 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/scopes/let_scope.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The scope of a local let declared immediately within a statement block is the body of that statement block. + The scope of a local let declaration declared immediately within the body of a function-like declaration is the body of that function-like declaration. + ---*/ + + +let GlobalScope = 1; + +function someFunc() { + let FunctionScope = 2; + if (true) { + let BlockScope = 3; + Assert.equal(FunctionScope, 2); + Assert.equal(BlockScope, 3); + } + Assert.equal(GlobalScope, 1); +} +someFunc(); \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/scopes/namespace.ts b/test_framework/test/spec/basic_concepts/scopes/namespace.ts new file mode 100644 index 0000000000000000000000000000000000000000..001156da54f2011aa94af7da1d8c318631fcf3a6 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/scopes/namespace.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + locally declared entities in a namespace are closer in scope than exported entities declared in other namespace declarations for the same namespace. + ---*/ + + +var x = 1; +namespace M { + export var x = 2; + Assert.equal(x, 2); +} +namespace M { + Assert.equal(x, 2); +} +namespace M { + var x = 3; + Assert.equal(x, 3); +} \ No newline at end of file diff --git a/test_framework/test/spec/basic_concepts/scopes/var_scope.ts b/test_framework/test/spec/basic_concepts/scopes/var_scope.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f52ec075550fdba3f1ca1d11bb6b85887e8a1e9 --- /dev/null +++ b/test_framework/test/spec/basic_concepts/scopes/var_scope.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: The scope of a local var name declared anywhere in the body of a function-like declaration is the body of that function-like declaration. + ---*/ + + +var GlobalScope = 1; + +function someFunc() { + var FunctionScope = 2; + if (true) { + var BlockScope = 3; + } + Assert.equal(FunctionScope, 2); + Assert.equal(BlockScope, 3); +} +Assert.equal(GlobalScope, 1); +someFunc(); \ No newline at end of file diff --git a/test_framework/test/spec/classes/class_declarations/class_body/class_body.ts b/test_framework/test/spec/classes/class_declarations/class_body/class_body.ts new file mode 100644 index 0000000000000000000000000000000000000000..f022efdfa4d0610821116cdea154b92d5ffb4107 --- /dev/null +++ b/test_framework/test/spec/classes/class_declarations/class_body/class_body.ts @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The class body consists of zero or more constructor or member declarations. + Statements are not allowed in the body of a class—they must be placed in the constructor or in members. + ---*/ + + +class Point { + x: number; + y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + public length() { + return Math.sqrt(this.x * this.x + this.y * this.y); + } + static origin = new Point(0, 0); +} +var p: Point = new Point(10, 20); +Assert.equal(10, p.x); +Assert.equal(20, p.y); + +// zero constructor +class Circle { + radius: number = 1; +} +const c = new Circle(); +Assert.equal(c.radius, 1); + +// more constructor +type TypeSummation = { + width?: number; + height?: number; +}; +class summation { + public width; + public height; + constructor(width: number, height: number); + constructor(ParamObje_: TypeSummation); + constructor(ParamObje_Obj_: any, height_ = 0) { + if (typeof ParamObje_Obj_ === "object") { + const { width, height } = ParamObje_Obj_; + this.width = width; + this.height = height; + } else { + this.width = ParamObje_Obj_; + this.height = height_; + } + } + sunArea(): number { + return this.width * this.height; + } +} +const sun = new summation(4, 5); +Assert.equal(sun.sunArea(), 20); +const obj: TypeSummation = { width: 10, height: 2 }; +const sun2 = new summation(obj); +Assert.equal(sun2.sunArea(), 20); diff --git a/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_1.ts b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f85e1583a47b565bb6ae31b184ae8d4357f8b36e --- /dev/null +++ b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_1.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A class declaration declares a class type and a constructor function: class BindingIdentifieropt TypeParametersopt ClassHeritage { ClassBody } + ---*/ + + +class Point { + constructor(public x: number, public y: number) { } + public length() { + return Math.sqrt(this.x * this.x + this.y * this.y); + } + static origin = new Point(0, 0); +} + +var p: Point = new Point(10, 20); +Assert.equal(10, p.x); +Assert.equal(20, p.y); + +class ChildPoint extends Point { + constructor(public x: number, public y: number) { + super(x, y); + } + public move() { + this.x += 1; + this.y += 1; + } +} + +var pChild: ChildPoint = new ChildPoint(10, 20); +pChild.move(); +Assert.equal(11, pChild.x); +Assert.equal(21, pChild.y); +class MyClass { + field: T; + constructor(field: T) { + this.field = field; + } + + public getFieldName(): T { + return this.field; + } +} +let mc: MyClass = new MyClass("a"); +Assert.equal("a", mc.field); +let mc2: MyClass = new MyClass(1); +Assert.equal(1, mc2.field); +let mc3: MyClass = new MyClass(false); +Assert.equal(false, mc3.field); +let mc4: MyClass = new MyClass(pChild); +Assert.isTrue(mc4.field instanceof ChildPoint); diff --git a/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_2.ts b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..99ba452e2a04651fd150faa185a9e7d713898574 --- /dev/null +++ b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_2.ts @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The heritage specification of a class consists of optional extends and implements clauses. + The extends clause specifies the base class of the class + and the implements clause specifies a set of interfaces for which to validate the class provides an implementation. + ---*/ + + +class Point { + constructor(public x: number, public y: number) { } + public length() { + return Math.sqrt(this.x * this.x + this.y * this.y); + } + static origin = new Point(0, 0); +} + +var p: Point = new Point(10, 20); +Assert.equal(10, p.x); +Assert.equal(20, p.y); +// the extends +class ChildPoint extends Point { + constructor(public x: number, public y: number) { + super(x, y); + } + public move() { + this.x += 1; + this.y += 1; + } +} +var pChild: ChildPoint = new ChildPoint(10, 20); +pChild.move(); +Assert.equal(11, pChild.x); +Assert.equal(21, pChild.y); +// the implements +interface InterPoint { + x: number; + y: number; +} +class Point2 implements InterPoint { + x = 1; + y = 1; + setarea(x: number, y: number) { + return x * y; + } +} +var ipoint = new Point2(); +Assert.equal(1, ipoint.x); +Assert.equal(1, ipoint.y); diff --git a/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_3.ts b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f8ee7d71b5cce196ad505f192cde8fa849a5226 --- /dev/null +++ b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_3.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The this-type (section 3.6.3) of the declared class + must be assignable (section 3.11.4) to the base type reference + and each of the type references listed in the implements clause. + ---*/ + + +class Point { + constructor(public x: number = 0) { } + public add(): this { + this.x++; + return this; + } +} +var p: Point = new Point(10); +Assert.equal(11, p.add().x); +// the extends +class ChildPoint extends Point { + constructor(public x: number = 0) { + super(x); + } + public move(): this { + this.x++; + return this; + } +} +var pChild: ChildPoint = new ChildPoint(10); +Assert.equal(11, pChild.move().x); +var basePoint: Point = pChild.move(); +Assert.equal(13, basePoint.add().x); +// the implements +interface InterPoint { + x: number; +} +class Point2 implements InterPoint { + x = 1; + setadd(): this { + this.x++; + return this; + } +} +var ipoint = new Point2(); +Assert.equal(2, ipoint.setadd().x); +var interpoint: InterPoint = ipoint.setadd(); +Assert.equal(3, interpoint.x); diff --git a/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_4.ts b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..6cda2c0273879ad375ef81ba40f5c42447fd5a13 --- /dev/null +++ b/test_framework/test/spec/classes/class_declarations/class_heritage_specification/class_heritage_specification_4.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + as is the case with every object type, type references (section 3.3.1) to the class + will appear to have the members of the global interface type named 'Object' + unless those members are hidden by members with the same name in the class. + ---*/ + + +class Point { + constructor(public x: number, public y: number) { } + public length() { } + static origin = new Point(0, 0); +} + +let p = new Point(1, 2); +let obj = new Object(); +Assert.equal(p.toString(), obj.toString()); diff --git a/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_1.ts b/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ddda360bfddaa53fd69c05fbdab4c90f3fb90d90 --- /dev/null +++ b/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If a class omits a constructor declaration, an automatic constructor is provided. + ---*/ + + +class A { + x: number = 1; + y: string = "go"; +} +let a = new A(); +Assert.equal(a.x, 1); +Assert.equal(a.y, "go"); +class B { + x: number; + y: string; + constructor(x: number = 1, y: string = "go") { + this.x = x; + this.y = y; + } +} +let b = new B(); +Assert.equal(a.x, 1); +Assert.equal(a.y, "go"); diff --git a/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_2.ts b/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d156ad6dc8b50008dfd0f5674b532ca33bd1fcba --- /dev/null +++ b/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_2.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a derived class, the automatic constructor has the same parameter list (and possibly overloads) as the base class constructor. + ---*/ + + +class Point1 { + x: string; + y: string; + constructor(x: string, y: string) { + this.x = x; + this.y = y; + } + toString() { + return this.x + " " + this.y; + } +} +class ColorPoint extends Point1 { + color: string; + constructor(x: string, y: string, color: string) { + super(x, y); + this.color = color; + } + toString() { + return this.color + " " + super.toString(); + } +} +let co = new ColorPoint("A", "B", "blue"); +Assert.equal(co.x, "A"); +Assert.equal(co.y, "B"); +Assert.equal(co.color, "blue"); +Assert.equal(co.toString(), "blue A B"); +class ColorPoint2 extends Point1 { + constructor(x: string, y: string) { + super(x, y); + } +} +let co2 = new ColorPoint2("a", "b"); +Assert.equal(co2.x, "a"); +Assert.equal(co2.y, "b"); +Assert.equal(co2.toString(), "a b"); diff --git a/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_3.ts b/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..933cca8fa699ea855a890404ef3eaeb2197f7763 --- /dev/null +++ b/test_framework/test/spec/classes/constructor_declarations/automatic_constructors/automatic_constructors_3.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a class with no extends clause, the automatic constructor has no parameters + and performs no action other than executing the instance member variable initializers (section 8.4.1), if any. + ---*/ + + +class Foo { + constructor(public x: number = 1, public y: string = "go") { + this.x = x; + this.y = y; + } +} +let foo = new Foo(); +Assert.equal(foo.x, 1); +Assert.equal(foo.y, "go"); +class Foo2 { + x: number; + y: string; + constructor(x: number, y: string) { + this.x = x; + this.y = y; + } +} +let foo2 = new Foo2(2, "go"); +Assert.equal(foo2.x, 2); +Assert.equal(foo2.y, "go"); diff --git a/test_framework/test/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_1.ts b/test_framework/test/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..97b6ab9557921b77ea500f994edf9440c6a37cc5 --- /dev/null +++ b/test_framework/test/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Constructor declarations that specify a body are called constructor implementations and + constructor declarations without a body are called constructor overloads. + It is possible to specify multiple constructor overloads in a class, + but a class can have at most one constructor implementation. + ---*/ + + +class Animal1 { + name: string | undefined; + age: number | undefined; + constructor(); + constructor(name: string); + constructor(age: number); + constructor(name: string, age: number); + constructor(nameorage?: string | number, age?: number) { + if (typeof nameorage == "number") { + this.age = nameorage; + } + if (typeof nameorage == "string") { + this.name = nameorage; + } + if (age) { + this.age = age; + } + } +} +let tt1 = new Animal1(); +let tt2 = new Animal1("caihua2", 12); +Assert.equal(tt2.name, "caihua2"); +Assert.equal(tt2.age, 12); +let tt3 = new Animal1("caihua3"); +Assert.equal(tt3.name, "caihua3"); +let tt4 = new Animal1(1230); +Assert.equal(tt4.age, 1230); + diff --git a/test_framework/test/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_2.ts b/test_framework/test/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f11618e1bc61e465c187a6ad0960119827509d66 --- /dev/null +++ b/test_framework/test/spec/classes/constructor_declarations/constructor_parameters/constructor_parameters_2.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parameter of a ConstructorImplementation may be prefixed with a public, private, or protected modifier. + This is called a parameter property declaration and is shorthand for declaring a property with the same name as the parameter + and initializing it with the value of the parameter. + ---*/ + + +class Point { + constructor(public x: number, private y: number = 1) { } + get foo(): number { + return this.y; + } +} +class Point2 { + public x: number; + protected y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + get foo(): number { + return this.y; + } +} +let p1 = new Point(1, 2); +Assert.equal(p1.x, 1); +Assert.equal(p1.foo, 2); +let p2 = new Point2(3, 4); +Assert.equal(p2.foo, 4); diff --git a/test_framework/test/spec/classes/constructor_declarations/super_calls/super_calls.ts b/test_framework/test/spec/classes/constructor_declarations/super_calls/super_calls.ts new file mode 100644 index 0000000000000000000000000000000000000000..de8d949a1e1b3597419afa57e96c47ff851faedf --- /dev/null +++ b/test_framework/test/spec/classes/constructor_declarations/super_calls/super_calls.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Super calls (section 4.9.1) are used to call the constructor of the base class. + ---*/ + + +class Point { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } +} +class ColoredPoint extends Point { + constructor(x: number, y: number, public color: string) { + super(x, y); + } +} +let p1 = new ColoredPoint(1, 2, "red"); +Assert.equal(p1.x, 1); +Assert.equal(p1.y, 2); +Assert.equal(p1.color, "red"); diff --git a/test_framework/test/spec/classes/index_member_declarations/index_member_declarations.ts b/test_framework/test/spec/classes/index_member_declarations/index_member_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d23f54471e8f9dd7c46f0e6fb2d9e65e2718a30 --- /dev/null +++ b/test_framework/test/spec/classes/index_member_declarations/index_member_declarations.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + All instance property members of a class must satisfy + the constraints implied by the index members of the class as specified in section 3.9.4. + ---*/ + + +class ind { + name: string; + [index: string]: number | string; + constructor(name: string) { + this.name = name; + } +} +let a = new ind("aa"); +a.name = "pig"; +a.age = 12; +Assert.equal(a.age, 12); +a.add = "qindao"; +Assert.equal(a.add, "qindao"); diff --git a/test_framework/test/spec/classes/members/accessibility/accessibility_1.ts b/test_framework/test/spec/classes/members/accessibility/accessibility_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..ae6b848362abf61ae36f7f796550b04af5afc6c7 --- /dev/null +++ b/test_framework/test/spec/classes/members/accessibility/accessibility_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Public property members can be accessed everywhere without restrictions. + ---*/ + + +class Base { + public a: number = 2; +} + +class Derived extends Base { + public b() { + return this.a; + } +} +const derived = new Derived(); +let a = derived.a; +Assert.equal(a, 2); +let b = derived.b(); +Assert.equal(b, 2); +const base = new Base(); +a = base.a; +Assert.equal(a, 2); diff --git a/test_framework/test/spec/classes/members/accessibility/accessibility_2.ts b/test_framework/test/spec/classes/members/accessibility/accessibility_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..21cd81fba191d6c4bc9df8191e1de7b3b49af936 --- /dev/null +++ b/test_framework/test/spec/classes/members/accessibility/accessibility_2.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Private property members can be accessed only within their declaring class. Specifically, + a private member M declared in a class C can be accessed only within the class body of C. + ---*/ + + +class Base { + private x: number = 1; + add() { + this.x++; + } + get foo() { + return this.x; + } +} +const a: Base = new Base(); +a.add(); +Assert.equal(a.foo, 2); diff --git a/test_framework/test/spec/classes/members/accessibility/accessibility_3.ts b/test_framework/test/spec/classes/members/accessibility/accessibility_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..8ef8ddddae6ac9c787a254a48e20d1ef436f6e0b --- /dev/null +++ b/test_framework/test/spec/classes/members/accessibility/accessibility_3.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Protected property members can be accessed only within their declaring class and classes derived from their declaring class, + and a protected instance property member must be accessed through an instance of the enclosing class or a subclass thereof. + ---*/ + + +class Base { + protected x: number = 1; + add() { + this.x++; + } + get foo() { + return this.x; + } +} +class Derived extends Base { + get foo() { + return this.x; + } +} +const a: Base = new Base(); +a.add(); +Assert.equal(a.foo, 2); +const b: Derived = new Derived(); +b.add(); +Assert.equal(b.foo, 2); diff --git a/test_framework/test/spec/classes/members/class_types/class_types.ts b/test_framework/test/spec/classes/members/class_types/class_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f902bc231637b91b263f90066492a1a8b2ef753 --- /dev/null +++ b/test_framework/test/spec/classes/members/class_types/class_types.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A class declaration declares a new named type called a class type. + Within the constructor and instance member functions of a class, the type of this is the this-type of that class type + ---*/ + + +class A { + public x: number; + public f() { } + public g(a: any): any { + return undefined; + } + constructor(x: number) { + this.x = x; + } + static s: string; +} +let a = new A(1); +class B extends A { + public y: number; + public g(b: boolean) { return false; } + constructor(x: number, y: number) { + super(x); + this.y = y; + } +} +let b = new B(1, 2); +interface C { + x: number; + f: () => void; + g: (a: any) => any; +} +let c: C = a; +Assert.equal(c.g(1), undefined) +interface D { + x: number; + y: number; + f: () => void; + g: (b: boolean) => boolean; +} +let d: D = b; +Assert.isFalse(d.g(true)) diff --git a/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_1.ts b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..33194aa8c2009e9603c7cec2e87b9ea44d6ed884 --- /dev/null +++ b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains no constructor declaration and has no base class, + a single construct signature with no parameters, + having the same type parameters as the class (if any) + and returning an instantiation of the class type with those type parameters passed as type arguments. + ---*/ + + +class A { + a: number = 0; + b: number = 0; + constructor() { } +} +let a = new A(); +Assert.equal(a.a, 0); diff --git a/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_2.ts b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a293993f4c542e940e9370c73b0cc5fcd5aa8e9b --- /dev/null +++ b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains no constructor declaration and has a base class, + a set of construct signatures with the same parameters as those of the base class constructor function type + following substitution of type parameters with the type arguments specified in the base class type reference, + all having the same type parameters as the class. + ---*/ + + +class A1 { + public num: number; + constructor(a: number) { + this.num = a; + } +} +class B1 extends A1 { + public numb: number; + constructor(a: number, b: number) { + super(a); + this.numb = b; + } +} +const b = new B1(2, 2); +Assert.equal(b.num, 2); diff --git a/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_3.ts b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..3c1d46f4789644d64461255d8f6ccbfc2d909882 --- /dev/null +++ b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains a constructor declaration with no overloads, + a construct signature with the parameter list of the constructor implementation, + having the same type parameters as the class (if any) + and returning an instantiation of the class type with those type parameters passed as type arguments. + ---*/ + + +class A { + r: number; + constructor(r: number) { + this.r = r; + } +} +let a = new A(1); +Assert.equal(a.r, 1); diff --git a/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_4.ts b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a1dea928ef026be069123c873e950985a427c87 --- /dev/null +++ b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_4.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the class contains a constructor declaration with overloads, a set of construct signatures + with the parameter lists of the overloads, all having the same type parameters as the class (if any) + and returning an instantiation of the class type with those type parameters passed as type arguments. + ---*/ + + +type TypeSummation = { + width?: number; + height?: number; +}; +class summation { + public width; + public height; + constructor(width: number, height: number); + constructor(ParamObje_: TypeSummation); + constructor(ParamObje_Obj_: any, height_ = 0) { + if (typeof ParamObje_Obj_ === "object") { + const { width, height } = ParamObje_Obj_; + this.width = width; + this.height = height; + } else { + this.width = ParamObje_Obj_; + this.height = height_; + } + } + sunArea(): number { + return this.width * this.height; + } +} +const sun = new summation(4, 5); +Assert.equal(sun.sunArea(), 20); +const obj: TypeSummation = { width: 10, height: 20 }; +Assert.equal(new summation(obj).sunArea(), 200); diff --git a/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_5.ts b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..051e8d7abe6f56ba38f817196b0c8d46cb4da7cd --- /dev/null +++ b/test_framework/test/spec/classes/members/constructorc_function_types/constructor_function_types_5.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Every class automatically contains a static property member named 'prototype', + the type of which is the containing class with type Any substituted for each type parameter. + ---*/ + + +class Pair { + constructor(public item1: T1, public item2: T2) { } +} +class TwoArrays extends Pair { } +let x: number = 1; +let y: number = 2; +let p = new Pair(x, y); +Assert.equal(p.item1, 1); +let x2: string = "one"; +let y2: string = "two"; +let p2 = new Pair(x2, y2); +Assert.equal(p2.item1, "one"); +let x3: boolean = true; +let p3 = new Pair(x3, y); +Assert.isBoolean(p3.item1); diff --git a/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_1.ts b/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b638796419c2ec251e02ad3e338391de12519c00 --- /dev/null +++ b/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A derived class inherits all members from its base class it doesn't override. + Inheritance means that a derived class implicitly contains all non-overridden members of the base class. + ---*/ + + +class Shape { + color: string = "black"; + switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } +} +class Circle extends Shape { } +const circle = new Circle(); +let a = circle.color; +Assert.equal(a, "black"); +circle.switchColor(); +let b = circle.color; +Assert.equal(b, "white"); diff --git a/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_2.ts b/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a670ee1ce6c199f15c2e5345ce1e95a48cef1791 --- /dev/null +++ b/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_2.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A property member in a derived class is said to override a property member in a base class + when the derived class property member has the same name and kind (instance or static) as the base class property member. + ---*/ + + +class Shape { + color: string = "black"; + switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } +} +class Circle extends Shape { + color: string = "red"; + + switchColor() { + this.color = this.color === "red" ? "green" : "red"; + } +} +const circle = new Circle(); +let a = circle.color; +Assert.equal(a, "red"); +circle.switchColor(); +let b = circle.color; +Assert.equal(b, "green"); diff --git a/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_3.ts b/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb1ef11592c2c95de701da7966ef986ac5a14fcc --- /dev/null +++ b/test_framework/test/spec/classes/members/inheritance_and_overriding/inheritance_and_overriding_3.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Base class static property members can be overridden by derived class static property members + of any kind as long as the types are compatible, as described above. + ---*/ + + +class Shape { + static color: string = "black"; + static switchColor() { + this.color = this.color === "black" ? "white" : "black"; + } +} +class Circle extends Shape { + static color: string = "red"; + + static switchColor() { + this.color = this.color === "red" ? "green" : "red"; + } +} +let a = Circle.color; +Assert.equal(a, "red"); +Circle.switchColor(); +let b = Circle.color; +Assert.equal(b, "green"); diff --git a/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_1.ts b/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1df4728fa01ddcb7ccde3afab0c9ea99b5ecca23 --- /dev/null +++ b/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Members are either instance members or static members. + ---*/ + + +class Person { + name: string = "rain"; + static age: number = 0; + constructor() { } +} +const per = new Person(); +Assert.equal(per.name, "rain"); +Assert.equal(Person.age, 0); diff --git a/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_2.ts b/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..05eea219cf6340f95604f82136a57b7fba23fe14 --- /dev/null +++ b/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Instance members are members of the class type and its associated this-type. Within constructors, + instance member functions, and instance member accessors, + the type of this is the this-type of the class. + ---*/ + + +class Counter { + private count: number = 0; + constructor(count_: number) { + this.count = count_; + } + public add(): this { + this.count++; + return this; + } + public subtract(): this { + this.count--; + return this; + } + + public getResult(): number { + return this.count; + } +} +const counter = new Counter(1); +counter.add(); +Assert.equal(counter.getResult(), 2); +counter.add(); +counter.subtract(); +Assert.equal(counter.getResult(), 2); diff --git a/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_3.ts b/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f856c9d971be55fe983eb2d4aa287775cbdbcfe --- /dev/null +++ b/test_framework/test/spec/classes/members/instance_and_static_members/instance_and_static_members_3.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Static members are declared using the static modifier and are members of the constructor function type. + Within static member functions and static member accessors, + the type of this is the constructor function type. + ---*/ + + +class Person { + static n2: number = 20; + tool(): number { + return 30; + } + static tool2() { + return this.n2; + } + static get foo() { + return this.n2; + } +} +let a: number = Person.n2; +Assert.equal(a, 20); +let b: number = Person.tool2(); +Assert.equal(b, 20); +let c: number = Person.foo; +Assert.equal(c, 20); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_accessor_declarations/member_accessor_declarations.ts b/test_framework/test/spec/classes/property_member_declarations/member_accessor_declarations/member_accessor_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f4bdbf22c53e88efe207a2fb72621c701fef24f --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_accessor_declarations/member_accessor_declarations.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member accessor declaration declares an instance member accessor or a static member accessor. + ---*/ + + +class A { + private _foo: number = 0; + get foo(): number { + return this._foo; + } + set foo(value: number) { + this._foo = value; + } + private static a: number = 0; + static get aa(): number { + return this.a; + } + static set aa(value: number) { + this.a = value; + } +} +let x = new A(); +Assert.equal(x.foo, 0); +x.foo = 10; +Assert.equal(x.foo, 10); +Assert.equal(A.aa, 0); +A.aa = 20; +Assert.equal(A.aa, 20); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_1.ts b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1c1f81e0cb72798cb32aad7b5534a919377f43b --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_1.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member function declaration declares an instance member function or a static member function. + ---*/ + + +class Point { + constructor(public x: number, public y: number) { } + public distance(p: Point) { + var dx = this.x - p.x; + var dy = this.y - p.y; + return Math.sqrt(dx * dx + dy * dy); + } + static distance(p1: Point, p2: Point) { + return p1.distance(p2); + } +} +var p1: Point = new Point(2, 2); +var p2: Point = new Point(1, 1); +Assert.equal(p1.distance(p2), Math.sqrt(2)); +Assert.equal(Point.distance(p1, p2), Math.sqrt(2)); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_2.ts b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..0bce1c88a16f0244a1ddd8a2ccb25358cbfb1af7 --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of an instance member function declaration, this is of the this-type (section 3.6.3) of the class. + ---*/ + + +class Circle { + radius: number = 1; + area(): number { + return Math.PI * this.radius * this.radius; + } +} +let c = new Circle(); +Assert.equal(c.radius, 1); +Assert.equal(c.area(), Math.PI); +c.radius = 2; +Assert.equal(c.area(), Math.PI * 4); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_3.ts b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4324d326536b7f15a2007c05a3bbe5703444c6ec --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of a static member function declaration, the type of this is the constructor function type. + ---*/ + + +class Point { + constructor(public x: number = 3, public y: number = 3) { } + static z: number; + static pro() { + return this.prototype; + } + static returnz() { + this.z; + } +} +Assert.equal(Point.pro(), "[object Object]"); +Assert.equal(Point.returnz(), undefined); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_4.ts b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..0504e3b64030469dcfa70d6bdea99ca6ed936a07 --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_4.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member function can access overridden base class members using a super property access (section 4.9.2). + ---*/ + + +class Point { + constructor(public x: number, public y: number) { } + public toString() { + return "x=" + this.x + " y=" + this.y; + } +} +class ColoredPoint extends Point { + constructor(x: number, y: number, public color: string) { + super(x, y); + } + public toString() { + return super.toString() + " color=" + this.color; + } +} +let cp = new ColoredPoint(1, 2, "red"); +Assert.equal(cp.color, "red"); +Assert.equal(cp.x, 1); +Assert.equal(cp.y, 2); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_5.ts b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..654df19addfbebd62394c104a49c7f2503ddf854 --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_function_declarations/member_function_declarations_5.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a static member function,a call to 'new this()' may actually invoke a derived class constructor + ---*/ + + +class A { + constructor(public a: number = 1) { } + static create() { + return new this(); + } +} +class B extends A { + constructor(public b: number = 2) { + super(); + } +} +// new A() +var x = A.create(); +// new B() +var y = B.create(); +Assert.equal(x.a, 1); +Assert.equal(y.a, 1); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_1.ts b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..67869399f19408bfd44263973cced0dbe5663d87 --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Member declarations with a static modifier are called static member declarations. + it is possible to have instance and static property members with the same name. + ---*/ + + +class Point { + constructor(public x: number, public y: number) { } + public distance(p: Point) { + var dx = this.x - p.x; + var dy = this.y - p.y; + return Math.sqrt(dx * dx + dy * dy); + } + static origin = new Point(0, 0); + static distance(p1: Point, p2: Point) { + return p1.distance(p2); + } + static x: number = 10; +} +var p1: Point = new Point(2, 2); +var p2: Point = new Point(1, 1); +Assert.equal(p1.distance(p2), Math.sqrt(2)); +Assert.equal(Point.distance(p1, p2), Math.sqrt(2)); +Assert.equal(p1.distance(Point.origin), Math.sqrt(8)); +Assert.equal(Point.x, 10); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_2.ts b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..e4335fa2d0286cf4657b922b8b1d8ec1240040ab --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Every class automatically contains a static property member named 'prototype' + ---*/ + + +class Point { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + static origin = new Point(0, 0); +} +class ColoredPoint extends Point { + constructor(x: number, y: number, public color: string) { + super(x, y); + } +} +Assert.equal(ColoredPoint.prototype.color, undefined); +Assert.equal(ColoredPoint.prototype.x, undefined); +Assert.equal(ColoredPoint.prototype.y, undefined); +Assert.equal(Point.prototype.x, undefined); +Assert.equal(Point.prototype.y, undefined); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_3.ts b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8b3769cbaf81593a843785fc10e8c7789a68121 --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A member variable declaration declares an instance member variable or a static member variable. + ---*/ + + +class Point { + public x: number; + public y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + static z: number = 3; +} +class ColoredPoint extends Point { + constructor(x: number, y: number, public color: string) { + super(x, y); + } + static r: number = 10; +} +let p = new Point(1, 2); +Assert.equal(p.x, 1); +Assert.equal(p.y, 2); +Assert.equal(Point.z, 3); +let cp = new ColoredPoint(4, 5, "red"); +Assert.equal(cp.x, 4); +Assert.equal(cp.y, 5); +Assert.equal(ColoredPoint.r, 10); diff --git a/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_4.ts b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a8d21f4c721d4226fcf0ffd1a347375124d079a --- /dev/null +++ b/test_framework/test/spec/classes/property_member_declarations/member_variable_declarations/member_variable_declarations_4.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Instance member variable initializers are equivalent to assignments to properties of this in the constructor + ---*/ + + +class Employee { + constructor(public name: string, public address: string, public retired = false) { } +} +class Employee2 { + public name: string; + public address: string; + public retired: boolean; + constructor(name: string, address: string, retired: boolean = false) { + this.name = name; + this.address = address; + this.retired = retired; + } +} +let em1 = new Employee("name", "qindao", false); +let em2 = new Employee2("name", "qingdao", true); +Assert.equal(em1.retired, false); +Assert.equal(em2.retired, true); + diff --git a/test_framework/test/spec/enums/constant_enum_declarations/constant_enum_declarations.ts b/test_framework/test/spec/enums/constant_enum_declarations/constant_enum_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c59531f0bdd4bdce3be2bf760981b0b475d6d6a --- /dev/null +++ b/test_framework/test/spec/enums/constant_enum_declarations/constant_enum_declarations.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + in a constant enum declaration, all members must have constant values and it is an error for a member declaration to specify an expression that isn't classified as a constant enum expression. + it is an error to reference a constant enum object in any other context than a property access that selects one of the enum's members. + the only permitted references to the enum object are those that are replaced with an enum member value. + ---*/ + + +const enum CED { + None = -1, + False, + True = 1, + DEF = 1024, +} +Assert.equal(CED.None, -1); +Assert.equal(CED.False, 0); +Assert.equal(CED.True, 1); +Assert.equal(CED.DEF, 1024); +Assert.equal(CED["None"], -1); +Assert.equal(CED["False"], 0); +Assert.equal(CED["True"], 1); +Assert.equal(CED["DEF"], 1024); + +enum CED_COPY { + None = -1, + False, + True = 1, + DEF = 1024, +} +Assert.equal(CED_COPY[-1], "None"); +Assert.equal(CED_COPY[0], "False"); +Assert.equal(CED_COPY[1], "True"); +Assert.equal(CED_COPY[1024], "DEF"); diff --git a/test_framework/test/spec/enums/declaration_merging/declaration_merging_1.ts b/test_framework/test/spec/enums/declaration_merging/declaration_merging_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f90205ae30da5f0fd5565f15018975191d53e47 --- /dev/null +++ b/test_framework/test/spec/enums/declaration_merging/declaration_merging_1.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + enums are "open-ended" and enum declarations with the same qualified name relative to a common root define a single enum type and contribute to a single enum object. + it isn't possible for one enum declaration to continue the automatic numbering sequence of another, and when an enum type has multiple declarations, only one declaration is permitted to omit a value for the first member. + ---*/ + + +enum A { + A1, + A2, +} +enum A { + A3 = 3, + A4, +} +Assert.equal(A.A1, 0); +Assert.equal(A.A2, 1); +Assert.equal(A.A3, 3); +Assert.equal(A.A4, 4); +namespace NSP { + export enum A { + A1, + A2, + } + export enum A { + A3 = 2, + A4, + } +} +Assert.equal(NSP.A.A1, 0); +Assert.equal(NSP.A.A2, 1); +Assert.equal(NSP.A.A3, 2); +Assert.equal(NSP.A.A4, 3); diff --git a/test_framework/test/spec/enums/declaration_merging/declaration_merging_2.ts b/test_framework/test/spec/enums/declaration_merging/declaration_merging_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7f9af5664cdb4a7e66ce5e6045604e957b34bdf7 --- /dev/null +++ b/test_framework/test/spec/enums/declaration_merging/declaration_merging_2.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when enum declarations are merged, they must either all specify a const modifier or all specify no const modifier. + ---*/ + + +const enum T1 { + Tex0, + Tex1, +} +const enum T1 { + Tex2 = 2, + Tex3, +} +Assert.equal(T1.Tex0, 0); +Assert.equal(T1.Tex1, 1); +Assert.equal(T1.Tex2, 2); +Assert.equal(T1.Tex3, 3); + +enum T2 { + Tex0, + Tex1, +} +enum T2 { + Tex2 = 2, + Tex3, +} +Assert.equal(T2.Tex0, 0); +Assert.equal(T2.Tex1, 1); +Assert.equal(T2.Tex2, 2); +Assert.equal(T2.Tex3, 3); diff --git a/test_framework/test/spec/enums/enum_declarations/enum_declarations_1.ts b/test_framework/test/spec/enums/enum_declarations/enum_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7846df7e83e6e2d264151356275ea3d723029a50 --- /dev/null +++ b/test_framework/test/spec/enums/enum_declarations/enum_declarations_1.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an EnumDeclaration introduces a named type and a named value in the containing declaration space. + The enum type is a distinct subtype of the Number primitive type. + the enum object is a value of an anonymous object type containing a set of properties, + all of the enum type, corresponding to the values declared for the enum type in the body of the declaration. + The enum object's type furthermore includes a numeric index signature with the signature '[x: number]: string'. + ---*/ + + +enum Color { + Red = 0xff0000, + Green = 0x00ff00, + Blue = 0x0000ff, +} +Assert.isString(Color[0xff0000]); +Assert.equal(Color[0x00ff00], "Green"); +Assert.isNumber(Color.Blue); + +enum TypeABC { + A, + B, + C, +} +var index = TypeABC.A; +Assert.equal(TypeABC.A, 0); +Assert.equal(TypeABC.B, index + 1); +Assert.equal(TypeABC.C, index + 2); diff --git a/test_framework/test/spec/enums/enum_declarations/enum_declarations_2.ts b/test_framework/test/spec/enums/enum_declarations/enum_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5bfe8cb8c7c6a781ba7d61dd11b77e214ee9916 --- /dev/null +++ b/test_framework/test/spec/enums/enum_declarations/enum_declarations_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the numeric index signature reflects a "reverse mapping" that is automatically generated in every enum object. + the reverse mapping provides a convenient way to obtain the string representation of an enum value + ---*/ + + +enum Color { + Red = 0xff0000, + Green = 0x00ff00, + Blue = 0x0000ff, +} +var c = Color.Green; +Assert.isNumber(c); +Assert.isString(Color[c]); +Assert.equal("Green", Color[c]); diff --git a/test_framework/test/spec/enums/enum_members/enum_members_1.ts b/test_framework/test/spec/enums/enum_members/enum_members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..35583f00932d6f6237c3ca922c4183539a29d6fc --- /dev/null +++ b/test_framework/test/spec/enums/enum_members/enum_members_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the member declaration specifies no value, the member is considered a constant enum member. + If the member is the first member in the enum declaration, it is assigned the value zero. + Otherwise, it is assigned the value of the immediately preceding member plus one, and an error occurs if the immediately preceding member is not a constant enum member. + if the member declaration specifies a value that can be classified as a constant enum expression, + the member is considered a constant enum member.otherwise, the member is considered a computed enum member. + ---*/ + + +enum ABCList { + A, + B, + C = "string".length, + D = 10, + E, + F = ~17, + G = 0x0f << 0x02, + H = 0xff & 0xaa, + I = E | F, +} +Assert.equal(ABCList.A, 0); +Assert.equal(ABCList.B, 1); +Assert.equal(ABCList.C, 6); +Assert.equal(ABCList.D, 10); +Assert.equal(ABCList.E, 11); +Assert.equal(ABCList.F, -18); +Assert.equal(ABCList.G, 60); +Assert.equal(ABCList.H, 170); +Assert.equal(ABCList.I, -17); diff --git a/test_framework/test/spec/expressions/array_literals/array_literal_1.ts b/test_framework/test/spec/expressions/array_literals/array_literal_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9675273c0d897a95f236db2707be4dcec7f60101 --- /dev/null +++ b/test_framework/test/spec/expressions/array_literals/array_literal_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the array literal contains no spread elements, and if the array literal is contextually typed (section 4.23) + by a type T and T has a property with the numeric name N, where N is the index of the element expression in the array literal, + the element expression is contextually typed by the type of that property. + ---*/ + + +interface MyArray { + 0: number; + 1: string; + length: 2; +} +const myArray: MyArray = [42, "hello"]; +Assert.isNumber(myArray[0]); +Assert.isString(myArray[1]); diff --git a/test_framework/test/spec/expressions/array_literals/array_literal_2.ts b/test_framework/test/spec/expressions/array_literals/array_literal_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5e5442f752d58b078d15bc4ea421cf90f58e3ebf --- /dev/null +++ b/test_framework/test/spec/expressions/array_literals/array_literal_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the array literal is contextually typed by a type T with a numeric index signature, + the element expression is contextually typed by the type of the numeric index signature + ---*/ + + +var MyArray: { + [0]: number; + [1]: string; + [2]: string; +} = [1, "aaa", "bbb"]; + +Assert.isNumber(MyArray[0]); +Assert.isString(MyArray[1]); +Assert.isString(MyArray[2]); diff --git a/test_framework/test/spec/expressions/array_literals/array_literal_3.ts b/test_framework/test/spec/expressions/array_literals/array_literal_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..01b73df1a68b9fe28e55038f7074a5cec3679bb3 --- /dev/null +++ b/test_framework/test/spec/expressions/array_literals/array_literal_3.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the array literal is empty, the resulting type is an array type with the element type Undefined. + ---*/ + + +const emptyArray: any[] = []; + +const emptyStringArray: string[] = emptyArray; +const emptyNumberArray: number[] = emptyArray; + +Assert.isUndefined(emptyArray[0]); diff --git a/test_framework/test/spec/expressions/array_literals/array_literal_4.ts b/test_framework/test/spec/expressions/array_literals/array_literal_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..56aa0065ea2fd2d975bd6ea114db756931c74105 --- /dev/null +++ b/test_framework/test/spec/expressions/array_literals/array_literal_4.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the array literal contains no spread elements and is contextually typed by a tuple-like type (section 3.3.3), + the resulting type is a tuple type constructed from the types of the element expressions + ---*/ + + +let myTuple: [string, number] = ["hello", 42]; +Assert.isString(myTuple[0]); +Assert.isNumber(myTuple[1]); + +function foo(pair: [string, number]) { + return pair +} +Assert.equal(JSON.stringify(foo(["hello", 42])), '["hello",42]'); diff --git a/test_framework/test/spec/expressions/array_literals/array_literal_5.ts b/test_framework/test/spec/expressions/array_literals/array_literal_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..b3de051c35b24582ac06f30fac2fa6c2cd909b26 --- /dev/null +++ b/test_framework/test/spec/expressions/array_literals/array_literal_5.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the array literal contains no spread elements and is an array assignment pattern in a destructuring assignment (section 4.21.1), + the resulting type is a tuple type constructed from the types of the element expressions. + ---*/ + + +let [x, y]: [string, number] = ["hello", 42]; + +Assert.isString(x); +Assert.isNumber(y); diff --git a/test_framework/test/spec/expressions/array_literals/array_literal_6.ts b/test_framework/test/spec/expressions/array_literals/array_literal_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..2c0cdc4e1fcd12c489dc5b37c020cc970d92844e --- /dev/null +++ b/test_framework/test/spec/expressions/array_literals/array_literal_6.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The resulting type is an array type with an element type that is the union of the types of + the non-spread element expressions and the numeric index signature types of the spread element expressions. + ---*/ + + +let arr = [1, 2, ...["three", "four"], 5]; + +Assert.isNumber(arr[0]); +Assert.isNumber(arr[1]); +Assert.isString(arr[2]); +Assert.isString(arr[3]); +Assert.isNumber(arr[4]); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/arrow_function/arrow_function_1.ts b/test_framework/test/spec/expressions/arrow_function/arrow_function_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..28d3b10bf5b1fa07504daf3262caae5a22103362 --- /dev/null +++ b/test_framework/test/spec/expressions/arrow_function/arrow_function_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Arrow functions are extended from JavaScript to optionally include parameter and return type annotations. + ---*/ + + +const addNumbers = (num1: number, num2: number): number => { + return num1 + num2; +}; + +const result1 = addNumbers(5, 10); +Assert.equal(result1, 15); + +const greet = (name: string) => { + return name; +}; +const result2 = greet("Alice"); +Assert.equal(result2, "Alice"); diff --git a/test_framework/test/spec/expressions/arrow_function/arrow_function_2.ts b/test_framework/test/spec/expressions/arrow_function/arrow_function_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a73f3e445817c9edbf2df20e894005a1248d6730 --- /dev/null +++ b/test_framework/test/spec/expressions/arrow_function/arrow_function_2.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + 1.( ... ) => expr is exactly equivalent to ( ... ) => { return expr ; } + 2.id => { ... } id => expr are exactly equivalent to + ( id ) => { ... } ( id ) => expr + ---*/ + + +const myFunctionA = (num: string): string => { + return num; +}; +const resultA = myFunctionA("hello,world"); +Assert.equal(resultA, "hello,world"); + +const myFunctionB: () => string = () => "hello,world"; +const resultB = myFunctionB(); +Assert.equal(resultB, "hello,world"); + +let getTempItem = (id: any) => ({ id: id, name: "Temp" }); +const TempAResult = getTempItem(123); +Assert.equal(TempAResult.id, 123); + +var getTempItemB = function (id: any) { + return { + id: id, + name: "Temp", + }; +}; +const TempBResult = getTempItemB(123); +Assert.equal(TempBResult.id, 123); diff --git a/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_1.ts b/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..187c6559e5765cf602bb097c3b584008dc60271b --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_1.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An assignment of the form 'v = expr' requires v to be classified as a reference or as an assignment pattern. + The expr expression is contextually typed by the type of v, and the type of expr must be assignable to the type of v, + or otherwise a compile-time error occurs. The result is a value with the type of expr. + ---*/ + + +var sum: Function = function (x: number, y: number) { + return x + y +} +Assert.equal(typeof sum, 'function') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_2.ts b/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f0cd13c100960590aefe14b2f0134c8dfbb597ea --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_2.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A compound assignment of the form 'v ??= expr' where '??=' is one of the compound assignment operators, + is subject to the same requirements, and produces a value of the same type, as the corresponding non-compound operation. + ---*/ + + +var x: number = 10 +var y: number = 20 +x *= y +Assert.isNumber(x) +x /= y +Assert.isNumber(x) +x %= y +Assert.isNumber(x) +x += y +Assert.isNumber(x) +x -= y +Assert.isNumber(x) +x <<= y +Assert.isNumber(x) +x >>= y +Assert.isNumber(x) +x >>>= y +Assert.isNumber(x) +x &= y +Assert.isNumber(x) +x ^= y +Assert.isNumber(x) +x |= y +Assert.isNumber(x) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_3.ts b/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f2ad6edae4a99399f2c486837d624787b439cfd --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/assignment_operators/assignment_operator_3.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A compound assignment furthermore requires v to be classified as a reference and the type of the non-compound operation to be assignable to the type of v. + ---*/ + + +let obj = { + name: 'xiao', + age: 18 +} +let v = obj +v.age += v.age +Assert.equal(v.age, 36) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_1.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..31603570f21e20c0881a69f03b76c2c7061fa0f7 --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_1.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V, if V is variable and S is assignable to the type of V. + ---*/ + + +let arr = [1, 2] +let [first, second] = arr +Assert.equal(first, 1) +Assert.equal(second, 2) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_2.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d7aaeeb74976ebde570b9f076189c3b809d27591 --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V, S is the type Any. + ---*/ + + +let obj: any = { + a: "foo", + b: 15, + c: "bar" +} +let { a, b, c } = obj; +Assert.equal(a, 'foo') +Assert.equal(b, 15) +Assert.equal(c, 'bar') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_3.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1d7921b57bf958cbb709dbac982e51dfd898f82b --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V, S has an apparent property with the property name specified in P of a type that is assignable to the target given in P. + ---*/ + + +let obj = { + a: "foo", + b: 15, + c: "bar" +} +let { a, b } = obj; +Assert.equal(a, 'foo'); +Assert.equal(b, 15); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_4.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..328c8297b757b45a328c1954de00b5bf5c8c81bc --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_4.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V, P specifies a numeric property name and S has a numeric index signature of a type that is assignable to the target given in P. + ---*/ + + +interface Foo { + [key: number]: string + 18: string + 180: string +} +let t: Foo = { + 18: 'age', + 180: 'height' +} +let v = { + 18: 'Age', + 180: 'Height' +} +v = t +Assert.equal(v[18], 'age'); +Assert.equal(v[180], 'height'); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_5.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..56dd3c7897e601e4a290f9494f25c889dd90472d --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_5.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an object assignment pattern and, + for each assignment property P in V,S has a string index signature of a type that is assignable to the target given in P. + ---*/ + + +interface Foo { + [key: string]: string + name: string + house: string +} +let t: Foo = { + name: 'xiao', + house: 'nanjing' +} +let v = { + name: '', + house: '' +} +v = t +Assert.equal(v.house, 'nanjing'); +Assert.equal(v.name, 'xiao'); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_6.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..75c585e5d0b24dd2a945f933f1d0d017ccdf2c24 --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_6.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an array assignment pattern, + for each assignment element E in V, S is the type Any. + ---*/ + + +let arr: any = [1, 2] +let [first, second]: [number, number] = arr +Assert.equal(first, 1) +Assert.equal(second, 2) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_7.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..d252bae14488a8740d864029e404b8e895b7ece9 --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_7.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an array assignment pattern, + for each assignment element E in V, S is a tuple-like type with a property named N of a type that is assignable to the target given in E, + where N is the numeric index of E in the array assignment pattern. + ---*/ + + +let s: ['0', string, number] = ['0', 'str', 10] +let [x, y, z]: [string, string, number] = s +Assert.equal(x, '0') +Assert.equal(y, 'str') +Assert.equal(z, 10) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_8.ts b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..aae858011a842c105aef42c3d00dd0a3190a22db --- /dev/null +++ b/test_framework/test/spec/expressions/assignment_operators/destructing_assignment/destructing_assignment_8.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a destructuring assignment expression, the type of the expression on the right must be assignable to the assignment target on the left. + An expression of type S is considered assignable to an assignment target V if V is an array assignment pattern, + S is the type Any or an array-like type, and, for each assignment element E in V, + S is not a tuple-like type and the numeric index signature type of S is assignable to the target given in E. + ---*/ + + +interface A { + [key: number]: string +} +let a: A = ['a', 'b', 'c'] +let [x, y, z] = [a[0], a[1], a[2]] +Assert.equal(x, 'a') +Assert.equal(y, 'b') +Assert.equal(z, 'c') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_1.ts b/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f19ba299328439130d5830f0611fe566ed0fe6af --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The binary + operator requires both operands to be of the Number primitive type or an enum type, + or at least one of the operands to be of type Any or the String primitive type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + ---*/ + + +var a: number = 10 +var b: number = 20 +var x = a + b +enum e1 { + A, + B, + C +} +enum e2 { + D, + E, + F +} +var c = e1.A +Assert.equal(c, 0) +var d = e2.D +Assert.equal(d, 0) +var y = c + d +Assert.equal(y, 0) +var e: any = true +Assert.isTrue(e) +var f: string = 's' +Assert.equal(f, 's') +var w = a + e +Assert.equal(w, 11) +var v = c + f +Assert.equal(v, '0s') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_2.ts b/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c78046ec5fd7eb57c1cea1e311804db7c72480dc --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The binary + operator, + If one or both operands are of the String primitive type, the result is of the String primitive type. + Otherwise, the result is of type Any. + ---*/ + + +var a: any = true +var b: any = 's' +var c: boolean = false +var d: string = 'str' +var e: number = 10 +var f: any = 20 +// the result is of type Any +var w = a + b +var v = a + f +Assert.isString(w) +Assert.isNumber(v) +var x = a + c +Assert.isNumber(x) +var y = a + d +Assert.isString(y) +var z = a + e +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_3.ts b/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..df6542790c07e31b35718d157d42f9aa218555e9 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_add_operator/the_add_operator_3.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The binary + operator, + A value of any type can converted to the String primitive type by adding an empty string. + ---*/ + + +function getValue() { } +var s = getValue() + "" +Assert.isString(s) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_and_operator/the_and_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_and_operator/the_and_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..ed973283e7bb6d1677cea1e50daf7059bb030c49 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_and_operator/the_and_operator.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The && operator permits the operands to be of any type and produces a result of the same type as the second operand. + ---*/ + + +var a: any = 10 +var b: any = 's' +var c: boolean = false +var d: boolean = true +var e: number = 20 +var f: number = 15 +var g: string = 'a' +var h: string = 'b' +var i: undefined = undefined +var j: undefined = undefined + +var k = a && b +Assert.isString(k) +var l = c && d +Assert.isBoolean(l) +var m = e && f +Assert.isNumber(m) +var n = g && h +Assert.isString(n) +var o = i && j +Assert.isUndefined(o) +var p = a && c +Assert.isBoolean(p) +var q = a && e +Assert.isNumber(q) +var r = a && g +Assert.isString(r) +var s = a && i +Assert.isUndefined(s) +var t = c && e +Assert.isBoolean(t) +var u = c && g +Assert.isBoolean(u) +var v = c && i +Assert.isBoolean(v) +var w = e && g +Assert.isString(w) +var x = e && i +Assert.isUndefined(x) +var y = g && i +Assert.isUndefined(y) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_congruence_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_congruence_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..de1f5a21efeee71879deafb8773c301492e241e4 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_congruence_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '===' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a === b) +Assert.isBoolean(a === c) +Assert.isBoolean(a === d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_equal_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_equal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..22c1471db8ae958c1276b24f37cb36e0c3bf461e --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_equal_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '==' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a == b) +Assert.isBoolean(a == c) +Assert.isBoolean(a == d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_greater_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_greater_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..2354014a96e62333040b4d6075fca5f481947b94 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_greater_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '>' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a > b) +Assert.isBoolean(a > c) +Assert.isBoolean(a > d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_greater_or_equal_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_greater_or_equal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..67c227ff633f3a1bd7148cad92829b9091229881 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_greater_or_equal_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '>=' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a >= b) +Assert.isBoolean(a >= c) +Assert.isBoolean(a >= d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_incongruence_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_incongruence_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e1f7f071d90049b3ac07f973d01d5a88b6dac50 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_incongruence_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '!==' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a !== b) +Assert.isBoolean(a !== c) +Assert.isBoolean(a !== d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_less_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_less_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..0a50c0004030b345007a56df4817eff8bae5490a --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_less_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '<' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a < b) +Assert.isBoolean(a < c) +Assert.isBoolean(a < d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_less_or_equal_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_less_or_equal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..36f9e65314c9a2379d2b252b81e21771424650de --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_less_or_equal_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '<=' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a <= b) +Assert.isBoolean(a <= c) +Assert.isBoolean(a <= d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_unequal_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_unequal_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..520f2c083dbe90eecd1477cc4de28538140d7ceb --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_greater_less_lessEqual_greaterEqual_equal_unequal_congruence_incongruence_operators/the_unequal_operator.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '!=' operator require one or both of the operand types to be assignable to the other. + The result is always of the Boolean primitive type. + ---*/ + + +var a: any = undefined +var b: boolean = true +var c: number = 10 +var d: string = 'str' +Assert.isBoolean(a != b) +Assert.isBoolean(a != c) +Assert.isBoolean(a != d) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_in_operator/the_in_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_in_operator/the_in_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..395d483c2e8fa7c25c7b031f49293f7b8432c414 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_in_operator/the_in_operator.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The in operator requires the left operand to be of type Any, the String primitive type, or the Number primitive type, + and the right operand to be of type Any, an object type, or a type parameter type. + The result is always of the Boolean primitive type. + ---*/ + + +interface A { + num: number +} +interface B { + str: string +} +function isString(k: A | B) { + if ('num' in k) { + Assert.isBoolean('num' in k) + return false + } + return true +} +isString({ num: 20 }) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_instanceof_operator/the_instanceof_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_instanceof_operator/the_instanceof_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..9f8a921a3ed837cfc05aa4bd73f8a14177d8caa7 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_instanceof_operator/the_instanceof_operator.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The instanceof operator requires the left operand to be of type Any, an object type, or a type parameter type, + and the right operand to be of type Any or a subtype of the 'Function' interface type. + The result is always of the Boolean primitive type. + ---*/ + + +interface Animal { + species: string +} +class Felidae implements Animal { + kind: string + species: string + constructor(species: string, kind: string) { + this.species = species + this.kind = kind + } +} +class Canidae implements Animal { + species: string + name: string + constructor(species: string, name: string) { + this.species = species + this.name = name + } +} +const getRandomAnimal = () => { + return Math.random() < 0.5 ? + new Canidae('Canidae', 'Wolf') : + new Felidae('Felidae', 'Tiger') +} +let Animal = getRandomAnimal() +if (Animal instanceof Canidae) { + Assert.isBoolean(Animal instanceof Canidae) +} +if (Animal instanceof Felidae) { + Assert.isBoolean(Animal instanceof Felidae) +} \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_and_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_and_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..6b92df9e75ff6d7654ec14013a5703ad02bfb1a8 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_and_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '&' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a & b +var y = a & c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a & d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_division_method_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_division_method_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..91a92e43f49d9c06680e7286fb9ed38a64ba4fc8 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_division_method_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '/' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a / b +var y = a / c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a / d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_left_shift_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_left_shift_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..023378157fdc7700351f8ced711a73f0b936bcd6 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_left_shift_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '<<' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a << b +var y = a << c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a << d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_multiplication_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_multiplication_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..ca238cdd51a227f1ed0825035411e4af5d162800 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_multiplication_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '*' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a * b +var y = a * c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a * d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_or_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_or_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..7839999d77f88bce708d311960734e892ed52c6c --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_or_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '|' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a | b +var y = a | c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a | d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_remainder_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_remainder_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..806f1af2eb45fd32179250c164c9b02f8581d8dd --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_remainder_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '%' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a % b +var y = a % c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a % d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_right_shift_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_right_shift_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..ca09f89d4666451e62a6dc1e7f8544d4ca06c90b --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_right_shift_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '>>' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a >> b +var y = a >> c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a >> d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_subtraction_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_subtraction_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..968601c8d4280c88d0a78a85077890b594859d56 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_subtraction_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '-' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a - b +var y = a - c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a - d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_unsigned_shift_to_the_right.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_unsigned_shift_to_the_right.ts new file mode 100644 index 0000000000000000000000000000000000000000..ed96ce222f9398ed125504ab04fbe3700f14753f --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_unsigned_shift_to_the_right.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '>>>' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a >>> b +var y = a >>> c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a >>> d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_xor_operator.ts b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_xor_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..0eb59672be71b608cbdc6ffd69165ab8875767d4 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_mul_div_rem_sub_left_right_unsignedRight_and_or_xor_operators/the_xor_operator.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The '^' operator require its operands to be of type Any, the Number primitive type, or an enum type. + Operands of an enum type are treated as having the primitive type Number. + If one operand is the null or undefined value, it is treated as having the type of the other operand. + The result is always of the Number primitive type. + ---*/ + + +var a: any = '10' +var b: any = true +var c: number = 20 +var x = a ^ b +var y = a ^ c +Assert.isNumber(x) +Assert.isNumber(y) +enum e { + A, + B, + C +} +var d = e.A +var z = a ^ d +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_or_operator/the_or_operator_1.ts b/test_framework/test/spec/expressions/binary_operators/the_or_operator/the_or_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8bb3f89e6dcedb2c9d947fff0a4a9bc058d9dc57 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_or_operator/the_or_operator_1.ts @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The || operator permits the operands to be of any type. + The type of the result is the union type of the two operand types. + ---*/ + + +var a: any = 10 +var b: any = 's' +var c: boolean = false +var d: boolean = true +var e: number = 20 +var f: number = 15 +var g: string = 'a' +var h: string = 'b' +var i: undefined = undefined +var j: undefined = undefined + +var k = a || b +Assert.isNumber(k) +var l = c || d +Assert.isBoolean(l) +var m = e || f +Assert.isNumber(m) +var n = g || h +Assert.isString(n) +var o = i || j +Assert.isUndefined(o) +var p = a || c +Assert.isNumber(p) +var q = a || e +Assert.isNumber(q) +var r = a || g +Assert.isNumber(r) +var s = a || i +Assert.isNumber(s) +var t = c || e +Assert.isNumber(t) +var u = c || g +Assert.isString(u) +var v = c || i +Assert.isUndefined(v) +var w = e || g +Assert.isNumber(w) +var x = e || i +Assert.isNumber(x) +var y = g || i +Assert.isString(y) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/binary_operators/the_or_operator/the_or_operator_2.ts b/test_framework/test/spec/expressions/binary_operators/the_or_operator/the_or_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3a70b83692b9379c01978d01765ce61bbd900561 --- /dev/null +++ b/test_framework/test/spec/expressions/binary_operators/the_or_operator/the_or_operator_2.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the || expression is contextually typed, the operands are contextually typed by the same type. + Otherwise, the left operand is not contextually typed and the right operand is contextually typed by the type of the left operand. + ---*/ + + +var sum = function (x: number, y: number) { + return x + y +} +var average = function (a: number, b: number) { + return (a + b) / 2 +} +var rela = sum || average +Assert.equal(typeof rela, 'function') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/class_expressions/class_expressions_1.ts b/test_framework/test/spec/expressions/class_expressions/class_expressions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..bb57e776f9b4c8a084f1173b81c1a2482905ee01 --- /dev/null +++ b/test_framework/test/spec/expressions/class_expressions/class_expressions_1.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Just as with class declarations, class expressions would create a constructor function that can be used to construct instances. + Like class declarations, you may also declare property members and index members, as well as use the constructor parameters. + ---*/ + + +var Rect = class { + area: number; + + constructor(public length: number, public width: number) { + this.area = this.length * this.width; + } +}; +var rect = new Rect(5, 10); +Assert.equal(rect.area, 50); + +class Reflmpl { + public readonly _v = true; + constructor(private _rawValue: T, public _shaw = false) { + this._rawValue = _rawValue; + this._shaw = this._shaw; + } + get value() { + return this._rawValue; + } + set value(newVal) { + this._rawValue = newVal; + } + get shaw() { + return this._shaw; + } + set shaw(newShaw) { + this._shaw = newShaw; + } +} + +let ref = new Reflmpl(10, true); +Assert.equal(ref.value, 10); +Assert.equal(ref._shaw, true); diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_1.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0e165cfbebf85b03f328681e22a7fd55d82e9d33 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a variable, parameter, binding property, binding element, or member declaration, + an initializer expression is contextually typed by the type given in the declaration's type annotation. + ---*/ + + +// In a variable +let a: number +a = 10 +Assert.isNumber(a) + +// In a parameter +function fun(x: string) { + let y = x + 'ing' + return y +} +Assert.isString(fun('str')) + +// In a member declaration +interface Obj { + name: string + age: number +} +let obj: Obj = { + name: 'xiao', + age: 18 +} +Assert.isString(obj.name) +Assert.isNumber(obj.age) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_10.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..19eceda5662fcf8fdb0bbdf8a594d92f2366881e --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_10.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a contextually typed array literal expression containing no spread elements, an element expression at index N is contextually typed by + the type of the property with the numeric name N in the contextual type, if any, or otherwise + the numeric index type of the contextual type, if any. + ---*/ + + +let arr: { + [key: number]: string +} = { + 1: 'a', + 2: 'b', + 3: 'c', + 4: 'd' +} +Assert.isString(arr[1]) +Assert.isString(arr[2]) +Assert.isString(arr[3]) +Assert.isString(arr[4]) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_11.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..09e15e53d3db04a7cb8fa2a507bfeb0df453447a --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_11.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a contextually typed array literal expression containing one or more spread elements, + an element expression at index N is contextually typed by the numeric index type of the contextual type, if any. + ---*/ + + +let first = [1, 2] +let second = [3, 4] +let bothPlus = [0, ...first, ...second, 5] +Assert.isNumber(bothPlus[2]) +Assert.isNumber(bothPlus[4]) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_12.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..32958b5736fd4f41544bf860bc651e537f94a14d --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_12.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a contextually typed parenthesized expression, the contained expression is contextually typed by the same type. + ---*/ + + +var a: number = 10 +var b: number = 5 +var c = (a + b) * (a - b) +Assert.isNumber(c) + +interface Foo { + num: number + str: string +} +const x: Foo[] = [{ num: 18, str: 'hello' }, { num: 20, str: 'world' }] +Assert.isNumber(x[0].num) +Assert.isNumber(x[1].num) +Assert.isString(x[0].str) +Assert.isString(x[1].str) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_13.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f2485133a785357591f77781990da80dbb46711 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_13.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a type assertion, the expression is contextually typed by the indicated type. + ---*/ + + +interface Person { + name: string + age: number +} +let student = {} as Person +student.name = 'xiao' +student.age = 18 +Assert.isNumber(student.age) +Assert.isString(student.name) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_14.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_14.ts new file mode 100644 index 0000000000000000000000000000000000000000..f91fe49f1435b5482bd054a1d46427e14c99d23b --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_14.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a || operator expression, if the expression is contextually typed, the operands are contextually typed by the same type. + Otherwise, the right expression is contextually typed by the type of the left expression. + ---*/ + + +var sum = function (x: number, y: number) { + return x + y +} +var average1 = function (a: number, b: number) { + return (a + b) / 2 +} +var rela1 = sum || average1 +Assert.equal(typeof rela1, 'function') + +var average2: number = 10 +var rela2 = average2 || sum +Assert.equal(typeof rela2, 'number') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_15.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_15.ts new file mode 100644 index 0000000000000000000000000000000000000000..d990539ba386bd9a3e8d7752fd0ddddcd2389256 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_15.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a contextually typed conditional operator expression, the operands are contextually typed by the same type. + ---*/ + + +function processValue(value: number | (() => number)) { + var x = typeof value !== "number" ? value() : value + Assert.isNumber(x) +} +processValue(5) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_16.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_16.ts new file mode 100644 index 0000000000000000000000000000000000000000..2b93ff6ea9e513089f6ea88a24ff05cdd34ad3f9 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_16.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In an assignment expression, the right hand expression is contextually typed by the type of the left hand expression. + ---*/ + + +interface A { + (a: number): void +} +const fn: A = function (a) { + Assert.isNumber(a) +} +fn(5) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_2.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..51208c719f5931e03e7a9446197f377689adbd55 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_2.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a variable, parameter, binding property, binding element, or member declaration, + an initializer expression is contextually typed by for a parameter, the type provided by a contextual signature. + ---*/ + + +var f: (s: string) => string = function (x) { + Assert.isString(x) + return x.toLowerCase() +} +f('str') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_3.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..e6c925424b1b29cbcabaa68f97532c862d574cb4 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_3.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a variable, parameter, binding property, binding element, or member declaration, + an initializer expression is contextually typed by the type implied by the binding pattern in the declaration. + ---*/ + + +function f({ a = true, b = "hello", c = 1 }) { + Assert.isBoolean(a) + Assert.isString(b) + Assert.isNumber(c) +} +f({ a: false }) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_4.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..68aa11a0f69d1aef5668c9ad1bab23efbbf5414f --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_4.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of a function declaration, function expression, arrow function, method declaration, + or get accessor declaration that has a return type annotation, + return expressions are contextually typed by the type given in the return type annotation. + ---*/ + + +function add(x: number, y: number): number { + return x + y +} +let sum = add(10, 20) +Assert.isNumber(sum) + + +type Fun = (a: number, b: number) => number +function fun(fn: Fun, x: number, y: number) { + return fn(x, y) +} +function minus(a: number, b: number) { + return a - b +} +function mul(a: number, b: number) { + return a * b +} +let m = fun(minus, 15, 7) +let n = fun(mul, 3, 6) +Assert.isNumber(m) +Assert.isNumber(n) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_5.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..10753733bfcb11976a4792a643ae41f305b547a4 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_5.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of a function expression or arrow function that has no return type annotation, + if the function expression or arrow function is contextually typed by a function type with exactly one call signature, + and if that call signature is non-generic, return expressions are contextually typed by the return type of that call signature. + ---*/ + + +type Fun = { + description: string + (someThing: number): boolean +} +function fun1(fn: Fun) { + return fn.description + fn(100) +} +function fun2(num: number) { + Assert.equal(num, 100) + return false +} +fun2.description = 'hello' +Assert.equal(fun1(fun2), 'hellofalse') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_6.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..55c594a0b9c5750249583ebb01fcb9f7da79a96a --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_6.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of a constructor declaration, return expressions are contextually typed by the containing class type. + ---*/ + + +class Person { + name: string + age: number + constructor(name: string, age: number) { + this.name = name + this.age = age + } +} +const p = new Person("xiao", 18) +Assert.isString(p.name) +Assert.isNumber(p.age) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_7.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8eece8774f5305dfee63a1a0c2b2bd4d5e5a0a1 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_7.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the body of a get accessor with no return type annotation, + if a matching set accessor exists and that set accessor has a parameter type annotation, + return expressions are contextually typed by the type given in the set accessor's parameter type annotation. + ---*/ + + +class Developer { + private _language = '' + private _tasks: string[] = [] + get language() { + return this._language + } + set language(value: string) { + this._language = value + } + get tasks() { + return this._tasks + } + set tasks(value: string[]) { + this._tasks = value + } +} + +const dev = new Developer() +dev.language = 'TS' +Assert.isString(dev.language) +dev.tasks = ['develop', 'test'] +dev.tasks.push('ship') +Assert.equal(dev.tasks, 'develop,test,ship') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_8.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..a3fcc8487c49986cd74ca021f74ba91010cdeaa4 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_8.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a typed function call, argument expressions are contextually typed by their corresponding parameter types. + ---*/ + + +const state: Record = { + isPending: false, + results: ['a', 'b', 'c'] +} + +const useValue = (name: string): [T, Function] => { + const value: T = state[name] + const setValue: Function = (value: T): void => { + state[name] = value + } + return [value, setValue] +} + +const [isPending, setIsPending] = useValue('isPending') +const [results, setResults] = useValue('results') + +Assert.isBoolean(isPending) +Assert.equal(results, 'a,b,c') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_9.ts b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..1bb1ae92f3182252c51b244c746855edb127f690 --- /dev/null +++ b/test_framework/test/spec/expressions/contextually_typed_expressions/contextually_typed_expressions_9.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a contextually typed object literal, each property value expression is contextually typed by + the type of the property with a matching name in the contextual type, if any, or otherwise + for a numerically named property, the numeric index type of the contextual type, if any, or otherwise + the string index type of the contextual type, if any. + ---*/ + + +interface Person { + (a: number): void +} +interface B { + fn: Person +} +const obj: B = { + fn: function (a) { + Assert.isNumber(a) + } +} +obj.fn(10) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/grammar_ambiguities/grammar_ambiguities.ts b/test_framework/test/spec/expressions/function_calls/grammar_ambiguities/grammar_ambiguities.ts new file mode 100644 index 0000000000000000000000000000000000000000..66c9b12e3fb59abe25b7656de643fb0b58102e83 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/grammar_ambiguities/grammar_ambiguities.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The inclusion of type arguments in the Arguments production gives rise to certain ambiguities in the grammar for expressions. + ---*/ + + +// a call to 'f' with two arguments, 'g < A' and 'B > (7)' +var g: number = 5 +var A: number = 3 +var B: number = 6 +function f(a: any, b?: any) { + return a +} + +Assert.isFalse(f(g < A, B > 7)) +Assert.isFalse(f(g < A, B > +(7))) + +// a call to a generic function 'g' with two type arguments and one regular argument +type A1 = number +type B1 = number +function g1(a: T) { + return a; +} +function f1(a: any, b?: any) { + return a +} + +Assert.equal(f1(g1(7)), 7) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_1.ts b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6964d1c440e0d89e90ed06a667bc2a239cfb7d36 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The compile-time processing of a typed function call consists, + a list of candidate signatures is constructed from the call signatures in the function type in declaration order. + A generic signature is a candidate in a function call with type arguments when the signature has the same number of type parameters + as were supplied in the type argument list, the type arguments satisfy their constraints, + and once the type arguments are substituted for their associated type parameters, + the signature is applicable with respect to the argument list of the function call. + ---*/ + + +interface IFnCall { + (fn: () => T, age: number): string +} +interface IFnCall { + (func: () => T, str: string): string +} +const foo: IFnCall = function () { + return 'a' +} + +var f = foo(() => { + return 'f' +}, 10) + +Assert.equal(f, 'a') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_2.ts b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..df5ef4f816b00b863b0783f62fe43854fdf31e2d --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_2.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The compile-time processing of a typed function call consists, + a list of candidate signatures is constructed from the call signatures in the function type in declaration order. + A generic signature is a candidate in a function call without type arguments when type inference succeeds for each type parameter, + once the inferred type arguments are substituted for their associated type parameters, + the signature is applicable with respect to the argument list of the function call. + ---*/ + + +interface IFnCall { + (fn: (name: S) => T, age: number): T +} +interface IFnCall { + (func: (str: S) => T, name: string): T +} +const foo: IFnCall = function (fn) { + return fn('xiao') +} +const res = foo((name) => { + return name +}, 10) +Assert.isString(res); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_3.ts b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..036cee590b7ae9ffaac97729d0f6b6275cfb62d5 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_3.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + list of candidate signatures is constructed from the call signatures in the function type in declaration order. + A non-generic signature is a candidate when + the function call has no type arguments, and + the signature is applicable with respect to the argument list of the function call. + ---*/ + + +interface IfnCall { + (name: string, age: number): string +} +interface IfnCall { + (str: string, num: string): string +} +const foo: IfnCall = function (name, age) { + return name + ":" + age +} +var f = foo("xiao", 18) +Assert.isString(f); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_4.ts b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..6a153644fc80b749733e26f84d3da7bf61b50286 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/overload_resolution/overload_resolution_4.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A signature is said to be an applicable signature with respect to an argument list when + the number of arguments is not less than the number of required parameters, + the number of arguments is not greater than the number of parameters, and + for each argument expression e and its corresponding parameter P, when e is contextually typed by the type of P, + no errors ensue and the type of e is assignable to the type of P. + ---*/ + + +interface IfnCall { + (name: string, age?: number): string +} +const foo1: IfnCall = function (name, age) { + return name + ":" + age +} +const foo2: IfnCall = function (name) { + return name +} +var f1 = foo1("xiao", 18) +var f2 = foo2("xi") +f1 = f2 +Assert.equal(f1, f2) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_1.ts b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..daf201c8f39ec5e34feca9755a3ff9027db5c927 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Given a type parameter T and set of candidate types, the actual inferred type argument is determined, + if the set of candidate argument types is empty, the inferred type argument for T is T's constraint. + ---*/ + + +type t = number | string +function choose(x: T, y: T): T { + return Math.random() < 0.5 ? x : y; +} + +var x = choose(10, 20); +Assert.isNumber(x) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_2.ts b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..11483fa1f896e685e553ec9be2470615419b4f44 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_2.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Given a type parameter T and set of candidate types, the actual inferred type argument is determined, + if at least one of the candidate types is a supertype of all of the other candidate types, + let C denote the widened form of the first such candidate type. + If C satisfies T's constraint, the inferred type argument for T is C. + Otherwise, the inferred type argument for T is T's constraint. + ---*/ + + +type c = number | undefined +type t = number | null | undefined +function choose(x: T, y: T): T { + return Math.random() < 0.5 ? x : y; +} + +var y = choose(10, undefined) +if (typeof y === 'number') { + Assert.isNumber(y) +} +else { + Assert.isUndefined(y) +} \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_3.ts b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d427ea07b9016999163c5acba808b38b583a2d02 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a function expression is inferentially typed and a type assigned to a parameter in that expression references type parameters for which inferences are being made, + the corresponding inferred type arguments to become fixed and no further candidate inferences are made for them. + ---*/ + + +function map(a: T[], f: (x: T) => U): U[] { + var result: U[] = []; + for (var i = 0; i < a.length; i++) result.push(f(a[i])); + return result; +} + +var names = ["Peter", "Paul", "Mary"]; +var lengths = map(names, s => s.length); +Assert.equal(typeof lengths, 'object') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_4.ts b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2ace86f0941df4d4310ac54ad98425c7c621348 --- /dev/null +++ b/test_framework/test/spec/expressions/function_calls/type_argument_inference/type_argument_inference_4.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If e is an expression of a function type that contains exactly one generic call signature and no other members, + and T is a function type with exactly one non-generic call signature and no other members, + then any inferences made for type parameters referenced by the parameters of T's call signature are fixed, + and e's type is changed to a function type with e's call signature instantiated in the context of T's call signature. + ---*/ + + +function zip(x: S[], y: T[], combine: (x: S) => (y: T) => U): U[] { + var len = Math.max(x.length, y.length); + var result: U[] = []; + for (var i = 0; i < len; i++) result.push(combine(x[i])(y[i])); + return result; +} + +var names = ["Peter", "Paul", "Mary"]; +var ages = [7, 9, 12]; +var pairs = zip(names, ages, s => n => ({ name: s, age: n })); +Assert.equal(typeof pairs, 'object') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/function_expression/function_expressions_1.ts b/test_framework/test/spec/expressions/function_expression/function_expressions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5d4dfcffd726619c8bcf827ce5285c8a645e5973 --- /dev/null +++ b/test_framework/test/spec/expressions/function_expression/function_expressions_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If T is a union type, let U be the set of element types in T that have call signatures. + If each type in U has exactly one call signature and that call signature is non-generic, and if all of the signatures are identical ignoring return types, + then S is a signature with the same parameters and a union of the return types. + ---*/ + + +type MyUnionType = ((x: number) => string) | ((x: number) => number); +function myFunction(fn: MyUnionType, arg: number) { + return fn(arg); +} +const fn1 = (x: number): string => `Hello,${x}`; +const fn2 = (x: number): number => x * 2; +const result1 = myFunction(fn1, 123); +const result2 = myFunction(fn2, 456); +Assert.equal(result1, "Hello,123"); +Assert.equal(result2, 912); diff --git a/test_framework/test/spec/expressions/function_expression/function_expressions_2.ts b/test_framework/test/spec/expressions/function_expression/function_expressions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..e43fe63e864a06adaa421395941bc78c748487ee --- /dev/null +++ b/test_framework/test/spec/expressions/function_expression/function_expressions_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If T is a function type with exactly one call signature, + and if that call signature is non-generic, S is that signature. + ---*/ + + +type MyFunctionType = (x: number, y: number) => number; +function myFunction(fn: MyFunctionType) { + return fn(2, 3); +} +const add: MyFunctionType = function (x, y) { + return x + y; +}; +const result = myFunction(add); +Assert.equal(result, 5); diff --git a/test_framework/test/spec/expressions/function_expression/function_expressions_3.ts b/test_framework/test/spec/expressions/function_expression/function_expressions_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..9bfd37e931cd2e339040c4255db5c84daccf24e4 --- /dev/null +++ b/test_framework/test/spec/expressions/function_expression/function_expressions_3.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Otherwise, no contextual signature can be extracted from T. + ---*/ + + +type MyType = { + fn: (a: number, b: string) => boolean; +}; +function myFunction(obj: MyType) { + const { fn } = obj; + const result = fn(123, "hello"); +} diff --git a/test_framework/test/spec/expressions/identifiers/class.ts b/test_framework/test/spec/expressions/identifiers/class.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc754bf7698a39a2eb18cf32eed04b6f702ac2fa --- /dev/null +++ b/test_framework/test/spec/expressions/identifiers/class.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: For a class, the constructor type associated with the constructor function object. + ---*/ + + +class Car { + // field + engine: string; + agelimit: number; + // Constructor function + constructor(engine: string, agelimit: number) { + this.engine = engine; + this.agelimit = agelimit; + } +} +var car = new Car("Benci", 2); +Assert.equal(car.agelimit, 2); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/identifiers/enum.ts b/test_framework/test/spec/expressions/identifiers/enum.ts new file mode 100644 index 0000000000000000000000000000000000000000..9b41668fd1f42e9f4a12bf2b78224d1593c51dfc --- /dev/null +++ b/test_framework/test/spec/expressions/identifiers/enum.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: For an enum, the object type associated with the enum object. + ---*/ + + +enum Direction { + up = 1, + down, + Left, + right, +} +const direction: Direction = Direction.up; +Assert.isNumber(direction); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/identifiers/function.ts b/test_framework/test/spec/expressions/identifiers/function.ts new file mode 100644 index 0000000000000000000000000000000000000000..d90db09174177676a44bf75646cfb65b42080bf1 --- /dev/null +++ b/test_framework/test/spec/expressions/identifiers/function.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: For a function, the function type associated with the function object. + ---*/ + + +function myFunction(x: number): number { + return x * 2; +} +Assert.isFunction(myFunction); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/identifiers/name_space.ts b/test_framework/test/spec/expressions/identifiers/name_space.ts new file mode 100644 index 0000000000000000000000000000000000000000..54e12820af7c72e4bbaf45c88b51195ad9f4db72 --- /dev/null +++ b/test_framework/test/spec/expressions/identifiers/name_space.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When an expression is an IdentifierReference, the expression refers to the most nested namespace, class, enum, function, variable, + or parameter with that name whose scope (section 2.4) includes the location of the reference. + The type of such an expression is the type associated with the referenced entity: + For a namespace, the object type associated with the namespace instance + ---*/ + + +namespace MyNamespace { + export interface Person { + name: string; + age: number; + } + export function greet(person: Person) { + Assert.equal(person.name, "John"); + Assert.equal(person.age, 30); + } +} + +const person: MyNamespace.Person = { + name: "John", + age: 30, +}; +MyNamespace.greet(person); diff --git a/test_framework/test/spec/expressions/identifiers/parameter.ts b/test_framework/test/spec/expressions/identifiers/parameter.ts new file mode 100644 index 0000000000000000000000000000000000000000..ae7975b3870e8c3b8d06657135fc9fcd6e4e4174 --- /dev/null +++ b/test_framework/test/spec/expressions/identifiers/parameter.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + For a parameter, the type of the parameter. + ---*/ + + +class paramMethod { + person: string; + age: number; + constructor(person: string, age: number) { + this.person = person; + this.age = age; + Assert.isString(person); + Assert.isNumber(age); + } +} +var newparam = new paramMethod("xiaoli", 18); diff --git a/test_framework/test/spec/expressions/identifiers/variable.ts b/test_framework/test/spec/expressions/identifiers/variable.ts new file mode 100644 index 0000000000000000000000000000000000000000..55a1d9d4cb613b044703a661e218a9721efea32f --- /dev/null +++ b/test_framework/test/spec/expressions/identifiers/variable.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + For a variable, the type of the variable. + ---*/ + + +let uname: string = "Runoob"; +let score1: number = 50; + +Assert.isString(uname); +Assert.isNumber(score1); diff --git a/test_framework/test/spec/expressions/literals/boolean.ts b/test_framework/test/spec/expressions/literals/boolean.ts new file mode 100644 index 0000000000000000000000000000000000000000..fb3a6d452b3a023c5a4f448409945eb150a89d5f --- /dev/null +++ b/test_framework/test/spec/expressions/literals/boolean.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The type of the literals true and false is the Boolean primitive type. + ---*/ + + +Assert.equal(typeof false, "boolean"); +Assert.equal(typeof true, "boolean"); diff --git a/test_framework/test/spec/expressions/literals/null.ts b/test_framework/test/spec/expressions/literals/null.ts new file mode 100644 index 0000000000000000000000000000000000000000..f35f6e92d8fc13c4bfe25899ededed5dbcde2fd6 --- /dev/null +++ b/test_framework/test/spec/expressions/literals/null.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type of the null literal is the Null primitive type. + ---*/ + + +// TODO judge the null type +Assert.isTrue(typeof null === "object"); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/literals/number.ts b/test_framework/test/spec/expressions/literals/number.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d3d927fc57cc42ea274874dc8da404478e3a613 --- /dev/null +++ b/test_framework/test/spec/expressions/literals/number.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type of numeric literals is the Number primitive type. + ---*/ + + +Assert.equal(typeof 1, "number"); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/literals/regexp.ts b/test_framework/test/spec/expressions/literals/regexp.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f37573966dfb680a2c275554451149c5ab067cc --- /dev/null +++ b/test_framework/test/spec/expressions/literals/regexp.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type of regular expression literals is the global interface type 'RegExp'. + ---*/ + + +Assert.isTrue(/hello world/ instanceof RegExp); diff --git a/test_framework/test/spec/expressions/literals/string.ts b/test_framework/test/spec/expressions/literals/string.ts new file mode 100644 index 0000000000000000000000000000000000000000..c3964db3bd21947fe085fd3107e7850b09fad37b --- /dev/null +++ b/test_framework/test/spec/expressions/literals/string.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type of string literals is the String primitive type. + ---*/ + + +Assert.isString("Hello world"); +Assert.isString("kitty"); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/object_literal/object_literal_1.ts b/test_framework/test/spec/expressions/object_literal/object_literal_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8f14a95ef934fc012225a761c3d19f9910c5e39b --- /dev/null +++ b/test_framework/test/spec/expressions/object_literal/object_literal_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the object literal is contextually typed and the contextual type contains a property with a matching name, + the property assignment is contextually typed by the type of that property. + ---*/ + + +let cc: { + name: string; + age: number; + callMe(name: string): string; + get job(): string; + set job(jobName: string); +} = { + name: "fan", + age: 20, + callMe(name) { + return name; + }, + job: "student", +}; +Assert.equal(cc.job, "student"); +cc.job = "teacher"; +Assert.equal(cc.job, "teacher"); diff --git a/test_framework/test/spec/expressions/object_literal/object_literal_2.ts b/test_framework/test/spec/expressions/object_literal/object_literal_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..767ed0e4e725d72d36af4021610946dbe00a0b33 --- /dev/null +++ b/test_framework/test/spec/expressions/object_literal/object_literal_2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the object literal is contextually typed, if the contextual type contains a numeric index signature, + and if the property assignment specifies a numeric property name, the property assignment is contextually typed by the type of the numeric index signature. + ---*/ + + +let numeric_object: { + 1: string; + 2: number; + 3(name: string): string; + get job(): string; + set job(jobName: string); +} = { + 1: "mingshuo", + 2: 18, + 3(name) { + return name; + }, + job: "student", +}; +Assert.equal(numeric_object.job, "student"); +numeric_object.job = "teacher"; +Assert.equal(numeric_object.job, "teacher"); diff --git a/test_framework/test/spec/expressions/object_literal/object_literal_3.ts b/test_framework/test/spec/expressions/object_literal/object_literal_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..5a6ef88e0825d94d6859917555925c0cdb87ce5a --- /dev/null +++ b/test_framework/test/spec/expressions/object_literal/object_literal_3.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the object literal is contextually typed and the contextual type contains a string index signature, + the property assignment is contextually typed by the type of the string index signature. + ---*/ + + +let animal: { + name: String; + age: Number; + run: Number; + bark(run: Number): Number; +} = { + name: "Tom", + age: 12, + run: 23, + bark(run) { + return run; + }, +}; +Assert.equal(animal.run, 23); +animal.run = 66; +Assert.equal(animal.run, 66); diff --git a/test_framework/test/spec/expressions/object_literal/object_literal_4.ts b/test_framework/test/spec/expressions/object_literal/object_literal_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..a9764a7b7c82f0f9c385c7c79bf74dba0c249921 --- /dev/null +++ b/test_framework/test/spec/expressions/object_literal/object_literal_4.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The property assignment is processed without a contextual type + ---*/ + + +interface Iperson { + name: String; + age: Number; + gender?: string; + sayName(): void; +} +const P: Iperson = { + name: "Tom", + age: 21, + gender: "male", + sayName() { + return this.name; + }, +}; +Assert.equal(P.gender, "male"); +P.gender = "female"; +Assert.equal(P.gender, "female"); diff --git a/test_framework/test/spec/expressions/object_literal/object_literal_5.ts b/test_framework/test/spec/expressions/object_literal/object_literal_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8e11b5b9d6747e0df23029a6adaf91fde9b53bb --- /dev/null +++ b/test_framework/test/spec/expressions/object_literal/object_literal_5.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A get accessor declaration is processed in the same manner as an ordinary function declaration (section 6.1) with no parameters. + A set accessor declaration is processed in the same manner as an ordinary function declaration with a single parameter and a Void return type. + When both a get and set accessor is declared for a property: + If both accessors include type annotations, the specified types must be identical. + ---*/ + + +class Example { + private _count: number = 0; + get count(): number { + return this._count; + } + set count(value: number) { + this._count = value; + } +} +const example = new Example(); +example.count = 1; +Assert.isNumber(example.count); diff --git a/test_framework/test/spec/expressions/object_literal/object_literal_6.ts b/test_framework/test/spec/expressions/object_literal/object_literal_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..b61b81bba3c5b9a8d40033687bc37654372d4a4c --- /dev/null +++ b/test_framework/test/spec/expressions/object_literal/object_literal_6.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If only one accessor includes a type annotation, the other behaves as if it had the same type annotation + ---*/ + + +class Person { + private _name: string = ""; + get name(): string { + return this._name; + } + set name(value: string) { + this._name = value; + } +} +const person = new Person(); +person.name = "john"; +Assert.isString(person.name); diff --git a/test_framework/test/spec/expressions/parentheses/parentheses.ts b/test_framework/test/spec/expressions/parentheses/parentheses.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc2979e116b72ae9c00f40fde07fb62cd8383732 --- /dev/null +++ b/test_framework/test/spec/expressions/parentheses/parentheses.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parenthesized expression has the same type and classification as the contained expression itself. + Specifically, if the contained expression is classified as a reference, so is the parenthesized expression. + ---*/ + + +var addparent = function (n1: number, n2: number): number { + return n1 + n2; +}; +// is equivalent to +var addnoparent = (n1: number, n2: number): number => n1 + n2; +let isCorrect = addparent(11, 22) === addnoparent(11, 22); +Assert.isTrue(isCorrect); + +let foo: string = "hello"; +let bar: string = foo; +Assert.isString(foo); +Assert.isString(bar); + +const obj = { value: "world" }; +function printValue(value: string) { + Assert.equal(value, "world"); + Assert.isString(value); +} +printValue(obj.value); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/property_access/object_access_2.ts b/test_framework/test/spec/expressions/property_access/object_access_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..ee2a60dab110efb95fdf775143d02c777322cab2 --- /dev/null +++ b/test_framework/test/spec/expressions/property_access/object_access_2.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If name denotes an accessible apparent property (section 3.11.1) in the widened type (section 3.12) of object, + the property access is of the type of that property. Public members are always accessible, + but private and protected members of a class have restricted accessibility, as described in 8.2.2. + ---*/ + + +var sites = { + site1: "Runoob", + site2: "Google", + sayHello: function () { }, +}; +sites.sayHello = function () { + return "hello " + sites.site1; +}; +Assert.isString(sites.sayHello()); + +class Person { + name: string; + age: string; + constructor(name: any, age: any) { + this.name = name; + this.age = age; + } + greet() { } +} +const person1 = new Person("Alice", 30); +const person2 = { name: "Bob", age: 35 }; +person1["greet"](); +Assert.equal(person1.name, "Alice"); +Assert.equal(person2.name, "Bob"); +Assert.equal(person1.age, 30); diff --git a/test_framework/test/spec/expressions/property_access/object_any_1.ts b/test_framework/test/spec/expressions/property_access/object_any_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1c534b1eacbe22da6b94539cebae9a21395357ea --- /dev/null +++ b/test_framework/test/spec/expressions/property_access/object_any_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If object is of type Any, any name is permitted and the property access is of type Any. + ---*/ + + +let myVariable: any = { name: "John", age: 30 }; + +Assert.isUndefined(myVariable.firstName); + +myVariable = 42; + +Assert.isUndefined(myVariable.length); + +myVariable = "Hello World"; + +Assert.isUndefined(myVariable.foo); diff --git a/test_framework/test/spec/expressions/property_access/object_any_type_6.ts b/test_framework/test/spec/expressions/property_access/object_any_type_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..28ed1a8e409266f48146ddbf0ebcc4d8b2e97d42 --- /dev/null +++ b/test_framework/test/spec/expressions/property_access/object_any_type_6.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If index is of type Any, the String or Number primitive type, + or an enum type, the property access is of type Any. + ---*/ + + +interface MyObject { + [key: string]: number; +} + +const obj: MyObject = { + prop1: 1, + prop2: 2, + prop3: 3, +}; + +const prop = "prop2"; +const value1 = obj[prop]; +const value2 = obj[0]; +enum MyEnum { + Prop1 = "prop1", + Prop2 = "prop2", + Prop3 = "prop3", +} + +const value3 = obj[MyEnum.Prop3]; +Assert.equal(value3, 3); diff --git a/test_framework/test/spec/expressions/property_access/object_apprent_5.ts b/test_framework/test/spec/expressions/property_access/object_apprent_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..c10af65f45526778d6bf26fbfe11c8857f4f7969 --- /dev/null +++ b/test_framework/test/spec/expressions/property_access/object_apprent_5.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If object has an apparent string index signature and index is of type Any, + the String or Number primitive type, or an enum type, the property access is of the type of that index signature. + ---*/ + + +interface ExampleObject { + [key: string]: string; +} + +const myObject: ExampleObject = { + foo: "bar", + baz: "qux", +}; + +const myIndex: any = "foo"; +const myValue = myObject[myIndex]; +Assert.isString(myValue); diff --git a/test_framework/test/spec/expressions/property_access/object_index_4.ts b/test_framework/test/spec/expressions/property_access/object_index_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..07df6393478d02524d033acc1faef2fa37b842bf --- /dev/null +++ b/test_framework/test/spec/expressions/property_access/object_index_4.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If object has an apparent numeric index signature and index is of type Any, + the Number primitive type, or an enum type, the property access is of the type of that index signature + ---*/ + + +interface MyObj { + [index: number]: string; +} + +const obj: MyObj = { + 0: "foo", + 1: "bar", + 2: "baz", +}; + +Assert.equal(obj[0], "foo"); +Assert.equal(obj[1], "bar"); +Assert.equal(obj[2], "baz"); +Assert.equal(obj["0"], "foo"); +Assert.equal(obj["1"], "bar"); +Assert.equal(obj["2"], "baz"); diff --git a/test_framework/test/spec/expressions/property_access/object_string_number_3.ts b/test_framework/test/spec/expressions/property_access/object_string_number_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d74ac782b37944b546e85d51c95397d43c55f23e --- /dev/null +++ b/test_framework/test/spec/expressions/property_access/object_string_number_3.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If index is a string literal or a numeric literal and object has an apparent property (section 3.11.1) with the name given by that literal + (converted to its string representation in the case of a numeric literal), the property access is of the type of that property. + ---*/ + + +const sites = { + siteName: "Runoob", + site2: 999, + sayHello: function () {}, +}; +Assert.isString(sites["siteName"]); +Assert.isNumber(sites["site2"]); +Assert.isFunction(sites["sayHello"]); + +interface Person { + name: string; + age: number; + city: string; +} + +const person: Person = { + name: "Alice", + age: 30, + city: "New York", +}; + +Assert.equal(person["name"], "Alice"); + +Assert.equal(person["city"], "New York"); diff --git a/test_framework/test/spec/expressions/the_comma_operator/the_comma_operator.ts b/test_framework/test/spec/expressions/the_comma_operator/the_comma_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a660e0cc273df170637ca1983c7d638793dcd3c --- /dev/null +++ b/test_framework/test/spec/expressions/the_comma_operator/the_comma_operator.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The comma operator permits the operands to be of any type and produces a result that is of the same type as the second operand. + ---*/ + + +let x: number = 20 +let y: boolean = true +let z: string = 'a' +let com1 = (x++, y) +let com2 = (x++, z = z + 'b') +Assert.isBoolean(com1) +Assert.isString(com2) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/the_conditional_operator/the_conditional_operator_1.ts b/test_framework/test/spec/expressions/the_conditional_operator/the_conditional_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..422ae89e0ea3ce3d0cd2ec09936045a6d7f96377 --- /dev/null +++ b/test_framework/test/spec/expressions/the_conditional_operator/the_conditional_operator_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a conditional expression of the form 'test ? expr1 : expr2', the test expression may be of any type. + The type of the result is the union type of the types of expr1 and expr2. + ---*/ + + +var x: number = 10 +var y: string = '5' +// the typeof z is string | number +var z = Math.random() < 0.5 ? x + y : y && x +if (typeof z === 'number') { + z++ + Assert.equal(z, 11) +} +else { + Assert.equal(z, '105') +} \ No newline at end of file diff --git a/test_framework/test/spec/expressions/the_conditional_operator/the_conditional_operator_2.ts b/test_framework/test/spec/expressions/the_conditional_operator/the_conditional_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f137e46d0a6e9d676dafc99930f33f0edf62550 --- /dev/null +++ b/test_framework/test/spec/expressions/the_conditional_operator/the_conditional_operator_2.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the conditional expression is contextually typed, expr1 and expr2 are contextually typed by the same type. + Otherwise, expr1 and expr2 are not contextually typed. + ---*/ + + +var sum = function (x: number, y: number) { + return x + y +} +var average = function (a: number, b: number) { + return (a + b) / 2 +} +var rela: number = Math.random() < 0.5 ? sum(5, 7) : average(6, 9) +Assert.isNumber(rela) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/the_new_operator/new_operator_1.ts b/test_framework/test/spec/expressions/the_new_operator/new_operator_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5154a37ee521e7cd4fd1e4ff302145efcbf3e294 --- /dev/null +++ b/test_framework/test/spec/expressions/the_new_operator/new_operator_1.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If C is of type Any, any argument list is permitted and the result of the operation is of type Any. + ---*/ + + +let C: any; +C = function (this: any, x: number, y: number) { + this.sum = x + y; +}; +const myObject = new C(2, 3); +Assert.equal(myObject.sum, 5); diff --git a/test_framework/test/spec/expressions/the_new_operator/new_operator_2.ts b/test_framework/test/spec/expressions/the_new_operator/new_operator_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..bdb103b17b13d5c266629c2582c7d20de25ed5aa --- /dev/null +++ b/test_framework/test/spec/expressions/the_new_operator/new_operator_2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If C has one or more apparent construct signatures (section 3.11.1), the expression is processed in the same manner as a function call, + but using the construct signatures as the initial set of candidate signatures for overload resolution. + The result type of the function call becomes the result type of the operation. + ---*/ + + +class Person { + constructor(public name: string, public age: number) {} +} +interface Animal { + speak(): void; +} + +class Dog implements Animal { + speak() { + return "Woof!"; + } +} +const p = new Person("Alice", 30); +const d = new Dog(); +Assert.isString(p.name); +Assert.equal(d.speak(), "Woof!"); diff --git a/test_framework/test/spec/expressions/the_new_operator/new_operator_3.ts b/test_framework/test/spec/expressions/the_new_operator/new_operator_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..86e54383878c9a044ac70ddac64aa8037e1141ae --- /dev/null +++ b/test_framework/test/spec/expressions/the_new_operator/new_operator_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If C has no apparent construct signatures but one or more apparent call signatures, the expression is processed as a function call. + A compile-time error occurs if the result of the function call is not Void. The type of the result of the operation is Any. + ---*/ + + +class Person { + sayHello() { + return "Hello,world"; + } +} + +let person: Person = new Person(); +Assert.equal(person.sayHello(), "Hello,world"); + +let person2: any = new Person(); +person2.sayHello(); +Assert.equal(person2.sayHello(), "Hello,world"); diff --git a/test_framework/test/spec/expressions/the_new_operator/new_operator_4.ts b/test_framework/test/spec/expressions/the_new_operator/new_operator_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..aacf507fbd308790b1b22d23c017d9a19fa28e05 --- /dev/null +++ b/test_framework/test/spec/expressions/the_new_operator/new_operator_4.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + new C + new C ( ... ) + new C < ... > ( ... ) + ---*/ + + +class MyClass { + constructor() { + return "MyClass"; + } +} + +const myInstance = new MyClass(); + + +class Person { + name: string; + age: number; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + } +} +const john = new Person("john", 30); +Assert.isString(john.name, "john"); + +class Box { + contents: T; + constructor(value: T) { + this.contents = value; + } +} +const myBox = new Box("hello"); +Assert.equal(myBox.contents, "hello"); diff --git a/test_framework/test/spec/expressions/the_super_keyword/super_calls/arguments.ts b/test_framework/test/spec/expressions/the_super_keyword/super_calls/arguments.ts new file mode 100644 index 0000000000000000000000000000000000000000..a0b544d637b3c1a56282234e9e18beed4ce181c0 --- /dev/null +++ b/test_framework/test/spec/expressions/the_super_keyword/super_calls/arguments.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type arguments cannot be explicitly specified in a super call. + ---*/ + + +class SuperClass { + value: T; + constructor(value: T) { + this.value = value; + } +} +class SubClass extends SuperClass { + constructor(value: T) { + super(value); + } +} +const sub = new SubClass("hello"); +Assert.equal(sub.value, "hello"); diff --git a/test_framework/test/spec/expressions/the_super_keyword/super_calls/gengric_extends.ts b/test_framework/test/spec/expressions/the_super_keyword/super_calls/gengric_extends.ts new file mode 100644 index 0000000000000000000000000000000000000000..3e532e1dffeccdcd24d50e853de81df40d900208 --- /dev/null +++ b/test_framework/test/spec/expressions/the_super_keyword/super_calls/gengric_extends.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the base class is a generic class, + the type arguments used to process a super call are always those specified in the extends clause that references the base class. + ---*/ + + +class BaseClass { + constructor(public value: T) {} +} +class SubClass extends BaseClass { + constructor(value: T) { + super(value); + } +} +class StringSubClass extends SubClass { + constructor() { + super("hello"); + } +} +const sub = new StringSubClass(); +Assert.equal(sub.value, "hello"); diff --git a/test_framework/test/spec/expressions/the_super_keyword/super_calls/super_calls.ts b/test_framework/test/spec/expressions/the_super_keyword/super_calls/super_calls.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5e2e2528eacb7419281ecf27944cce6e4f35489 --- /dev/null +++ b/test_framework/test/spec/expressions/the_super_keyword/super_calls/super_calls.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The super keyword can be used in expressions to reference base class properties and the base class constructor. + Super calls consist of the keyword super followed by an argument list enclosed in parentheses. + Super calls are only permitted in constructors of derived classes, as described in section 8.3.2. + A super call invokes the constructor of the base class on the instance referenced by this. + A super call is processed as a function call (section 4.15) using the construct signatures of the base class constructor function type + as the initial set of candidate signatures for overload resolution. + The type of a super call expression is Void. + The JavaScript code generated for a super call is specified in section 8.7.2. + ---*/ + + +class Animal { + private name: string; + constructor(name: any) { + this.name = name; + } + speak() { + return `${this.name} makes a noise`; + } +} + +class Dog extends Animal { + private breed: string; + constructor(name: any, breed: any) { + super(name); + this.breed = breed; + } + extendsSpeak() { + super.speak(); + Assert.isString(super.speak(), "Fodo makes a noise"); + return `${this.breed}`; + } +} +const d = new Dog("Fido", "Golden Retriever"); +d.extendsSpeak(); +Assert.isString(d.extendsSpeak(), "Golden Retriever"); diff --git a/test_framework/test/spec/expressions/the_super_keyword/super_property_access/derived_class_access.ts b/test_framework/test/spec/expressions/the_super_keyword/super_property_access/derived_class_access.ts new file mode 100644 index 0000000000000000000000000000000000000000..c19acd574c8bbf39445bd56666b11db3a255152e --- /dev/null +++ b/test_framework/test/spec/expressions/the_super_keyword/super_property_access/derived_class_access.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a constructor, instance member function, instance member accessor, or instance member variable initializer where this references a derived class instance, + a super property access is permitted and must specify a public instance member function of the base class. + ---*/ + + +class AnimalA { + public makeSound(): string { + return "the animal makes"; + } +} + +class DogA extends AnimalA { + public makeSound(): string { + super.makeSound(); + Assert.equal(super.makeSound(), "the animal makes"); + return "the dog barks"; + } +} +const myDog = new DogA(); +myDog.makeSound(); +Assert.equal(myDog.makeSound(), "the dog barks"); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/the_super_keyword/super_property_access/static_member_access.ts b/test_framework/test/spec/expressions/the_super_keyword/super_property_access/static_member_access.ts new file mode 100644 index 0000000000000000000000000000000000000000..2d2bbbd25198f54c54c5491efd7889b00f812de1 --- /dev/null +++ b/test_framework/test/spec/expressions/the_super_keyword/super_property_access/static_member_access.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a static member function or static member accessor where this references the constructor function object of a derived class, + a super property access is permitted and must specify a public static member function of the base class. + ---*/ + + +class Base { + static greet() { + return "hello world"; + } +} +class Derived extends Base { + static greet() { + super.greet(); + Assert.equal(super.greet(), "hello world"); + return "hola,mundo"; + } +} +Derived.greet(); +Assert.equal(Derived.greet(), "hola,mundo"); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/this_key_word/this_key_word_1.ts b/test_framework/test/spec/expressions/this_key_word/this_key_word_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9957af4a38394e71c230618b0c7909a3516517c4 --- /dev/null +++ b/test_framework/test/spec/expressions/this_key_word/this_key_word_1.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a constructor, instance member function, instance member accessor, + or instance member variable initializer, this is of the this-type (section 3.6.3) of the containing class. + ---*/ + + +// CHECK this in constructor +class Dog { + name: string; + age: number; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + } + + getName(): string { + return this.name; + } + + bark(): string { + return this.getName() + " is bark"; + } +} + +var dog = new Dog("doggy", 7); +Assert.equal(dog.age, 7); + +// CHECK this in instance member function and instance member accessor +Assert.equal("doggy is bark", dog.bark()) + +// CHECK this in instance member variable initializer +class Duck { + age: number = this.b; + constructor(private readonly b: number) { + } +} +var duck = new Duck(6); +Assert.equal(6, duck.age) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/this_key_word/this_key_word_2.ts b/test_framework/test/spec/expressions/this_key_word/this_key_word_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e371ca4c5fcdd22f5cbb13dd843ed9dd30c0e24 --- /dev/null +++ b/test_framework/test/spec/expressions/this_key_word/this_key_word_2.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a static member function or static member accessor, + the type of this is the constructor function type of the containing class. + ---*/ + + +class Animal { + age: number; + static size = this.length; + static test() { + Assert.equal(typeof this, typeof Animal) + Assert.equal(Animal.length, this.length); + } + + constructor(age: number) { + this.age = age; + } +} + +Animal.test(); +Assert.equal(Animal.size, Animal.length); \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_assertion/type_assertion_1.ts b/test_framework/test/spec/expressions/type_assertion/type_assertion_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..31776be732a783fc1be39e002425cbaf19842c48 --- /dev/null +++ b/test_framework/test/spec/expressions/type_assertion/type_assertion_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type assertions check for assignment compatibility in both directions. + Thus, type assertions allow type conversions that might be correct, but aren't known to be correct. + ---*/ + + +class Shape { } + +class Circle extends Shape { } + +function createShape(kind: string): Shape { + if (kind === "circle") { + return new Circle() + } + return Circle +} + +var circle = createShape("circle") +Assert.equal(JSON.stringify(circle), '{}') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_assertion/type_assertion_2.ts b/test_framework/test/spec/expressions/type_assertion/type_assertion_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d97d2ea0ad2b1b6a8b08f2f3e16cabc0783d854e --- /dev/null +++ b/test_framework/test/spec/expressions/type_assertion/type_assertion_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + type assertions are not checked at run-time and it is up to the programmer to guard against errors, + for example using the instanceof operator + ---*/ + + +class Foo { + foo = 123; + common = '123' +} +class Bar { + bar = 123 + common = '123' +} +function doStuff(arg: Foo | Bar) { + if (arg instanceof Foo) { + Assert.equal(arg.foo, 123) + } else { + Assert.equal(arg.bar, 123) + } + +} +doStuff(new Foo()) +doStuff(new Bar()) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_assertion/type_assertion_3.ts b/test_framework/test/spec/expressions/type_assertion/type_assertion_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..398a7d555dae71824306d88fa8794d8aa36d352a --- /dev/null +++ b/test_framework/test/spec/expressions/type_assertion/type_assertion_3.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In a type assertion expression of the form < T > e, e is contextually typed by T and the resulting type of* e* is required to be assignable to T, + or T is required to be assignable to the widened form of the resulting type of e, or otherwise a compile-time error occurs. The type of the result is T. + ---*/ + + +function getLength(something: string | number): number { + if ((something).length) { + return (something).length + } else { + return something.toString().length + } +} +Assert.equal(getLength('length'), 6) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_1.ts b/test_framework/test/spec/expressions/type_guards/type_guards_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0c57a6836a1ba77203cfe71bf18d3156f74e107c --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type guards are particular expression patterns involving the 'typeof' and 'instanceof' operators + that cause the types of variables or parameters to be narrowed to more specific types. + ---*/ + + +function foo(x: number | string) { + if (typeof x === 'string') { + Assert.isString(x) + } + else { + Assert.isNumber(x) + } +} +foo('string') +foo(10) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_10.ts b/test_framework/test/spec/expressions/type_guards/type_guards_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..67c1448b3626e12f3edc3a7ce63734c221930925 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_10.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form typeof x === s, where s is a string literal with any value but 'string', 'number', or 'boolean', + when true, if x is a union type, removes from the type of x all constituent types that are subtypes of the string, number, or boolean primitive type, + or when false, has no effect on the type of x. + ---*/ + + +function f1(x: string | number | undefined) { + if (typeof x === "undefined") { + return undefined + } + else { + return x + } +} +var a = f1(10) +Assert.isNumber(a) +var b = f1('s') +Assert.isString(b) +var c = f1(undefined) +Assert.isUndefined(c) + +function f2(x: string | number | boolean) { + if (typeof x === "undefined") { + return undefined + } + else { + return x + } +} +var a1 = f2(10) +Assert.isNumber(a1) +var b1 = f2('s') +Assert.isString(b1) +var c1 = f2(true) +Assert.isBoolean(c1) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_11.ts b/test_framework/test/spec/expressions/type_guards/type_guards_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..ee26c02ab800ab23d02093953e6826ffb51261e5 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_11.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form typeof x !== s, where s is a string literal, + when true, narrows the type of x by typeof x === s when false, + or when false, narrows the type of x by typeof x === s when true. + ---*/ + + +function f(x: string | number) { + if (typeof x !== "string") { + Assert.isNumber(x) + } + else { + Assert.isString(x) + } +} +f(10) +f('s') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_12.ts b/test_framework/test/spec/expressions/type_guards/type_guards_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..40aef9c5d12a978dba2c463f4ee1cb3bad49568a --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_12.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form !expr, + when true, narrows the type of x by expr when false, + or when false, narrows the type of x by expr when true. + ---*/ + + +function f(x: string | number) { + if (!(typeof x === "string")) { + Assert.isNumber(x) + } + else { + Assert.isString(x) + } +} +f(10) +f('s') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_13.ts b/test_framework/test/spec/expressions/type_guards/type_guards_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..4c83746abbaed196d645ceab27b8a096e1a4a35a --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_13.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form expr1 && expr2, + when true, narrows the type of x by expr1 when true and then by expr2 when true, or + when false, narrows the type of x to T1 | T2, where T1 is the type of x narrowed by expr1 when false, + and T2 is the type of x narrowed by expr1 when true and then by expr2 when false. + ---*/ + + +function f(x: string | number | undefined) { + if (typeof x === "string" && typeof x === "number") { + return undefined + } + else { + return x + } +} +var a = f(10) +Assert.isNumber(a) +var b = f('s') +Assert.isString(b) +var c = f(undefined) +Assert.isUndefined(c) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_14.ts b/test_framework/test/spec/expressions/type_guards/type_guards_14.ts new file mode 100644 index 0000000000000000000000000000000000000000..7bae557742359ba28f8d540359d17472f236f522 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_14.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form expr1 || expr2, + when true, narrows the type of x to T1 | T2, where T1 is the type of x narrowed by expr1 when true, + and T2 is the type of x narrowed by expr1 when false and then by expr2 when true, or + when false, narrows the type of x by expr1 when false and then by expr2 when false. + ---*/ + + +function f(x: string | number | undefined) { + if (typeof x === "string" || typeof x === "number") { + return x + } + else { + return undefined + } +} +var a = f(10) +Assert.isNumber(a) +var b = f('s') +Assert.isString(b) +var c = f(undefined) +Assert.isUndefined(c) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_15.ts b/test_framework/test/spec/expressions/type_guards/type_guards_15.ts new file mode 100644 index 0000000000000000000000000000000000000000..f984f6a438233387e34314e9ab2b2acce65ec0a9 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_15.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of any other form has no effect on the type of x. + ---*/ + + +class Person { + name: string + age: number + public constructor(name: string, age: number) { + this.name = name + this.age = age + } +} +class Animal { + height: number + weight: number + public constructor(height: number, weight: number) { + this.height = height + this.weight = weight + } +} +function func(arg: Person | Animal) { + if ('age' in arg) { + Assert.isString(arg.name) + } + if ('height' in arg) { + Assert.isNumber(arg.height) + } +} + +var p = new Person('x', 18) +func(p) +var a = new Animal(200, 180) +func(a) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_2.ts b/test_framework/test/spec/expressions/type_guards/type_guards_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c17b6db188fa50f394842419ed7de6bf198ff327 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the true branch statement of an 'if' statement, + the type of a variable or parameter is narrowed by a type guard in the 'if' condition when true, + provided no part of the 'if' statement contains assignments to the variable or parameter. + ---*/ + + +function foo(x: number | string) { + if (typeof x === 'string') { + Assert.isString(x) + } + else { + return x + 1 + } +} +foo('string') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_3.ts b/test_framework/test/spec/expressions/type_guards/type_guards_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..77a4a8dceaaac616eaede8842492076ea0ed05fb --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the false branch statement of an 'if' statement, + the type of a variable or parameter is narrowed by a type guard in the 'if' condition when false, + provided no part of the 'if' statement contains assignments to the variable or parameter. + ---*/ + + +function foo(x: number | string) { + if (!(typeof x === 'string')) { + Assert.isNumber(x) + } + else { + return x.length + } +} +foo(10) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_4.ts b/test_framework/test/spec/expressions/type_guards/type_guards_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..7588b5db81be3bd9a53e0a6f04b4858dbe7bf233 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_4.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the true expression of a conditional expression, + the type of a variable or parameter is narrowed by a type guard in the condition when true, + provided no part of the conditional expression contains assignments to the variable or parameter. + ---*/ + + +function processValue(value: number | (() => number)) { + var x = typeof value == "number" ? value : value() + Assert.isNumber(x) +} +processValue(5) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_5.ts b/test_framework/test/spec/expressions/type_guards/type_guards_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..be1794f1998766aca7aadc8f01457cb1751a2659 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_5.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the false expression of a conditional expression, + the type of a variable or parameter is narrowed by a type guard in the condition when false, + provided no part of the conditional expression contains assignments to the variable or parameter. + ---*/ + + +function processValue(value: number | (() => number)) { + var x = typeof value !== "number" ? value() : value + Assert.isNumber(x) +} +processValue(5) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_6.ts b/test_framework/test/spec/expressions/type_guards/type_guards_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..f1a52f6209f5d55ed03bc98b9983c98e8693ec33 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_6.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the right operand of a && operation, + the type of a variable or parameter is narrowed by a type guard in the left operand when true, + provided neither operand contains assignments to the variable or parameter. + ---*/ + + +function isString(obj: any) { + typeof obj === "string" && obj.length < 10 + Assert.isString(obj) +} +isString('s') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_7.ts b/test_framework/test/spec/expressions/type_guards/type_guards_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..76dae2d59979e78efd0676b42f534daa2708d2f1 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_7.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In the right operand of a || operation, + the type of a variable or parameter is narrowed by a type guard in the left operand when false, + provided neither operand contains assignments to the variable or parameter. + ---*/ + + +function isString(obj: any) { + typeof obj === "string" || obj.length > 10 + Assert.isString(obj) +} +isString('s') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_8.ts b/test_framework/test/spec/expressions/type_guards/type_guards_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..f3fd14784d50da7daa6b4160883d4d043df449c7 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_8.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form 'x instanceof C', where x is not of type Any, + C is of a subtype of the global type 'Function', and C has a property named 'prototype', + when true, narrows the type of x to the type of the 'prototype' property in C provided it is a subtype of the type of x, + or, if the type of x is a union type, removes from the type of x all constituent types that aren't subtypes of the type of the 'prototype' property in C, + or when false, has no effect on the type of x. + ---*/ + + +class Person { + height: number + age: number + public constructor(height: number, age: number) { + this.height = height + this.age = age + } +} +class Animal { + height: number + weight: number + public constructor(height: number, weight: number) { + this.height = height + this.weight = weight + } +} +function func(arg: Person | Animal) { + if (arg instanceof Person) { + return arg.age = 18 + } + if (arg instanceof Animal) { + return arg.weight = 300 + } +} + +var p = new Person(150, 18) +Assert.equal(func(p), 18) +var a = new Animal(200, 180) +Assert.equal(func(a), 300) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/type_guards/type_guards_9.ts b/test_framework/test/spec/expressions/type_guards/type_guards_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f1988cf123341209749009d9fd530c58897ab72 --- /dev/null +++ b/test_framework/test/spec/expressions/type_guards/type_guards_9.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type guard of the form typeof x === s, where s is a string literal with the value 'string', 'number', or 'boolean', + when true, narrows the type of x to the given primitive type provided it is a subtype of the type of x, + or, if the type of x is a union type, removes from the type of x all constituent types that aren't subtypes of the given primitive type, + or when false, removes the primitive type from the type of x. + ---*/ + + +function f(x: string | number) { + if (typeof x === "string") { + Assert.isString(x) + } + else { + Assert.isNumber(x) + } +} +f(10) +f('s') \ No newline at end of file diff --git a/test_framework/test/spec/expressions/unary_operators/the_!_operator.ts b/test_framework/test/spec/expressions/unary_operators/the_!_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..63523a95da9add0947a07ddf5c321e7c247c2964 --- /dev/null +++ b/test_framework/test/spec/expressions/unary_operators/the_!_operator.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The ! operator permits its operand to be of any type and produces a result of the Boolean primitive type. + Two unary ! operators in sequence can conveniently be used to convert a value of any type to the Boolean primitive type. + ---*/ + + +var num1: number = 20 +var num2: number = 90 +var res = !((num1 > 50) && (num2 > 80)) +Assert.isBoolean(res) + +function getValue(): any { +} +var b = !!getValue() +Assert.isBoolean(b) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/unary_operators/the_++_and_--_operators.ts b/test_framework/test/spec/expressions/unary_operators/the_++_and_--_operators.ts new file mode 100644 index 0000000000000000000000000000000000000000..bc041b1c27bbf76908244da02b2d809e036b4350 --- /dev/null +++ b/test_framework/test/spec/expressions/unary_operators/the_++_and_--_operators.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The ++ and -- operators in prefix or postfix form, require their operand to be of type Any, the Number primitive type, or an enum type, + and classified as a reference. They produce a result of the Number primitive type. + ---*/ + + +var a: any = '10' +a++ +a-- +--a +++a +Assert.isNumber(a) + +enum e { + A, + B, + C, + D +} +let b = e.A +b++ +b-- +++b +--b +Assert.isNumber(b) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/unary_operators/the_+_and_~_operators.ts b/test_framework/test/spec/expressions/unary_operators/the_+_and_~_operators.ts new file mode 100644 index 0000000000000000000000000000000000000000..e5af34b448888a96bb1d393a3a61a7024b99d51f --- /dev/null +++ b/test_framework/test/spec/expressions/unary_operators/the_+_and_~_operators.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The +, -, and ~ operators permit their operand to be of any type and produce a result of the Number primitive type. + The unary + operator can conveniently be used to convert a value of any type to the Number primitive type. + ---*/ + + +var a: any = 'a' +a = a + 1 +a = a - 1 +a = ~a +Assert.isNumber(a) + +function getValue() { } +var n = +getValue() +Assert.isNumber(n) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/unary_operators/the_delete_operator.ts b/test_framework/test/spec/expressions/unary_operators/the_delete_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..76013b502f2b968eed77d0611a0b81eb22bae61d --- /dev/null +++ b/test_framework/test/spec/expressions/unary_operators/the_delete_operator.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The 'delete' operator takes an operand of any type and produces a result of the Boolean primitive type. + ---*/ + + +interface Foo { + prop1?: string + prop2?: boolean + prop3?: number +} +let obj: Foo = { + prop1: 'value', + prop2: true, + prop3: 20 +} +Assert.isBoolean(delete obj.prop1) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/unary_operators/the_typeof_operator.ts b/test_framework/test/spec/expressions/unary_operators/the_typeof_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..1c06d9c734325f4c2646128ecabd3d73bfbaf1d0 --- /dev/null +++ b/test_framework/test/spec/expressions/unary_operators/the_typeof_operator.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The 'typeof' operator takes an operand of any type and produces a value of the String primitive type. In positions where a type is expected, 'typeof' can also be used in a type query to produce the type of an expression. + ---*/ + + +var x = 5 +var y = typeof x +Assert.equal(y, 'number') +var z: typeof x = 10 +Assert.isNumber(z) \ No newline at end of file diff --git a/test_framework/test/spec/expressions/unary_operators/the_void_operator.ts b/test_framework/test/spec/expressions/unary_operators/the_void_operator.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a6977b23222b3138c7c405f25e088e2dcf5f096 --- /dev/null +++ b/test_framework/test/spec/expressions/unary_operators/the_void_operator.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The 'void' operator takes an operand of any type and produces the value 'undefined'. The type of the result is the Undefined type. + ---*/ + + +let num: any = 10 +num = 'string' +let un = void num +Assert.isUndefined(un) \ No newline at end of file diff --git a/test_framework/test/spec/functions/asynchronous_functions/asynchronous_functions_1.ts b/test_framework/test/spec/functions/asynchronous_functions/asynchronous_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a406b1acee245db351d91a6c431894877760e5e --- /dev/null +++ b/test_framework/test/spec/functions/asynchronous_functions/asynchronous_functions_1.ts @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + An Async Function is a JavaScript Function, Parameterized Arrow Function, Method that has been prefixed with the async modifier. + An Async Function must provide a return type annotation that points to a compatible Promise type. + Return type inference can only be used if there is a globally defined, compatible Promise type. +options: + lib:es2015 + ---*/ + + +// globally defined +let pp: Promise = Promise.resolve(1); +// Function +async function fetchTest1(): Promise { + return await pp; +} +fetchTest1().then((params) => { + Assert.equal(params, 1); +}); +// Function : No Return Type +async function fetchTest1NoReturnType() { + return await pp; +} +fetchTest1NoReturnType().then((params) => { + Assert.equal(params, 1); +}); +// Arrow Function +const fetchTest2 = async (): Promise => { + return await pp; +}; +fetchTest2().then((params) => { + Assert.equal(params, 1); +}); +// Arrow Function : No Return Type +const fetchTest2NoReturnType = async () => { + return await pp; +}; +fetchTest2NoReturnType().then((params) => { + Assert.equal(params, 1); +}); + +class Person { + // Method + async fetchTest3(): Promise { + return await pp; + } + // Method : No Return Type + async fetchTest3NoReturnType() { + return await pp; + } +} +new Person().fetchTest3().then((params) => { + Assert.equal(params, 1); +}); +new Person().fetchTest3NoReturnType().then((params) => { + Assert.equal(params, 1); +}); diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_1.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..af0faeb60765216220520bf633d0fa1aeff02cdc --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + a destructuring parameter declaration introduces zero or more named locals + and initializes them with values extracted from properties or elements of the object or array passed as an argument for the parameter. + ---*/ + + +class Person { + m_name: string; + m_age: number; + constructor(name: string, age: number) { + this.m_name = name; + this.m_age = age; + } +} +let tt: Person = new Person("caihua", 12); +function showInfo({ m_name, m_age }: Person) { + Assert.isString(m_name); + Assert.equal(m_name, tt.m_name); + Assert.isNumber(m_age); + Assert.equal(m_age, tt.m_age); +} +showInfo(tt); + +let tt1: Person = new Person("caihua1", 121); +let tt2: Person = new Person("caihua2", 122); +let person_array: Person[] = [tt1, tt2]; + +function showArrayInfo(v_array: Array) { + let [tt1, tt2] = v_array; + Assert.equal(tt1.m_name, v_array[0].m_name); + Assert.equal(tt1.m_age, v_array[0].m_age); + Assert.equal(tt2.m_name, v_array[1].m_name); + Assert.equal(tt2.m_age, v_array[1].m_age); +} +showArrayInfo(person_array); diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_2.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c96e46cbdd9660015a875c11fbedfb9e262c1b0 --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the type T associated with a destructuring parameter declaration is determined as follows + If the declaration includes a type annotation, T is that type. + ---*/ + + +class Person { + m_name: string; + m_age: number; + constructor(name: string, age: number) { + this.m_name = name; + this.m_age = age; + } +} +let tt: Person = new Person("caihua", 12); +function showInfo(v: Person) { + let { m_name, m_age }: { m_name: string; m_age: number } = v; + Assert.isString(m_name); + Assert.equal(m_name, "caihua"); + Assert.isNumber(m_age); + Assert.equal(m_age, 12); +} +showInfo(tt); + diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_3.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..33464f49212400c623bf8b818973e0b949cacfe3 --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_3.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the type T associated with a destructuring parameter declaration is determined as follows + If the declaration occurs in a function expression for which a contextual signature is available, + T is the type obtained from the contextual signature. + ---*/ + + +class Person { + m_name: string; + m_age: number; + constructor(name: string, age: number) { + this.m_name = name; + this.m_age = age; + } +} +let tt: Person = new Person("caihua", 12); +const showInfo: (v: Person) => void = function (v) { + let { m_name, m_age } = v; + Assert.isString(m_name); + Assert.equal(m_name, "caihua"); + Assert.isNumber(m_age); + Assert.equal(m_age, 12); +}; +showInfo(tt); diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_4.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..00eeead3eebc6c486015c8dcd6c6bafc027b66d8 --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_4.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the type T associated with a destructuring parameter declaration is determined as follows + if the declaration includes an initializer expression, T is the widened form of the type of the initializer expression. + ---*/ + + +// string literal type +type testStr = "caihua"; +// number literal type +type testStrNum = 12; +class Person { + m_name: testStr | undefined; + m_age: testStrNum | undefined; + constructor(); + constructor(name?: testStr, age?: testStrNum) { + if (name && typeof name === "string") { + this.m_name = name; + } else { + this.m_name = undefined; + } + + if (age && typeof age === "number") { + this.m_age = age; + } else { + this.m_age = undefined; + } + } +} +let tt: Person = new Person(); + +function showInfo(v: Person) { + let { + m_name = "caihua", + m_age = 12, + }: { m_name: testStr | undefined; m_age: testStrNum | undefined } = v; + Assert.equal(typeof m_name === "string", true); + Assert.equal(typeof m_age === "number", true); +} +showInfo(tt); diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_5.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..5cec17edfff96d9e29737b83fd00bb8a1d7c3025 --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_5.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the type T associated with a destructuring parameter declaration is determined as follows + if the declaration specifies a binding pattern, T is the implied type of that binding pattern + ---*/ + + +type cc = { + name: string; + age: number; + isJob: boolean; +}; +// T is the implied type of that binding pattern +function showInfo({ age, name, isJob }: cc) { + Assert.isNumber(age); + Assert.isBoolean(isJob); + Assert.isString(name); +} +showInfo({ + name: "caihua", + age: 90, + isJob: true, +}); diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_6.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..5835dc5f7d3fe9acf1a3cc1fbc18bab01c35ae04 --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_6.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Destructuring parameter declarations do not permit type annotations on the individual binding patterns, + as such annotations would conflict with the already established meaning of colons in object literals. + Type annotations must instead be written on the top-level parameter declaration. + ---*/ + + +interface DrawTextInfo { + text?: string; + location?: [number, number]; + bold?: boolean; +} + +function drawText({ text, location, bold }: DrawTextInfo) { + if (text) { + Assert.equal(typeof text == "string", true); + } + if (location) { + Assert.equal(Array.isArray(location), true); + } + if (bold) { + Assert.equal(typeof bold === "boolean", true); + } +} + +drawText({ text: "text" }); +drawText({ bold: false }); +drawText({ location: [0, 1] }); diff --git a/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_7.ts b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..b4d4b7b2533dfdb900cac3f84a7de846cb6764a8 --- /dev/null +++ b/test_framework/test/spec/functions/destructuring_parameter_declarations/destructuring_parameter_declarations_7.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the type T associated with a destructuring parameter declaration is determined as follows + if the parameter is a rest parameter, T is any[]. + Otherwise,T is any + ---*/ + + +// v is any[]. +function test(...v:any[]) { + Assert.isString(v[0]); + Assert.isNumber(v[1]); + Assert.isBoolean(v[2]); +} +test("caihua", 90, true); diff --git a/test_framework/test/spec/functions/function_declarations/function_declarations_1.ts b/test_framework/test/spec/functions/function_declarations/function_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7cc936c079fc2848b6c0207be894c30f8887fa5e --- /dev/null +++ b/test_framework/test/spec/functions/function_declarations/function_declarations_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Function declarations are extended to permit the function body to be omitted in overload declarations. + a function can have at most one implementation. + When a function has both overloads and an implementation, the overloads must precede the implementation + and all of the declarations must be consecutive with no intervening grammatical elements. + ---*/ + + +function add(a: number, b: number): number; +function add(a: string, b: number): string; +function add(a: string, b: string): string; +function add(arg1: string | number, arg2: string | number) { + if (typeof arg1 === "number" && typeof arg2 === "number") { + return arg1 + arg2; + } + + if (typeof arg1 === "string" || typeof arg2 === "string") { + return `${arg1}${arg2}`; + } +} +// (number, number) +Assert.equal(add(0, 1), 1); +// (string, number) +Assert.equal(add("0", 1), "01"); +// (string, string) +Assert.equal(add("0", "1"), "01"); diff --git a/test_framework/test/spec/functions/function_declarations/function_declarations_2.ts b/test_framework/test/spec/functions/function_declarations/function_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d8eaf1e17764b0ac236bd1b1b9c2bb6474ad2bb9 --- /dev/null +++ b/test_framework/test/spec/functions/function_declarations/function_declarations_2.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + All declarations for the same function must specify the same set of modifiers (the same combination of declare, export, and default) + ---*/ + + +function add(a: string, b: string): string; +function add(a: number, b: number): number; +function add(a: string, b: number): string; +function add(arg1: string | number, arg2: string | number) { + if (typeof arg1 === "number" && typeof arg2 === "number") { + return arg1 + arg2; + } + + if (typeof arg1 === "string" || typeof arg2 === "string") { + return `${arg1}${arg2}`; + } +} + +// (number, number) +Assert.equal(add(0, 1), 1); +// (string, number) +Assert.equal(add("0", 1), "01"); +// (string, string) +Assert.equal(add("0", "1"), "01"); diff --git a/test_framework/test/spec/functions/function_implementations/function_implementations_1.ts b/test_framework/test/spec/functions/function_implementations/function_implementations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..fddc21dabe77cecc36928888876a3cdfe9aeface --- /dev/null +++ b/test_framework/test/spec/functions/function_implementations/function_implementations_1.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + If there are no return statements with expressions in f's function body, the inferred return type is Void. + ---*/ + + +function f(x: number) { + Assert.isNumber(x); +} +// get function's return type +type voidTest = ReturnType; +let tt1: void = undefined; +let tt2: voidTest = undefined; +Assert.equal(tt1, tt2); +Assert.equal(tt1 === tt2, true); diff --git a/test_framework/test/spec/functions/function_implementations/function_implementations_2.ts b/test_framework/test/spec/functions/function_implementations/function_implementations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c05e2dbd0aae001464ed774b44e74df5b5b7d1d9 --- /dev/null +++ b/test_framework/test/spec/functions/function_implementations/function_implementations_2.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + if f is a contextually typed function expression + the inferred return type is the union type of the types of the return statement expressions in the function body, + ignoring return statements with no expressions. + ---*/ + + +function f(x: number) { + switch (x) { + case 0: + return "hello"; + case 1: + return 1; + default: + return true; + } +} +type testType = ReturnType; + +type ttStr = testType & string; +let tt1: ttStr = "hello"; +Assert.isString(tt1); +type ttBoo = testType & boolean; +let tt2: ttBoo = true; +Assert.isBoolean(tt2); +type ttNum = testType & number; +let tt3: ttNum = 1; +Assert.isNumber(tt3); diff --git a/test_framework/test/spec/functions/function_implementations/function_implementations_3.ts b/test_framework/test/spec/functions/function_implementations/function_implementations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..31c84b1c3b4bbfd9cd2ad955fec2ea0754ff5957 --- /dev/null +++ b/test_framework/test/spec/functions/function_implementations/function_implementations_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the inferred return type is the first of the types of the return statement expressions in the function body + that is a supertype of each of the others, ignoring return statements with no expressions. + A compile-time error occurs if no return statement expression has a type that is a supertype of each of the others. + ---*/ + + +function foo(x: string | number, y: string | boolean) { + if (typeof x === "string") { + return x.toUpperCase(); + } else if (typeof x === "number") { + return x.toString(); + } else if (typeof y === "string") { + return y.toUpperCase(); + } else { + return y; + } +} +type cc = ReturnType; +let dd: cc = "string"; +Assert.isString(dd); +dd = true; +Assert.isBoolean(dd); diff --git a/test_framework/test/spec/functions/function_implementations/function_implementations_4.ts b/test_framework/test/spec/functions/function_implementations/function_implementations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..d9ec787cc074c1b70e37e9ace0e267ebf3ccace8 --- /dev/null +++ b/test_framework/test/spec/functions/function_implementations/function_implementations_4.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + In the signature of a function implementation, a parameter can be marked optional by following it with an initializer. + When a parameter declaration includes both a type annotation and an initializer, the initializer expression is contextually typed by the stated type + and must be assignable to the stated type, or otherwise a compile-time error occurs. + When a parameter declaration has no type annotation but includes an initializer, + the type of the parameter is the widened form (section 3.12) of the type of the initializer expression + ---*/ + + +// stated type +class Test { + a: number; + constructor(a: number) { this.a = a; } +} +function f(x: number, y: Test = { a: 1 }, z = "hello") { + // the type of the parameter is the widened + Assert.isString(z); + return { + x, + yy: y, + z: z, + }; +} +// y and z are optional +Assert.equal(f(0).yy.a, 1); +Assert.equal(f(0).z, "hello"); diff --git a/test_framework/test/spec/functions/function_implementations/function_implementations_5.ts b/test_framework/test/spec/functions/function_implementations/function_implementations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..669f1a8f1b55adff1692cfd6dcecdd6ee922796f --- /dev/null +++ b/test_framework/test/spec/functions/function_implementations/function_implementations_5.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Initializer expressions are evaluated in the scope of the function body but are not permitted to reference local variables + and are only permitted to access parameters that are declared to the left of the parameter they initialize, + unless the parameter reference occurs in a nested function expression. + ---*/ + + +let a = 1; +function f(x: number, y = x * 2, z = x + y) { + let b = 12; + function g(xx = b) { + return xx; + } + let c = g(); + return { x, y, z, g: c }; +} +Assert.equal(f(1).x, 1); +Assert.equal(f(1).y, 2); +Assert.equal(f(1).z, 3); +Assert.equal(f(1).g, 12); diff --git a/test_framework/test/spec/functions/function_overloads/function_overloads_1.ts b/test_framework/test/spec/functions/function_overloads/function_overloads_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..46730b8a907e0cc9fc0c22ebe83c63bb375b4ee3 --- /dev/null +++ b/test_framework/test/spec/functions/function_overloads/function_overloads_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The parameter list of a function overload cannot specify default values for parameters. + In other words, an overload may use only the ? form when specifying optional parameters. + ---*/ + + +function add(): number; +function add(x: number): number; +function add(y: number): number; +function add(x: number, y: number): number; +function add(xOry?: number, y?: number): number { + let sum: number = 0; + if (xOry) { + sum += xOry; + } + if (y) { + sum += y; + } + return sum; +} +Assert.equal(add(), 0); +Assert.equal(add(1), 1); +Assert.equal(add(1, 1), 2); diff --git a/test_framework/test/spec/functions/generator_functions/generator_functions_1.ts b/test_framework/test/spec/functions/generator_functions/generator_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7b9cafad6a3555c41fc88e3578a3c6c3f923775e --- /dev/null +++ b/test_framework/test/spec/functions/generator_functions/generator_functions_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + for..of loops over an iterable object, invoking the Symbol.iterator property on the object. + ---*/ + + +let someArray = [1, "string", false]; + +for (let item of someArray) { + if (typeof item === "number") { + Assert.equal(item, 1); + } else if (typeof item === "string") { + Assert.equal(item, "string"); + } else if (typeof item === "boolean") { + Assert.equal(item, false); + } +} diff --git a/test_framework/test/spec/functions/generator_functions/generator_functions_2.ts b/test_framework/test/spec/functions/generator_functions/generator_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7063c6fe2b6a62cd9236cc589bd1e0b53f97d193 --- /dev/null +++ b/test_framework/test/spec/functions/generator_functions/generator_functions_2.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Generator Functions +options: + lib: es2015 + target: es2015 + ---*/ + + +function* generator(): IterableIterator { + yield 0; + yield 1; + yield 2; +} +const iterator = generator(); +let cc = iterator.next(); +Assert.equal(cc.value, 0); +Assert.equal(cc.done, false); +cc = iterator.next(); +Assert.equal(cc.value, 1); +Assert.equal(cc.done, false); +cc = iterator.next(); +Assert.equal(cc.value, 2); +Assert.equal(cc.done, false); +cc = iterator.next(); +Assert.equal(cc.value, undefined); +Assert.equal(cc.done, true); diff --git a/test_framework/test/spec/functions/generic_functions/generic_functions_1.ts b/test_framework/test/spec/functions/generic_functions/generic_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4f9e3824a002a7ce5ffe21ef21cacaa218713f45 --- /dev/null +++ b/test_framework/test/spec/functions/generic_functions/generic_functions_1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Type parameters declared in the signature of a function implementation are + in scope in the signature and body of that function implementation. + ---*/ + + +function identity(arg: T): T { + return arg; +} +Assert.equal(identity(0), 0); +Assert.equal(identity("hello"), "hello"); +Assert.equal(identity(true), true); diff --git a/test_framework/test/spec/functions/generic_functions/generic_functions_2.ts b/test_framework/test/spec/functions/generic_functions/generic_functions_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..16e85cc00c4a58a6e6e178e709551788681cbfd6 --- /dev/null +++ b/test_framework/test/spec/functions/generic_functions/generic_functions_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The type arguments of a call to a generic function may be explicitly specified in a call operation + or may, when possible, be inferred from the types of the regular arguments in the call. + ---*/ + + +function identity(arg: T): T { + return arg; +} +Assert.equal(identity(0), 0); +Assert.equal(identity(0), 0); +Assert.equal(identity("hello"), "hello"); +Assert.equal(identity("hello"), "hello"); +Assert.equal(identity(true), true); +Assert.equal(identity(true), true); diff --git a/test_framework/test/spec/functions/type_guard_functions/type_guard_functions_1.ts b/test_framework/test/spec/functions/type_guard_functions/type_guard_functions_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..21e00ec4507701088aae838aca37a26d17452a1d --- /dev/null +++ b/test_framework/test/spec/functions/type_guard_functions/type_guard_functions_1.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: TypeScript compiler narrows the type of a variable within a type guard. + ---*/ + + +class Student { + name: string; + age: number; + constructor(name: string, age: number) { + this.name = name; + this.age = age; + } +} + +class Teacher { + name: string; + age: number; + job: string; + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + this.job = job; + } +} + +function isTeacher(obj: Student | Teacher): obj is Teacher { + return "job" in obj; +} +function printInfo(obj: Student | Teacher) { + if ( + isTeacher(obj) && + typeof obj.age === "number" && + obj instanceof Teacher + ) { + Assert.equal(obj.name, "caihuaTeacher"); + Assert.equal(obj.age, 20); + Assert.equal(obj.job, "teacher"); + return "teacher"; + } else { + Assert.equal(obj.name, "caihuaStudent"); + Assert.equal(obj.age, 20); + return "student"; + } +} + +let tt: Teacher = new Teacher("caihuaTeacher", 20, "teacher"); +let ss: Student = new Student("caihuaStudent", 20); +Assert.equal(printInfo(tt), "teacher"); +Assert.notEqual(printInfo(tt), "student"); +Assert.equal(printInfo(ss), "student"); +Assert.notEqual(printInfo(ss), "teacher"); diff --git a/test_framework/test/spec/grammar/ambients/ambients.d.ts b/test_framework/test/spec/grammar/ambients/ambients.d.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e9e3bb0e4c356a007caaf92bf9d2b05ee02588e --- /dev/null +++ b/test_framework/test/spec/grammar/ambients/ambients.d.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +declare class ClassA { + constructor(cname?: string, cost?: number); + public cname: string; + public ccost: number; +} +declare enum Code { + stop = 0x00, + run +} +declare type F1 = () => void; +declare type F2 = (a: string, b: string) => string; +declare type F3 = (a: number, b?: number) => string; +declare namespace AmbientNamespace { + var an1: string; + export var an2: string; + interface ColorInterface { + Red: number; + Green: number; + Blue: number; + } +} +declare var varAny: any; +declare let letString: string; +declare var varBool: boolean; \ No newline at end of file diff --git a/test_framework/test/spec/grammar/classes/classes.ts b/test_framework/test/spec/grammar/classes/classes.ts new file mode 100644 index 0000000000000000000000000000000000000000..6f4a13a73c37d6b72de980c1a25b085e2cd742b1 --- /dev/null +++ b/test_framework/test/spec/grammar/classes/classes.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +interface PointInterface { + x: number; + y: number; + z: number; + getPointArray(): number[]; +} +class Point implements PointInterface { + x: number = 0; + y: number = 0; + z: number = 0; + getPointArray() { + return [this.x, this.y, this.z]; + } + constructor(x: number, y: number, z: number) { + this.x = x; + this.y = y; + this.z = z; + } +} +class ColorC extends Point { + static Red: number = 0; + static Green: number = 0; + static Blue: number = 0; + static getColorArray(): number[] { + return [this.Red, this.Green, this.Blue] + } + toJSON() { + return JSON.stringify(ColorC.getColorArray()); + } + constructor(Red: number, Green: number, Blue: number, x: number = 0, y: number = 0, z: number = 0) { + super(x, y, z); + ColorC.Red = Red; + ColorC.Green = Green; + ColorC.Blue = Blue; + } +} +let colorPoint = new ColorC(255, 0, 0, 1, 1, 1); +Assert.equal(JSON.stringify(colorPoint.getPointArray()), "[1,1,1]"); +Assert.equal(colorPoint.toJSON(), "[255,0,0]"); diff --git a/test_framework/test/spec/grammar/enums/enums.ts b/test_framework/test/spec/grammar/enums/enums.ts new file mode 100644 index 0000000000000000000000000000000000000000..f8dcfdece8f04f3e87ba9395111a078bdd6e39b1 --- /dev/null +++ b/test_framework/test/spec/grammar/enums/enums.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +const enum CED { + None = -1, + False, + True = 1, + DEF = 1024, + Ver = "1.0.1", +} +Assert.equal(CED.None, -1); +Assert.equal(CED.False, 0); +Assert.equal(CED.True, 1); +Assert.equal(CED.DEF, 1024); +Assert.equal(CED["None"], -1); +Assert.equal(CED["False"], 0); +Assert.equal(CED["True"], 1); +Assert.equal(CED["DEF"], 1024); +Assert.equal(CED.Ver, "1.0.1"); diff --git a/test_framework/test/spec/grammar/expressions/expressions.ts b/test_framework/test/spec/grammar/expressions/expressions.ts new file mode 100644 index 0000000000000000000000000000000000000000..373cd3d769ff31edd033745a95c71276dde43d81 --- /dev/null +++ b/test_framework/test/spec/grammar/expressions/expressions.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +class EXP { + cname: string; + private _ver: number = -1; + addName(ad: string) { + this.cname = this.cname + " " + ad; + return this.cname; + } + get ver() { + return this._ver; + } + set ver(v: number) { + this._ver = v; + } + constructor(cname: string) { + this.cname = cname; + } +} +let exp = new EXP("EXP"); +exp.addName("Class"); +Assert.equal(exp.cname, "EXP Class"); + +exp.ver = 1; +Assert.equal(exp.ver, 1); + +function Ax2(a: number) { + return a * 2; +} +Assert.equal(Ax2(2), 4); + +let fun1: (a: number, b: number) => number = (a: number, b: number) => { + return a + b; +} +Assert.equal(fun1(1, 2), 3); \ No newline at end of file diff --git a/test_framework/test/spec/grammar/functions/functions.ts b/test_framework/test/spec/grammar/functions/functions.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1425b00d4e39b4cb3f5b482901300858a0f6607 --- /dev/null +++ b/test_framework/test/spec/grammar/functions/functions.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +function addXYZ(x: number, y: number, z: number): number { + return x + y + z; +} +Assert.equal(addXYZ(1, 2, 3), 6); + +function addXY(x: number, y: number): number; +function addXY(x: number, y: number): number { + return x + y; +} +Assert.equal(addXY(1, 2), 3); \ No newline at end of file diff --git a/test_framework/test/spec/grammar/interfaces/interfaces.ts b/test_framework/test/spec/grammar/interfaces/interfaces.ts new file mode 100644 index 0000000000000000000000000000000000000000..cb7d31de72a5aa3e6330d965f3d1ebac07c05ded --- /dev/null +++ b/test_framework/test/spec/grammar/interfaces/interfaces.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +interface X { + x: number; +} +interface Y { + y: number; +} +interface Z { + z: number; +} +interface PointXYZ extends X, Y, Z { + toJSON(): string; +} +let pa: PointXYZ = { + x: 3, + y: 4, + z: 5, + toJSON() { + let pArr = [this.x, this.y, this.z]; + return JSON.stringify(pArr); + }, +} +Assert.equal(pa.toJSON(), "[3,4,5]"); \ No newline at end of file diff --git a/test_framework/test/spec/grammar/namespaces/namespaces.ts b/test_framework/test/spec/grammar/namespaces/namespaces.ts new file mode 100644 index 0000000000000000000000000000000000000000..654cba643b4b5642986d10704154c562ad9c1268 --- /dev/null +++ b/test_framework/test/spec/grammar/namespaces/namespaces.ts @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +namespace ExampleG { + var namespace_name = "Example"; + export function exampleName(str: string): string { + return str + " " + namespace_name; + } + class Point { + x: number = 0; + y: number = 0; + } + interface PointXYZ { + x: number; + y: number; + z: number; + } + type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + }; + namespace SubNamespace { } + declare var __TEST__: boolean; + export import EE = ExportExampleG; +} +namespace ExportExampleG { + export var namespace_name = "ExportExampleG"; + export function exampleEName(str: string): string { + return str + " " + namespace_name; + } + export class Point { + x: number = 0; + y: number = 0; + } + export interface PointXYZ { + x: number; + y: number; + z: number; + } + export type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + } + export namespace SubNamespace { } + export declare var __TEST__: boolean; + export import E = ExampleG; +} +Assert.equal(ExampleG.exampleName("G"), "G Example"); +Assert.equal(ExampleG.Color.RED, 0xFF0000); +Assert.equal(ExportExampleG.exampleEName("G"), "G ExportExampleG"); \ No newline at end of file diff --git a/test_framework/test/spec/grammar/scripts_and_modules/scripts_and_modules.ts b/test_framework/test/spec/grammar/scripts_and_modules/scripts_and_modules.ts new file mode 100644 index 0000000000000000000000000000000000000000..50581a1deb5f48adf1a8f914655ba786a10c94fa --- /dev/null +++ b/test_framework/test/spec/grammar/scripts_and_modules/scripts_and_modules.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + module: ESNext + isCurrent: true + ---*/ + + +import * as sam from "./scripts_and_modules_cp.js" +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +equal(sam.pi, 3.14); + +let a100 = sam.area(10); +equal(a100, 314); + +let cr = new sam.Color(0, 255, 0); +equal(cr.toColorJSON(), "[0,255,0]"); + +let w: sam.Weapon = { Damage: 12500, DamageType: "XO" }; +let wjson = JSON.stringify(w); +equal(wjson, "{\"Damage\":12500,\"DamageType\":\"XO\"}"); + +equal(sam.CommandE.end, -1); +equal(sam.CommandE.stop, 0); +equal(sam.CommandE.run, 1); + +let p: sam.AE.PointXYZ = { x: -1, y: -1, z: -1 }; +let pjson = JSON.stringify(p); +equal(pjson, "{\"x\":-1,\"y\":-1,\"z\":-1}"); + diff --git a/test_framework/test/spec/grammar/scripts_and_modules/scripts_and_modules_cp.ts b/test_framework/test/spec/grammar/scripts_and_modules/scripts_and_modules_cp.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ef4230f46cf6eda0f70f1a6488fce932e56812b --- /dev/null +++ b/test_framework/test/spec/grammar/scripts_and_modules/scripts_and_modules_cp.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +var pi: number = 3.14 +function area(r: number) { + return r * r * pi; +} +class Color { + Red: number = 0; + Green: number = 0; + Bule: number = 0; + constructor(r: number, g: number, b: number) { + this.Red = r; + this.Green = g; + this.Bule = b; + } + toColorJSON() { + let a: number[] = [this.Red, this.Green, this.Bule]; + return JSON.stringify(a); + } +} +interface Weapon { + Damage: number; + DamageType: string; +} +type Skill = Weapon | { Damage: number, Data: string }; +enum CommandE { + end = -1, + stop, + run, +} +namespace AE { + export interface PointXYZ { + x: number; + y: number; + z: number; + } + export type StrNumBool = string | number | boolean; +} +export { pi, area, Color, Weapon, Skill, CommandE, AE }; diff --git a/test_framework/test/spec/grammar/statements/statements.ts b/test_framework/test/spec/grammar/statements/statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..71165d88ac1fc7ff42a2ab5849b1acd3c98aea09 --- /dev/null +++ b/test_framework/test/spec/grammar/statements/statements.ts @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + ---*/ + + +interface Skill { + SkillName: string; + Damage: number; + Data: string; +} +let aoe: Skill = { SkillName: "AoE", Damage: 495, Data: "AreaFire" }; +Assert.equal(aoe.SkillName, "AoE"); +Assert.equal(aoe.Damage, 495); +Assert.equal(aoe.Data, "AreaFire"); + +type NumStr = number | string; +let ns: NumStr; +ns = 1024; +Assert.isNumber(ns); +ns = "A"; +Assert.isString(ns); + +enum Color { + Red = 0xFF0000, + Green = 0x00FF00, + Bule = 0x0000FF, +} +Assert.equal(Color.Green, 0x00FF00); + +let aaa; +aaa = true; +Assert.isBoolean(aaa); +aaa = 1408; +Assert.isNumber(aaa); +let abc: string; +abc = "ABC"; +Assert.isString(abc); + +type TF = -1 | 0 | 1; +let tf0: TF = 0; +let tf1: TF = 1; +let tf2: TF = -1; +Assert.equal(tf0, 0); +Assert.equal(tf1, 1); +Assert.equal(tf2, -1); diff --git a/test_framework/test/spec/grammar/types/types.ts b/test_framework/test/spec/grammar/types/types.ts new file mode 100644 index 0000000000000000000000000000000000000000..faef90a4d6e961e5b07f8ad82d56790473e082f4 --- /dev/null +++ b/test_framework/test/spec/grammar/types/types.ts @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + this appendix contains a summary of the grammar found in the main document. + typescript grammar is a superset of the grammar defined in the ECMAScript 2015 Language Specification (specifically, the ECMA-262 Standard, 6th Edition) and this appendix lists only productions that are new or modified from the ECMAScript grammar. + options: + target: es2015 + lib: es2015 + ---*/ + + +let num: number = 1024; +let str: string = "string"; +let bool: boolean = false; +let big: bigint = 1n; +let sym: symbol = Symbol(); +let obj: object = {}; +let fun: Function = () => { return "Function"; } +let udf: undefined = undefined; +Assert.isNumber(num); +Assert.isString(str); +Assert.isBoolean(bool); +Assert.equal(typeof big, "bigint"); +Assert.isSymbol(sym); +Assert.equal(typeof obj, "object"); +Assert.isFunction(fun); +Assert.isUndefined(udf); + +type Ver = { ver: string }; +interface Data extends Ver { + data: number; +} +let v: Data = { data: 1024, ver: "1.0.1" }; +Assert.equal(v.data, 1024); +Assert.equal(v.ver, "1.0.1"); + +class AB{ + a: T | undefined; + b: U | undefined; + public c: string = ""; + private d: string = ""; + protected e: string = ""; + setD(val: string) { + this.d = val; + } + setE(val: string) { + this.e = val; + } + show() { + return JSON.stringify({ d: this.d, e: this.e }); + } + constructor(a?: T, b?: U) { + this.a = a; + this.b = b; + } +} +let ab = new AB(123, "ABC"); +Assert.equal(ab.a, 123); +Assert.equal(ab.b, "ABC"); + +let ab2 = new AB(1024); +Assert.equal(ab2.a, 1024); +Assert.isUndefined(ab2.b); + +ab.c = "1024"; +Assert.equal(ab.c, "1024"); + +ab.setD("D"); +ab.setE("E"); +Assert.equal(ab.show(), "{\"d\":\"D\",\"e\":\"E\"}"); + +type ONS = { n1?: number | string, n2?: number | string }; +let ons: ONS = { n1: 0, n2: "ABC" }; +Assert.equal(ons.n1, 0); +Assert.equal(ons.n2, "ABC"); + + +type NumStrBool = number | string | boolean; +type NumObjBool = number | object | boolean; +type NumBool = NumObjBool & NumStrBool; +let nb: NumBool = 1; +Assert.isNumber(nb); +nb = true; +Assert.isBoolean(nb); + +type StrFun = (a: string, b: string) => string; +let strFun: StrFun = (a: string, b: string) => { return a + b; } +Assert.equal(strFun("AB", "CD"), "ABCD"); + +let a: any; +let vo: void; + +let arr: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; +Assert.equal(arr[0], 0); + +type type0 = "A" | "B" | "C" | "D"; +let t0: type0 = "A"; +Assert.equal(t0, "A"); + +type NumStrArray = number[] | string[] | (number | string)[]; +let nsa1: NumStrArray = [1, 3, 5]; +let ar1 = [1, 3, 5] +let nsa2: NumStrArray = ["A", "B", "C"]; +let ar2 = ["A", "B", "C"]; +let nsa3: NumStrArray = ["A", 1, "B", 2, "C", 3]; +let ar3 = ["A", 1, "B", 2, "C", 3]; +Assert.equal(JSON.stringify(nsa1), JSON.stringify(ar1)); +Assert.equal(JSON.stringify(nsa2), JSON.stringify(ar2)); +Assert.equal(JSON.stringify(nsa3), JSON.stringify(ar3)); + +type ABC = [number, string, boolean]; +let abc: ABC = [1, "A", true]; +let abc2 = [1, "A", true]; +Assert.equal(JSON.stringify(abc), JSON.stringify(abc2)); \ No newline at end of file diff --git a/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_1.ts b/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..56b2ce0dac032755509fcf961af8c61c5bb28499 --- /dev/null +++ b/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_1.ts @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An interface declaration declares an interface type. + An InterfaceDeclaration introduces a named type (section 3.7) in the containing declaration space. + The BindingIdentifier of an interface declaration may not be one of the predefined type names (section 3.8.1). + ---*/ + + +{ + interface Mover { + x: number; + + move(): void; + + getStatus(): { speed: number; }; + } + + interface Shaker { + y: number; + + shake(): void; + + getStatus(): { frequency: number; }; + } + + let result1: { frequency: number; } = { + frequency: 1 + }; + let result2: { speed: number; } = { + speed: 1, + }; + + let point: Mover = { + x: 1, + getStatus(): { speed: number; } { + return result2 + }, + move(): void { + } + }; + let pointer: Shaker = { + y: 1, + getStatus(): { frequency: number; } { + return result1 + }, + shake(): void { + }, + }; + + Assert.equal(point.x, 1); + Assert.equal(pointer.y, 1); + Assert.equal(point.getStatus(), result2); + Assert.equal(pointer.getStatus(), result1); + Assert.equal(pointer.shake(), undefined); + Assert.equal(point.move(), undefined); +} + + + + diff --git a/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_2.ts b/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9f4c45b21b71fd645b96ebb255a7757234f929ac --- /dev/null +++ b/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_2.ts @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An interface can inherit from zero or more base types which are specified in the InterfaceExtendsClause. + The base types must be type references to class or interface types. + ---*/ + + +{ + interface Mover { + x: number; + + move(): void; + + getStatus(): { speed: number; }; + } + + interface Shaker { + y: number; + + shake(): void; + + getStatus(): { frequency: number; }; + } + + interface MoverShaker extends Mover, Shaker { + z: number; + + getStatus(): { speed: number; frequency: number; }; + } + + let result: { speed: number; frequency: number; } = { + speed: 1, + frequency: 1 + }; + + let point: MoverShaker = { + x: 1, + z: 1, + y: 1, + getStatus(): { speed: number; frequency: number; } { + return result + }, + shake(): void { + }, + move(): void { + } + }; + + Assert.equal(point.x, 1); + Assert.equal(point.y, 1); + Assert.equal(point.z, 1); + Assert.equal(point.getStatus(), result); + Assert.equal(point.shake(), undefined); + Assert.equal(point.move(), undefined); +} + + + + diff --git a/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_3.ts b/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1fba9b8093c6a222525087d24a1fbc6217305c62 --- /dev/null +++ b/test_framework/test/spec/interfaces/1_interface_declarations/interface_declarations_3.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Since function and constructor types are just object types containing call and construct signatures, interfaces can + be used to declare named function and constructor types + ---*/ + + +{ + interface StringComparer { + (a: string, b: string): number; + } + + let ps: StringComparer = + (a: string, b: string): number => { + return 1 + }; + + class Class { + x: number; + y: number; + + constructor(a: number, b: number) { + this.x = a; + this.y = b; + } + } + + let pt: Class = new Class(0, 1); + // assert declare named function + Assert.equal(pt.x, 0); + // assert constructor types + Assert.equal(pt.y, 1); +} + + + + + diff --git a/test_framework/test/spec/interfaces/2_declaration_merging/declaration_merging_1.ts b/test_framework/test/spec/interfaces/2_declaration_merging/declaration_merging_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..237804a9aee1cdf7853f928f4144c987e2810394 --- /dev/null +++ b/test_framework/test/spec/interfaces/2_declaration_merging/declaration_merging_1.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a generic interface has multiple declarations, all declarations must have identical type parameter lists, + i.e. identical type parameter names with identical constraints in identical order. + ---*/ + + +{ + interface Documents { + createElement1(a: T): number; + } + + interface Documents { + createElement2(a: T): number; + + createElement3(a: T): number; + } + + let name: Documents = { + createElement1(a: string): number { + return 0 + }, + createElement2(a: boolean): number { + return 0 + }, + createElement3(a: number): number { + return 0 + } + }; + + class Class { + static getName(name: Documents): Documents { + return name; + } + } + + Assert.equal(Class.getName(name).createElement1(''), 0); + Assert.equal(Class.getName(name).createElement3(0), 0); + Assert.equal(Class.getName(name).createElement2(true), 0); +} \ No newline at end of file diff --git a/test_framework/test/spec/interfaces/2_declaration_merging/declaration_merging_2.ts b/test_framework/test/spec/interfaces/2_declaration_merging/declaration_merging_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..2e7d7e53e64c2e325434ffa76b75e0b93430aa10 --- /dev/null +++ b/test_framework/test/spec/interfaces/2_declaration_merging/declaration_merging_2.ts @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + multiple declarations is equivalent to the following single declaration. + ---*/ + + +{ + interface Document1 { + createElement1(a: string): number; + } + + interface Document1 { + createElement2(a: boolean): number; + + createElement3(a: number): number; + } + + interface DocumentAssert { + createElement4(a: string): number; + + createElement5(a: boolean): number; + + createElement6(a: number): number; + } + + let name: Document1 & DocumentAssert = { + createElement1(a: string): number { + return 0 + }, + createElement2(a: boolean): number { + return 0 + }, + createElement3(a: number): number { + return 0 + }, + createElement4(a: string): number { + return 0 + }, + createElement5(a: boolean): number { + return 0 + }, + createElement6(a: number): number { + return 0 + } + }; + + class Class { + static getName(name: Document1 & DocumentAssert): Document1 & DocumentAssert { + return name; + } + } + + Assert.equal(Class.getName(name).createElement1(''), 0); + Assert.equal(Class.getName(name).createElement3(0), 0); + Assert.equal(Class.getName(name).createElement2(true), 0); + Assert.equal(Class.getName(name).createElement4(''), 0); + Assert.equal(Class.getName(name).createElement5(true), 0); + Assert.equal(Class.getName(name).createElement6(0), 0); +} \ No newline at end of file diff --git a/test_framework/test/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_1.ts b/test_framework/test/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..28153f4941610fbaa77170a9c7e057a27a13fabe --- /dev/null +++ b/test_framework/test/spec/interfaces/3_interfaces_extending_classes/interfaces_extending_classes_1.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When an interface type extends a class type it inherits the members of the class but not their implementations. + It is as if the interface had declared all of the members of the class without providing an implementation. + ---*/ + + +{ + class Control { + private state: any = 0; + point: any = 0; + } + + interface SelectableControl extends Control { + select(): number; + } + + class Button extends Control implements SelectableControl { + select(): number { + return 0 + } + } + + Assert.isTrue(new Button().hasOwnProperty('point')); + Assert.isTrue(new Button().hasOwnProperty('state')); + +} diff --git a/test_framework/test/spec/interfaces/4_dynamic_type_checks/dynamic_type_checks_1.ts b/test_framework/test/spec/interfaces/4_dynamic_type_checks/dynamic_type_checks_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9c56c9ebfbccee3a01f37fad952f9516d4150480 --- /dev/null +++ b/test_framework/test/spec/interfaces/4_dynamic_type_checks/dynamic_type_checks_1.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + TypeScript does not provide a direct mechanism for dynamically testing whether an object implements a particular + interface. Instead, TypeScript code can use the JavaScript technique of checking whether an appropriate set of members + are present on the object + ---*/ + + +{ + interface MoverShaker { + move(): void; + + shake(): void; + + getStatus(): void; + } + + function asMoverShaker(obj: any): MoverShaker { + return obj && obj.move && obj.shake && obj.getStatus ? obj : null; + } + + let point: MoverShaker = { + getStatus(): void { + }, + shake(): void { + }, + move(): void { + } + }; + + Assert.equal(asMoverShaker(point), point); +} \ No newline at end of file diff --git a/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_1.ts b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c354256a2575a822de88aa517e0b21100e94b06b --- /dev/null +++ b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_1.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +namespace outer { + var local = 1; + export var a = local; + export namespace inner { + export var x = 10; + } + export interface OA { + OA: string; + } +} diff --git a/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_2.ts b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..ed7244a05de27173be1639dd9074444ccd803605 --- /dev/null +++ b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_2.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +namespace outer { + var local = 2; + export var b = local; + export namespace inner { + export var y = 20; + } + export interface OB { + OB: number; + } +} diff --git a/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_3.ts b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..257c3d508998c6a0b1f65e9e70c6c9b2938c7ed5 --- /dev/null +++ b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_1_3.ts @@ -0,0 +1,26 @@ +/// +/// +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + namespaces are "open-ended" and namespace declarations with the same qualified name relative to a common root (as defined in section 2.3) contribute to a single namespace. + ---*/ + + +var o1: outer.OA = { OA: "OA" }; +var o2: outer.OB = { OB: 1024 }; +Assert.isString(o1.OA); +Assert.isNumber(o2.OB); \ No newline at end of file diff --git a/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_2.ts b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..28a5fa1fd3a7ca9981af6225052220f1c768b364 --- /dev/null +++ b/test_framework/test/spec/namespaces/declaration_merging/declaration_merging_2.ts @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + declaration merging also extends to namespace declarations with the same qualified name relative to a common root as a function, class, or enum declaration. + when merging a non-ambient function or class declaration and a non-ambient namespace declaration, the function or class declaration must be located prior to the namespace declaration in the same source file. + this ensures that the shared object instance is created as a function object. + ---*/ + + +interface Point { + x: number; + y: number; +} +function Point(x: number, y: number): Point { + return { x: x, y: y }; +} +namespace Point { + export var origin = Point(0, 0); + export function equals(p1: Point, p2: Point) { + return p1.x == p2.x && p1.y == p2.y; + } +} +var p1 = Point(0, 0); +var p2 = Point.origin; +var b = Point.equals(p1, p2); +Assert.equal(p1.x, 0); +Assert.equal(p1.y, 0); +Assert.equal(p2.x, 0); +Assert.equal(p2.y, 0); +Assert.equal(b, true); + +enum Color { + Red = 0xFF0000, + Green = 0x00FF00, + Blue = 0x0000FF, +} +namespace Color { + export interface Color { + Red: number; + Green: number; + Blue: number; + } +} +Assert.equal(Color.Red, 0xFF0000); +var color: Color.Color = { Red: 255, Green: 255, Blue: 255 }; +Assert.equal(color.Green, 255); + +class PointXYZ { + public x: number; + public y: number; + public z: number; + constructor(x: number = 0, y: number = 0, z: number = 0) { + this.x = x; + this.y = y; + this.z = z; + } + toString() { + return "( " + this.x + " , " + this.y + " , " + this.z + " )"; + } +} +namespace PointXYZ { + export var xyz = new PointXYZ(1, 1, 1); + export interface PointXYZ { + x: number; + y: number; + z: number; + } +} +var xyz1 = new PointXYZ(1, 3, 5); +var xyz2: PointXYZ.PointXYZ = { x: 2, y: 3, z: 4 }; +var xyz3 = PointXYZ.xyz; +Assert.equal(xyz1.toString(), "( 1 , 3 , 5 )"); +Assert.equal(xyz2.x, 2); +Assert.equal(xyz3.toString(), "( 1 , 1 , 1 )"); \ No newline at end of file diff --git a/test_framework/test/spec/namespaces/export_declarations/export_declarations_1.ts b/test_framework/test/spec/namespaces/export_declarations/export_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3b657f1818fca63e3a9b47ccd5cc669160c0e27e --- /dev/null +++ b/test_framework/test/spec/namespaces/export_declarations/export_declarations_1.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an export declaration declares an externally accessible namespace member. An export declaration is simply a regular declaration prefixed with the keyword export. + ---*/ + + +namespace ED1 { + export interface Weapon { WName: string, Damage: number } + export var def = 1024; +} +var weapon1: ED1.Weapon = { WName: "40mmAT", Damage: 40 }; +var def1 = ED1.def; +Assert.equal(weapon1.WName, "40mmAT"); +Assert.equal(weapon1.Damage, 40); +Assert.equal(def1, 1024); \ No newline at end of file diff --git a/test_framework/test/spec/namespaces/export_declarations/export_declarations_2.ts b/test_framework/test/spec/namespaces/export_declarations/export_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a68c7ec51b2171c7be77ed2ae30ba7849d2741a2 --- /dev/null +++ b/test_framework/test/spec/namespaces/export_declarations/export_declarations_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the members of a namespace's export declaration space constitute the namespace's export member set. + a namespace's instance type is an object type with a property for each member in the namespace's export member set that denotes a value. + ---*/ + + +namespace ED2 { + export var def = 1024; + interface Weapon { WName: string, Damage: number } + export var weapon1: Weapon = { WName: "Lightting", Damage: 200 } + export var tf = "False"; +} +var ed2 = ED2; +var ed2_1 = ed2.def; +var ed2_2 = ed2.weapon1; +var ed2_3 = ed2.tf; +Assert.equal(typeof ed2, "object"); +Assert.isNumber(ed2_1); +Assert.equal(typeof ed2_2, "object"); +Assert.isString(ed2_3); diff --git a/test_framework/test/spec/namespaces/export_declarations/export_declarations_3.ts b/test_framework/test/spec/namespaces/export_declarations/export_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..64aa9f1ae57d6b0e057bfe98e689f1878ea26cae --- /dev/null +++ b/test_framework/test/spec/namespaces/export_declarations/export_declarations_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + an exported member depends on a (possibly empty) set of named types. Those named types must be at least as accessible as the exported member, or otherwise an error occurs. + ---*/ + + +interface WeaponDamage { DamageType: string, Damage: number } +namespace ED3 { + export interface WeaponData { WName: string, ROT: number, Range: number } + export interface Weapon { WeaponDamage: WeaponDamage, WeaponData: WeaponData } + export function getWeapon(Weapon: Weapon) { + return "WeaponName = " + Weapon.WeaponData.WName + "\n" + "DamageType = " + Weapon.WeaponDamage.DamageType + "\n" + "Damage = " + Weapon.WeaponDamage.Damage + "\n" + "ROT = " + Weapon.WeaponData.ROT + "\n" + "Range = " + Weapon.WeaponData.Range; + } +} +var weapon_damage: WeaponDamage = { DamageType: "EXPLODE", Damage: 1024 }; +var weapon_data: ED3.WeaponData = { WName: "AntiTankMissiles", ROT: 75, Range: 16 }; +var weapon: ED3.Weapon = { WeaponData: weapon_data, WeaponDamage: weapon_damage }; +var weaponStr = ED3.getWeapon(weapon); +Assert.isString(weaponStr); diff --git a/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_1.ts b/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0754ea40352a52a1a5a04addfdc0e499e2a15997 --- /dev/null +++ b/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_1.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + import alias declarations are used to create local aliases for entities in other namespaces. + an EntityName consisting of a single identifier is resolved as a NamespaceName and is thus required to reference a namespace. The resulting local alias references the given namespace and is itself classified as a namespace. + ---*/ + + +namespace B { + interface A { n: number } + export import Y = A; + import Z = A.X; + export var vb: Z = { s: "v" }; +} +namespace A { + export interface X { s: string } + export import va = B.vb; +} +var v1: B.Y.X = B.vb; +var v2 = A.va; +let iadFlag: boolean = false; +if (v1 == v2) { + iadFlag = true; +} +Assert.isTrue(iadFlag); + diff --git a/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_2.ts b/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..8b81041cef88e17da10293ae2c81180c89aa800c --- /dev/null +++ b/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_2.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the NamespaceName portion of an EntityName references an instantiated namespace, the NamespaceName is required to reference the namespace instance when evaluated as an expression. + error: + code: TS2437 + message: Module 'A' is hidden by a local declaration with the same name. + type: + phase: compiler + ---*/ + + +namespace A { + interface X { s: string } + export var v: number = 1408; +} + +namespace B { + var A = 1; + export import Y = A; +} +var iadv = B.Y.v; diff --git a/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_3.ts b/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..920cfd5cef9a9b9293aec8bfc86e571b2ee4497d --- /dev/null +++ b/test_framework/test/spec/namespaces/import_alias_declarations/import_alias_declarations_3.ts @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when an import statement includes an export modifier, all meanings of the local alias are exported. + module: ESNext + isCurrent: true + ---*/ + + +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} +namespace IAD1 { + export var lv: number = 5; + export interface GGI { + UIName: string; + Cost: number; + } +} +namespace IAD2 { + import I1 = IAD1; + import I2 = IAD1.GGI; + export var i2 = I1.lv * 2; + export interface GI extends I2 { + Strength: number; + } +} +var i1 = IAD2.i2; +var gi: IAD2.GI = { UIName: "GI", Cost: 200, Strength: 100 }; +equal(i1, 10); +equal(gi.UIName, "GI"); +equal(gi.Cost, 200); +equal(gi.Strength, 100); + +namespace IAD3 { + export var i = 0; + interface U { + times: number; + } + var u1: U = { times: 5 }; + export function ux5() { + return u1.times * 5; + } +} +export namespace IAD4 { + export import I3 = IAD3; +} +equal(IAD4.I3.i, 0); +equal(IAD4.I3.ux5(), 25); diff --git a/test_framework/test/spec/namespaces/namespace_body/namespace_body.ts b/test_framework/test/spec/namespaces/namespace_body/namespace_body.ts new file mode 100644 index 0000000000000000000000000000000000000000..d1fdb7768f63cc6ef2e90e2439eeb576f7e09fcb --- /dev/null +++ b/test_framework/test/spec/namespaces/namespace_body/namespace_body.ts @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the body of a namespace corresponds to a function that is executed once to initialize the namespace instance. + ---*/ + + +namespace Example { + var namespace_name = "Example"; + export function exampleName(str: string): string { + return str + " " + namespace_name; + } + class Point { + x: number = 0; + y: number = 0; + } + interface PointXYZ { + x: number; + y: number; + z: number; + } + type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + } + namespace SubNamespace { } + declare var __TEST__: boolean; + import EE = ExportExample; +} +namespace ExportExample { + export var namespace_name = "ExportExample"; + export function exampleEName(str: string): string { + return str + " " + namespace_name; + } + export class Point { + x: number = 0; + y: number = 0; + } + export interface PointXYZ { + x: number; + y: number; + z: number; + } + export type StrNumBool = string | number | boolean; + export enum Color { + RED = 0xFF0000, + GREEN = 0x00FF00, + BLUE = 0x0000FF, + } + export namespace SubNamespace { } + export declare var __TEST__: boolean; + export import E = Example; +} +Assert.equal(Example.exampleName("G"), "G Example"); +Assert.equal(Example.Color.RED, 0xFF0000); +Assert.equal(ExportExample.exampleEName("G"), "G ExportExample"); \ No newline at end of file diff --git a/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_1.ts b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..75dad98cdaf13f0a8559fb1fab3975caa3934b8a --- /dev/null +++ b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_1.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + namespaces are declared using the namespace keyword, + but for backward compatibility of earlier versions of TypeScript a module keyword can also be used. + ---*/ + + +namespace A { + export interface TA { + TName: string; + Ver: number; + } +} +var varA: A.TA = { TName: "TA", Ver: 1.5 } +Assert.equal(varA.TName, "TA"); +Assert.equal(varA.Ver, 1.5); +module B { + export interface TB { + SkillName: string; + Damage: number; + } +} +var varB: B.TB = { SkillName: "OverKill", Damage: 1024 }; +Assert.equal(varB.SkillName, "OverKill"); +Assert.equal(varB.Damage, 1024); diff --git a/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_2.ts b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..29c4c0001801f96b367bb05d533c3ca5a3e59c90 --- /dev/null +++ b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_2.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + namespaces are either instantiated or non-instantiated. A non-instantiated namespace is a namespace containing only interface types, type aliases, and other non-instantiated namespace. + an instantiated namespace is a namespace that doesn't meet this definition. In intuitive terms, an instantiated namespace is one for which a namespace instance is created, whereas a non-instantiated namespace is one for which no code is generated. + ---*/ + + +namespace NonInstantiated { + export interface A { + TName: string; + Ver: number; + } + export type TF = boolean | number; + export namespace NI { + export interface B { + SkillName: string; + Damage: number; + } + export type StrNum = string | number; + } +} +var ni1: NonInstantiated.A = { TName: "Non", Ver: 1.0 }; +var ni2: NonInstantiated.NI.StrNum = "ni2"; +Assert.equal(ni1.Ver, 1.0); +Assert.equal(ni2, "ni2"); + +namespace Instantiated { + export function returnName() { + return "Instantiated"; + } + export var nsname: string = "Instantiated"; +} +Assert.equal(Instantiated.returnName(), "Instantiated"); +Assert.equal(Instantiated.nsname, "Instantiated"); \ No newline at end of file diff --git a/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_3.ts b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..c4a9f61e2d034e3300982ca15ed6c7bc3ad5d355 --- /dev/null +++ b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_3.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when a namespace identifier is referenced as a NamespaceName it denotes a container of namespace and type names, and when a namespace identifier is referenced as a PrimaryExpression it denotes the singleton namespace instance. + ---*/ + + +namespace A { + export interface TypeA { + AName: string; + Lv: number; + UID: number; + } + export var player: TypeA = { AName: "Player", Lv: 5, UID: 0x0000A0F0 }; + export var playerRandom: TypeA = { AName: "playerRandom", Lv: getRandomNumber(99), UID: getRandomNumber(0xFFFFFFFF) }; + function getRandomNumber(x: number): number { + return Math.floor(Math.random() * x); + } +} +var playerA = A.player; +var playerB = A.playerRandom; +var an = A; +var playerC: A.TypeA = { AName: "PlayerC", Lv: 95, UID: 6250 }; +var playerD = an.player; +Assert.equal(playerA.AName, "Player"); +Assert.equal(playerA.Lv, 5); +Assert.equal(playerA.UID, 41200); +Assert.equal(playerB.AName, "playerRandom"); +Assert.isNumber(playerB.Lv); +Assert.isNumber(playerB.UID); +Assert.equal(playerC.AName, "PlayerC"); +Assert.equal(playerC.Lv, 95); +Assert.equal(playerC.UID, 6250); +Assert.equal(playerD.AName, "Player"); +Assert.equal(playerD.Lv, 5); +Assert.equal(playerD.UID, 41200); diff --git a/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_4.ts b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..1e284ea906a53895d67e5b971a26e48d1dcec3fa --- /dev/null +++ b/test_framework/test/spec/namespaces/namespace_declarations/namespace_declarations_4.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a namespace declaration that specifies an IdentifierPath with more than one identifier is equivalent to a series of nested single-identifier namespace declarations where all but the outermost are automatically exported. + ---*/ + + +namespace A.B.C { + export var x = "This is equivalent to the code below."; +} +Assert.equal(A.B.C.x, "This is equivalent to the code below."); +namespace A { + export namespace B { + export namespace C { + export var x = "This is equivalent to the code above."; + } + } +} +Assert.equal(A.B.C.x, "This is equivalent to the code above."); diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_1.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..0b7a2e4ef958d3edf522f1c30d8a5f0af6434f43 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_1.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export function message():string { + return 'string'; +} \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_10.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..dde02469651490a2033eea0e29b4d26512ac20c5 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_10.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export interface Point { x: number; y: number }; + +export function point(x: number, y: number): Point { + return { x, y }; +} \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_2.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..011b35013642ccc8fb2b4e86d596ed16273c3b4b --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_2.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export let x:number = 0; +export let y:number = 1; +export let z:number = 2; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_3.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f005b1ecb34a5095fa4b687c3b5bcae031e359b --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_3.ts @@ -0,0 +1,18 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +let x:number = 0; +export default x; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_4.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..021fa6f9857adb101358187e64c2ad4a902c2dc2 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_4.ts @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default function message():string { + return 'string'; +} \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_5.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..7bb278e263064cf4bed8b70a21f43bcb74d52510 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_5.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function message():string { + return 'string'; +} + +export default message; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_6.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..0ad23bd163e206df8b953e3488df6d852db4a6f1 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_6.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function message():string { + return 'string'; +} + +export { message as default }; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_7.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..fab5d937f616b564705120e7e519f5dcbde1b4a2 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_7.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function message():string { + return 'string'; +} + +interface message { + x: number; + y: number; +} + + +export default message; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_8.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..5f1327d564e48e559c9b94a129f6777d1db1fde1 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_8.ts @@ -0,0 +1,17 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +export default 'hello'; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_9.ts b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..899278f98f694dfaa6f921611f183cb5aee2275b --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/1_programs_and_source_files/source_9.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +class Point { + public x: number; + public y: number; + constructor(a: number, b: number) { + this.x = a; + this.y = b; + } +} +export default Point; \ No newline at end of file diff --git a/test_framework/test/spec/scripts_and_modules/2_modules/modules_1.ts b/test_framework/test/spec/scripts_and_modules/2_modules/modules_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..538b0556c8606388f921958faf496b937f94cd0d --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/2_modules/modules_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + An interface declaration declares an interface type. + An InterfaceDeclaration introduces a named type (section 3.7) in the containing declaration space. + The BindingIdentifier of an interface declaration may not be one of the predefined type names (section 3.8.1). + module: ESNext + isCurrent: true + ---*/ + + +import { message } from '../1_programs_and_source_files/source_1.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(message(),'string'); + + diff --git a/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_1.ts b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d225817d0b8c10508443ba1739e3074bce24bbe6 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Import declarations are used to import entities from other modules and provide bindings for them in the current module. + module: ESNext + isCurrent: true + ---*/ + + +import * as m from '../1_programs_and_source_files/source_2.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(m.x,0); +equal(m.y,1); +equal(m.z,2); diff --git a/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_2.ts b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..58ad2fd429f244911e518bf4d250c182e97864b6 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_2.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + imports the module with the given name and creates a local binding for the module itself. The local binding is + classified as a value (representing the module instance) and a namespace (representing a container of types and + namespaces). + module: ESNext + isCurrent: true + ---*/ + + +import { x, y, z} from '../1_programs_and_source_files/source_2.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(x,0); +equal(y,1); +equal(z,2); diff --git a/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_3.ts b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..377da85c230521eae0f173bae8313cfabc7a50ea --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_3.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + imports a given module and creates local bindings for a specified list of exported members of the module. The specified + names must each reference an entity in the export member set (11.3.4.4) of the given module. The local bindings have t + he same names and classifications as the entities they represent unless as clauses are used to that specify different + local names. + module: ESNext + isCurrent: true + ---*/ + + +import { x as a, y as b, z as c} from '../1_programs_and_source_files/source_2.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(a,0); +equal(b,1); +equal(c,2); diff --git a/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_4.ts b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..1bb9aa967161050f913aae76dd666baa66a17fc7 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/3_import_declarations/import_declarations_4.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + imports a given module and creates local bindings for a specified list of exported members of the module. The specified + names must each reference an entity in the export member set (11.3.4.4) of the given module. The local bindings have t + he same names and classifications as the entities they represent unless as clauses are used to that specify different + local names. + module: ESNext + isCurrent: true + ---*/ + + +import x from '../1_programs_and_source_files/source_3.js'; +import { default as a} from '../1_programs_and_source_files/source_3.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(x,0); +equal(a,0); diff --git a/test_framework/test/spec/scripts_and_modules/4_import_require_declarations/import_require_declarations_1.ts b/test_framework/test/spec/scripts_and_modules/4_import_require_declarations/import_require_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f981591124ed2c1e1e1f9af51b9c7419613a9cc --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/4_import_require_declarations/import_require_declarations_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Import require declarations exist for backward compatibility with earlier versions of TypeScript. + An import require declaration of the form + module: ESNext + isCurrent: true + ---*/ + + +import * as m from "../1_programs_and_source_files/source_2" +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(m.x, 0); +equal(m.y, 1); +equal(m.z, 2); diff --git a/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_1.ts b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7d23e933c32e6ca26a4330bf919ca4d0afff40de --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_1.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import message from '../1_programs_and_source_files/source_4.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(message(),'string'); + diff --git a/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_2.ts b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..373b4a5b9807f12f219e81e7e40c0624bae33da4 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_2.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import message from '../1_programs_and_source_files/source_5.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(message(),'string'); + diff --git a/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_3.ts b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..167fbe9300fd74ffce74314a9eb5d5446fdb174f --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_3.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import message from '../1_programs_and_source_files/source_6.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +equal(message(),'string'); + diff --git a/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_4.ts b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..f1ad3a57d11a23f3f0e47e2157068511fefc24df --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_4.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import message from '../1_programs_and_source_files/source_7.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(message(),'string'); + diff --git a/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_5.ts b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..b6c1012f2374a5574462e9943f466e3886d54491 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/5_export_declarations/export_default_declarations_5.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export default declarations provide short-hand syntax for exporting an entity named default. + module: ESNext + isCurrent: true + ---*/ + + +import message from '../1_programs_and_source_files/source_8.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +equal(message,'hello'); + diff --git a/test_framework/test/spec/scripts_and_modules/6_export_assignments/export_assignments_1.ts b/test_framework/test/spec/scripts_and_modules/6_export_assignments/export_assignments_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..baec7385ba4186467fcad70a6841824593b2d997 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/6_export_assignments/export_assignments_1.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Export assignments exist for backward compatibility with earlier versions of TypeScript. An export assignment + designates a module member as the entity to be exported in place of the module itself. + module: ESNext + isCurrent: true + ---*/ + + +import Pt from '../1_programs_and_source_files/source_9'; +class AssertionError extends Error { + constructor(public msg: string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + +let point = new Pt(10, 20); +equal(point.x, 10); +equal(point.y, 20); + diff --git a/test_framework/test/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_1.ts b/test_framework/test/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..c14d2b2734dc42af934b1ccff7280562d33af012 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_1.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A variable declaration and 'require' call is emitted for a particular imported module only if the imported module, or + a local alias that references the imported module, is referenced as a PrimaryExpression somewhere in the body of the + importing module. If an imported module is referenced only as a NamespaceName or TypeQueryExpression,nothing is emitted. + module: ESNext + isCurrent: true + ---*/ + + +import * as g from '../1_programs_and_source_files/source_10.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +let p = g.point(10, 20); +equal(p.x,10); +equal(p.y,20); diff --git a/test_framework/test/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_2.ts b/test_framework/test/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..abb652172c7f541092a920bb2e654f32328b9779 --- /dev/null +++ b/test_framework/test/spec/scripts_and_modules/7_commonJS_modules/commonJS_modules_2.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A variable declaration and 'require' call is emitted for a particular imported module only if the imported module, or + a local alias that references the imported module, is referenced as a PrimaryExpression somewhere in the body of the + importing module. If an imported module is referenced only as a NamespaceName or TypeQueryExpression,nothing is emitted. + module: ESNext + isCurrent: true + ---*/ + + +import * as g from '../1_programs_and_source_files/source_10.js'; +class AssertionError extends Error { + constructor(public msg :string) { + super(); + this.msg = ""; + this.msg = msg; + } +} + +function defaultMessage(actual: any, expect: any, flag: boolean = true) { + if (flag == true) { + return "expected '" + expect + "' ,but was '" + actual + "'."; + } else { + return "expected not '" + expect + "' ,but was '" + actual + "'."; + } + +} + +function equal(actual: any, expect: any, msg?: string) { + if (actual != expect) { + throw new AssertionError(msg ? msg : defaultMessage(actual, expect)); + } +} + + +let p: g.Point = { x: 10, y: 20 }; +equal(p.x,10); +equal(p.y,20); diff --git a/test_framework/test/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_1.ts b/test_framework/test/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ae4509aa285e1d5651a45a9c55434080bf4bb67 --- /dev/null +++ b/test_framework/test/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_1.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Expressions controlling 'if' statements can be of any type (and not just type Boolean). +---*/ + + +let a = true; + +if (a) { + a = false; +} + +Assert.isFalse(a); + +let b = "string b"; +if (b) { + b = "string b 2"; +} +Assert.equal("string b 2", b); + +let c = 1; +if (c) { + c = 2 +} +Assert.equal(2, c); + +var myObject = { + d: 4 +} +if (myObject) { + myObject.d = 5; +} +Assert.equal(5, myObject.d); + +let myObject2 = 1; +if (myObject2 == 2) { + myObject2 = 3; +} +Assert.equal(1, myObject2); \ No newline at end of file diff --git a/test_framework/test/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_2.ts b/test_framework/test/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4da4dbcd0a50b300d5417fd15c87fcca44b92054 --- /dev/null +++ b/test_framework/test/spec/statements/If_do_and_while_Statements/if_do_and_while_Statements_2.ts @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Expressions controlling 'Do' statements can be of any type (and not just type Boolean). +---*/ + + +var num = 1; +do { + if (num % 5 == 0) { + + break; + } + num++; +} while (num); +Assert.equal(5, num); + +do { + num++; +} while (num < 100); + +Assert.equal(100, num); + + +var myObject = { + d: 1, +} +do { + if (myObject.d % 5 == 0) { + + break; + } + myObject.d++; +} while (myObject); +Assert.equal(5, myObject.d); \ No newline at end of file diff --git a/test_framework/test/spec/statements/blocks/blocks_1.ts b/test_framework/test/spec/statements/blocks/blocks_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..9a7578eedfda9b42f8afef43a17396f0929e6731 --- /dev/null +++ b/test_framework/test/spec/statements/blocks/blocks_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Blocks are extended to include, local interface +---*/ + + +interface BlocksInterface { + name: string; +} +class MyBlocks implements BlocksInterface { + name: string; + + constructor(name: string) { + this.name = name; + } + + public getName() { + return this.name; + } + +} +var myBlocks = new MyBlocks("Open Harmony"); + +Assert.equal("Open Harmony", myBlocks.getName()) \ No newline at end of file diff --git a/test_framework/test/spec/statements/blocks/blocks_2.ts b/test_framework/test/spec/statements/blocks/blocks_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..7321221741154d8196894f61d81185bbcb466f66 --- /dev/null +++ b/test_framework/test/spec/statements/blocks/blocks_2.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Blocks are extended to include, type alias +---*/ + + +type Parent = { + name: string; +}; + +type Child = Parent & { + age: number +}; + + +const child: Child = { name: "name", age: 10 }; + +Assert.equal("name", child.name) +Assert.equal(10, child.age) \ No newline at end of file diff --git a/test_framework/test/spec/statements/blocks/blocks_3.ts b/test_framework/test/spec/statements/blocks/blocks_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b4fe0113f399c41a1657ecd40d587994a4ddafb9 --- /dev/null +++ b/test_framework/test/spec/statements/blocks/blocks_3.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Blocks are extended to include, enum +---*/ + + +enum Direction { + Forward, + Back, + Left, + Right, +} + +class Car { + name!: string; + direction!: Direction; +} + +const car = new Car(); +car.name = "myCar"; +car.direction = Direction.Forward; + +Assert.equal("myCar", car.name) +Assert.equal(Direction.Forward, car.direction) \ No newline at end of file diff --git a/test_framework/test/spec/statements/break_statements/break_statements.ts b/test_framework/test/spec/statements/break_statements/break_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..ed02bdbe6ca07525a231bcfbde33004ebd7f46eb --- /dev/null +++ b/test_framework/test/spec/statements/break_statements/break_statements.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: A 'break' statement is required to be nested, directly or indirectly (but not crossing function boundaries), within an iteration ('do', 'while', 'for', or 'for-in') or 'switch' statement. +---*/ + + +let num: number = 0; +let count: number = 0; +do { + num++; + if (num >= 5) { + break; + } +} while (num < 10); +Assert.equal(5, num); + + +num = 0; +for (num = 0; num <= 20; num++) { + if (num >= 5) { + break; + } +} +Assert.equal(5, num); + +let arr = [0, 1, 2, 3, 4, 5]; +for (let index in arr) { + if (arr[index] >= 2) { + break; + } + count++; +} +Assert.equal(2, count); \ No newline at end of file diff --git a/test_framework/test/spec/statements/continue_statements/continue_statements.ts b/test_framework/test/spec/statements/continue_statements/continue_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..8d7648c7f29824ec391d5c06469fcb41b538c54b --- /dev/null +++ b/test_framework/test/spec/statements/continue_statements/continue_statements.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: A 'continue' statement is required to be nested, directly or indirectly (but not crossing function boundaries), within an iteration ('do', 'while', 'for', or 'for-in') statement. +---*/ + + +let num: number = 0; +let count: number = 0; +do { + num++; + if (num % 2 == 0) { + continue; + } + count++; +} while (num < 10); +Assert.equal(5, count); + + +num = 0; +count = 0; +for (num = 0; num <= 20; num++) { + if (num % 2 == 0) { + continue; + } + count++; +} +Assert.equal(10, count); + +count = 0; +let arr = [0, 1, 2, 3, 4, 5]; +for (let index in arr) { + if (arr[index] % 2 == 0) { + continue; + } + count++; +} +Assert.equal(3, count); \ No newline at end of file diff --git a/test_framework/test/spec/statements/for_in_statements/for_in_statements.ts b/test_framework/test/spec/statements/for_in_statements/for_in_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..6c92d8868fa3866743babd055a4de30fa481430d --- /dev/null +++ b/test_framework/test/spec/statements/for_in_statements/for_in_statements.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: test for (v in expr) statement +---*/ + + +let arr = [4, 5, 6]; + +let i = 0; +for (let index in arr) { + i++; +} +Assert.equal(3, i); + +const person = { + name: "opharmony", + role: "tools", + age: 3, +}; + +i = 0; +for (const key in person) { + i++; +} +Assert.equal(3, i); + +interface ABC { + a: number + b: string +} + +const x: ABC = { + a: 1, + b: '2' +} + +i = 0; +for (let key in x) { + i++; +} +Assert.equal(2, i); + +i = 0; +for (var key in x) { + i++; +} +Assert.equal(2, i); \ No newline at end of file diff --git a/test_framework/test/spec/statements/for_of_statements/for_of_statements.ts b/test_framework/test/spec/statements/for_of_statements/for_of_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..aaeb79193b0ec98b1bbf0f2956ca6d236696d4db --- /dev/null +++ b/test_framework/test/spec/statements/for_of_statements/for_of_statements.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: test for (v of expr) statement +---*/ + + +let count = 0; +for (let word of ["one", "two", "three"]) { + count++; +} +Assert.equal(3, count); + +count = 0; +let s = [0, 1, 2, 3, 4]; +for (let value of s) { + count++; +} +Assert.equal(5, count); + +count = 0; +let blogName: string = "openHarmony"; +for (let character of blogName) { + count++; +} +Assert.equal(11, count); \ No newline at end of file diff --git a/test_framework/test/spec/statements/for_statements/for_statements.ts b/test_framework/test/spec/statements/for_statements/for_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..87d81655f89362c1a0dd7f3704149d8330deca33 --- /dev/null +++ b/test_framework/test/spec/statements/for_statements/for_statements.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Variable declarations in 'for' statements are extended in the same manner as variable declarations in variable statements +---*/ + + +let result = 0; +for (let i = 0; i < 10; i++) { + result++; +} + +Assert.equal(10, result); \ No newline at end of file diff --git a/test_framework/test/spec/statements/let_and_const_declarations/let_and_const_declarations.ts b/test_framework/test/spec/statements/let_and_const_declarations/let_and_const_declarations.ts new file mode 100644 index 0000000000000000000000000000000000000000..0456db5755f8231f1ef50fac0ab5be9b6d66990d --- /dev/null +++ b/test_framework/test/spec/statements/let_and_const_declarations/let_and_const_declarations.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Let and Const Declarations +---*/ + + +let a = 1; +Assert.equal(1, a); + +a = 2; +Assert.equal(2, a); + +const num = 9; +Assert.equal(9, num); + +const myname = 'openharmony'; +Assert.equal('openharmony', myname); + +const myBoolean = true; +Assert.isTrue(myBoolean); diff --git a/test_framework/test/spec/statements/return_statements/return_statements.ts b/test_framework/test/spec/statements/return_statements/return_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..88d2207cb23e70e1d6d6e9e1ae849cf329875ac6 --- /dev/null +++ b/test_framework/test/spec/statements/return_statements/return_statements.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Return Statements +---*/ + + +function f(): (x: string) => number { + return s => s.length; +} + +Assert.equal(11, f()("openharmony")); + + + +function testReruen() { + for (let i = 0; i < 10; i++) { + + if (i == 5) { + return i; + } + } +} +Assert.equal(5, testReruen()); diff --git a/test_framework/test/spec/statements/switch_statements/switch_statements.ts b/test_framework/test/spec/statements/switch_statements/switch_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..8486833f09e733ee5e3161c9846c1399bbcadbc0 --- /dev/null +++ b/test_framework/test/spec/statements/switch_statements/switch_statements.ts @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: In a 'switch' statement, each 'case' expression must be of a type that is assignable to or from (section 3.11.4) the type of the 'switch' expression +---*/ + + +function testSwitch(inputVar: number) { + let outputVar: string; + switch (inputVar) { + case 1: + outputVar = 'a'; + break; + case 2: + outputVar = 'b'; + break; + case 3: + outputVar = 'c'; + break; + case 4: + outputVar = 'd'; + break; + default: + outputVar = 'e'; + break; + } + return outputVar; +} + +Assert.equal("a", testSwitch(1)); +Assert.equal("b", testSwitch(2)); +Assert.equal("c", testSwitch(3)); +Assert.equal("d", testSwitch(4)); +Assert.equal("e", testSwitch(5)); + + + +function testSwitchExpression(x: number, y: number) { + let outputVar: string; + switch (x + y) { + case 0: + outputVar = 'a'; + break; + case 5: + outputVar = 'b'; + break; + case 10: + outputVar = 'c'; + break; + + default: + outputVar = 'd'; + } + return outputVar; +} + +Assert.equal("a", testSwitchExpression(-1, 1)); +Assert.equal("b", testSwitchExpression(1, 4)); +Assert.equal("c", testSwitchExpression(5, 5)); +Assert.equal("d", testSwitchExpression(8, 10)); \ No newline at end of file diff --git a/test_framework/test/spec/statements/throw_statements/throw_statements.ts b/test_framework/test/spec/statements/throw_statements/throw_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..e48c21761bb30700b4d426347ef07cf03cfb2fc8 --- /dev/null +++ b/test_framework/test/spec/statements/throw_statements/throw_statements.ts @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: The expression specified in a 'throw' statement can be of any type. +---*/ + + +function testThrow(inputVar: number) { + let outputVar: string; + if (inputVar == 1) { + throw 100; + } + + if (inputVar == 2) { + throw 'a'; + } + + if (inputVar == 3) { + throw true; + } + + if (inputVar == 4) { + throw new Error('Something bad happened'); + } +} + +try { + testThrow(1); +} catch (error) { + Assert.equal(100, error); +} + +try { + testThrow(2); +} catch (error) { + Assert.equal('a', error); +} + +try { + testThrow(3); +} catch (error) { + Assert.isTrue(error); +} + +try { + testThrow(4); +} catch (error: any) { + Assert.equal('Something bad happened', error.message); +} + +try { + testThrow(4); +} catch (error) { + Assert.isTrue(error instanceof Error); +} \ No newline at end of file diff --git a/test_framework/test/spec/statements/try_statements/try_statements.ts b/test_framework/test/spec/statements/try_statements/try_statements.ts new file mode 100644 index 0000000000000000000000000000000000000000..dd324f72e89f222fbc734697a40cdac18ef6a3d0 --- /dev/null +++ b/test_framework/test/spec/statements/try_statements/try_statements.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: The variable introduced by a 'catch' clause of a 'try' statement is always of type Any. It is not possible to include a type annotation in a 'catch' clause. +---*/ + + +try { + new Array(100000000000000000000); +} +catch (err) { + Assert.isTrue(err instanceof RangeError); +} + +try { + let a: any; + let b = a.name; +} +catch (err) { + Assert.isTrue(err instanceof TypeError); +} + +try { + decodeURI('%'); +} +catch (err) { + Assert.isTrue(err instanceof URIError); +} \ No newline at end of file diff --git a/test_framework/test/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_1.ts b/test_framework/test/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..27b8ff95848d77a9416dd0e9f0535c90d291463c --- /dev/null +++ b/test_framework/test/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_1.ts @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The type T of a variable introduced by a simple variable declaration is determined as follows: + If the declaration includes a type annotation, T is that type. + Otherwise, if the declaration includes an initializer expression, T is the widened form (section 3.12) of the type of the initializer expression. + Otherwise, T is the Any type. +---*/ + + +// any +var a; + +// number +var b: number; + +// number +var c = 30; + +// { x: number; y: string; } +var d = { x: 40, y: "hello" }; + +// any +var e: any = "test"; +a = 1; +Assert.equal(1, a); + +a = '111'; +Assert.equal('111', a); + +b = 20; +Assert.equal(20, b); + +Assert.equal(30, c); + +Assert.equal(40, d.x); +Assert.equal("hello", d.y); + +var x = 50; +Assert.equal(50, x); +var x: number; +Assert.equal(50, x); +if (x == 50) { + var x = 100; + Assert.equal(100, x); + + x = 200; + + Assert.equal(200, x); +} + +Assert.equal(200, x); \ No newline at end of file diff --git a/test_framework/test/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_2.ts b/test_framework/test/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9f57f9989e6e089fa5eef693cccf38d1a6590a65 --- /dev/null +++ b/test_framework/test/spec/statements/variable_statements/1_simple_variable_declarations/simple_variable_declarations_2.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type T of a variable introduced by a simple variable declaration is determined as follows: + If the declaration includes a type annotation, T is that type. + Otherwise, if the declaration includes an initializer expression, T is the widened form (section 3.12) of the type of the initializer expression. + Otherwise, T is the Any type. + ---*/ + + +interface Point { x: number; y: number; } + +var a = { x: 0, y: 1 }; +var b: Point = { x: 10, y: 11 }; +var c = { x: 100, y: 111 }; +var d: { x: number; y: number; } = { x: 1000, y: 1001 }; +var e = <{ x: number; y: number; }>{ x: 10000, y: 10001 }; + +Assert.equal(0, a.x); +Assert.equal(1, a.y); + +Assert.equal(10, b.x); +Assert.equal(11, b.y); + +Assert.equal(100, c.x); +Assert.equal(111, c.y); + +Assert.equal(1000, d.x); +Assert.equal(1001, d.y); + +Assert.equal(10000, e.x); +Assert.equal(10001, e.y); \ No newline at end of file diff --git a/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_1.ts b/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..dce867167ae02363e87a1a835187882eaa175e61 --- /dev/null +++ b/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: A destructuring variable declaration introduces zero or more named variables and initializes them with values extracted from properties of an object +---*/ + + +var object_name = { + key1: "value1", + key2: "value2", + key3: ["content1", "content2"], + key4: true, + key5: undefined +} + +var { key1, key2: y, key3, key4: z = false, key5: k = true } = object_name; + +Assert.equal("value1", key1); +Assert.equal("value2", y); +Assert.equal("content1", key3[0]); +Assert.equal("content2", key3[1]); +Assert.isTrue(z); +Assert.isTrue(k); + + diff --git a/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_2.ts b/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3d94351d2c8322dcde7ec2b2cc4b4b438ce4ff6c --- /dev/null +++ b/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_2.ts @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The type T associated with a destructuring variable declaration is determined as follows: + If the declaration includes a type annotation, T is that type. + Otherwise, if the declaration includes an initializer expression, T is the type of that initializer expression. + Otherwise, T is the Any type. +---*/ + + +interface IPerson { + myname: string, + myage: number, +} + +var customer: IPerson = { + myname: "Tom", + myage: 10, +} +let { myname, myage } = customer; + +Assert.isString(myname); +Assert.isNumber(myage) + + +let o = { + a: "foo", + b: 12, + c: "bar" +}; +let { a: newName1 } = o; +Assert.isString(newName1); +var ohArray: number[] = [10, 20, 30]; + +var [x, y, z = 10, k = 10] = ohArray; +Assert.equal(10, x); +Assert.equal(20, y); +Assert.equal(30, z); +Assert.equal(10, k); + diff --git a/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_3.ts b/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..538113828acaceb3a8cbe8a77079e0c4905750f0 --- /dev/null +++ b/test_framework/test/spec/statements/variable_statements/2_destructuring_variable_declarations/destructuring_variable_declarations_3.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The type T associated with a binding property is determined as follows: + Let S be the type associated with the immediately containing destructuring variable declaration, binding property, or binding element. + If S is the Any type: + If the binding property specifies an initializer expression, T is the type of that initializer expression. + Otherwise, T is the Any type. + Let P be the property name specified in the binding property. + If S has an apparent property with the name P, T is the type of that property. + Otherwise, if S has a numeric index signature and P is a numerical name, T is the type of the numeric index signature. + Otherwise, if S has a string index signature, T is the type of the string index signature. + Otherwise, no type is associated with the binding property and an error occurs. +---*/ + + +var object_name = { + key1: "value1", + key2: "value2", + key3: ["content1", "content2"], + key4: true, + key5: undefined +} + + +var ohArray: number[] = [10, 20, 30]; + +var { key1, key3: [y, z = 10, k = 10] = ohArray, key5: [a, b] = ohArray } = object_name; + +Assert.equal("value1", key1); +Assert.equal("content1", y); +Assert.equal("content2", z); +Assert.equal(10, k); +Assert.equal(10, a); +Assert.equal(20, b); \ No newline at end of file diff --git a/test_framework/test/spec/statements/variable_statements/3_implied_type/implied_type.ts b/test_framework/test/spec/statements/variable_statements/3_implied_type/implied_type.ts new file mode 100644 index 0000000000000000000000000000000000000000..2a9f74c1bf5f8fc556aeddeb5b5a83eafd4d7e39 --- /dev/null +++ b/test_framework/test/spec/statements/variable_statements/3_implied_type/implied_type.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: A variable, parameter, binding property, or binding element declaration that specifies a binding pattern has an implied type which is determined +---*/ + + +var object_name = { + key1: "value1" +} + +// If the declaration specifies an object binding pattern, the implied type is an object type with a set of properties corresponding to the specified binding property declarations. The type of each property is the type implied by its binding property declaration, and a property is optional when its binding property declaration specifies an initializer expression. +function f({ aa = {}, b = "hello", c = 3, d = object_name }) { + Assert.equal("object", typeof (aa)); + Assert.equal("string", typeof (b)); + Assert.equal("number", typeof (c)); + Assert.equal("object", typeof (d)); +} + +var objectFun = { + a: [1, 2], + b: "2", + c: 3, + d: object_name +}; +f(objectFun); + + +// If the declaration specifies an array binding pattern without a rest element, the implied type is a tuple type with elements corresponding to the specified binding element declarations. The type of each element is the type implied by its binding element declaration. +var [a1, b1, c1, d1] = [1, "hello", true, object_name]; + +Assert.equal("number", typeof (a1)); +Assert.equal("string", typeof (b1)); +Assert.equal("boolean", typeof (c1)); +Assert.equal("object", typeof (d1)); + +// If the declaration specifies an array binding pattern with a rest element, the implied type is an array type with an element type of Any +function testRest(...restElements: any[]): any { + Assert.isTrue(restElements.length > 0); + return restElements[0]; +} + +Assert.isString(testRest("str", "str2")); +Assert.isNumber(testRest(1, 2)); \ No newline at end of file diff --git a/test_framework/test/spec/types/Intersection_Types/Intersection_types_1.ts b/test_framework/test/spec/types/Intersection_Types/Intersection_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..3f890efff80a5a1bacf6ca57be262ff0669cc223 --- /dev/null +++ b/test_framework/test/spec/types/Intersection_Types/Intersection_types_1.ts @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Intersection types represent values that simultaneously have multiple types. + A value of an intersection type A & B is a value that is both of type A and type B. + Intersection types are written using intersection type literals. + ---*/ + + +interface A { + a: number; +} +interface B { + b: string; +} +var ab: A & B = { a: 1, b: "b" }; +Assert.equal(ab.a, 1); +Assert.equal(ab.b, "b"); +enum Color { + Red1 = 1, + Green1, + Blue1, +} +interface A2 { + a2: [string, number]; +} +interface B2 { + b2: Color; +} +var ab2: A2 & B2 = { a2: ["a2", 1], b2: Color.Red1 }; +Assert.equal(ab2.a2[0], "a2"); +Assert.equal(ab2.b2, 1); +interface A3 { + a3: number[]; +} +interface B3 { + b3: boolean; +} +var ab3: A3 & B3 = { a3: [1, 2, 3], b3: true }; +Assert.equal(ab3.b3, true); +interface A4 { + a4: number; +} +interface B4 { + b4: string; +} +interface C4 { + c: any; +} +var ab4: A4 & B4 & C4 = { a4: 1, b4: "b4", c: 3 }; +Assert.equal(ab4.a4, 1); +Assert.equal(ab4.b4, "b4"); +Assert.equal(ab4.c, 3); +interface X { + p: A; +} +interface Y { + p: B; +} +var xy: X & Y = { p: ab }; +Assert.equal(xy.p.a, 1); +type F1 = (a: string, b: string) => void; +type F2 = (a: number, b: number) => void; +var f: F1 & F2 = (a: string | number, b: string | number) => { }; +f("hello", "world"); +f(1, 2); +type F3 = (a: any, b: boolean) => void; +var f2: F1 & F2 & F3 = ( + a: string | number | any, + b: string | number | boolean +) => { }; \ No newline at end of file diff --git a/test_framework/test/spec/types/Intersection_Types/intersection_types_2.ts b/test_framework/test/spec/types/Intersection_Types/intersection_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3acc8145ed2df910554e9149c2d4532c69fe3d21 --- /dev/null +++ b/test_framework/test/spec/types/Intersection_Types/intersection_types_2.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The union and intersection type operators can be applied to type parameters. + ---*/ + + +function getSmallPet(name: string | number) { + return name; +} +let pet = getSmallPet("fishbird"); +Assert.equal(pet, "fishbird"); +interface Person1 { name: string } +interface People1 { sex: string } +type PersonMan = Person1 & People1 +function getPerson(person: PersonMan) { + return person.name; +} +let man: PersonMan = { + name: "join", + sex: "man" +} +let getpersonname = getPerson(man); +Assert.equal(getpersonname, "join"); + diff --git a/test_framework/test/spec/types/named_types/named_types_1.ts b/test_framework/test/spec/types/named_types/named_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4ae28f5f5b048b6fe3f9e365b8f6177d6ec1dc8b --- /dev/null +++ b/test_framework/test/spec/types/named_types/named_types_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Classes, enums, and type aliases are named types +---*/ + + +class Test { + name: string; + constructor(name: string) { + this.name = name; + } +} +// create instances of implementations of named types +const tt = new Test("caihua"); +Assert.equal(tt.name, "caihua"); + +enum Color { + Red, + Green, + Blue, +} + +let a: Color.Red = Color.Red; +Assert.equal(a, Color.Red); + +type pp = number | string; +let b: pp; +b = 10; +Assert.isNumber(b); +b = "hello"; +Assert.isString(b); diff --git a/test_framework/test/spec/types/named_types/named_types_2.ts b/test_framework/test/spec/types/named_types/named_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9e22d105a04f62b2beccb3db97846d603c140e8f --- /dev/null +++ b/test_framework/test/spec/types/named_types/named_types_2.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + class declarations with only public members introduce named types + that function exactly like those created by interface declarations. +---*/ + + +interface TestInterface { + age: number; +} +class TestClass { + age: number; + constructor(age:number){ + this.age=age + } +} +function test1(v: TestInterface) { + return v.age; +} +function test2(v: TestClass) { + return v.age; +} +let ee = { + age: 18, +}; + +Assert.equal(test1(ee), 18); +Assert.equal(test2(ee), 18); + diff --git a/test_framework/test/spec/types/named_types/named_types_3.ts b/test_framework/test/spec/types/named_types/named_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..063a67ac85cbdc7cdc8a608c6982289948d74f50 --- /dev/null +++ b/test_framework/test/spec/types/named_types/named_types_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Generic types are "templates" from which multiple actual types can be created by writing type references + that supply type arguments to substitute in place of the generic type's type parameters +---*/ + + +interface Person { + m_name: string; + m_age: number; + getName(name: string): string; +} + +type Optional = { [P in keyof T]: T[P] }; +let cc: Optional = { + m_age: 18, + m_name: "caihua", + getName(name: string) { + return name; + }, +}; +Assert.equal(cc.m_age, 18); +Assert.equal(cc.m_name, "caihua"); +Assert.equal(cc.getName("caihua"), "caihua"); diff --git a/test_framework/test/spec/types/named_types/named_types_4.ts b/test_framework/test/spec/types/named_types/named_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..31f4c4a1e1d939b390483665ebefd0d9b3c3bdbe --- /dev/null +++ b/test_framework/test/spec/types/named_types/named_types_4.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + TypeScript has a structural type system, and therefore an instantiation of a generic type + is indistinguishable from an equivalent manually written expansion. +---*/ + + +class Entity { + x: number; + y: number; + constructor(x:number,y:number){ + this.x = x; + this.y = y; + } +} +interface Pair { + first: T1; + second: T2; +} + +function test(v: Pair) { + Assert.equal(v.first, "abc"); + Assert.equal(v.second.x, 1); + Assert.equal(v.second.y, 1); +} +// object literal +test({ first: "abc", second: { x: 1, y: 1 } }); +let cc: Pair; +cc = { + first: "abc", + second: { + x: 1, + y: 1, + }, +}; +// type reference +test(cc); diff --git a/test_framework/test/spec/types/object_types/array_types/array_types_1.ts b/test_framework/test/spec/types/object_types/array_types/array_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d15e098344f9b5c6f62989e7f9fc1e457c7d2f62 --- /dev/null +++ b/test_framework/test/spec/types/object_types/array_types/array_types_1.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + array types represent JavaScript arrays with a common element type. + array types are named type references created from the generic interface type 'Array' in the global namespace with the array element type as a type argument. + ---*/ + + +var arr1: Array = ["ABC", "DEF", "GHI"]; +var arr2: Array = [1, 3, 5, 7, 9]; +var arr3: Array = [true, false]; +var arr4: Array = [Object, { 0xff: "0xFF" }]; +Assert.isString(arr1[2]); +var objArray: object[] = []; +arr1.forEach(function (element, index, arr) { + objArray[index] = { index: index, element: element, arr: arr }; +}); +Assert.isString(arr2.toString()); +Assert.equal(arr2.toString(), "1,3,5,7,9"); +Assert.isNumber(arr3.length); +Assert.equal(arr3.length, 2); +Assert.isNumber(arr2.pop()); +Assert.equal(arr2.pop(), 7); +arr2.push(15); +Assert.equal(arr2.toString(), "1,3,5,15"); +arr3[0] = false; +Assert.equal(arr3[0], false); diff --git a/test_framework/test/spec/types/object_types/array_types/array_types_2.ts b/test_framework/test/spec/types/object_types/array_types/array_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d915279f467ba8aff610f349ee3374546479eb66 --- /dev/null +++ b/test_framework/test/spec/types/object_types/array_types/array_types_2.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the declaration of the 'Array' interface includes a property 'length' and a numeric index signature for the element type, along with other members + ---*/ + + +var arr: Array = ["A", "B", "C", "D", "E", "F", "G", "H", "I"]; +Assert.isString(arr[5]); +Assert.equal(arr[5], "F"); +Assert.isNumber(arr.length); +Assert.equal(arr.length, 9); diff --git a/test_framework/test/spec/types/object_types/array_types/array_types_3.ts b/test_framework/test/spec/types/object_types/array_types/array_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1389e5d597abdd88b0a42b1facfd8bf3a2d2c928 --- /dev/null +++ b/test_framework/test/spec/types/object_types/array_types/array_types_3.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + array literals may be used to create values of array types. + ---*/ + + +var arr6: number[] = [1, 3, 5, 7, 9]; +var arr7: boolean[] = [true, false, true, false, true]; +var arr8: string[] = ["a", "b", "c", "d", "e", "f"]; +var arr9: object[] = [{ 0x00: "0x00" }, { 0x01: "0x01" }]; +arr6[3] = 14; +Assert.equal(arr6[3], 14); +Assert.isBoolean(arr7[2]); +Assert.equal(true, arr7[2]); +Assert.equal(arr8.toString(), "a,b,c,d,e,f"); +arr8.pop(); +Assert.equal(arr8.toString(), "a,b,c,d,e"); +arr8.push("0x02"); +Assert.equal(arr8.toString(), "a,b,c,d,e,0x02"); diff --git a/test_framework/test/spec/types/object_types/constructor_types/constructor_types_1.ts b/test_framework/test/spec/types/object_types/constructor_types/constructor_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..48b250294005137c9c0aa247fb6cffef6014b7b1 --- /dev/null +++ b/test_framework/test/spec/types/object_types/constructor_types/constructor_types_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + An object type containing one or more construct signatures is said to be a constructor type. + ---*/ + + +class Person { + name: string; + age: number; + constructor(n: string, age: number) { + this.name = n; + this.age = age; + } + run(): string { + return "person"; + } +} +class Teacher extends Person { + run(): string { + return "teacher"; + } +} +// alias +type constructor = new (name: string, age: number) => T; + +const testClass1: constructor = Teacher; +let testObj1: Person = new testClass1("caihua", 20); +Assert.equal(testObj1.age, 20); +Assert.equal(testObj1.name, "caihua"); +Assert.equal(testObj1.run(), "teacher"); diff --git a/test_framework/test/spec/types/object_types/constructor_types/constructor_types_2.ts b/test_framework/test/spec/types/object_types/constructor_types/constructor_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b46fd46483c044a9bea6b862776b23c57b68593a --- /dev/null +++ b/test_framework/test/spec/types/object_types/constructor_types/constructor_types_2.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Constructor types may be written using constructor type literals + or by including construct signatures in object type literals. + ---*/ + + +class Person { + name: string; + age: number; + constructor(n: string, age: number) { + this.name = n; + this.age = age; + } + run(): string { + return "person"; + } +} +class Student extends Person { + run(): string { + return "student"; + } +} +class Teacher extends Person { + run(): string { + return "teacher"; + } +} +// literal type +const testClass1: new (name: string, age: number) => Person = Student; +const testObj1: Person = new testClass1("caihua1", 12); +Assert.equal(testObj1.age, 12); +Assert.equal(testObj1.name, "caihua1"); +const testClass2: { new(n: string, a: number): Person } = Teacher; +const testObj2: Person = new testClass2("caihua2", 120); +Assert.equal(testObj2.age, 120); +Assert.equal(testObj2.name, "caihua2"); diff --git a/test_framework/test/spec/types/object_types/constructor_types/constructor_types_3.ts b/test_framework/test/spec/types/object_types/constructor_types/constructor_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a5082faf169a6954f3917be0b93ff508313ac5a --- /dev/null +++ b/test_framework/test/spec/types/object_types/constructor_types/constructor_types_3.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + An object type containing one or more construct signatures is said to be a constructor type. + ---*/ + + +interface Point { + x: number; + y: number; +} +interface PointConstructor { + new(x: number, y: number): Point; +} + +class Point2D implements Point { + readonly x: number; + readonly y: number; + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } +} + +function newPoint( + pointConstructor: PointConstructor, + x: number, + y: number +): Point { + return new pointConstructor(x, y); +} + +const point1: Point = new Point2D(1, 2); +Assert.equal(point1.x, 1); +Assert.equal(point1.y, 2); + +const point: Point = newPoint(Point2D, 2, 2); +Assert.equal(point.x, 2); +Assert.equal(point.y, 2); diff --git a/test_framework/test/spec/types/object_types/function_types/function_types.ts b/test_framework/test/spec/types/object_types/function_types/function_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1b5ec72fdff3f1a0829e9db025c41db91ac48df --- /dev/null +++ b/test_framework/test/spec/types/object_types/function_types/function_types.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + an object type containing one or more call signatures is said to be a function type. + function types may be written using function type literals or by including call signatures in object type literals. + ---*/ + + +var fun1: (num1: number, num2: number) => number = ( + num1: number, + num2: number +) => { + return num1 + num2; +}; +Assert.isNumber(fun1(3, 5)); +Assert.equal(fun1(3, 5), 8); +var fun2: { (num1: number, num2: number, num3: number): number } = ( + num1: number, + num2: number, + num3: number +) => { + return num1 + num2 + num3; +}; +Assert.isNumber(fun2(1, 3, 5)); +Assert.equal(fun2(1, 3, 5), 9); diff --git a/test_framework/test/spec/types/object_types/tuple_types/tuple_types_1.ts b/test_framework/test/spec/types/object_types/tuple_types/tuple_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..4450477f48f886911a10cfffb7db712093cf610f --- /dev/null +++ b/test_framework/test/spec/types/object_types/tuple_types/tuple_types_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Tuple types represent JavaScript arrays with individually tracked element types. + A tuple type combines a set of numerically named properties with the members of an array type. + ---*/ + + +let cc: [number, string, boolean]; +cc = [12, "abcd", true]; +Assert.isNumber(cc[0]); +Assert.equal(cc[0].toString(), "12"); +Assert.isString(cc[1]); +Assert.equal(cc[1].length, 4); +Assert.isBoolean(cc[2]); +let dd = cc[2] ? 0 : 1; +Assert.equal(dd, 0); diff --git a/test_framework/test/spec/types/object_types/tuple_types/tuple_types_2.ts b/test_framework/test/spec/types/object_types/tuple_types/tuple_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..d8b1707ed40508fb7a806a40576141f59e90f685 --- /dev/null +++ b/test_framework/test/spec/types/object_types/tuple_types/tuple_types_2.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Array literals may be used to create values of tuple types. + the members of an array type whose element type is the union type of the tuple element types + ---*/ + + +let cc: [number, string, boolean]; +cc = [12, "abcd", true]; +let index: number=0; +// Type of cc[index] is number | string | boolean +var x = cc[index]; +x = 12; +Assert.isNumber(x); +x = false; +Assert.isBoolean(x); +x = "string"; +Assert.isString(x); diff --git a/test_framework/test/spec/types/object_types/tuple_types/tuple_types_3.ts b/test_framework/test/spec/types/object_types/tuple_types/tuple_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..433c23800737ec6954a9706bea0b64a3f9b94c20 --- /dev/null +++ b/test_framework/test/spec/types/object_types/tuple_types/tuple_types_3.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Named tuple types can be created by declaring interfaces that derive from Array + and introduce numerically named properties +---*/ + + +interface tt extends Array { + 0: K; + 1: V; +} +let x: tt = [10, "ten"]; +Assert.isNumber(x[0]); +Assert.equal(x[0], 10); +Assert.isString(x[1]); +Assert.equal(x[1], "ten"); diff --git a/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_1.ts b/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..5d53f003f951414e2db65283b3b1666ea775c8cb --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_1.ts @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Boolean primitive type corresponds to the similarly + named JavaScript primitive type and represents logical values that are either true or false. + ---*/ + + +function test1(): number { + return 0; +} +function test2(): number { + return 1; +} +function test3() { + return 1; +} +function test4(i: number): number { + return i; +} +var a: boolean = true; +Assert.equal(a, true); +var b = false; +Assert.equal(b, false); +// conditional expression +let c: number = a ? 0 : 1; +Assert.equal(c, 0); +c = b ? 0 : 1; +Assert.equal(c, 1); +// if +if (a) { + Assert.equal(test1(), 0); +} else { + Assert.equal(test2(), 1); +} +// while , do ...while +while (a) { + Assert.equal(test3(), 1); + break; +} +// for +for (let i = 0; a && i < 5; i++) { + Assert.equal(test4(i), i); +} diff --git a/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_2.ts b/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a1012df2d47cd85e45fab8e3e254175c1f7ba69 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_2.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The boolean keyword references the Boolean primitive + type and the true and false literals reference the two Boolean truth values. + ---*/ + + +var a: boolean = true; +Assert.isBoolean(a); +Assert.equal(a, true); +a = false; +Assert.isBoolean(a); +Assert.equal(a, false); +// > < ! +var b: boolean = 2 > 1; +Assert.isBoolean(b); +Assert.equal(b, true); +b = !b; +Assert.isBoolean(b); +Assert.equal(b, false); +// && +var c: boolean = 2 > 1 && 7 < 8; +Assert.isBoolean(c); +Assert.equal(c, true); +c = 2 < 1 && 7 < 8; +Assert.isBoolean(c); +Assert.equal(c, false); +// || +c = 2 > 1 || 7 < 8; +Assert.isBoolean(c); +Assert.equal(c, true); +c = 2 < 1 || 7 > 8; +Assert.isBoolean(c); +Assert.equal(c, false); + diff --git a/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_3.ts b/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..601a419b699bf7bbab056124d9d03b89886c8e1c --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_boolean_type/the_boolean_type_3.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Boolean primitive type behaves as an object + type with the same properties as the global interface type 'Boolean'. + ---*/ + + +var a = true; +Assert.equal(a.valueOf(), true); diff --git a/test_framework/test/spec/types/primitive_types/the_enum_type/the_enum_type_1.ts b/test_framework/test/spec/types/primitive_types/the_enum_type/the_enum_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..cc970af660846e0ff521cfb9f261b3c67170378a --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_enum_type/the_enum_type_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: Enum types are distinct user defined subtypes of the Number primitive type and vice versa + ---*/ + + +enum Color { + Red, + Green, + Blue, + Black, +} +let cc: Color = Color.Blue; +// true +Assert.isNumber(cc); +var ee: Color = Color.Blue; +var dd: number = cc; +Assert.equal(dd, Color.Blue); +dd = 15; +ee = dd; +Assert.equal(ee, 15); diff --git a/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_1.ts b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..762e7f69406733f105aa40e0296e162c92d6ff58 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_1.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The Null type corresponds to the similarly named JavaScript primitive type and is the type of the null literal. + ---*/ + + +var n: null = null; +let flag = false; +if (n == null) { + flag = true; +} +Assert.isTrue(flag); \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_1.ts b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7334327136cc01b7325eed99671abc9e267545a3 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_1.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The null literal references the one and only value of the Null type. + error: + code: TS2322 + message: Type '""' is not assignable to type 'null'. + ---*/ + + +var nString: null = ""; \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_2.ts b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..571ae73acd456e1258c1729c31fa88a73ca07934 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_2.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The null literal references the one and only value of the Null type. + error: + code: TS2322 + message: Type '3' is not assignable to type 'null'. + ---*/ + + +var nNumber: null = 3; \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_3.ts b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..4d6806f5a10ef07485cf7e714d5608614cd6b90f --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_2_3.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The null literal references the one and only value of the Null type. + error: + code: TS2322 + message: Type 'false' is not assignable to type 'null'. + ---*/ + + +var nBoolean: null = false; \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_3.ts b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..19e449bb4c5ec20042125ac6657ca0cb14e94ffd --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_3.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + It is not possible to directly reference the Null type itself. + error: + code: TS2304 + message: Cannot find name 'Null'. + ---*/ + +var directlyReference: Null; \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_4.ts b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..bfbfa2e2b315d93ceecf35cff9df06d2fd882751 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_null_type/the_null_type_4.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Null type is a subtype of all types, except the Undefined type. + This means that null is considered a valid value for all primitive types, object types, union types, intersection types, and type parameters, including even the Number and Boolean primitive types. + error: + code: TS2322 + message: Type 'null' is not assignable to type 'boolean'. + ---*/ + + +var nullNumber: number = null; +var nullBoolean: boolean = null; +var nullString: string = null; +var nullSymbol: symbol = null; +var nullVoid: void = null; diff --git a/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_1.ts b/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..58bcd21cce28c18b3316359472760914c597a4f4 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_1.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Test "Number primitive type corresponds to the similarly named JavaScript primitive type + and represents double-precision 64-bit format IEEE 754 floating point values." + ---*/ + + +// add +Assert.notEqual(0.1 + 0.2, 0.3); +Assert.notEqual(0.7 + 0.1, 0.8); +Assert.notEqual(0.2 + 0.4, 0.6); +// sub +Assert.notEqual(1.5 - 1.2, 0.3); +Assert.notEqual(0.3 - 0.2, 0.1); +// multiplication +Assert.notEqual(19.9 * 100, 1990); +Assert.notEqual(0.8 * 3, 2.4); +Assert.notEqual(35.41 * 100, 3541); +// division +Assert.notEqual(0.3 / 0.1, 3); +Assert.notEqual(0.69 / 10, 0.069); + +// Number.toFiexed() +Assert.equal((1.335).toFixed(2), 1.33); +Assert.equal((1.3335).toFixed(3), 1.333); +Assert.equal((1.33335).toFixed(4), 1.3334); +Assert.equal((1.333335).toFixed(5), 1.33333); +Assert.equal((1.3333335).toFixed(6), 1.333333); + + diff --git a/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_2.ts b/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..1be696bc15965eb9e386812a5d90e518891df10a --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_2.ts @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The number keyword references the Number primitive type and numeric literals may be used to write values of the Number primitive type. + ---*/ + + +// Integral type +let n1: number = 1; +Assert.isNumber(n1); +Assert.equal(n1, 1); +n1 = 2; +Assert.isNumber(n1); +Assert.equal(n1, 2); +// Floating point number +let n2: number = 1.51; +Assert.isNumber(n2); +Assert.equal(n2, 1.51); +n2 = 3.53; +Assert.isNumber(n2); +Assert.equal(n2, 3.53); +// binary +let n3: number = 0b1011; +Assert.isNumber(n3); +Assert.equal(n3, 0b1011); +n3 = 0b1111; +Assert.isNumber(n3); +Assert.equal(n3, 0b1111); +// octal +let n4: number = 0o17; +Assert.isNumber(n4); +Assert.equal(n4, 0o17); +n4 = 0o24; +Assert.isNumber(n4); +Assert.equal(n4, 0o24); +// hexadecimal +let n5: number = 0xf00d; +Assert.isNumber(n5); +Assert.equal(n5, 0xf00d); +n5 = 0xf01c; +Assert.isNumber(n5); +Assert.equal(n5, 0xf01c); diff --git a/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_3.ts b/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d6b6a7cf4e6e82017cd131e7ee48ff0b05c9497f --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_number_type/the_number_type_3.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: the Number primitive type behaves as an object type with the same properties as the global interface type 'Number'. + ---*/ + + +// Same as z: number = 123.456 +var z = 123.456; +// Property of Number interface +var s = z.toFixed(2); +Assert.equal(s, 123.46); +var a = z.toString(); +Assert.equal(a, "123.456"); +var b = z.toExponential(); +Assert.equal(b, "1.23456e+2"); +b = z.toExponential(2); +Assert.equal(b, "1.23e+2"); +var c = z.toPrecision(); +Assert.equal(c, "123.456"); +c = z.toPrecision(3); +Assert.equal(c, "123"); +var d = z.valueOf(); +Assert.equal(d, 123.456); \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_1.ts b/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7118df17ce18fb594c143dd86bcaa86c27655513 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + All string literal types are subtypes of the String primitive type. + ---*/ + + +var cc: "hello" = "hello"; +Assert.isString(cc); +var dd: string = cc; +Assert.equal(dd, "hello"); +var ee: "" = ""; +Assert.isString(ee); +var ff: string = ee; +Assert.equal(ff, ""); \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_2.ts b/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c642286cd856ac3f08bc2e188d4fe9ba3c86b251 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_2.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Specialized signatures permit string literals to be used as types in parameter type annotations. + ---*/ + + +interface Test { + v: "hello"; +} + +function test(dd: Test) { + Assert.equal(dd.v.length, 5); + return dd; +} +var cc: Test = { + v: "hello", +}; +Assert.isString(test(cc).v); diff --git a/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_3.ts b/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..8ccfdab2e6fcd7bc0b296aeee0d93dcfb0a2bf89 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_string_literal_type/the_string_literal_type_3.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + String literal types are permitted only in that context and nowhere else. + string literals no longer always have the type string. + ---*/ + + +// If you define a variable type without explicit type annotations, the variable defaults to string instead of the string literal type it should be +let HELLO: "HELLO" = "HELLO"; +let WORLD: "WORLD" = "WORLD"; +// type: string +let hello = HELLO.toLowerCase(); +Assert.equal(hello, "hello"); +// type: string +let HELLOWORLD = HELLO + WORLD; +Assert.equal(HELLOWORLD, "HELLOWORLD"); +// valid +let a: "foo" | number = "foo"; diff --git a/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_1.ts b/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..f93232c02a9a610d5f350acc9898b179abdb3af6 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_1.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The String primitive type corresponds to the similarly named JavaScript primitive type + and represents sequences of characters stored as Unicode UTF-16 code units. + ---*/ + + +var str: string = "\u4f60\u597d"; +Assert.equal(str, "你好"); \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_2.ts b/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a658754ca2173e7bb8dff087f13c8423967412d8 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_2.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + The string keyword references the String primitive type and + string literals may be used to write values of the String primitive type. + ---*/ + + +var str: string; +str = "abcd"; +Assert.equal(str, "abcd"); +Assert.isString(str); \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_3.ts b/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..b72b6bb7c1a9acf0075cd4c9083ac0c866d8f4a0 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_string_type/the_string_type_3.ts @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the String primitive type behaves as an object type + with the same properties as the global interface type 'String'. + ---*/ + + +var str: string = "string"; +Assert.equal(str.charAt(1), "t"); + +Assert.equal(str.charCodeAt(2), 114); + +Assert.equal(str.toString(), 'string'); + +Assert.equal(str.concat("new"), "stringnew"); + +str = "cbaabcda"; +Assert.equal(str.indexOf("a"), 2); + +Assert.equal(str.indexOf("a", 4), 7); + +Assert.equal(str.lastIndexOf("b"), 4); + +Assert.equal(str.lastIndexOf("b", 2), 1); + +Assert.equal(str.localeCompare("cbaabcda"), 0); +Assert.equal(str.localeCompare("cbaabcddd"), -1); +Assert.equal(str.localeCompare("cbaab"), 1); + + + + + + + diff --git a/test_framework/test/spec/types/primitive_types/the_symbol_type/the_symbol_type_1.ts b/test_framework/test/spec/types/primitive_types/the_symbol_type/the_symbol_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..647be2e86f67c83ad55d1fb9059b345656d5e688 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_symbol_type/the_symbol_type_1.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the Symbol primitive type represents a unique token + that can be used as a property key for an object. + options: + lib: es2015 + ---*/ + + +var sym: symbol = Symbol(); +var obj1: object = { [sym]: "symbol" }; +type Key = keyof typeof obj1; +Assert.equal(obj1[sym as Key], "symbol"); diff --git a/test_framework/test/spec/types/primitive_types/the_symbol_type/the_symbol_type_2.ts b/test_framework/test/spec/types/primitive_types/the_symbol_type/the_symbol_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..c15df5da1eee650eb03d90fe2a94e3e0b13a475a --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_symbol_type/the_symbol_type_2.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The symbol keyword references the Symbol primitive type. + Symbol values are obtained using the global object 'Symbol' which has a number of methods and properties and can be invoked as a function. + options: + lib: es2019 + ---*/ + + +var sym: symbol = Symbol("NARC"); +var sym2: symbol = Symbol("TypeScript"); +Assert.equal(sym.description, "NARC"); + +var s1: string = sym2.toString(); +var s2: symbol = sym.valueOf(); +Assert.equal(s1, "Symbol(TypeScript)"); + +var flag1: boolean = false; +if (sym != sym2) { + flag1 = true; +} +Assert.isTrue(flag1); + +var flag2: boolean = false; +if (sym == s2) { + flag2 = true; +} +Assert.isTrue(flag2); diff --git a/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_1.ts b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..e326ee406212450a8216196f31236c9fb10b03c9 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_1.ts @@ -0,0 +1,21 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: The Undefined type corresponds to the similarly named JavaScript primitive type and is the type of the undefined literal. + ---*/ + + +var u: undefined = undefined; +Assert.isUndefined(u); \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_2.ts b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..77cb5c221f78aa85daae47150ae6016750fcdd32 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_2.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The undefined literal denotes the value given to all uninitialized variables + error: + code: TS2454 + message: Variable 'uString' is used before being assigned. + ---*/ + + +var uString: string; +Assert.isUndefined(uString); diff --git a/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_3.ts b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..2463e649eecadbee2165c49bbf2dc1fb6447d587 --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_3.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The undefined is the one and only value of the Undefined type. + error: + code: TS2322 + message: Type '3' is not assignable to type 'undefined'. + ---*/ + + +var uNumber: undefined = 3; diff --git a/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_4.ts b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..01832063377cbbe71c814e4473da8c4cf51bc19d --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_4.ts @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + It is not possible to directly reference the Undefined type itself. + error: + code: TS2304 + message: Cannot find name 'Undefined'. + ---*/ + + +var directlyReference: Undefined; \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_5.ts b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..b71e3de1179f2cc81d26070e163edd4f36581fed --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_undefined_type/the_undefined_type_5.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The undefined type is a subtype of all types. + This means that undefined is considered a valid value for all primitive types, object types, union types, intersection types, and type parameters. + error: + code: TS2322 + message: Type 'undefined' is not assignable to type 'number'. + ---*/ + + +var undefinedNumber: number = undefined; +var undefinedBoolean: boolean = undefined; +var undefinedString: string = undefined; +var undefinedSymbol: symbol = undefined; +var undefinedVoid: void = undefined; + +//TODO add object types, union types, intersection types, and type parameters \ No newline at end of file diff --git a/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_1.ts b/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..912462c330de2d543244ad251ce463202f56390e --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_1.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the Void type, referenced by the void keyword, + represents the absence of a value and is used as the return type of functions with no return value. + ---*/ + + +var v: void = undefined; +function noReturn(): void { } +Assert.isUndefined(v); +Assert.isUndefined(noReturn()); diff --git a/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_2.ts b/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..58e40cbf5ed20c4eb979c54ca90218758559382f --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_2.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the only possible values for the Void type are null and undefined. + the void type can be assigned a null value, but this is only allowed in non-strict mode. + ---*/ + + +var v: void; +Assert.isUndefined(v); +var v2: void = undefined; +Assert.isUndefined(v2); +var nu = null; +var u: undefined = undefined; +v = u; +Assert.isUndefined(v); diff --git a/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_3.ts b/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..7c3ef02ecb3776e55f3046acfdf4487e143e342c --- /dev/null +++ b/test_framework/test/spec/types/primitive_types/the_void_type/the_void_type_3.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the Void type is a subtype of the any type and a supertype of the null and undefined types. + ---*/ + + +var v: void; +var nu = null; +var u: undefined = undefined; +var a: any; +a = v; +Assert.isUndefined(a); +v = u; +Assert.isUndefined(v); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_1.ts b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..64351e46f0b0690706b2c1d61aa6f49f5d9e8fd4 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_1.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A signature's parameter list consists of zero or more required parameters, followed by zero or more optional parameters, + finally followed by an optional rest parameter. + ---*/ + + +function fun0() { + return "zero parameter"; +} +function fun1(firstParameter: string, lastParameter?: string) { + if (lastParameter) return firstParameter + " " + lastParameter; + else return firstParameter; +} +let result1 = fun1("Bob"); +let result3 = fun1("Bob", "Adams"); +Assert.equal(result1, "Bob"); +Assert.equal(result3, "Bob Adams"); + +function fun2(firstParameter: string, ...restParameter: string[]) { + return firstParameter + " " + restParameter.join(" "); +} +let employeeName = fun2("Joseph", "Samuel", "Lucas", "MacKinzie"); +Assert.equal(employeeName, "Joseph Samuel Lucas MacKinzie"); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_2.ts b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..97a28925b9efdcae6f0715102db8d937ff6b0043 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A parameter declaration may specify either an identifier or a binding pattern. + The identifiers specified in parameter declarations and binding patterns in a parameter list must be unique within that parameter list. + ---*/ + + +class Animal { + fname: string; + constructor(fname: string) { + this.fname = fname; + } +} +let name1 = "Anny"; +let animal = new Animal(name1); +name1 = "Bob"; +Assert.notEqual(animal.fname, name1); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_3.ts b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..1cc276b1a2d5208e1864bae3bc58885290403383 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_3.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the declaration includes a type annotation, the parameter is of that type. + ---*/ + + +function ff(first: string, second: number) { + return first; +} +let first = "first"; +let second = 2; +ff(first, second); +Assert.equal(first, "first"); +Assert.equal(second, 2); + diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_4.ts b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..603b6d8f833bd821d502d76276e62c3db0e9d676 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_4.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the declaration specifies a binding pattern, + the parameter type is the implied type of that binding pattern. + ---*/ + + +const x: number = 3; +function fun(a: number, c = 1, b = x) { + return a + c + b; +} +let y = 1; +Assert.equal(y, 1); +let z = fun(y); +Assert.equal(z, 5); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_5.ts b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..710c2465ec6991bd1c003d27aefac9721930dfa6 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_5.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if the parameter is a rest parameter, the parameter type is any[]. + A type annotation for a rest parameter must denote an array type. + ---*/ + + +function restFun(first: any, ...restname: any[]) { + return first + " " + restname.join(" "); +} + +let restname1 = restFun("aa", "bb", "cc"); +Assert.equal(restname1, "aa bb cc"); + +let restname2 = restFun(1, 2, 3, 4); +Assert.equal(restname2, "1 2 3 4"); +// boolean +let restname3 = restFun(true, false, true); +Assert.equal(restname3, "true false true"); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_6.ts b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..63b7440c4b7ab4de4393ea9da1e9aa25f3b40245 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/parameter_list/parameter_list_6.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + A parameter is permitted to include a public, private, + or protected modifier only if it occurs in the parameter list of a ConstructorImplementation (section 8.3.1) + and only if it doesn't specify a BindingPattern. + ---*/ + + +class Person2 { + public name: string; + private age: number; + protected sex: string; + constructor(name: string, age: number, sex: string) { + this.name = name; + this.age = age; + this.sex = sex; + } + get _age() { + return this.age; + } + set _age(value) { + if (value >= 0) { + this.age = value; + } + } +} +class Child extends Person2 { + f() { + if (this.sex === "man") { + this.sex = "male"; + } else { + this.sex = "female"; + } + } +} +const a = new Child("wangwu", 15, "man"); +a.name = "lisi"; +a._age = 19; +Assert.equal(a.name, "lisi"); +Assert.equal(a._age, 19); + diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_1.ts b/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..a154852681b802d734d1778a8d7c60b5dc8886e5 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_1.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a call signature's return type annotation specifies the type of the value computed and returned by a call operation. + A void return type annotation is used to indicate that a function has no return value. + ---*/ + + +function returnNum(a: number, b: number): number { + return a + b; +} +let aa = returnNum(1, 2); +Assert.isNumber(aa); +function returnString(name: string): string { + return name + " b!"; +} +let bb = returnString("rush"); +Assert.isString(bb); +function returnBoolean(a: number, b: number): Boolean { + return a > b ? true : false; +} +let cc = returnBoolean(1, 2); +Assert.isBoolean(cc); +function returnUndefine(a: undefined): undefined { + return a; +} +let ad: undefined; +let dd = returnUndefine(ad); +Assert.isUndefined(dd); +function returnVoid(a: number): void { } +let ee = returnVoid(1); +Assert.equal(ee, null); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_2.ts b/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..93d7298200c4a7902a2511fa16055a4e970b5a1e --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_2.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a call signature with no return type annotation occurs in a context without a function body, + the return type is assumed to be the Any type. + ---*/ + + +let add: { (x: number, y: number): any }; +type anyT = ReturnType; +let x: anyT; +x = 1; +Assert.isNumber(x); +x = "any"; +Assert.isString(x); +x = true; +Assert.isBoolean(x); +x = undefined; +Assert.isUndefined(x); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_3.ts b/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..5c980d12b0a7444ecd117564c663b73814eff8d0 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/return_type/return_type_3.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When a call signature with no return type annotation occurs in a context that has a function body , + he return type is inferred from the function body. + ---*/ + + +let add = function (x: number, y: number) { + return x + y; +}; +type Tadd = ReturnType; +let x: Tadd = 1; +Assert.equal(typeof x, "number"); +let sum = function (x: string, y: string) { + return x + y; +}; +type Tsum = ReturnType; +let y: Tsum = "hello"; +Assert.equal(typeof y, "string"); +let booltp = function (x: boolean) { + return true; +}; +let z = booltp(false); +Assert.equal(z, true); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/specialized_signatures/specialized_signatures_1.ts b/test_framework/test/spec/types/specifying_members/call_signatures/specialized_signatures/specialized_signatures_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..7bb6a87aad89a09a642b4ad64bece0e54e37d986 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/specialized_signatures/specialized_signatures_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Specialized signatures are used to express patterns where specific string values for some parameters + cause the types of other parameters or the function result to become further specialized. + ---*/ + + +interface specialType { + Tfun(x: "hello"): "hello"; + Tfun(x: "world"): "world"; + Tfun(x: string): string; +} +class getType implements specialType { + Tfun(x: any): any { + const xx: "hello" = "hello"; + const xx2: "world" = "world"; + if (x === xx) { + return x; + } else if (x === xx2) { + return x; + } else if (typeof x === "string") { + return "isstring"; + } + } +} +const x1 = new getType(); +const xx1: "hello" = "hello"; +const y1: "hello" = x1.Tfun(xx1); +Assert.isTrue(y1 === xx1); +const x2 = new getType(); +const xx2: "world" = "world"; +const y2 = x2.Tfun(xx2); +Assert.isTrue(xx2 === y2); +const x3 = new getType(); +const y3 = x3.Tfun("helloworld"); +Assert.equal(y3, "isstring"); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_1.ts b/test_framework/test/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..fc5c4ce763427e25d445951932a0ede6e22effee --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_1.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type parameters in call signatures provide a mechanism for expressing the relationships of parameter and return types in call operations. + ---*/ + + +function identity(x: T): T { + return x; +} +let x: number = 1; +Assert.equal(x, identity(x)); +function identity2(x: T, y: U): T { + return x; +} +Assert.equal(x, identity2(x, 1)); +function identity3(obj: T, key: K) { + return obj[key]; +} +let y = { a: 1, b: 2, c: 3, d: 4 }; +identity3(y, "a"); +function identity4(arg: T[]): T[] { + return arg; +} +let arg: number[] = [1, 2, 3]; +Assert.equal(arg, identity4(arg)); diff --git a/test_framework/test/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_2.ts b/test_framework/test/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b7dd82a63a1299b701bbb6d25364a7372806d5a7 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/call_signatures/type_parameters/type_parameters_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Type arguments for call signature type parameters may be explicitly specified in a call operation or may, + when possible, be inferred from the types of the regular arguments in the call. + ---*/ + + +function identity(x: T): T { + return x; +} +let x: number = 3; +// CHECK explicitly specified type in call +Assert.isNumber(identity(x)); +let y = "string"; +// CHECK inferred from argument in the call +Assert.isString(identity(y)); diff --git a/test_framework/test/spec/types/specifying_members/construct_signatures/construct_signatures.ts b/test_framework/test/spec/types/specifying_members/construct_signatures/construct_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..899729fe8dc3450d414491bed8cf9ca9b5090c73 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/construct_signatures/construct_signatures.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: A type may overload new operations by defining multiple construct signatures with different parameter lists. + ---*/ + + +class Animal { + name: string | undefined; + age: number | undefined; + constructor(); + constructor(name: string); + constructor(age: number); + constructor(nameorage?: string | number, age?: number) { + if (typeof nameorage == "number") { + this.age = nameorage; + } + if (typeof nameorage == "string") { + this.name = nameorage; + } + if (age) { + this.age = age; + } + } +} +type AnimalConstructor = { + new (name: string, age: number): Animal; + new (name: string): Animal; + new (age: number): Animal; + new (): Animal; +}; + +const AnimalConstructor: AnimalConstructor = Animal; + +let tt1 = new AnimalConstructor(); +Assert.isUndefined(tt1.age); +Assert.isUndefined(tt1.name); +let tt2 = new AnimalConstructor("caihua2", 12); +Assert.equal(tt2.name, "caihua2"); +Assert.equal(tt2.age, 12); +let tt3 = new AnimalConstructor("caihua3"); +Assert.equal(tt3.name, "caihua3"); +Assert.isUndefined(tt3.age); +let tt4 = new AnimalConstructor(1230); +Assert.equal(tt4.age, 1230); +Assert.isUndefined(tt4.name); diff --git a/test_framework/test/spec/types/specifying_members/index_signatures/index_signatures.ts b/test_framework/test/spec/types/specifying_members/index_signatures/index_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..142b89050fe4d725164df0edea8fd30b252c3df0 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/index_signatures/index_signatures.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + string index signatures, specified using index type string, define type constraints for all properties and numeric index signatures in the containing type. + numeric index signatures, specified using index type number, define type constraints for all numerically named properties in the containing type. + ---*/ + + +var si: { [key: string]: number } = { a: 97, A: 65 }; +si["b"] = 98; +Assert.isNumber(si["a"]); +Assert.equal(si["A"], 65); + +var ni: { [key: number]: boolean } = { 0: false, 1: true }; +ni[-1] = true; +Assert.isBoolean(ni[0]); +Assert.equal(ni[-1], true); + +interface UnionKey { + [key: string | number]: string | number; +} +var uk: UnionKey = { Name: "UnionKey", 0: "NARC", 0x0a: 10 }; +Assert.equal(uk["Name"], "UnionKey"); +Assert.equal(uk[0], "NARC"); +Assert.equal(uk[0x0a], 10); +Assert.equal(uk[0xff], undefined); + +interface StringKey { + [key: string]: string; +} +var sk: StringKey = { "1": "0x01", "2": "0x02", 3: "0x03", "4": "0x04" }; +Assert.isString(sk["1"]); +Assert.isString(sk[2]); +Assert.equal(sk[3], 0x03); + +interface NumberKey { + [key: number]: string; +} +var nk: NumberKey = { 1: "0x01", 2: "0x02", "3": "0x03", 4: "0x04" }; +Assert.isString(nk["1"]); +Assert.isString(nk[2]); +Assert.equal(nk["3"], "0x03"); + +var rk1: Record = { one: 1, two: 2, three: 3 }; +Assert.equal(rk1["one"], 1); +var rk2: Record<"a" | "b" | "c", string> = { a: "A", b: "B", c: "C" }; +Assert.equal(rk2["a"], "A"); diff --git a/test_framework/test/spec/types/specifying_members/method_signatures/method_signatures_1.ts b/test_framework/test/spec/types/specifying_members/method_signatures/method_signatures_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..26c39896f0b47d4cd16f25462b372dddb7b5da2b --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/method_signatures/method_signatures_1.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + If the PropertyName is followed by a question mark, the property is optional. + Only object type literals and interfaces can declare optional properties. + ---*/ + + +// interfaces +interface point { + point(x: number, y: number, z?: number): number; +} +class P implements point { + point(x: number, y: number, z?: number | undefined): number { + if (z) return x + y + z; + return x + y; + } +} +let p1 = new P(); +let x: number = 1; +let y: number = 2; +let z: number = 3; +let sum = p1.point(x, y, z); +Assert.equal(sum, 6); +sum = p1.point(x, y); +Assert.equal(sum, 3); +// object type literals +let point2: { x: number; y: number; z?: number }; +point2 = { x: 1, y: 2, z: 3 }; +let pp1 = point2; +Assert.equal(pp1.x, 1); +Assert.equal(pp1.y, 2); +point2 = { x: 0, y: 0, z: 0 }; +let pp2 = point2; +Assert.equal(pp2.x, 0); +Assert.equal(pp2.y, 0); +Assert.equal(pp2.z, 0); diff --git a/test_framework/test/spec/types/specifying_members/method_signatures/method_signatures_2.ts b/test_framework/test/spec/types/specifying_members/method_signatures/method_signatures_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..23c19bb0a7cc8713a07d5c28d5e4be22217c7307 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/method_signatures/method_signatures_2.ts @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A literal type may overload a method by declaring multiple method signatures + with the same name but differing parameter lists. + ---*/ + + +class A { + add(x: number, y: number): number; + add(x: number, y: number, z: number): number; + add(x: any, y: any, z?: any): any { + if ( + typeof x == "number" && + typeof y == "number" && + typeof z == "undefined" + ) { + return x + y; + } + if ( + typeof x === "number" && + typeof y === "number" && + typeof z === "number" + ) { + return x + y + z; + } + } +} +let a = new A(); +Assert.equal(a.add(1, 2), 3); +let b = new A(); +Assert.equal(b.add(1, 2, 3), 6); diff --git a/test_framework/test/spec/types/specifying_members/property_signatures/property_signatures.ts b/test_framework/test/spec/types/specifying_members/property_signatures/property_signatures.ts new file mode 100644 index 0000000000000000000000000000000000000000..338811724ecb1f8630187566dcbe6abe735592f0 --- /dev/null +++ b/test_framework/test/spec/types/specifying_members/property_signatures/property_signatures.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the PropertyName of a property signature must be unique within its containing type, and must denote a well-known symbol if it is a computed property name. + if the property name is followed by a question mark, the property is optional. Otherwise, the property is required. + ---*/ + + +var fname: { fristName: string, middleName?: string, lastName: string; } = { fristName: "NARC", lastName: "TypeScript" }; +var f2name: { fristName: string, middleName?: string, lastName: string; } = { + fristName: "Isaac", + middleName: "F", + lastName: "Newton", +}; +function fullName(name: { fristName: string, middleName?: string, lastName: string; }): string { + if (name.middleName != undefined) { + return name.fristName + " " + name.middleName + " " + name.lastName; + } else { + return name.fristName + " " + name.lastName; + } +} +var fn1: string = fullName(fname); +Assert.isString(fn1); +Assert.equal(fn1, "NARC TypeScript"); + +var fn2: string = fullName(f2name); +Assert.isString(fn2); +Assert.equal(fn2, "Isaac F Newton"); diff --git a/test_framework/test/spec/types/the_any_type/any_type_1.ts b/test_framework/test/spec/types/the_any_type/any_type_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..8a38183d6dc7c148c68542966fda66caa9624d51 --- /dev/null +++ b/test_framework/test/spec/types/the_any_type/any_type_1.ts @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The Any type is a supertype of all types, + and is assignable to and from all types. + options: + lib: es2015 + ---*/ + + +var x: any; +x = 12; +Assert.isNumber(x); +x = "abc"; +Assert.isString(x); +x = true; +Assert.isBoolean(x); +x = Symbol(); +Assert.isSymbol(x); +x = null; +let flag = false; +if (x === null) { + flag = true +} +Assert.isTrue(flag); +x = { 0x00: "0x00" }; +Assert.equal(JSON.stringify(x), '{"0":"0x00"}') +x = function voidFunction() { }; +Assert.isFunction(x); +x = undefined; +Assert.isUndefined(x); + +var y: any; +var n: number = 1; +n = y; +var s: string = "string"; +s = y; +var b1: boolean = false; +b1 = y; +var sym: symbol = Symbol(); +sym = y; +var v: void; +v = y; +var obj: object = { 0x11: "0x11" }; +obj = y; +var fun1: Function = function add(a: number, b: number, c: number) { + return a + b + c; +}; +fun1 = y; +var u: undefined; +u = y; diff --git a/test_framework/test/spec/types/the_any_type/any_type_2.ts b/test_framework/test/spec/types/the_any_type/any_type_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9fe525c4b92e69fdc238d868b962fa5f556bea0e --- /dev/null +++ b/test_framework/test/spec/types/the_any_type/any_type_2.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + in places where a type is not explicitly provided + and TypeScript cannot infer one, the Any type is assumed. + options: + lib: es2015 + ---*/ + + +var a; +a = 25.25; +Assert.isNumber(a); +a = "narc"; +Assert.isString(a); +a = function add(a: any, b: any) { + return a + b; +}; +Assert.isFunction(a); +a = false; +Assert.isBoolean(a); +a = Symbol(); +Assert.isSymbol(a); +a = { 1408: "Movie" }; +Assert.equal(JSON.stringify(a), '{"1408":"Movie"}'); +a = null; +let flag = false; +if (a === null) { + flag = true; +} +Assert.isTrue(flag); +a = undefined; +Assert.isUndefined(a); diff --git a/test_framework/test/spec/types/the_any_type/any_type_3.ts b/test_framework/test/spec/types/the_any_type/any_type_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..6448994dd1ccb001fc1bb2e5e0f7dc2895a67c20 --- /dev/null +++ b/test_framework/test/spec/types/the_any_type/any_type_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + properties of any name can be accessed through an Any value + and Any values can be called as functions with any argument list. + ---*/ + + +var x: any +x = 1024; +x.toString(); +Assert.isString(x.toString()); +x = "AAA"; +x.length; +Assert.isNumber(x.length); +var fun3: any; +fun3 = () => { }; +fun3(); diff --git a/test_framework/test/spec/types/type_parameters/this_types/this_types.ts b/test_framework/test/spec/types/type_parameters/this_types/this_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..961309a83ac4d8b2b9d806cb7c346057cc18c8c2 --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/this_types/this_types.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + every class and interface has a this-type that represents the actual type of instances of the class or interface within the declaration of the class or interface. The this-type is referenced using the keyword this in a type position. + within instance methods and constructors of a class, the type of the expression this is the this-type of the class. + ---*/ + + +class CA { + name: string = "CA"; + foo() { + return this; + } + caName(cname: string) { + this.name = cname; + return "CA:" + this.name; + } +} + +class CB extends CA { + bar() { + return this; + } + cbName(cname: string) { + this.name = cname; + return "CB:" + this.name; + } +} + +let bB: CB = new CB(); +let xB = bB.foo().bar(); +Assert.equal(bB, xB); + +var thisStrB: string = bB.cbName("CB"); +Assert.equal(thisStrB, "CB:CB"); +var thisStrA: string = bB.caName("CA"); +Assert.equal(thisStrA, "CA:CA"); diff --git a/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_1.ts b/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..daa499b29b475dde5c99f6590f19cccf69638008 --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_1.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type reference to a generic type must include + a list of type arguments enclosed in angle brackets and separated by commas. + ---*/ + + +function identity1(value: T, message: U): [T, U] { + return [value, message]; +} +let identi = identity1(1, 2); +Assert.equal(identi[0], 1); +Assert.equal(identi[1], 2); + diff --git a/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_2.ts b/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..da34b48e5644b3b28a7f1847595b0de3e5e1509e --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a call to a generic function may explicitly include a type argument list instead of relying on type inference. + ---*/ + + +interface GenericIdentityFn { + (arg: T): T; +} +function identity(arg: T): T { + return arg; +} +let myidenti: number = 1; +let myIdentity: GenericIdentityFn = identity; +let myidenti2 = myIdentity(myidenti); +Assert.equal(myidenti2, 1); + diff --git a/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_3.ts b/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..6fa42f3785a1125655cd6029c164004b91e73503 --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_argument_lists/type_argument_lists_3.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + A type argument list is required to specify exactly one type argument for each corresponding type parameter, + and each type argument for a constrained type parameter is required to satisfy the constraint of that type parameter. + ---*/ + + +function getname(name: T, number: Y): T { + return name; +} +interface Lengthwise { + length: number; +} +interface Search { + (name: T, number: Y): T; +} +let fn: Search = getname; +let aa: Lengthwise = { length: 11 }; +let a = fn("wan", aa); +Assert.equal(a, "wan"); +let fn2: Search = getname; +let bb: Lengthwise = { length: 33 }; +let b = fn2(22, bb); +Assert.equal(b, 22); +let fn3: Search = getname; +let cc: Lengthwise = { length: 44 }; +let c = fn3(true, cc); +Assert.equal(c, true); diff --git a/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_1.ts b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..6fc7618ab07fc8b6b633f1d37ac93bfafb3dd3e2 --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_1.ts @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + ---*/ + + +// class +class MinClass { + public list: T[] = []; + add(v: T) { + this.list.push(v); + } + findminNumberValue(): T { + let minNum = this.list[0]; + + for (let i = 0; i < this.list.length; i++) { + if (minNum > this.list[i]) { + minNum = this.list[i]; + } + } + return minNum; + } +} +// string type +let minStringValue = new MinClass(); +minStringValue.add("a"); +minStringValue.add("b"); +minStringValue.add("c"); +minStringValue.add("d"); +Assert.equal(minStringValue.findminNumberValue(), "a"); +// number type +let minNumberValue = new MinClass(); +minNumberValue.add(1); +minNumberValue.add(2); +minNumberValue.add(3); +minNumberValue.add(4); +Assert.equal(minNumberValue.findminNumberValue(), 1); diff --git a/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_2.ts b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..f9bf4d9aff93708088f307429927490ea12f595d --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + ---*/ + + +// interface +interface OutputValue { + (arg: T): T; +} +let tt1: OutputValue = function (arg: T): T { + return arg; +}; +Assert.equal(tt1("abc"), "abc"); + diff --git a/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_3.ts b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..471b3db6b1a4398489659553e00ba881e22a5c1b --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + ---*/ + + +// alias +class Person { + name: string; + age: number; + job: string; + constructor(name: string, age: number, job: string) { + this.name = name; + this.age = age; + this.job = job; + } +} +// Mapping: This parameter is optional +type optionallyTest = { + [P in keyof T]?: T[P]; +}; +let pp: optionallyTest = {}; +pp = { name: "dog" }; +Assert.isUndefined(pp.job); +Assert.isUndefined(pp.age); +pp = { name: "dog", age: 18 }; +Assert.isUndefined(pp.job); +pp = { name: "dog", age: 18, job: "teacher" }; diff --git a/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_4.ts b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..33e123f045ba46c764aa69c8dcd305645364e3a2 --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_4.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + Class, interface, type alias, and function declarations may optionally include + lists of type parameters enclosed in < and > brackets. + ---*/ + + +function returnAnyTypeValue(a: T): T { + return a; +} +Assert.equal(returnAnyTypeValue(1), 1); +Assert.equal(returnAnyTypeValue("char"), "char"); diff --git a/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_5.ts b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..118cab3481b061b97201a56ac3c92395d2bd0335 --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_5.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + Type parameter names must be unique. A compile-time error occurs + if two or more type parameters in the same TypeParameterList have the same name. + ---*/ + + +// function declaration +function sum(a: T, b: K): string { + return "a"; +} +Assert.equal(sum(1, 1), "a"); + diff --git a/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_6.ts b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..92a3d0346b045b25ff8941ca60ff4d9f9646de5b --- /dev/null +++ b/test_framework/test/spec/types/type_parameters/type_parameter_lists/type_parameter_lists_6.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + A type parameter may have an associated type parameter constraint that establishes an upper bound for type arguments. + Type parameters may be referenced in type parameter constraints within the same type parameter list, + including even constraint declarations that occur to the left of the type parameter. + ---*/ + + +type Dog = { + m_name: string; + m_age: number; + m_job: string; +}; +type PickProperty = { [P in K]: T[P] }; +type test = PickProperty; +let cc: test = { + m_age: 20, + m_name: "wangwang", +}; +Assert.isNumber(cc.m_age); +Assert.isString(cc.m_name); diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_1.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..39f487e5ee0859e406bbc2ade1c7d51f6d3f1a52 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_1.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The apparent members of the primitive type Number and all enum types are the apparent members of the global interface type 'Number'. + ---*/ + + +let num: Number = 1; +enum Color { + red, + black, + green +} +let c: Color.black = Color.black; +num.toString(); +c.toString(); +Assert.equal(num.valueOf(), c.valueOf()); \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_2.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..5ed90e295321053135a258986d684324004d5d31 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_2.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The apparent members of the primitive type Boolean are the apparent members of the global interface type 'Boolean'. + The apparent members of the primitive type String and all string literal types are the apparent members of the global interface type 'String' + ---*/ + + +let bool: Boolean = true; +Assert.isBoolean(bool.valueOf()) + +let str1: String = "string"; +type color = "RED" | "BLUE" | "BALCK" +let str2: color = "BLUE"; +str1.toString; +str2.toString; +Assert.equal(typeof str1.valueOf(), typeof str2.valueOf()); \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_3.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..d10d0266ba00b1b29f2ccb9628a9d5e29e993bd8 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_3.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The apparent members of an object type T are the combination of the following: + The declared and/or inherited members of T. + The properties of the global interface type 'Object' that aren't hidden by properties with the same name in T. + If T has one or more call or construct signatures, the properties of the global interface type 'Function' that aren't hidden by properties with the same name in T. + ---*/ + + +let obj: Object; +obj = { + toString() { + return '123'; + } +} +Assert.notEqual(obj.toString, "123"); + +class Person { + name?: string; + age?: number; + toString(): string { + return "hello" + } +} +function test() { + let myTest1: new (str: string, num: number) => Person +} +Assert.notEqual(Person.toString(), "hello"); \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_4.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..f04c5c99dc1cee5324e531e6aa126f2f85d367a5 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_4.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When all constituent types of U have an apparent string index signature, + U has an apparent string index signature of a union type of the respective string index signature types. + ---*/ + + +interface Array1 { + [index1: string]: string +} +interface Array2 { + [index2: string]: string +} + +function test(obj: Array1 | Array2) { + return "right" +} +const stu1 = { + age: "19" +} +const stu2 = { + name: "xiao" +} +Assert.equal(test(stu1), "right") +Assert.equal(test(stu2), "right") \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_5.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..4081bd3ec12cf9e8bc24e9566e122fe6f0bb74b7 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_5.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When all constituent types of U have an apparent numeric index signature, + U has an apparent numeric index signature of a union type of the respective numeric index signature types. + ---*/ + + +interface Array1 { + [index1: string]: number +} +interface Array2 { + [index2: string]: number +} + +function test(obj: Array1 | Array2) { + return "right" +} +const stu1 = { + age: 19 +} +const stu2 = { + number: 10001 +} +Assert.equal(test(stu1), "right") +Assert.equal(test(stu2), "right") \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_6.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..74bc066b03cec6fd704dc41385195a4dc09cff84 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_6.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When one or more constituent types of I have an apparent string index signature, + I has an apparent string index signature of an intersection type of the respective string index signature types. + ---*/ + + +interface Array1 { + [index1: string]: string +} +interface Array2 { + [index2: string]: string +} + +function test(obj: Array1 & Array2) { + return "right" +} +const stu1 = { + age: "19" +} +const stu2 = { + name: "xiao" +} +Assert.equal(test(stu1), "right") +Assert.equal(test(stu2), "right") \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_7.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..cd98a0b5e0b859eaf9a06c4afd5e15a80fbbd407 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_7.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When one or more constituent types of I have an apparent numeric index signature, + I has an apparent numeric index signature of an intersection type of the respective numeric index signature types. + ---*/ + + +interface Array1 { + [index1: string]: number +} +interface Array2 { + [index2: string]: number +} + +function test(obj: Array1 & Array2) { + return "right" +} +const stu1 = { + age: 19 +} +const stu2 = { + number: 10001 +} +Assert.equal(test(stu1), "right") +Assert.equal(test(stu2), "right") \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_8.ts b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..00efa5d172684460e3db6cdf06eb0ed1d7846529 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/apparent_members/apparent_members_8.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + a type's apparent members make it a subtype of the 'Object' or 'Function' interface + ---*/ + + +var o: Object = { x: 10, y: 20 }; +Assert.equal(JSON.stringify(o), '{"x":10,"y":20}') + +var f: Function = (x: number) => x * x; +Assert.isUndefined(JSON.stringify(f)) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_1.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..d7a2dd0842acfd1d803e79c1cef7069ffbb4f974 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_1.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S and T are identical types. + ---*/ + + +interface T { + name: string +} +interface S { + name: string +} +let t: T; +let s: S = { name: 'xiao' } +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_10.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..f53647ffb2991dd291f8b0ffd1e5044a31e4c3c0 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_10.ts @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and T is an intersection type and S is assignable to each constituent type of T. + ---*/ + + +interface Foo { + name: string +} +interface Bar { + age: number +} +interface S { + name: string + age: number +} +type T = Foo & Bar +let t: T +let s: S = { + name: 'xiao', + age: 18 +} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_11.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..6a41d7ca207b0e63e7dba75d0ba861e81f6133ed --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_11.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is a type parameter and the constraint of S is assignable to T. + ---*/ + + +interface event { + data: T +} +type T = event +type S = event +let t: T +let s: S = { data: 10 } +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_12.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..1d2de8ba3a75348a4c7af722be04ac94040db459 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_12.ts @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a property and S has an apparent property N where + M and N have the same name, + the type of N is assignable to that of M, + if M is a required property, N is also a required property, and + M and N are both public, M and N are both private and originate in the same declaration, + M and N are both protected and originate in the same declaration, + or M is protected and N is declared in a class derived from the class in which M is declared. + ---*/ + + +type T = { + name: any + age: number +} +type S = { + name: string + age: number +} +let t: T +let s: S = { + name: "Xi", + age: 20, +} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_13.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..f5db69faa961b2827c56bfe5006f16a71a13bec2 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_13.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, + when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any), + the signatures are of the same kind (call or construct), + M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M, + for parameter positions that are present in both signatures, each parameter type in N is assignable to or from the corresponding parameter type in M, + and the result type of M is Void, or the result type of N is assignable to that of M. + ---*/ + + +interface T { + (x: any, y: any): void +} +interface S { + (x: number): void +} +var t: T = (x, y): void => { + return; +} +var s: S = (x): void => { + return; +} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_14.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_14.ts new file mode 100644 index 0000000000000000000000000000000000000000..be579b3205dc67d5b53126ae57a2215412edd88f --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_14.ts @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is an optional property and S has no apparent property of the same name as M. + ---*/ + + +interface T { + name?: string + age?: number +} +interface S { + name?: string + age?: number +} +let t: T +let s: S = { name: 'xiao' } +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_15.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_15.ts new file mode 100644 index 0000000000000000000000000000000000000000..525b3778f33638919752ee3bc103e5f194aa1e7e --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_15.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a string index signature of type U, + and U is the Any type or S has an apparent string index signature of a type that is assignable to U. + ---*/ + + +interface U { + anything: string +} +interface T { + [key: string]: U +} +interface S { + [keyname: string]: U +} +let t: T = {} +let s: S = {} +s['obj'] = { anything: "thing" } +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_16.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_16.ts new file mode 100644 index 0000000000000000000000000000000000000000..4a7f0dd1f6023a8d31e7b5165518a02a40b4ed33 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_16.ts @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, + and M is a numeric index signature of type U, + and U is the Any type or S has an apparent string or numeric index signature of a type that is assignable to U. + ---*/ + + +interface U { + anything: string +} +interface T { + [key: number]: U +} +interface S { + [keyname: number]: U +} +let t: T = {} +let s: S = {} +s[10] = { anything: "thing" } +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_17.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_17.ts new file mode 100644 index 0000000000000000000000000000000000000000..eb5e9c8ba1418372002b02789b4634aa16173876 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_17.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The assignment compatibility and subtyping rules differ only in that, + an object type without a particular property is assignable to an object type in which that property is optional. + ---*/ + + +interface T { + name?: string + age?: number +} +interface S { +} +let t: T +let s: S = {} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_18.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_18.ts new file mode 100644 index 0000000000000000000000000000000000000000..9dff87e93498b924d5b1d352ff2401480e6f09b0 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_18.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when assigning values or passing parameters, + optional properties must either be present and of a compatible type, or not be present at all. + ---*/ + + +function foo(x: { id: number; name?: string }) { + return x +} +var f1 = foo({ id: 1234 }); +Assert.equal(JSON.stringify(f1), '{"id":1234}') + +var f2 = foo({ id: 1234, name: "xiao" }) +Assert.equal(JSON.stringify(f2), '{"id":1234,"name":"xiao"}') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_2.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..a81c992edf644a25014a4820112a2b5474af7972 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_2.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S or T is the Any type. + ---*/ + + +interface T { + name: string +} +let t: T +const S: any = 'str' +t = S +Assert.equal(t, S) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_3.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..0f8d7c9e68025af951a9b337a9ea9d3e23e6c7eb --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_3.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is the Undefined type. + ---*/ + + +interface T { + name: undefined +} +interface S { + name: undefined +} +let t: T +let s: S = { name: undefined } +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_4.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..28a3827210716614d16cabd27289910a7cedb7c5 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_4.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is the Null type and T is not the Undefined type. + ---*/ + + +let T: any; +const S: null = null; +T = S; +Assert.equal(T, S) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_5.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..c0906ed9b62e0277dc392fc36b3d54ddc4bfbe08 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_5.ts @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S or T is an enum type and the other is the primitive type Number. + ---*/ + + +enum T { + A, + B, + C +} +let S: number = 5 +let t: T +t = S +Assert.equal(t, S) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_6.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..1f0688a4c04c66892b9cd8e225834e32b95af9a2 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_6.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is a string literal type and T is the primitive type String. + ---*/ + + +var T: string = "T"; +type S = "s" | "str" | "string"; +let s: S = "str" +T = s +Assert.equal(T, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_7.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..7fb70a98342465d3e222e141800095a0966128a3 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_7.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and S is a union type and each constituent type of S is assignable to T. + ---*/ + + +interface T { + name: string + age: number + hobby: string +} +interface Foo { + name: string + age: number +} +interface Bar { + hobby: string +} +type S = Foo & Bar +let t: T; +let s: S = { + name: "S", + age: 20, + hobby: "drawing" +} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_8.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..078398a96005ffa60f6f11b65efa58de16f58e58 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_8.ts @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and T is an intersection type and S is assignable to each constituent type of T. + ---*/ + + +interface T { + name: string + age: number +} +interface Foo { + name: string + age: number +} +interface Bar { + hobby: string +} +type S = Foo & Bar +let t: T; +let s: S = { + name: "S", + age: 20, + hobby: "drawing" +} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_9.ts b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..61ad9b19180103d585dcf34fcb427aed00620af4 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/assignment_compatibility/assignment_compatibility_9.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is assignable to a type T, and T is assignable from S, + if S has no excess properties with respect to T, and T is a union type and S is assignable to at least one constituent type of T. + ---*/ + + +interface Foo { + name: string +} +interface Bar { + age: number +} +interface S { + age: number +} +type T = Foo | Bar +let t: T +let s: S = { + age: 18 +} +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/contextual_signature_instantiation/contextual_signature_instantiation.ts b/test_framework/test/spec/types/type_relationships/contextual_signature_instantiation/contextual_signature_instantiation.ts new file mode 100644 index 0000000000000000000000000000000000000000..075bbe057e7f062999f33e57de99951433ad3a48 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/contextual_signature_instantiation/contextual_signature_instantiation.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + During type argument inference in a function call, + it is in certain circumstances necessary to instantiate a generic call signature of an argument expression in the context of a non-generic call signature of a parameter such that further inferences can be made. + ---*/ + + +function zip(x: S[], y: T[], combine: (x: S) => (y: T) => U): U[] { + var len = Math.max(x.length, y.length); + var result: U[] = []; + for (var i = 0; i < len; i++) result.push(combine(x[i])(y[i])); + return result; +} + +var names = ["Peter", "Paul", "Mary"]; +var ages = [7, 9, 12]; +// the type of 'pairs' is '{ name: string; age: number }[]' +var pairs = zip(names, ages, s => n => ({ name: s, age: n })); +Assert.equal(JSON.stringify(pairs), '[{"name":"Peter","age":7},{"name":"Paul","age":9},{"name":"Mary","age":12}]') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_1.ts b/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..588b48c923bdeadb659022a2272de977217345d2 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_1.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the type contains only optional properties, without the excess property check, + any object literal would be assignable to the variable. + ---*/ + + +interface CompilerOptions { + strict?: boolean + sourcePath?: string + targetPath?: string +} +var options: CompilerOptions = { + strict: true, + sourcePath: "./src", + targetPath: "./bin" +} +Assert.equal(JSON.stringify(options), '{"strict":true,"sourcePath":"./src","targetPath":"./bin"}') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_2.ts b/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..755e65fc27f37f6ddc52cd7a56736c06d6182dfc --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_2.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In cases where excess properties are expected, an index signature can be added to the target type as an indicator of intent. + ---*/ + + +interface InputElement { + name: string; + visible?: boolean; + // Allow additional properties of any type + [x: string]: any; +} + +var address: InputElement = { + name: "Address", + visible: true, + // Allowed because of index signature + help: "Enter address here", + // Allowed because of index signature + shortcut: "Alt-A" +}; +Assert.equal(JSON.stringify(address), '{"name":"Address","visible":true,"help":"Enter address here","shortcut":"Alt-A"}') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_3.ts b/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..18c46dd85cca8928b41ca18f2e878286e6d8c9e4 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/excess_properties/excess_properties_3.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The type inferred for an object literal is considered a fresh object literal type. + The freshness disappears when an object literal type is widened or is the type of the expression in a type assertion. + ---*/ + + +// { x: 0, y: 0 } is the fresh object literal type +const p: { x: number, y: number } = { x: 0, y: 0 } +Assert.equal(JSON.stringify(p), '{"x":0,"y":0}') + +// The freshness disappears when the type of the express in a type assertion +// There is Excess Property y but no error +const p1: { x: number } = { x: 0, y: 0 } as { x: 0, y: 0 } +Assert.equal(JSON.stringify(p1), '{"x":0,"y":0}') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/recursive_types/recursive_types.ts b/test_framework/test/spec/types/type_relationships/recursive_types/recursive_types.ts new file mode 100644 index 0000000000000000000000000000000000000000..341a2238bc96aa5a8a7d625490bd0c44a82316a9 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/recursive_types/recursive_types.ts @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + when comparing types S and T for a given relationship, + the relationship in question is assumed to be true for every directly or indirectly nested occurrence of the same S and the same T + ---*/ + + +interface A { + next: A; +} +interface B { + next: C; +} +interface C { + next: D; +} +interface D { + next: B; +} +type IsEqual = + (() => T1 extends T ? 1 : 2) extends + (() => T2 extends U ? 1 : 2) + ? true + : false +var t1: IsEqual = true +Assert.isTrue(t1) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_1.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..233a725f0e6d0ae8710dffbbf77392f51f87e407 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_1.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, S and T are identical types. + ---*/ + + +interface T { + name: string + age: number +} +interface S { + name: string + age: number + hobbies: string[] +} +let t: T = { + name: "T", + age: 20 +}; +let s: S = { + name: "S", + age: 19, + hobbies: ["draw", "game"] +}; + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_10.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_10.ts new file mode 100644 index 0000000000000000000000000000000000000000..4eb374f44b5a2c5332db540c908bb61f46cea143 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_10.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and T is an intersection type and S is a subtype of each constituent type of T. + ---*/ + + +interface S { + name: string + age: number + height: number +} +interface Foo { + name: string +} +interface Bar { + age: number +} +type T = Foo & Bar +let s: S = { + name: "T", + age: 18, + height: 180 +} +let t: T = { + name: "S", + age: 20 +} + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_11.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_11.ts new file mode 100644 index 0000000000000000000000000000000000000000..baae91c77854b3ca2a777c88e50e78889f62b8c1 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_11.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is a type parameter and the constraint of S is a subtype of T. + ---*/ + + +let test = (obj: T, key: S): any => { + return obj[key]; +} +let obj = { + a: 'a', + b: 'b' +} +let res = test(obj, "a"); +Assert.equal(res, 'a'); \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_12.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_12.ts new file mode 100644 index 0000000000000000000000000000000000000000..b8805993a492d7355bff65553b413ff2f0e44b66 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_12.ts @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, + and for each member M in T, M is a property and S has an apparent property N where, + M and N have the same name, + the type of N is a subtype of that of M, + if M is a required property, N is also a required property, and + M and N are both public, M and N are both private and originate in the same declaration, + M and N are both protected and originate in the same declaration, + or M is protected and N is declared in a class derived from the class in which M is declared. + ---*/ + + +type T = { + name: any + age: number +} +type S = { + name: string + age: number + height: number +} +let t: T = { + name: "xiao", + age: 19 +} +let s: S = { + name: "Xi", + age: 20, + height: 180 +} + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_13.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_13.ts new file mode 100644 index 0000000000000000000000000000000000000000..178c291eca4e5d7ce2dce60cd524374979463315 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_13.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, + when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any), + and for each member M in T, M is a property and S has an apparent property N where, + the signatures are of the same kind (call or construct), + M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M, + for parameter positions that are present in both signatures, each parameter type in N is a subtype or supertype of the corresponding parameter type in M, + and the result type of M is Void, or the result type of N is a subtype of that of M. + ---*/ + + +interface T { + (x: any, y: any): void +} +interface S { + (x: number): void +} +var t: T = (x, y): void => { + return; +} +var s: S = (x): void => { + return; +} + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_14.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_14.ts new file mode 100644 index 0000000000000000000000000000000000000000..aa4e8faac7a94e03ecfdb2cfc9f40cad058c556f --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_14.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, + and for each member M in T, M is a string index signature of type U, + and U is the Any type or S has an apparent string index signature of a type that is a subtype of U. + ---*/ + + +interface T { + name: string +} +interface S { + name: string + age: number +} +interface U { + [name: string]: any +} +let obj: U = { name: "xiao", age: 18 } +Assert.equal(JSON.stringify(obj), '{"name":"xiao","age":18}') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_15.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_15.ts new file mode 100644 index 0000000000000000000000000000000000000000..5d195b193642927a29748c242ae79c9ba5aa4586 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_15.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, + and for each member M in T, M is a numeric index signature of type U, + and U is the Any type or S has an apparent string or numeric index signature of a type that is a subtype of U. + ---*/ + + +interface T { + age: number +} +interface S { + age: number + height: number +} +interface U { + [age: number]: any +} +let obj: U = {} +obj[5] = { age: 18, height: 180 } +Assert.equal(JSON.stringify(obj[5]), '{"age":18,"height":180}') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_16.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_16.ts new file mode 100644 index 0000000000000000000000000000000000000000..be1ecf030c50a491582825c32b5e432acefd1e12 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_16.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + the only subtypes of a type parameter T are T itself and other type parameters that are directly or indirectly constrained to T. + ---*/ + + +function getProperty(obj: T, key: K) { + return obj[key]; +} +let x = { a: 1, b: 2, c: 3, d: 4 }; +Assert.equal(getProperty(x, 'a'), 1) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_2.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..9cec27810bd5e070cad1d9f3f46c7ee545948896 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_2.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, and T is the Any type. + ---*/ + + +interface T { + name: any + age: any +} +interface S { + name: string + age: number + hobbies: string[] +} +let t: T = { + name: "T", + age: 20 +}; +let s: S = { + name: "S", + age: 19, + hobbies: ["draw", "game"] +}; + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_3.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..20101a7e2e32539b8c923d33923ce2ad66d88eb9 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_3.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T and S is the Undefined type. + ---*/ + + +interface T { + name: string | undefined + age: number | undefined +} +interface S { + name: undefined + age: undefined + hobbies: undefined +} +let t: T = { + name: "T", + age: 20 +}; + +let s: S = { + name: undefined, + age: undefined, + hobbies: undefined +}; + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_4.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..30796c64db9745fe34b2a6a78da2c841b6718147 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_4.ts @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, and S is the Null type and T is not the Undefined type. + ---*/ + + +interface T { + name: any + age: any +} +interface S { + name: null + age: null + hobbies: null +} +let t: T = { + name: "T", + age: 20 +}; + +let s: S = { + name: null, + age: null, + hobbies: null +}; + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_5.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..84cacc1a0df812edc8ca16098f5c7f5dece44b88 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_5.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, and S is an enum type and T is the primitive type Number. + ---*/ + + +var T: number = 4; +enum S { + Mon = 1, + Tue, + Wde, + Thur, + Fri +} + +T = S.Mon; +Assert.equal(T, S.Mon) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_6.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..5faf6789e990edafbe5c62e459d054079f62826e --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_6.ts @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is a string literal type and T is the primitive type String. + ---*/ + + +var T: string = "T"; +type S = "s" | "str" | "string"; +let s: S = "str" + +T = s +Assert.equal(T, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_7.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..22408d0ebc14865cb0b929c472c7d1d750663ceb --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_7.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is a union type and each constituent type of S is a subtype of T. + ---*/ + + +var T: any = true; +var S: number | string = 18; + +T = S +Assert.equal(T, 18) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_8.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_8.ts new file mode 100644 index 0000000000000000000000000000000000000000..49344fe9092b01ef0e91cf4866da763b2be22e7b --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_8.ts @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and S is an intersection type and at least one constituent type of S is a subtype of T. + ---*/ + + +interface T { + name: string + age: number +} +interface Foo { + name: string + age: number +} +interface Bar { + hobby: string +} +type S = Foo & Bar +let t: T = { + name: "T", + age: 18 +} +let s: S = { + name: "S", + age: 20, + hobby: "drawing" +} + +t = s +Assert.equal(t, s) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_9.ts b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_9.ts new file mode 100644 index 0000000000000000000000000000000000000000..891094696be034aad1924f7138d46193c2434495 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/subtypes_and_supertypes/subtypes_and_supertypes_9.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + S is a subtype of a type T, and T is a supertype of S, + if S has no excess properties with respect to T, + and T is a union type and S is a subtype of at least one constituent type of T. + ---*/ + + +var T: number | string = "18"; +var S: number = 20; + +T = S +Assert.equal(T, S) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_1.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..b1c1842f16b97f241d8b6f5f6e3054af9cfdc7e6 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_1.ts @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In certain contexts, + inferences for a given set of type parameters are made from a type S, in which those type parameters do not occur, + to another type T, in which those type parameters do occur. + ---*/ + + +interface Cb { + (a: number): void +} +interface Fn { + (cb: Cb): void +} +const fn: Fn = function (cb) { } +// The argument a to the anonymous function is of type number +fn(function (a) { + Assert.isNumber(a) + a = a + 1 +}) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_2.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..4e738df5e7c701e485d3edfac9696819eab686a9 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are references to the same generic type, + inferences are made from each type argument in S to each corresponding type argument in T. + ---*/ + + +function map(a: T[], f: (x: T) => U): U[] { + var result: U[] = []; + for (var i = 0; i < a.length; i++) result.push(f(a[i])); + return result; +} + +var names = ["Peter", "Paul", "Mary"]; +// the type of lengths is number[] +var lengths = map(names, s => s.length); +Assert.equal(typeof lengths, 'object') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_3.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..304a8ffd4bcae1f29a31e6610b6cecb66c0f16c8 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_3.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are tuple types with the same number of elements, + inferences are made from each element type in S to each corresponding element type in T. + ---*/ + + +// the type of T is string | number | boolean +let T = [1, 'a', true] +Assert.equal(typeof T, 'object') +// the type of S is string | number | number[] +let S = [[1, 2, 3], 2, 'b'] +Assert.equal(typeof S, 'object') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_4.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..d4c8e1af55e775e09608ea742cace6e741f70939 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_4.ts @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if T is a union or intersection type: + First, inferences are made from S to each constituent type in T + that isn't simply one of the type parameters for which inferences are being made. + If the first step produced no inferences then + if T is a union type and exactly one constituent type in T is simply a type parameter for which inferences are being made, + inferences are made from S to that type parameter. + Otherwise, if S is a union or intersection type, inferences are made from each constituent type in S to T. + ---*/ + + +type Maybe = T | void +function isDefined(x: Maybe): x is T { + return x! == undefined && x !== null; +} +function isUndefined(x: Maybe): x is void { + return x === undefined || x === null; +} +function getOrElse(x: Maybe, defaultValue: T): T { + return isDefined(x) ? x : defaultValue; +} +function test1(x: Maybe) { + // x1,x2,x3 are of type string + let x1 = getOrElse(x, "Undefined"); + Assert.isString(x1) + let x2 = isDefined(x) ? x : "undefined"; + Assert.isString(x2) + let x3 = isUndefined(x) ? "Undefined" : x; + Assert.isString(x3) +} +test1('t1') +function test2(x: Maybe) { + // x1,x2,x3 are of type number + let x1 = getOrElse(x, - 1); + Assert.isNumber(x1) + let x2 = isDefined(x) ? x : -1; + Assert.isNumber(x2) + let x3 = isUndefined(x) ? -1 : x; + Assert.isNumber(x3) +} +test2(5) \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_5.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..bc52941ec4b91eaa2af9c6ebb899d540d545b119 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_5.ts @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are object types, then for each member M in T, + If M is a property and S contains a property N with the same name as M, + inferences are made from the type of N to the type of M. + ---*/ + + +interface T { + name: any +} +interface S { + name: string +} +var t: T +var s: S = { name: "xxx" } +t = s + +Assert.isString(t.name) diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_6.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..1584f059850afa01c1b681547ca6c50156d51494 --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_6.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are object types, then for each member M in T, + if M is a string index signature and S contains a string index signature N, + inferences are made from the type of N to the type of M. + ---*/ + + +interface T { + [key: string]: string +} +interface S { + [value: string]: string +} +let s: S = { + 'name': 'xiao' +} +Assert.equal(typeof s, 'object') \ No newline at end of file diff --git a/test_framework/test/spec/types/type_relationships/type_inference/type_inference_7.ts b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..9da3f0938372c4f4ae0147e7afc51d34e30281df --- /dev/null +++ b/test_framework/test/spec/types/type_relationships/type_inference/type_inference_7.ts @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + if S and T are object types, then for each member M in T, + if M is a numeric index signature and S contains a numeric index signature N, + inferences are made from the type of N to the type of M. + ---*/ + + +interface T { + [key: number]: string +} +let S: { [value: number]: T } = {} +Assert.equal(typeof S, 'object') \ No newline at end of file diff --git a/test_framework/test/spec/types/union_types/union_types_1.ts b/test_framework/test/spec/types/union_types/union_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..573ca8846b0f8032d51619a35520a21731b26965 --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_1.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + union types represent values that may have one of several distinct representations. + a value of a union type A | B is a value that is either of type A or type B. Union types are written using union type literals + ---*/ + + +var n_s: number | string; +var b_s: boolean | string; +var o_na: object | number[]; +n_s = 2048; +Assert.isNumber(n_s); +b_s = false; +Assert.isFalse(b_s); +n_s = "NARC"; +Assert.isString(n_s); +b_s = "真"; +Assert.equal(b_s, "真"); +o_na = { 0x00: "0x00", 0xff: "0xFF" }; +Assert.equal(JSON.stringify(o_na), '{"0":"0x00","255":"0xFF"}'); +o_na = [0, 2, 4, 6, 8]; +Assert.equal(JSON.stringify(o_na), '[0,2,4,6,8]'); + diff --git a/test_framework/test/spec/types/union_types/union_types_2.ts b/test_framework/test/spec/types/union_types/union_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..b6c149b1f556230ff0409572158051ac98b9a81b --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_2.ts @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + a union type encompasses an ordered set of constituent types. + while it is generally true that A | B is equivalent to B | A, the order of the constituent types may matter when determining the call and construct signatures of the union type. + ---*/ + + +var ab: number | string; +var ba: string | number; +ab = 1408; +ba = 1408; +Assert.equal(ab, ba); +Assert.equal(typeof ab, typeof ba); +ab = "Shift"; +ba = "Shift"; +Assert.equal(ab, ba); +Assert.equal(typeof ab, typeof ba); diff --git a/test_framework/test/spec/types/union_types/union_types_3.ts b/test_framework/test/spec/types/union_types/union_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..e1560272532e1c63b2a3130bd9bf21bb8cf20b4e --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_3.ts @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + a union type U is a subtype of a type T if each type in U is a subtype of T. + a type T is a subtype of a union type U if T is a subtype of any type in U. + ---*/ + + +class ClassT { + size: number = 0; + description: string = ''; +} + +class ClassU1 extends ClassT { + alive: boolean = false; +} + +class ClassU2 extends ClassT { + weight: number = 0; +} + +interface InterfaceU1 { + speak(): string; +} + +interface InterfaceU2 { + eat(): string; +} + +class ClassT2 implements InterfaceU1, InterfaceU2 { + food: string = ''; + language: string = ''; + speak() { + return this.language; + } + + eat() { + return this.food; + } + constructor(food: string, language: string) { + this.food = food; + this.language = language; + } +} + +var u1: ClassU1 | ClassU2 = { size: 7, description: "A", alive: false }; +var t1: ClassT; +t1 = u1; +Assert.equal(JSON.stringify(t1), '{"size":7,"description":"A","alive":false}'); + +var u2: InterfaceU1 | InterfaceU2; +var t2: ClassT2 = new ClassT2("rice", "Chinese"); +u2 = t2; +Assert.equal(JSON.stringify(u2), '{"food":"rice","language":"Chinese"}') + diff --git a/test_framework/test/spec/types/union_types/union_types_4.ts b/test_framework/test/spec/types/union_types/union_types_4.ts new file mode 100644 index 0000000000000000000000000000000000000000..2ac9e205041f7abe84fe283a6fd7c6af6be2ac4e --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_4.ts @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + a union type U is assignable to a type T if each type in U is assignable to T. + a type T is assignable to a union type U if T is assignable to any type in U. + ---*/ + + +type numType = { num: number }; +type strType = { str: string }; +type boolType = { bool: boolean }; +type objType = { obj: Object }; +var u4a: any; +var u4: numType | strType | boolType | objType | undefined; +var u4_1: numType = { num: 0xCA }; +u4a = u4_1; +Assert.equal(JSON.stringify(u4a), '{"num":202}'); + +var u4_2: strType = { str: "QWER" }; +u4a = u4_2; +Assert.equal(JSON.stringify(u4a), '{"str":"QWER"}'); + +var u4_3: boolType = { bool: false }; +u4a = u4_3; +Assert.equal(JSON.stringify(u4a), '{"bool":false}'); + +var u4_4: objType = { obj: { 0: "ZERO" } }; +u4a = u4_4; +Assert.equal(JSON.stringify(u4a), '{"obj":{"0":"ZERO"}}'); + + +u4 = { num: 0xCA, str: "ABC", bool: false, obj: u4_4 }; +u4a = u4; +Assert.equal(JSON.stringify(u4a), '{"num":202,"str":"ABC","bool":false,"obj":{"obj":{"0":"ZERO"}}}'); + + +u4 = u4_1; +Assert.equal(JSON.stringify(u4), '{"num":202}'); + +u4 = u4_2; +Assert.equal(JSON.stringify(u4), '{"str":"QWER"}'); + +u4 = u4_3; +Assert.equal(JSON.stringify(u4), '{"bool":false}'); + +u4 = u4_4; +Assert.equal(JSON.stringify(u4), '{"obj":{"0":"ZERO"}}'); diff --git a/test_framework/test/spec/types/union_types/union_types_5.ts b/test_framework/test/spec/types/union_types/union_types_5.ts new file mode 100644 index 0000000000000000000000000000000000000000..e0cc00dd9248e5cb2f540f6dbd8ececd90885e72 --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_5.ts @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + the || and conditional operators may produce values of union types, and array literals may produce array values that have union types as their element types. + ---*/ + + +var ns5: A1 = 1408 || "NARC"; +var ns5_1: number | string; +var ns5_2: string | number; +ns5 = 1500; +ns5_2 = ns5 <= 1408 ? "NARC" : 1024; +Assert.equal(ns5_2, 1024); +ns5 = 100; +ns5_2 = ns5 <= 1408 ? "NARC" : 1024; +Assert.equal(ns5_2, "NARC"); + +type A1 = string | number | object; +type B1 = number | boolean | string; +var ns5_3: A1 & B1; +ns5_3 = 125; +Assert.isNumber(ns5_3); +ns5_3 = "Fn"; +Assert.isString(ns5_3); + +var arruni1: Array | Array; +arruni1 = [0, true, -1, false]; +Assert.equal(JSON.stringify(arruni1), '[0,true,-1,false]'); +arruni1 = [true, "True", false, "False"]; +Assert.equal(JSON.stringify(arruni1), '[true,"True",false,"False"]'); + +var arruni2: number[] | boolean[]; +arruni2 = [2, 4, 6]; +Assert.equal(JSON.stringify(arruni2), '[2,4,6]'); +arruni2 = [true, false]; +Assert.equal(JSON.stringify(arruni2), '[true,false]'); + +var arruni3: (number | string)[] | (boolean | Object)[]; +arruni3 = [1, 3, 5, "AND", "OR"]; +Assert.equal(JSON.stringify(arruni3), '[1,3,5,"AND","OR"]'); +arruni3 = [true, false, { 0x00: "0x00" }, { 0xFA: "0xFA" }]; +Assert.equal(JSON.stringify(arruni3), '[true,false,{"0":"0x00"},{"250":"0xFA"}]'); \ No newline at end of file diff --git a/test_framework/test/spec/types/union_types/union_types_6.ts b/test_framework/test/spec/types/union_types/union_types_6.ts new file mode 100644 index 0000000000000000000000000000000000000000..e7b2f93c4c7d6b1b11af34f3e9a64ff852ec03fb --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_6.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + type guards may be used to narrow a union type to a more specific type. + in particular, type guards are useful for narrowing union type values to a non-union type values. + ---*/ + + +var x: string | number; +x = 37; +x = "hello"; +var n = typeof x === "string" ? x.length : x; +Assert.isNumber(n) diff --git a/test_framework/test/spec/types/union_types/union_types_7.ts b/test_framework/test/spec/types/union_types/union_types_7.ts new file mode 100644 index 0000000000000000000000000000000000000000..27c4ccf1a24b486b2cdbe6b536da502154c8f0d6 --- /dev/null +++ b/test_framework/test/spec/types/union_types/union_types_7.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- +description: > + for purposes of property access and function calls, the apparent members of a union type are those that are present in every one of its constituent types, + with types that are unions of the respective apparent members in the constituent types. + ---*/ + + +type numType = { num: number }; +type strType = { str: string }; +type boolType = { bool: boolean }; +type objType = { obj: Object }; +type NS = numType | strType; +type OB = objType | boolType; +var nsv: NS = { num: 1024, str: "NS" }; +var obv: OB = { bool: true, obj: { 0xFF: "0xFF" } }; +var nsobv_1: NS | OB = { num: 1024, obj: { 0xFF: "0xFF" } } +var nsobv_2: NS | OB = { bool: false, str: "nsobv_2" }; +var nsobv_3: NS | OB = { num: 114, bool: false, str: "nsobv_3", obj: { 0xAF: "0xAF" } } +Assert.equal(typeof nsv, "object"); +Assert.equal(typeof obv, "object"); +Assert.equal(typeof nsobv_1, "object"); +Assert.equal(typeof nsobv_2, "object"); +Assert.equal(typeof nsobv_3, "object"); \ No newline at end of file diff --git a/test_framework/test/spec/types/widend_types/widend_types_1.ts b/test_framework/test/spec/types/widend_types/widend_types_1.ts new file mode 100644 index 0000000000000000000000000000000000000000..80714a690650b2ba772964b6dea961968c3aa226 --- /dev/null +++ b/test_framework/test/spec/types/widend_types/widend_types_1.ts @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + In several situations TypeScript infers types from context, + alleviating the need for the programmer to explicitly specify types that appear obvious. + ---*/ + + +var str = "xiao" +Assert.isString(str); \ No newline at end of file diff --git a/test_framework/test/spec/types/widend_types/widend_types_2.ts b/test_framework/test/spec/types/widend_types/widend_types_2.ts new file mode 100644 index 0000000000000000000000000000000000000000..3d1e74c25e14c1112b0f8d8ed573acb9c2a6136d --- /dev/null +++ b/test_framework/test/spec/types/widend_types/widend_types_2.ts @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + When inferring the type of a variable, property or function result from an expression, + the widened form of the source type is used as the inferred type of the target. + ---*/ + + +var x = 10 +var y = 'a' +var z = x + y +Assert.isString(z); \ No newline at end of file diff --git a/test_framework/test/spec/types/widend_types/widend_types_3.ts b/test_framework/test/spec/types/widend_types/widend_types_3.ts new file mode 100644 index 0000000000000000000000000000000000000000..5076a2a050edbfb0c48b45a36bb1f852d535087b --- /dev/null +++ b/test_framework/test/spec/types/widend_types/widend_types_3.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/**--- + description: > + The widened form of a type is the type in which all occurrences of the Null and Undefined types have been replaced with the type any. + ---*/ + + +var a = null +console.log(typeof a) +Assert.equal(typeof a, 'object') +var b = undefined +Assert.isUndefined(b) +var c = { x: 0, y: null } +Assert.equal(typeof c, 'object') +var d = [null, undefined] +Assert.equal(typeof d, 'object') \ No newline at end of file diff --git a/test_framework/tool/test_helper.py b/test_framework/tool/test_helper.py new file mode 100644 index 0000000000000000000000000000000000000000..be23f7ffc290e6bc67ff8e01a8524ae69ad616d9 --- /dev/null +++ b/test_framework/tool/test_helper.py @@ -0,0 +1,64 @@ +import re +import os + +def read_declaration(path): + start_pattern = re.compile(r'^\/\*\*\-*') + end_pattern = re.compile(r'^\s*\-+\*\/') + context = "" + with open(path,'r', encoding='utf-8', errors='ignore') as f: + declaration_begin = False + while True: + line = f.readline() + if not line: + break + if start_pattern.match(line): + declaration_begin = True + continue + if end_pattern.match(line): + declaration_begin = False + break + if declaration_begin == True: + context += line + return context + +def get_path_file(dir_path, all_file_path=None): + if all_file_path is None: + all_file_path = [] + file_or_dir = os.listdir(dir_path) + for file_dir in file_or_dir: + file_or_dir_path = os.path.join(dir_path, file_dir) + if '\\' in file_or_dir_path: + file_or_dir_path = file_or_dir_path.replace('\\', '/') + + # 判断该路径是不是路径,如果是,递归调用 + if os.path.isdir(file_or_dir_path): + # 递归 + get_path_file(file_or_dir_path, all_file_path) + else: + all_file_path.append(file_or_dir_path) + + return all_file_path + +def get_disable_list(file_path): + disable_list = [] + with open(file_path,'r', encoding='utf-8', errors='ignore') as f: + while True: + line = f.readline() + if not line: + break + disable_list.append(os.path.abspath(line.strip())) + return disable_list + +def is_disable_case(file_path, disable_list): + if disable_list == None: + return False + if file_path in disable_list: + return True + for disable_path in disable_list: + if disable_path in file_path: + return True + + + + + diff --git a/test_framework/tool/testcfg.py b/test_framework/tool/testcfg.py new file mode 100644 index 0000000000000000000000000000000000000000..d513e6bfe14ff39fc705faa03346b241044a4ecd --- /dev/null +++ b/test_framework/tool/testcfg.py @@ -0,0 +1,246 @@ +import os +import re +import yaml +import platform +import subprocess +from tool.test_helper import read_declaration + +STRICT_OFF = ['--strict', 'false'] +STRICT_ON = ['--strict', 'true'] +HARNESS_PATH = os.path.abspath('./harness/') +MODULE = ['--module'] + +def get_error_message(str, filename): + line_number = re.findall(filename + r':(\d+)', str)[-1] + err_message = str #re.findall(r'\[AssertionError\]: ([\S ]+?)\\', str)[0] + return err_message, line_number + + +class TestCase(): + + temp_path = "" + ld_library_path = "" + js_runtime_path = "" + + def __init__(self, path): + self.path = path + self.target_js_path ="" + try: + data = yaml.load(read_declaration(path), yaml.SafeLoader) + except: + data = {} + self.declaration = data + self.includes = ['assertionError.ts', 'assert.ts'] + self.fail = False + self.is_test_case = False if data is None else True + self.detail_result = "" + self.err_line = 0 + self.abc_file_path = "" + self.files_info_path = os.path.join(os.path.splitext(self.path)[0], '_filesInfo.txt') + + + def execute(self, arkruntime = False): + if not self.is_test_case: + return + if arkruntime: + self.__test_es2abc() + if(os.path.exists(self.abc_file_path)): + os.remove(self.abc_file_path) + else: + self.__tsc_test() + + def is_negative(self): + if 'error' in self.declaration: + return True + return False + def is_current(self): + if 'isCurrent' in self.declaration: + return True + return False + def is_set_module(self): + if 'module' in self.declaration: + return True + return False + '''检查测试用例声明是否合法''' + def check_declaration(self): + if self.declaration == {}: + self.detail_result = "parse test case declaration failed, maybe bad format." + return False + if 'error' in self.declaration: + if self.declaration['error'] is None or 'code' not in self.declaration['error'] and 'type' not in self.declaration['error']: + self.detail_result = "neither error code nor error type are defined in negative case." + return False + return True + + def __error_code(self): + if 'code' in self.declaration['error']: + return self.declaration['error']['code'] + return None + + def __error_type(self): + if 'type' in self.declaration['error']: + return self.declaration['error']['type'] + return None + + def __get_includes(self): + includes = ['assertionError.ts', 'assert.ts'] + if "includes" in self.declaration: + includes.extend(self.declaration['includes']) + for index, value in enumerate(includes): + includes[index] = os.path.join(HARNESS_PATH, value) + return includes + + def __get_tsc_cmd(self): + if platform.system().lower() == 'windows': + cmd = ['cmd', '/c', 'tsc', '--target', 'es2020'] + else: + cmd = ['tsc', '--target', 'es2020'] + if self.__is_strict(): + cmd.extend(STRICT_ON) + else: + cmd.extend(STRICT_OFF) + if self.is_set_module(): + cmd.extend(MODULE) + cmd.append('ESNext') + if self.is_current(): + cmd.append(self.path) + cmd.append('--outDir') + cmd.append(TestCase.temp_path) + self.target_js_path = TestCase.temp_path + self.__get_js_basename() + else: + cmd.append('--outFile') + cmd.append(TestCase.temp_path + self.__get_js_basename()) + cmd.extend(self.__get_includes()) + cmd.append(self.path) + cmd.append(HARNESS_PATH + "/console.ts") + print(' '.join(cmd)) + return cmd + + def __get_node_cmd(self): + cmd = ['node'] + if self.is_current(): + cmd.append(self.target_js_path) + else: + cmd.append(TestCase.temp_path + self.__get_js_basename()) + return cmd + + ''' + 生成fileinfo文件,供--merge-abc使用 + ''' + def __gen_files_info(self): + fd = os.open(self.files_info_path, os.O_RDWR | os.O_CREAT | os.O_TRUNC) + f = os.fdopen(fd, "w") + files = self.__get_includes() + files.append(self.path) + files.append(HARNESS_PATH + "print.ts") + for test_path in files: + record_name = os.path.relpath(test_path, os.path.dirname(self.files_info_path)).split('.')[0] + module_kind = "esm" + file_info = ('%s;%s;%s;%s;%s' % (test_path, record_name, module_kind, test_path, record_name)) + f.writelines(file_info + '\n') + f.close() + + ''' + 获取es2abc --merge-abc命令 + ''' + def __get_es2abc_cmd(self): + self.abc_file_path = ("%s.abc" % (os.path.splitext(self.path)[0])) + cmd = ['es2abc', '--merge-abc'] + cmd.extend(['--output', self.abc_file_path, self.files_info_path]) + return cmd + + ''' + 获取执行abc文件的命令 + ''' + def _get_ark_js_cmd(self): + os.environ.setdefault("LD_LIBRARY_PATH", TestCase.ld_library_path) + run_abc_cmd = [os.path.join(TestCase.js_runtime_path, 'ark_js_vm')] + run_abc_cmd.append(self.abc_file_path) + pass + + + def __get_js_basename(self): + return os.path.basename(self.path).replace('.ts', '.js') + + def __is_strict(self): + if 'strict' in self.declaration: + return bool(self.declaration['strict']) + return True + + def __tsc_test(self): + process = subprocess.Popen(self.__get_tsc_cmd(), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE) + out, err = process.communicate() + returncode = process.returncode + if self.is_negative(): + if returncode == 0: + self.fail = True + self.detail_result = "No error found in negative case." + return + if self.__error_code() in out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"): + return + self.fail = True + self.detail_result = "Error code not as expected." + return + #positive case + if returncode != 0 : + self.detail_result = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + self.fail = True + return + if self.is_current(): + with open(self.target_js_path, 'a') as fileAdded: + fileAdded.write('console.log("TESTCASE SUCCESS");') + #run node command + process = subprocess.Popen(self.__get_node_cmd(), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE) + out, err = process.communicate() + returncode = process.returncode + if self.is_current(): + if os.path.exists(self.target_js_path): + os.remove(self.target_js_path) + if returncode != 0: + err_msg, line = get_error_message(out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"), self.__get_js_basename()) + self.detail_result = err_msg + print(err_msg) + self.err_line = line + self.fail = True + return + # check std out + if "TESTCASE SUCCESS" not in out.decode("utf-8", errors="ignore"): + self.detail_result = "check stdout failed!" + self.fail = True + return + + def __test_es2abc(self): + self.__gen_files_info() + # compiler to abc + process = subprocess.Popen(self.__get_es2abc_cmd(), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE) + out, err = process.communicate() + returncode = process.returncode + if self.is_negative(): + if returncode == 0: + self.fail = True + return + if self.__error_type() in out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"): + return + self.fail = True + self.detail_result = "Error type not as expected." + return + #positive case + if returncode != 0 : + self.detail_result = out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore") + self.fail = True + return + # execute ark_js_vm + process = subprocess.Popen(self._get_ark_js_cmd(), stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE) + out, err = process.communicate() + returncode = process.returncode + if returncode != 0: + err_msg, line = get_error_message(out.decode("utf-8", errors="ignore") + err.decode("utf-8", errors="ignore"), os.path.basename(self.abc_file_path)) + self.detail_result = err_msg + self.err_line = line + self.fail = True + return + # check std out + if "TESTCASE SUCCESS" not in out.decode("utf-8", errors="ignore"): + self.detail_result = "check stdout failed!" + self.fail = True + return \ No newline at end of file diff --git a/test_framework/tsstt.py b/test_framework/tsstt.py new file mode 100644 index 0000000000000000000000000000000000000000..299e2775fce5e56eb54807c9517f4e49b5a069e9 --- /dev/null +++ b/test_framework/tsstt.py @@ -0,0 +1,124 @@ +# importing required modules +import argparse +import os +import shutil +import tempfile + +from tool.test_helper import get_path_file, get_disable_list, is_disable_case +from tool.testcfg import TestCase + + +TEST_PATH = './' +TEST_TMP_PATH = '/testTmp/' +TEMP_PATH = os.getcwd() + TEST_TMP_PATH + + +if os.path.exists(TEMP_PATH): + shutil.rmtree(TEMP_PATH) + + +if (os.path.exists(TEMP_PATH) == False): + os.mkdir(TEMP_PATH) + +total_case = 0 +failed_case = 0 +TestCase.temp_path = TEMP_PATH +def is_testcase_exist(parser, arg): + if not os.path.isabs(arg): + arg = TEST_PATH + arg + if not os.path.exists(arg): + parser.error("The directory or file '%s' does not exist" % arg) + return os.path.abspath(arg) + +def is_file(parser, arg): + if not os.path.isfile(arg): + parser.error("The file '%s' does not exist" % arg) + + return os.path.abspath(arg) + +def is_directory(parser, arg): + if not os.path.isdir(arg): + parser.error("The directory '%s' does not exist" % arg) + + return os.path.abspath(arg) + +def parse_and_execute(path, arkruntime = False, skip_negative = True): + if path.endswith(".ts"): + test_case = TestCase(path) + if not test_case.is_test_case: + return False, False + # check test case declare + if not test_case.check_declaration(): + print(test_case.path, test_case.detail_result, sep='\t') + return True, True + if skip_negative and test_case.is_negative(): + return False, False + else: + test_case.execute(arkruntime) + if test_case.fail: + print('TESTCASE Fail! Fail reason is coming:') + print(test_case.path, test_case.detail_result, sep='\t') + return True, True + return True, False + +# create a parser object +parser = argparse.ArgumentParser(description = "TypeScript Spec&Feature Test Tool") + + +# 指定测试范围,可以指定多个目录 or 文件,指定目录下的所有测试都会被执行 +parser.add_argument("release", nargs = '*', metavar = "release", type = lambda arg: is_testcase_exist(parser, arg), + help = "All test case in the release will be execute") + +parser.add_argument("-a", "--arkruntime", action="store_true", default=False, help= "test on arkruntime") + +parser.add_argument("-s", "--skip-abnormal-case", action="store_true", default=False, help= "skip abnormal test case") + +# skip list 被指定的场合,优先级最高,例如一个case在ski-list的文件中被指定,同时也被指定执行,那么它会被忽略 +parser.add_argument("-d", "--disable-list", type= lambda arg: is_file(parser, arg), default=None, + help= "path to the file that contains test to skip") + +parser.add_argument( + '--js-runtime', dest='js_runtime_path', default=None, type=lambda arg: is_directory(parser, arg), + help='the path of js vm runtime') +parser.add_argument( + '--LD_LIBRARY_PATH', dest='ld_library_path', default=None, help='LD_LIBRARY_PATH') + +# parse the arguments from standard input +args = parser.parse_args() +if args.js_runtime_path: + TestCase.js_runtime_path = args.js_runtime_path +if args.ld_library_path: + TestCase.ld_library_path = args.ld_library_path + +disable_list = [] +if args.disable_list: + disable_list = get_disable_list(args.disable_list) + +# 执行测试脚本、这里需要分情况 tsc + node / es2abc +print("TEST CASE", "FAIL REASON", "FAIL LINE", sep="\t") +for file_path in args.release: + if is_disable_case(file_path, disable_list): + continue + if os.path.isfile(file_path): + is_test_count , failed = parse_and_execute(file_path, args.arkruntime, args.skip_abnormal_case) + if is_test_count: + total_case += 1 + if failed: + failed_case += 1 + continue + for file_path in get_path_file(file_path): + if False == file_path.endswith(".ts"): + continue + if is_disable_case(file_path, disable_list): + continue + is_test_count , failed = parse_and_execute(file_path, args.arkruntime, args.skip_abnormal_case) + if is_test_count: + total_case += 1 + if failed : + failed_case += 1 + +print("TOTAL CASE COUNT:%d" % total_case) +print("FAILED CASE COUNT:%d" % failed_case) +# delete temp dir +if os.path.exists(TEMP_PATH): + shutil.rmtree(TEMP_PATH) \ No newline at end of file