Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chiranSachintha committed Jan 23, 2023
1 parent 04d0403 commit 77e4305
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.ballerina.runtime.internal.util.exceptions.BallerinaErrorReasons;
import io.ballerina.runtime.internal.util.exceptions.BallerinaException;
import io.ballerina.runtime.internal.util.exceptions.RuntimeErrors;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.charset.Charset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import io.ballerina.runtime.internal.types.BAnnotatableType;
import io.ballerina.runtime.internal.types.BTypedescType;
import io.ballerina.runtime.internal.util.exceptions.BallerinaException;

import java.util.Map;

import static io.ballerina.runtime.api.utils.TypeUtils.getReferredType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,9 @@ public Object[] dataToTestAnnotationsOfLocalRecord() {
"testAnnotationOnLocalRecordWithGlobalVariable",
"testAnnotationOnLocalRecordWithinLambdaFunction",
"testAnnotationOnLocalRecordWithinLambdaFunction1",
"testAnnotationOnLocalRecordWithinNestedLambdaFunctions",
"testAnnotationOnLocalRecordWithinNestedLambdaFunctions1",
"testAnnotationOnLocalRecordWithinNestedLambdaFunctions2",
"testAnnotationWithMultipleFieldsOnLocalRecord",
"testAnnotationOnLocalRecordWithMultipleFields"
"testAnnotationOnLocalRecordWithMultipleFields",
"testGlobalAnnotationsOnFunctionPointerReturnType"
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,34 @@ type Details record {|
|};

annotation AnnotRecord annot on type, field;

annotation AnnotRecord annot1 on type, field;

annotation Details details on field;

function testAnnotationOnLocalRecord() {
string k = "chiranS";
record {@annot {value: k} string x;} r = {x : ""};
record {@annot {value: k}
string x;} r = {x: ""};

map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"}, <map<anydata>>m["annot"]);
}

function testAnnotationWithMultipleFieldsOnLocalRecord() {
string k = "chiranS";
record {@details {name: k, age: 26} string x;} r = {x : ""};
record {@details {name: k, age: 26}
string x;} r = {x: ""};

map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({name: "chiranS", age: 26}, <map<anydata>>m["details"]);
assertEquality({name: "chiranS", age: 26}, <map<anydata>>m["details"]);
}

function testAnnotationOnLocalRecordWithMultipleFields() {
string k = "chiranS";
record {@annot {value: k} string x; @annot {value: k} string y; } r = {x : "", y: ""};
record {@annot {value: k}
string x; @annot {value: k}
string y;} r = {x: "", y: ""};

map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"}, <map<anydata>>m["annot"]);
Expand All @@ -59,104 +65,85 @@ function testAnnotationOnLocalRecordWithMultipleFields() {
string gVar = "foo";

function testAnnotationOnLocalRecordWithGlobalVariable() {
record {@annot {value: gVar} string x;} r = {x : ""};
record {@annot {value: gVar}
string x;} r = {x: ""};

map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "foo"}, <map<anydata>>m["annot"]);
}

function testAnnotationOnLocalRecordWithinLambdaFunction() {
string k = "chiranS";
function() func = function () {
record {@annot {value: k} string x;} r = {x : ""};
map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"} , <map<anydata>>m["annot"]);
};
func();
function () returns map<any> func = function() returns map<any> {
record {@annot {value: k}string x;} r = {x: ""};
return getLocalRecordAnnotations(typeof r, "$field$.x");
};
map<any> x = func();
assertEquality({value: "chiranS"}, <map<anydata>>x["annot"]);
}

function testAnnotationOnLocalRecordWithinLambdaFunction1() {
string k = "chiranS";
function(string) func = function (string value) {
record {@annot {value: value} string x;} r = {x : ""};
map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"}, <map<anydata>>m["annot"]);
};
func(k);
}

function testAnnotationOnLocalRecordWithinNestedLambdaFunctions() {
string k = "chiranS";
function () returns function() var1 = function () returns function() {
function() func = function () {
record {@annot {value: k} string x;} r = {x : ""};
map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"}, <map<anydata>>m["annot"]);
};
return func;
};
function() func = var1();
func();
}

function testAnnotationOnLocalRecordWithinNestedLambdaFunctions1() {
string k = "chiranS";
function (string) returns function() var1 = function (string val) returns function() {
function() func = function () {
record {@annot {value: val} string x;} r = {x : ""};
map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"}, <map<anydata>>m["annot"]);
};
return func;
};
function() func = var1(k);
func();
}

function testAnnotationOnLocalRecordWithinNestedLambdaFunctions2() {
string k = "chiranS";
function (string) returns function() var1 = function (string val) returns function() {
string name = "Name: ";
function() func = function () {
record {@annot {value: name + val} string x;} r = {x : ""};
map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "Name: chiranS"}, <map<anydata>>m["annot"]);
};
return func;
};
function() func = var1(k);
func();
function (string) returns map<any> func = function(string value) returns map<any> {
record {@annot {value: value}
string x;} r = {x: ""};
return getLocalRecordAnnotations(typeof r, "$field$.x");

};
map<any> x = func(k);
assertEquality({value: "chiranS"}, <map<anydata>>x["annot"]);
}

