-
Notifications
You must be signed in to change notification settings - Fork 759
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 #43825 from ballerina-platform/str-template-cost-expr
Add support for string template expression as constant expression
- Loading branch information
Showing
9 changed files
with
217 additions
and
5 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
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
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
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
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
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
64 changes: 64 additions & 0 deletions
64
...-test/src/test/java/org/ballerinalang/test/types/constant/StringTemplateConstantTest.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,64 @@ | ||
/* | ||
* Copyright (c) 2025, WSO2 LLC. (http://www.wso2.org). | ||
* | ||
* 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.types.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.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
/** | ||
* @since 2201.12.0 | ||
* | ||
* This class contains a set of test cases to test the string template expression as a constant expression. | ||
*/ | ||
public class StringTemplateConstantTest { | ||
|
||
private CompileResult compileResult; | ||
|
||
@BeforeClass | ||
public void setup() { | ||
compileResult = BCompileUtil.compile("test-src/types/constant/string_template_constant.bal"); | ||
} | ||
|
||
@Test | ||
public void testStringTemplateConstantExpr() { | ||
BRunUtil.invoke(compileResult, "testStringTemplateConstantExpr"); | ||
} | ||
|
||
@Test | ||
public void testStringTemplateConstantExprNegative() { | ||
CompileResult negativeResult = BCompileUtil.compile( | ||
"test-src/types/constant/string_template_constant_negative.bal"); | ||
int index = 0; | ||
BAssertUtil.validateError(negativeResult, index++, "incompatible types: expected '(int|float|" + | ||
"decimal|string|boolean)', found '(record {| 4 a; |} & readonly)'", 17, 27); | ||
BAssertUtil.validateError(negativeResult, index++, "expression is not a constant expression", | ||
20, 30); | ||
BAssertUtil.validateError(negativeResult, index++, "expression is not a constant expression", | ||
21, 30); | ||
BAssertUtil.validateError(negativeResult, index++, "expression is not a constant expression", | ||
29, 21); | ||
|
||
Assert.assertEquals(negativeResult.getErrorCount(), index); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
...llerina-unit-test/src/test/resources/test-src/types/constant/string_template_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,67 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
// | ||
// 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. | ||
const v1 = string `hello ${"world"}`; | ||
const v2 = string `hello${" world"}`; | ||
|
||
const boolean bool = true; | ||
const v3 = string `This is ${bool}`; | ||
const v4 = string `This is ${!bool}`; | ||
|
||
const int intVal = 444; | ||
string v5 = string `${intVal} is greater than ${100}.`; | ||
|
||
const float floatVal = 5.0090; | ||
string v6 = string `this is a float value ${floatVal}. ${v2}. Have a nice day.`; | ||
|
||
const decimal b = 5.7888; | ||
const decimal c = 5.7888; | ||
const v7 = string `hello ${b + c}. This is ${b - c}.`; | ||
|
||
public function testStringTemplateConstantExpr() { | ||
assertEquality("hello world", v1); | ||
assertEquality("hello world", v2); | ||
assertEquality("This is true", v3); | ||
assertEquality("This is false", v4); | ||
assertEquality("444 is greater than 100.", v5); | ||
assertEquality("this is a float value 5.009. hello world. Have a nice day.", v6); | ||
assertEquality("hello 11.5776. This is 0.", v7); | ||
|
||
// define a new non constant variables for the above v1 to v7 locally | ||
string v11 = string `hello ${"world"}`; | ||
string v12 = string `hello${" world"}`; | ||
string v13 = string `This is ${bool}`; | ||
string v14 = string `This is ${!bool}`; | ||
string v15 = string `${intVal} is greater than ${100}.`; | ||
string v16 = string `this is a float value ${floatVal}. ${v12}. Have a nice day.`; | ||
string v17 = string `hello ${b + c}. This is ${b - c}.`; | ||
|
||
// assert the equality of the above local variables with the constant variables | ||
assertEquality(v1, v11); | ||
assertEquality(v2, v12); | ||
assertEquality(v3, v13); | ||
assertEquality(v4, v14); | ||
assertEquality(v5, v15); | ||
assertEquality(v6, v16); | ||
assertEquality(v7, v17); // Todo: This needs to be fixed | ||
} | ||
|
||
function assertEquality(anydata expected, anydata actual) { | ||
if expected == actual { | ||
return; | ||
} | ||
|
||
panic error(string `expected '${expected.toBalString()}', found '${actual.toBalString()}'`); | ||
} |
29 changes: 29 additions & 0 deletions
29
...nit-test/src/test/resources/test-src/types/constant/string_template_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,29 @@ | ||
// Copyright (c) 2025 WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
// | ||
// 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. | ||
const v1 = {a:4}; | ||
const v2 = string `hello${v1}`; | ||
|
||
boolean bool = true; | ||
const v3 = string `This is ${bool}`; | ||
const v4 = string `This is ${foo()}`; | ||
|
||
function foo() returns boolean { | ||
return false; | ||
} | ||
|
||
final string baz = "FOO"; | ||
string gim = "BAR"; | ||
const A = string `${baz}:${gim}`; |