From 21c0b3917e90775aa5f47e94b392bf14b02ff066 Mon Sep 17 00:00:00 2001 From: ushirask Date: Tue, 15 Aug 2023 12:50:58 +0530 Subject: [PATCH] Address review suggestions --- .../expressions/access/MemberAccessTest.java | 8 ++-- .../expressions/access/member_access.bal | 37 +++++++++++++++---- .../access/member_access_negative.bal | 14 +++---- 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/access/MemberAccessTest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/access/MemberAccessTest.java index 1628b1e10945..6ed683b0416e 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/access/MemberAccessTest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/expressions/access/MemberAccessTest.java @@ -237,11 +237,11 @@ public void testNegativeCases() { 392, 9); validateError(negativeResult, i++, "incompatible types: expected 'int', found '(string|int)'", 396, 9); - validateError(negativeResult, i++, "incompatible types: expected 'int', found 'STRINT'", + validateError(negativeResult, i++, "incompatible types: expected 'int', found 'StrOrInt1'", 399, 9); validateError(negativeResult, i++, "incompatible types: expected 'int', found '(string|int)'", 403, 7); - validateError(negativeResult, i++, "incompatible types: expected 'int', found '(int|STR)'", + validateError(negativeResult, i++, "incompatible types: expected 'int', found '(int|Str)'", 406, 7); Assert.assertEquals(negativeResult.getErrorCount(), i); } @@ -544,8 +544,8 @@ public void testNestedMemberAccessOnIntersectionTypes() { } @Test - public void testNestedMemberAccessWithSameTypeUnionExprAsIndex() { - BRunUtil.invoke(result, "testUnionTypeExprMemberAccess"); + public void testMemberAccessWithUnionTypedIndexExpr() { + BRunUtil.invoke(result, "testMemberAccessWithUnionTypedIndexExpr"); } @AfterClass diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access.bal b/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access.bal index 3379ff01ffbb..1ede31616442 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access.bal @@ -1178,11 +1178,12 @@ function testUnspecifiedFieldRecordMemberAccessWithStringCharKeyExpr() { assertTrue(v is ()); } -type INT int; -type INT2 int|int:Signed16; -type STR2 string|string:Char; +type IntType1 int; +type IntType2 int|int:Signed16; +type StrType1 string|string:Char; +type IntType3 1|2|3|4; -function testUnionTypeExprMemberAccess() { +function testMemberAccessWithUnionTypedIndexExpr() { record {| int a; |} r = {a: 1}; @@ -1213,7 +1214,7 @@ function testUnionTypeExprMemberAccess() { assertEquality("d", arr1[1]); [string, int] t1 = ["a", 1]; - int|INT|int y1 = 0; + int|IntType1|int y1 = 0; t1[y1] = "b"; assertEquality("b", t1[0]); @@ -1226,14 +1227,36 @@ function testUnionTypeExprMemberAccess() { string a; int b; |} r2 = {a: "a", b: 1}; - string|STR2 s2 = "b"; + string|StrType1 s2 = "b"; r2[s2] = 2; assertEquality(2, r2.b); float[] arr2 = [0.1, 1.2, 2.3]; - INT2|int:Signed8 x2 = 0; + IntType2|int:Signed8 x2 = 0; arr2[x2] = 4.5; assertEquality(4.5, arr2[0]); + + int[] arr3 = [1, 2, 3]; + int|IntType3 x3 = 1; + arr3[x3] = 4; + assertEquality(4, arr3[1]); + + record {| + int a; + |} r3 = {a: 1}; + "a"|"b" s3 = "a"; + r3[s3] = 2; + assertEquality(2, r3.a); + + [int, string] tup = [1, "x"]; + 0|1|int a = 0; + tup[a] = 4; + assertEquality(4, tup[0]); + + [int, string] tup2 = [1, "x"]; + 0|1|2 a2 = 1; + tup2[a2] = "y"; + assertEquality("y", tup2[1]); } const ASSERTION_ERROR_REASON = "AssertionError"; diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access_negative.bal b/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access_negative.bal index 747cc412041a..706233149953 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access_negative.bal +++ b/tests/jballerina-unit-test/src/test/resources/test-src/expressions/access/member_access_negative.bal @@ -377,10 +377,10 @@ function testCustomTupleTypesAccessNegative() { boolean _ = x14; //error: incompatible types: expected 'boolean', found 'int' } -type STR string; -type STRINT string|int; +type Str string; +type StrOrInt1 string|int; -function testMemberAccessWithInvalidUnionTypeExpr() { +function testMemberAccessWithUnionTypedIndexExprNegative() { record {| int a; |} r = {a: 1}; @@ -395,13 +395,13 @@ function testMemberAccessWithInvalidUnionTypeExpr() { string|int x2 = 1; arr[x2] = 4; // error: incompatible types: expected 'int', found '(string|int)' - STRINT x3 = 2; - arr[x3] = "4"; // error: incompatible types: expected 'int', found 'STRINT' + StrOrInt1 x3 = 2; + arr[x3] = "4"; // error: incompatible types: expected 'int', found 'StrOrInt1' [string, int] t = ["a", 1]; string|int y = 0; t[y] = "b"; // error: incompatible types: expected 'int', found '(string|int)' - int|STR y2 = 1; - t[y2] = 0; // error: incompatible types: expected 'int', found '(int|STR)' + int|Str y2 = 1; + t[y2] = 0; // error: incompatible types: expected 'int', found '(int|Str)' }