function testMultipleAnnotationsOnLocaRecord() {
string k = "chiranS";
record {@annot {value: k} @annot1 {value: k} string x;} r = {x : ""};
record {@annot {value: k}
@annot1 {value: k}
string x;} r = {x: ""};

map<any> m = getLocalRecordAnnotations(typeof r, "$field$.x");
assertEquality({value: "chiranS"}, <map<anydata>>m["annot"]);
assertEquality({value: "chiranS"}, <map<anydata>>m["annot1"]);
}

function testLocalRecordAnnotations() {
testMultipleAnnotationsOnLocaRecord();
testAnnotationOnLocalRecord();
testAnnotationOnLocalRecordWithGlobalVariable();
testAnnotationOnLocalRecordWithinLambdaFunction();
testAnnotationOnLocalRecordWithinLambdaFunction1();
testAnnotationOnLocalRecordWithinNestedLambdaFunctions();
testAnnotationOnLocalRecordWithinNestedLambdaFunctions1();
testAnnotationOnLocalRecordWithinNestedLambdaFunctions2();
testAnnotationWithMultipleFieldsOnLocalRecord();
testAnnotationOnLocalRecordWithMultipleFields();
string gVar1 = "bar";

function () returns record {string x;} x = function() returns record {@annot {value: "value"}
string x;} {
return {x: ""};
};
function () returns record {string x;} x2 = function() returns record {@annot {value: gVar1}
@annot1 {value: gVar2}
string x;} {
return {x: ""};
};
function () returns record {int x; int y;} x3 = function() returns record {@annot {value: gVar1}
int x; @details {name: "name", age: gVar3}
int y;} {
return {x: 10, y: 10};
};

function testGlobalAnnotationsOnFunctionPointerReturnType() {
map<any> m1 = getLocalRecordAnnotations(typeof x(), "$field$.x");
map<any> m2 = getLocalRecordAnnotations(typeof x2(), "$field$.x");
map<any> m3 = getLocalRecordAnnotations(typeof x3(), "$field$.x");
map<any> m4 = getLocalRecordAnnotations(typeof x3(), "$field$.y");
assertEquality({value: "value"}, <map<anydata>>m1["annot"]);
assertEquality({value: "bar"}, <map<anydata>>m2["annot"]);
assertEquality({value: "baz"}, <map<anydata>>m2["annot1"]);
assertEquality({value: "bar"}, <map<anydata>>m3["annot"]);
assertEquality({name: "name", age: 10}, <map<anydata>>m4["details"]);
}

string gVar2 = "baz";
int gVar3 = 10;

function getLocalRecordAnnotations(typedesc<any> obj, string annotName) returns map<any> =
@java:Method {
'class: "org/ballerinalang/test/annotations/LocalRecordAnnotationTest",
name: "getLocalRecordAnnotations"
} external;

function assertEquality(anydata expected, anydata actual) {
function assertEquality(anydata expected, anydata actual) {

if expected == actual {
return;
Expand Down

0 comments on commit 77e4305

Please sign in to comment.