-
Notifications
You must be signed in to change notification settings - Fork 758
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43289 from poorna2152/const_list_8.x
[2201.8.x] Fix error on accessing a constant list defined in a module
- Loading branch information
Showing
5 changed files
with
229 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...-unit-test/src/test/java/org/ballerinalang/test/bala/constant/ListConstantInBalaTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (https://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. | ||
*/ | ||
package org.ballerinalang.test.bala.constant; | ||
|
||
import org.ballerinalang.test.BAssertUtil; | ||
import org.ballerinalang.test.BCompileUtil; | ||
import org.ballerinalang.test.BRunUtil; | ||
import org.ballerinalang.test.CompileResult; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.DataProvider; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* Test cases for reading list constants. | ||
* | ||
* @since 2201.8.8 | ||
*/ | ||
public class ListConstantInBalaTest { | ||
|
||
private CompileResult compileResult; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
BCompileUtil.compileAndCacheBala("test-src/bala/test_projects/test_project"); | ||
compileResult = BCompileUtil.compile("test-src/bala/test_bala/constant/list_literal_constant.bal"); | ||
} | ||
|
||
@Test(dataProvider = "constantListAccessTestDataProvider") | ||
public void testConstantListAccess(String testCase) { | ||
BRunUtil.invoke(compileResult, testCase); | ||
} | ||
|
||
@DataProvider(name = "constantListAccessTestDataProvider") | ||
public Object[] constantListAccessTestDataProvider() { | ||
return new Object[]{ | ||
"testSimpleArrayAccess", | ||
"testSimpleTupleAccess", | ||
"test2DTupleAccess", | ||
"test2DArrayAccess", | ||
"testFixedLengthArrayAccess", | ||
"testArrayWithRestAccess", | ||
"test2DUnionArrayAccess" | ||
}; | ||
} | ||
|
||
@Test | ||
public void testConstantListAccessNegative() { | ||
CompileResult compileResult = BCompileUtil.compile( | ||
"test-src/bala/test_bala/constant/list_constant_negative.bal"); | ||
int i = 0; | ||
BAssertUtil.validateError(compileResult, i++, | ||
"incompatible types: expected '[string,string,int...]', found '[\"a\",\"b\",\"c\"] & readonly'", 20, | ||
34); | ||
BAssertUtil.validateError(compileResult, i++, | ||
"incompatible types: expected 'int[]', found '[true,false,true] & readonly'", 21, 15); | ||
BAssertUtil.validateError(compileResult, i++, | ||
"cannot update 'readonly' value of type '[\"a\",\"b\",\"c\"] & readonly'", 26, 5); | ||
BAssertUtil.validateError(compileResult, i++, | ||
"cannot update 'readonly' value of type '[\"a\",\"b\",\"c\"] & readonly'", 27, 5); | ||
BAssertUtil.validateError(compileResult, i++, | ||
"incompatible types: expected '(\"a\"|\"b\"|\"c\")', found 'string:Char'", 27, 12); | ||
BAssertUtil.validateError(compileResult, i++, | ||
"cannot update 'readonly' value of type '[1,\"f\",\"g\"] & readonly'", 30, 5); | ||
BAssertUtil.validateError(compileResult, i++, "incompatible types: expected '(1|\"f\"|\"g\")', found 'string'", | ||
30, 12); | ||
BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l13'", 34, 9); | ||
BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l14'", 35, 9); | ||
BAssertUtil.validateError(compileResult, i++, "attempt to refer to non-accessible symbol 'l10'", 39, 9); | ||
BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l10'", 39, 9); | ||
BAssertUtil.validateError(compileResult, i++, "attempt to refer to non-accessible symbol 'l11'", 40, 9); | ||
BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l11'", 40, 9); | ||
BAssertUtil.validateError(compileResult, i++, "attempt to refer to non-accessible symbol 'l12'", 41, 9); | ||
BAssertUtil.validateError(compileResult, i++, "undefined symbol 'l12'", 41, 9); | ||
Assert.assertEquals(compileResult.getErrorCount(), i); | ||
} | ||
|
||
@AfterClass | ||
public void tearDown() { | ||
compileResult = null; | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_constant_negative.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2024 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. | ||
|
||
import testorg/foo; | ||
|
||
function testIncompatibleAssignment() { | ||
[string, string, int...] _ = foo:l1; | ||
int[] _ = foo:l5; | ||
} | ||
|
||
function testInvalidUpdates() { | ||
var a = foo:l1; | ||
a[0] = "1"; | ||
a.push("2"); | ||
|
||
var b = foo:l7; | ||
b.push("l7"); | ||
} | ||
|
||
function testUndefinedSymbolAccess() { | ||
_ = foo:l13; | ||
_ = foo:l14; | ||
} | ||
|
||
function testNonPublicConstAccess() { | ||
_ = foo:l10; | ||
_ = foo:l11; | ||
_ = foo:l12; | ||
} |
53 changes: 53 additions & 0 deletions
53
...a-unit-test/src/test/resources/test-src/bala/test_bala/constant/list_literal_constant.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2024, WSO2 LLC. (https://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 | ||
|
||
import testorg/foo; | ||
|
||
function testSimpleArrayAccess() { | ||
assertEqual(foo:l1, ["a", "b", "c"]); | ||
} | ||
|
||
function testSimpleTupleAccess() { | ||
assertEqual(foo:l2, [1, "d"]); | ||
} | ||
|
||
function test2DTupleAccess() { | ||
assertEqual(foo:l3, [1, ["e", 2]]); | ||
} | ||
|
||
function test2DArrayAccess() { | ||
assertEqual(foo:l4, [[1, 2, 3], [4, 5, 6]]); | ||
} | ||
|
||
function testFixedLengthArrayAccess() { | ||
assertEqual(foo:l5, [true, false, true]); | ||
} | ||
|
||
function testArrayWithRestAccess() { | ||
assertEqual(foo:l7, [1, "f", "g"]); | ||
} | ||
|
||
function test2DUnionArrayAccess() { | ||
assertEqual(foo:l8, [[1, "2", 3], [4, 5, 6]]); | ||
assertEqual(foo:l9, [[1, 2, 3], ["4", "5", "6"]]); | ||
} | ||
|
||
function assertEqual(anydata actual, anydata expected) { | ||
if expected == actual { | ||
return; | ||
} | ||
panic error(string `expected '${expected.toBalString()}', found '${actual.toBalString()}'`); | ||
} |
32 changes: 32 additions & 0 deletions
32
...a-unit-test/src/test/resources/test-src/bala/test_projects/test_project/list_constant.bal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2024, WSO2 LLC. (https://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 | ||
|
||
type IntArr int[]; | ||
|
||
const int length = 3; | ||
|
||
public const string[] l1 = ["a", "b", "c"]; | ||
public const [int, string] l2 = [1, "d"]; | ||
public const [int, [string, int]] l3 = [1, ["e", 2]]; | ||
public const int[][] l4 = [[1, 2, 3], [4, 5, 6]]; | ||
public const boolean[length] l5 = [true, false, true]; | ||
public const IntArr l6 = [1, 2, 3]; | ||
public const [int, string...] l7 = [1, "f", "g"]; | ||
public const (string|int)[][] l8 = [[1, "2", 3], [4, 5, 6]]; | ||
public const [(string[]|int[])...] l9 = [[1, 2, 3], ["4", "5", "6"]]; | ||
const int[] l10 = [1, 2, 3]; | ||
const [int, int, boolean] l11 = [1, 2, true]; | ||
const l12 = l1; |