diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java index 31fef1d87b6f..351655a59661 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/worker/BasicWorkerTest.java @@ -18,6 +18,7 @@ import io.ballerina.runtime.api.utils.StringUtils; import io.ballerina.runtime.api.values.BMap; +import org.ballerinalang.test.BAssertUtil; import org.ballerinalang.test.BCompileUtil; import org.ballerinalang.test.BRunUtil; import org.ballerinalang.test.CompileResult; @@ -143,10 +144,28 @@ public void testAsyncSendAsExpression(String funcName) { public Object[] asyncSendAsExpressionFunctions() { return new Object[]{ "testAsyncSendAsExpressionReturnType", - "testAsyncSendAsExpressionWithPanic" + "testAsyncSendAsExpressionWithPanic", + "testAsyncSendAsExpressionWithTrapAndCheckExpr", + "testAsyncSendAsExpressionWithTypeCastExpr", + "testAsyncSendAsExpressionWithReturnStmt", + "testAsyncSendAsExpressionWithWildCardBindingPattern", + "testAsyncSendAsExpressionWithMatchStmt" }; } + @Test + public void testAsyncSendNegative() { + CompileResult asyncSendNegativeResult = + BCompileUtil.compile("test-src/workers/worker_async_send_negative.bal"); + int i = 0; + BAssertUtil.validateError(asyncSendNegativeResult, i++, "incompatible types: '()' is not an " + + "iterable collection", 19, 26); + BAssertUtil.validateError(asyncSendNegativeResult, i++, "compound assignment not allowed with " + + "nullable operands", 34, 9); + BAssertUtil.validateError(asyncSendNegativeResult, i++, "operator '+' not defined for 'int' and '()'", 34, 9); + Assert.assertEquals(i, asyncSendNegativeResult.getErrorCount()); + } + @AfterClass public void tearDown() { result = null; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal b/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal index a1dfa75f2168..91bd2bf8bfbc 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_as_expression.bal @@ -1,6 +1,6 @@ -// Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) // -// WSO2 Inc. licenses this file to you under the Apache License, +// WSO2 LLC. licenses this file to you under the Apache License, // Version 2.0 (the "License"); you may not use this file except // in compliance with the License. // You may obtain a copy of the License at @@ -49,7 +49,91 @@ function testAsyncSendAsExpressionWithPanic() { x = <- w1; } - map mapResult = wait {w1, w2}; + map mapResult = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithTrapAndCheckExpr() { + worker w1 { + () a = check (5 -> w2); + assertValueEquality(a, ()); + error? b = trap (15 -> w2); + if b is error { + assertValueEquality("This is an error", b.message()); + } else { + assertValueEquality(b, ()); + } + } + + worker w2 returns error? { + int x = <- w1; + if (x == 5) { + return error("This is an error"); + } + x = <- w1; + } + + map mapResult = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithTypeCastExpr() { + worker w1 { + () a = <()>(5 -> w2); + assertValueEquality((), a); + } + + worker w2 { + int x = <- w1; + assertValueEquality(5, x); + } + + _ = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithReturnStmt() { + worker w1 { + return (5 -> w2); + } + + worker w2 { + int x = <- w1; + assertValueEquality(5, x); + } + + _ = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithWildCardBindingPattern() { + worker w1 { + _ = 5 -> w2; + } + + worker w2 { + int x = <- w1; + assertValueEquality(5, x); + } + + _ = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithMatchStmt() { + worker w1 { + match (5 -> w2) { + () => { + return; + } + + _ => { + panic error("This is an error"); + } + } + } + + worker w2 { + int x = <- w1; + assertValueEquality(5, x); + } + + _ = wait {w1, w2}; } type AssertionError distinct error; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_negative.bal new file mode 100644 index 000000000000..eb9aec80a5b1 --- /dev/null +++ b/tests/jballerina-unit-test/src/test/resources/test-src/workers/worker_async_send_negative.bal @@ -0,0 +1,42 @@ +// Copyright (c) 2023 WSO2 LLC. (http://www.wso2.com) +// +// WSO2 LLC. licenses this file to you under the Apache License, +// Version 2.0 (the "License"); you may not use this file except +// in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT 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 testAsyncSendAsExpressionWithForEachStmt() { + worker w1 { + foreach var j in 5 -> w2 { + + } + } + + worker w2 { + int x = <- w1; + } + + _ = wait {w1, w2}; +} + +function testAsyncSendAsExpressionWithCompoundAssignmentStmt() { + worker w1 { + int result = 4; + result += 5 -> w2; + } + + worker w2 { + int x = <- w1; + } + + _ = wait {w1, w2}; +}