Skip to content

Commit

Permalink
Merge pull request #43436 from poorna2152/test_package
Browse files Browse the repository at this point in the history
Fix `CCE` for annotation symbols in tests in SymbolFinder
  • Loading branch information
gimantha authored Oct 3, 2024
2 parents fe0509b + 0450329 commit 73e6a19
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,13 @@ private BAnnotationSymbol findAnnotationSymbol(BAnnotationAttachmentSymbol annot
symTable.pkgEnvMap.get(symTable.langAnnotationModuleSymbol), annotTagRef);
}

boolean testable = annotPkgId.isTestPkg;
for (Entry<BPackageSymbol, SymbolEnv> entry : symTable.pkgEnvMap.entrySet()) {
if (entry.getKey().pkgID.equals(annotPkgId)) {
BPackageSymbol pkgSymbol = entry.getKey();
if (pkgSymbol.pkgID.equals(annotPkgId)) {
if (testable && !pkgSymbol.pkgID.isTestPkg) {
continue;
}
return (BAnnotationSymbol) symResolver.lookupSymbolInAnnotationSpace(entry.getValue(), annotTagRef);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import static io.ballerina.semantic.api.test.util.SemanticAPITestUtils.assertBasicsAndGetSymbol;
import static io.ballerina.semantic.api.test.util.SemanticAPITestUtils.getDefaultModulesSemanticModel;
import static io.ballerina.semantic.api.test.util.SemanticAPITestUtils.getDocumentForSingleSource;
import static io.ballerina.semantic.api.test.util.SemanticAPITestUtils.getDocumentForSingleTestSource;
import static io.ballerina.tools.text.LinePosition.from;
import static java.util.List.of;
import static org.testng.Assert.assertEquals;
Expand All @@ -66,11 +67,17 @@ public class AnnotationSymbolTest {
private SemanticModel model;
private Document srcFile;

private SemanticModel testProjectModel;
private Document testFile;
@BeforeClass
public void setup() {
Project project = BCompileUtil.loadProject("test-src/symbols/annotation_symbol_test.bal");
model = getDefaultModulesSemanticModel(project);
srcFile = getDocumentForSingleSource(project);

Project testProject = BCompileUtil.loadProject("test-src/annotations_in_tests_project/");
testProjectModel = getDefaultModulesSemanticModel(testProject);
testFile = getDocumentForSingleTestSource(testProject);
}

@Test(dataProvider = "PosProvider")
Expand Down Expand Up @@ -154,4 +161,59 @@ public Object[][] getFunctionAnnotPos() {
{85, 8, RECORD_FIELD}
};
}

@Test(dataProvider = "TestPosProvider")
public void testInTestDir(int line, int col, SymbolKind kind, List<String> annots) {
Optional<Symbol> symbol = testProjectModel.symbol(testFile, from(line, col));
assertEquals(symbol.get().kind(), kind);

List<AnnotationSymbol> annotSymbols = ((Annotatable) symbol.get()).annotations();

assertEquals(annotSymbols.size(), annots.size());
for (int i = 0; i < annotSymbols.size(); i++) {
assertEquals(annotSymbols.get(i).getName().get(), annots.get(i));
}
}

@DataProvider(name = "TestPosProvider")
public Object[][] getPosTests() {
return new Object[][]{
{37, 11, RECORD_FIELD, of("Meta")},
{44, 6, CONSTANT, of("v1")},
{50, 12, TYPE_DEFINITION, of("v1")},
{51, 15, RECORD_FIELD, of("v5")},
{60, 6, CLASS, of("v2", "v2")},
{61, 15, CLASS_FIELD, of("v5")},
{66, 20, METHOD, of("v3")},
{66, 69, PARAMETER, of("v4")},
{76, 16, FUNCTION, of("v3")},
{84, 11, WORKER, of("v1")},
};
}

@Test(dataProvider = "AnnotRefTestsPosProvider")
public void testAnnotInTestTypes(int line, int col, String annotName, String typeName) {
AnnotationSymbol symbol =
(AnnotationSymbol) assertBasicsAndGetSymbol(testProjectModel, testFile, line, col, annotName,
ANNOTATION);

Optional<TypeSymbol> typeSymbol = symbol.typeDescriptor();

if (typeName != null) {
assertTrue(typeSymbol.isPresent());
assertEquals(typeSymbol.get().typeKind(), TYPE_REFERENCE);
assertEquals(typeSymbol.get().getName().get(), typeName);
} else {
assertTrue(typeSymbol.isEmpty());
}
}

@DataProvider(name = "AnnotRefTestsPosProvider")
public Object[][] getTestsAnnotRefPos() {
return new Object[][]{
{46, 1, "v1", "Annot"},
{66, 29, "v4", "Annot"},
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
org = "testorg"
name = "annotations_in_tests_project"
version = "0.1.0"

[build-options]
observabilityIncluded = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// 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.

public type MetaInfo record {|
string name;
|};

type Annot record {
string foo;
int bar?;
};

public const annotation MetaInfo Meta on record field;

public const annotation Annot v1 on source const, type, source worker;
annotation Annot[] v2 on class;
public annotation Annot v3 on function;
annotation Annot v4 on parameter;
public annotation v5 on field;

public type QueryWithDifferentAnnotation record {|
@Meta {
name: "Potter"
}
string firstName;
int age;
|};

@v1 {
foo: "annot on constant"
}
const strValue = "v1 value";

@v1 {
foo: strValue,
bar: 1
}
public type T1 record {
@v5 string name;
};

@v2 {
foo: "value 1"
}
@v2 {
foo: "value 2"
}
class Foo {
@v5 string name = "ballerina";

@v3 {
foo: "v31 value"
}
public function setName(@v4 { foo: "v41 value required" } string name,
@v4 { foo: "v41 value defaultable" } int id = 0,
@v4 { foo: "v41 value rest" } string... others) returns @v5 () {
self.name = name;
}
}

@v3 {
foo: "annot on function"
}
public function sum(int x, int y) returns int => x + y;

function test() {
int a = 10;

@v1 {
foo: "annot on worker"
}
worker w1 {
a += 10;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.ballerinalang.test.context.BMainInstance;
import org.ballerinalang.test.context.BallerinaTestException;
import org.ballerinalang.testerina.test.utils.AssertionUtils;
import org.ballerinalang.testerina.test.utils.CommonUtils;
import org.ballerinalang.testerina.test.utils.FileUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -95,16 +94,9 @@ public void testAssertSequenceTypes() throws BallerinaTestException, IOException

@Test
public void testAnnotationAccess() throws BallerinaTestException, IOException {
String endString = " SEVERE {b7a.log.crash} - ";
String firstString = "We thank you for helping make us better.";
String endString2 = "********";
String firstString2 = "unnamed module of loader 'app')";
String[] args = mergeCoverageArgs(new String[]{"annotation-access"});
String output = balClient.runMainAndReadStdOut("test", args,
new HashMap<>(), projectPath, true);
output = output + "********";
output = CommonUtils.replaceVaryingString(firstString, endString, output);
output = CommonUtils.replaceVaryingString(firstString2, endString2, output);
AssertionUtils.assertOutput("BasicCasesTest-testAnnotationAccess.txt", output);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
Compiling source
intg_tests/annotation_access:0.0.0
HINT [tests/main_test.bal:(73:5,73:5)] concurrent calls will not be made to this method since the service is not an 'isolated' service
ballerina: Oh no, something really went wrong. Bad. Sad.

We appreciate it if you can report the code that broke Ballerina in
https://github.com/ballerina-platform/ballerina-lang/issues with the
log you get below and your sample code.
Running Tests with Coverage

We thank you for helping make us better.***** SEVERE {b7a.log.crash} - class org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol cannot be cast to class org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol and org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol are in unnamed module of loader 'app')*************
annotation_access
[pass] testServiceAnnotReordering
[pass] testTestConstructSrcAnnotAccess
[pass] testTestConstructTestAnnotAccess
[pass] testTestSrcConstructSrcAnnotAccess


4 passing
0 failing
0 skipped

Test execution time :*****s

Generating Test Report
annotation-access/target/report/test_results.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
Compiling source
intg_tests/annotation_access:0.0.0
HINT [tests\main_test.bal:(73:5,73:5)] concurrent calls will not be made to this method since the service is not an 'isolated' service
ballerina: Oh no, something really went wrong. Bad. Sad.

We appreciate it if you can report the code that broke Ballerina in
https://github.com/ballerina-platform/ballerina-lang/issues with the
log you get below and your sample code.
Running Tests with Coverage

annotation_access
[pass] testServiceAnnotReordering
[pass] testTestConstructSrcAnnotAccess
[pass] testTestConstructTestAnnotAccess
[pass] testTestSrcConstructSrcAnnotAccess


4 passing
0 failing
0 skipped

Test execution time :*****s

Generating Test Report
annotation-access\target\report\test_results.json

We thank you for helping make us better.***** SEVERE {b7a.log.crash} - class org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol cannot be cast to class org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol (org.wso2.ballerinalang.compiler.semantics.model.symbols.BSymbol and org.wso2.ballerinalang.compiler.semantics.model.symbols.BAnnotationSymbol are in unnamed module of loader 'app')*************

0 comments on commit 73e6a19

Please sign in to comment.