From e286e0956ff1abb33db0b1e957aeecbda24903eb Mon Sep 17 00:00:00 2001 From: bergamot14 Date: Mon, 23 Oct 2023 18:57:18 +0300 Subject: [PATCH] add lambda tests Signed-off-by: Tokmakov Alexander --- .../lambdas/callArrayLengthFromLambda.ets | 17 +++++++++++ .../callAsyncMethodFromAsyncLambda1.ets | 30 +++++++++++++++++++ .../callAsyncMethodFromAsyncLambda2.ets | 28 +++++++++++++++++ .../lambdas/callMethodFromAsyncLambda1.ets | 29 ++++++++++++++++++ .../lambdas/callMethodFromAsyncLambda2.ets | 25 ++++++++++++++++ .../spec/lambdas/callRegularLambda.ets | 30 +++++++++++++++++++ .../callRegularLambdaWithAsyncOverload.ets | 27 +++++++++++++++++ .../spec/lambdas/compareBooleanWithTrue.ets | 23 ++++++++++++++ .../incorrectAsyncLambdaReturnValue.ets | 11 +++++++ .../lambdas/incorrectVariableDeclaration.ets | 13 ++++++++ .../spec/lambdas/resultFromAwait.ets | 24 +++++++++++++++ .../lambdas/resultFromAwaitResolution.ets | 24 +++++++++++++++ .../plugins/ets/ets-func-tests-ignored.txt | 23 ++++++++++++++ 13 files changed, 304 insertions(+) create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callArrayLengthFromLambda.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda1.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda2.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda1.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda2.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambda.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambdaWithAsyncOverload.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/compareBooleanWithTrue.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectAsyncLambdaReturnValue.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectVariableDeclaration.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwait.ets create mode 100644 plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwaitResolution.ets diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callArrayLengthFromLambda.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callArrayLengthFromLambda.ets new file mode 100644 index 000000000..036cde145 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callArrayLengthFromLambda.ets @@ -0,0 +1,17 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callArrayLengthFromLambda +tags: [compile-only] +---*/ + +function callArrayLengthFromLambda() { + const cb: (arg: number) => void = + (arg: number) => { + let arr: number[] = []; + arr.length; + } +} + +function main(): void { + callArrayLengthFromLambda(); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda1.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda1.ets new file mode 100644 index 000000000..5f84ce610 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda1.ets @@ -0,0 +1,30 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callAsyncMethodFromAsyncLambda1 +tags: [] +---*/ + +type asyncLambda = () => Promise + +class LambdaTests { + async asyncFunc(): Promise { + return true; + } + + testCallAsyncLambda(): void { + let call_async_lambda = false; + + let async_lambda: asyncLambda = async (): Promise => { + await this.asyncFunc(); + call_async_lambda = true; + } + + assert(call_async_lambda == false); + await async_lambda(); + assert(call_async_lambda == true); + } +} + +function main(): void { + new LambdaTests().testCallAsyncLambda(); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda2.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda2.ets new file mode 100644 index 000000000..6a5793cb8 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callAsyncMethodFromAsyncLambda2.ets @@ -0,0 +1,28 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callAsyncMethodFromAsyncLambda2 +tags: [] +---*/ + +type asyncLambda = () => Promise + +async function asyncFunc(): Promise { + return true; +} + +function testCallAsyncLambda(): void { + let call_async_lambda = false; + + let async_lambda: asyncLambda = async (): Promise => { + await asyncFunc(); + call_async_lambda = true; + } + + assert(call_async_lambda == false); + await async_lambda(); + assert(call_async_lambda == true); +} + +function main(): void { + testCallAsyncLambda(); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda1.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda1.ets new file mode 100644 index 000000000..68d4e3612 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda1.ets @@ -0,0 +1,29 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callMethodFromAsyncLambda1 +tags: [] +---*/ + +type asyncLambda = () => Promise + +class LambdaTests { + regularFunc(): boolean { + return true; + } + + testCallFromAsyncLambdaRegularFunc(): void { + let is_call_async_lambda = false; + + let async_lambda: asyncLambda = async (): Promise => { + is_call_async_lambda = this.regularFunc(); + } + + assert(is_call_async_lambda == false); + await async_lambda(); + assert(is_call_async_lambda == true); + } +} + +function main(): void { + new LambdaTests().testCallFromAsyncLambdaRegularFunc(); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda2.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda2.ets new file mode 100644 index 000000000..c9a5b13e9 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callMethodFromAsyncLambda2.ets @@ -0,0 +1,25 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callMethodFromAsyncLambda2 +tags: [] +---*/ + +type asyncLambda = () => Promise + +function regularFunc(): boolean { + return true; +} + +function testCallFromAsyncLambdaRegularFunc(): void { + let is_call_async_lambda = false; + let async_lambda: asyncLambda = async (): Promise => { + is_call_async_lambda = regularFunc(); + } + assert(is_call_async_lambda == false); + await async_lambda(); + assert(is_call_async_lambda == true); +} + +function main(): void { + testCallFromAsyncLambdaRegularFunc(); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambda.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambda.ets new file mode 100644 index 000000000..6dea3dda6 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambda.ets @@ -0,0 +1,30 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callRegularLambda +tags: [] +---*/ + +type regularLambda = () => void + +class Test { + regularFunc(): boolean { + return true; + } + + testCallRegularLambda(): void { + let is_call_regular_lambda: boolean = false; + + let regular_lambda: regularLambda = (): void => { + this.regularFunc(); + is_call_regular_lambda = true; + } + + assert(is_call_regular_lambda == false); + regular_lambda(); + assert(is_call_regular_lambda == true); + } +} + +function main(): void { + new Test().testCallRegularLambda(); +} diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambdaWithAsyncOverload.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambdaWithAsyncOverload.ets new file mode 100644 index 000000000..96ab85ae2 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/callRegularLambdaWithAsyncOverload.ets @@ -0,0 +1,27 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/callRegularLambdaWithAsyncOverload +tags: [] +---*/ + +class Test { + regularFunc(): boolean { + return true; + } + + async regularFunc(param: () => boolean): Promise { + return param(); + } + + callOverloadRegularFunc(): void { + let is_call_regular_func1 = await this.regularFunc((): boolean => { return true }); + assert(is_call_regular_func1 == true); + + let is_call_regular_func2 = await this.regularFunc((): boolean => { return false }); + assert(is_call_regular_func2 == false); + } +} + +function main(): void { + new Test().callOverloadRegularFunc(); +} diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/compareBooleanWithTrue.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/compareBooleanWithTrue.ets new file mode 100644 index 000000000..1e92b8f44 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/compareBooleanWithTrue.ets @@ -0,0 +1,23 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/compareBooleanWithTrue +tags: [] +---*/ + +type asyncLambda = () => Promise + +function regularFunc(): boolean { + return true; +} + +function main(): void { + let booleanPromise: asyncLambda = async (): Promise => { + return Promise.resolve(regularFunc()); + } + + let is_resolution1: Boolean = new Boolean(true); + assert(is_resolution1 == true); + + let is_resolution2: Boolean = booleanPromise().awaitResolution(); + assert(is_resolution2 == true); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectAsyncLambdaReturnValue.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectAsyncLambdaReturnValue.ets new file mode 100644 index 000000000..96cb89301 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectAsyncLambdaReturnValue.ets @@ -0,0 +1,11 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/incorrectAsyncLambdaReturnValue +tags: [negative, compile-only] +---*/ + +type fooLambda = () => void + +function main(): void { + let x: fooLambda = async (): void => {} +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectVariableDeclaration.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectVariableDeclaration.ets new file mode 100644 index 000000000..f6fa058af --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/incorrectVariableDeclaration.ets @@ -0,0 +1,13 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/incorrectVariableDeclaration +tags: [negative, compile-only] +---*/ + +type fooLambda = () => int + +function foo() { + let y: int = 1; + let x = () => { return y+1 }; // must be - let x: fooLambda = () => { return y+1 }; + console.log(x()); +} diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwait.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwait.ets new file mode 100644 index 000000000..b0e25eeea --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwait.ets @@ -0,0 +1,24 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/resultFromAwait +tags: [] +---*/ + +type asyncLambda = () => Promise + +function regularFunc(): boolean { + return true; +} + +function main(): void { + let booleanPromise: asyncLambda = async (): Promise => { + return Promise.resolve(regularFunc()); + } + + let is_true: Boolean = new Boolean(true); + + let is_resolution2: Boolean = await booleanPromise(); + + assert is_resolution2 instanceof Boolean; + assert(is_resolution2 == is_true); +} \ No newline at end of file diff --git a/plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwaitResolution.ets b/plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwaitResolution.ets new file mode 100644 index 000000000..0aad7caa7 --- /dev/null +++ b/plugins/ets/tests/ets_func_tests/spec/lambdas/resultFromAwaitResolution.ets @@ -0,0 +1,24 @@ +/*--- +desc: 07.31 Lambda Expressions +name: 07.expressions/31.lambda_expressions/resultFromAwaitResolution +tags: [] +---*/ + +type asyncLambda = () => Promise + +function regularFunc(): boolean { + return true; +} + +function main(): void { + let booleanPromise: asyncLambda = async (): Promise => { + return Promise.resolve(regularFunc()); + } + + let is_true: Boolean = new Boolean(true); + + let is_resolution1: Boolean = booleanPromise().awaitResolution(); + + assert is_resolution1 instanceof Boolean; + assert(is_resolution1 == is_true); +} \ No newline at end of file diff --git a/tests/tests-u-runner/runner/plugins/ets/ets-func-tests-ignored.txt b/tests/tests-u-runner/runner/plugins/ets/ets-func-tests-ignored.txt index af134d128..d9117410f 100644 --- a/tests/tests-u-runner/runner/plugins/ets/ets-func-tests-ignored.txt +++ b/tests/tests-u-runner/runner/plugins/ets/ets-func-tests-ignored.txt @@ -107,3 +107,26 @@ escompat/escompat_Array_Test_escompat_Array_003.ets std/containers/BasicMapTest.ets std/containers/BasicSetTest.ets ### Issue xxx end + +### lambda issues begin +### Issue 14018 +spec/lambdas/callArrayLengthFromLambda.ets + +### Issue 14054 +spec/lambdas/callAsyncMethodFromAsyncLambda1.ets +spec/lambdas/callMethodFromAsyncLambda1.ets + +### Issue 14209 +spec/lambdas/callAsyncMethodFromAsyncLambda2.ets +spec/lambdas/callMethodFromAsyncLambda2.ets + +### Issue 14131 +spec/lambdas/callRegularLambdaWithAsyncOverload.ets + +### Issue 14234 +spec/lambdas/resultFromAwait.ets +spec/lambdas/resultFromAwaitResolution.ets + +### Issue 14235 +spec/lambdas/compareBooleanWithTrue.ets +### lambda issues end -- Gitee