Skip to content

Commit

Permalink
Add more compiler plugin tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshanWeerasinghe committed Jun 18, 2024
1 parent d8d1338 commit c80e000
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,33 @@ public void testComplexUnionTypeWithUnsupportedTypeAndDuplicateFields() {
"invalid field: duplicate field found");
Assert.assertEquals(errorDiagnosticsList.get(2).diagnosticInfo().messageFormat(), UNSUPPORTED_TYPE);
}

@Test
public void testPackageWithErroneousCompilation() {
DiagnosticResult diagnosticResult =
CompilerPluginTestUtils.loadPackage("sample_package_11").getCompilation().diagnosticResult();
List<Diagnostic> errorDiagnosticsList = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)).toList();
Assert.assertEquals(errorDiagnosticsList.size(), 1);
}

@Test
public void testPackageWithOtherModuleImports() {
DiagnosticResult diagnosticResult =
CompilerPluginTestUtils.loadPackage("sample_package_12").getCompilation().diagnosticResult();
List<Diagnostic> errorDiagnosticsList = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)).toList();
Assert.assertEquals(errorDiagnosticsList.size(), 0);
}

@Test
public void testUnsupportedTupleType() {
DiagnosticResult diagnosticResult =
CompilerPluginTestUtils.loadPackage("sample_package_13").getCompilation().diagnosticResult();
List<Diagnostic> errorDiagnosticsList = diagnosticResult.diagnostics().stream()
.filter(r -> r.diagnosticInfo().severity().equals(DiagnosticSeverity.ERROR)).toList();
Assert.assertEquals(errorDiagnosticsList.size(), 2);
Assert.assertEquals(errorDiagnosticsList.get(0).diagnosticInfo().messageFormat(), UNSUPPORTED_TYPE);
Assert.assertEquals(errorDiagnosticsList.get(1).diagnosticInfo().messageFormat(), UNSUPPORTED_TYPE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

import ballerina/data.yaml;

const VALUE = "1";

public function main() returns error? {
int|table<record {|string a;|}>|record {| int b;|} val = check yaml:parseString("1");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "yamldata_test"
name = "sample_11"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// 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 ballerina/data.yaml;

function call(string data) returns error? {
string value = "value";
anydata _ = check yaml:parseString(value);
int _ = "3";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "yamldata_test"
name = "sample_12"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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 ballerina/lang.'int as i;
import ballerina/data.yaml;

function call(stream<byte[], error?> strm) returns error? {
string value = "value";
anydata _ = check yaml:parseStream(strm);
int _ = i:abs(12);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[package]
org = "yamldata_test"
name = "sample_13"
version = "0.1.0"
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// 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 ballerina/data.yaml;

type Tuple [xml, xml...];
type Tuple2 [xml, xml];

public function main() returns error? {
byte[] bytes = [];
Tuple _ = check yaml:parseBytes(bytes);
Tuple2 _ = check yaml:parseBytes(bytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ type Data record {
}
string A;
string B;
@display {
label: "C"
}
string C;
};
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ type UnionType table<record {|int a;|}>|record {|string b;|};
type IntersectionType UnionType & readonly;

public function main() returns error? {
string str = string `{"a": 1, "b": "str"}`;
IntersectionType _ = check yaml:parseString(str);
byte[] bytes = [];
IntersectionType _ = check yaml:parseBytes(bytes);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
*/
public class Constants {

private Constants() {
}

static final String BALLERINA = "ballerina";
static final String PARSE_STRING = "parseString";
static final String PARSE_BYTES = "parseBytes";
static final String PARSE_STREAM = "parseStream";
static final String TO_YAML_STRING = "toYamlString";
static final String NAME = "Name";
static final String YAML = "yaml";
static final String DATA_YAML = "data.yaml";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public void perform(SyntaxNodeAnalysisContext ctx) {
processModuleVariableDeclarationNode((ModuleVariableDeclarationNode) member, ctx);
case TYPE_DEFINITION ->
processTypeDefinitionNode((TypeDefinitionNode) member, ctx);
default -> { }
}
}
}
Expand Down Expand Up @@ -206,6 +207,8 @@ private void validateTupleType(TupleTypeSymbol tupleTypeSymbol, SyntaxNodeAnalys
for (TypeSymbol memberType : tupleTypeSymbol.memberTypeDescriptors()) {
validateExpectedType(memberType, ctx);
}
Optional<TypeSymbol> restTypeSymbol = tupleTypeSymbol.restTypeDescriptor();
restTypeSymbol.ifPresent(typeSymbol -> validateExpectedType(typeSymbol, ctx));
}

private void validateRecordType(RecordTypeSymbol recordTypeSymbol, SyntaxNodeAnalysisContext ctx) {
Expand Down

0 comments on commit c80e000

Please sign in to comment.