Skip to content

Commit

Permalink
Add negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dulajdilshan committed Aug 1, 2024
1 parent 82ff635 commit edebb78
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,34 @@ public void testNegativeRecordVariables() {
Assert.assertEquals(resultNegative.getDiagnostics().length, i);
}

@Test
public void testDestructuringWithRecordReferenceNegative() {
CompileResult resultNegative = BCompileUtil.compile(
"test-src/expressions/varref/record-variable-reference-in-destructuring-negative.bal");
int i = 0;
BAssertUtil.validateError(resultNegative, i++, "incompatible types: " +
"expected '[record {| int a; int b; (any|error)...; |}]', " +
"found '[record {| int a; int b?; |}]'", 22, 15);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: " +
"expected '[int,[record {| int b; (any|error)...; |}]]', " +
"found '[int,[record {| int b?; anydata...; |}]]'", 24, 18);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: " +
"expected '[record {| int a; int b; int c; (any|error)...; |}]', " +
"found '[record {| int a; int b; anydata...; |}]'", 32, 18);
BAssertUtil.validateError(resultNegative, i++, "incompatible types: " +
"expected '[int,[record {| int b; int c; (any|error)...; |}]]', " +
"found '[int,[record {| int b; anydata...; |}]]'", 34, 21);
BAssertUtil.validateError(resultNegative, i++, "invalid field binding pattern; can only bind required fields",
41, 9);
BAssertUtil.validateError(resultNegative, i++, "invalid field binding pattern; can only bind required fields",
43, 14);
BAssertUtil.validateError(resultNegative, i++, "invalid record binding pattern; " +
"unknown field 'b' in record type 'record {| int a; anydata...; |}'", 50, 5);
BAssertUtil.validateError(resultNegative, i++, "invalid record binding pattern; " +
"unknown field 'b' in record type 'record {| |} & readonly'", 52, 13);
Assert.assertEquals(resultNegative.getDiagnostics().length, i);
}

@AfterClass
public void tearDown() {
result = null;
Expand Down
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.

function testTupleDestructuringWithOptionalFieldBindingPattern() {
[record {|int a; int b?;|}] l1 = [{a: 1, b: 2}];
int a;
int b;
int c;
[{a, b}]= l1;
[int, [record {int b?;}]] l2= [1, [{b: 1}]];
[a, [{b}]] = l2;
}

function testTupleDestructuringWithUnspecifiedFields() {
[record {int a; int b;}] l1 = [{a: 1, b: 2, c: 3}];
int a;
int b;
int c;
[{a, b, c}]= l1;
[int, [record {int b;}]] l2= [1, [{b: 1}]];
[a, [{b, c}]] = l2;
}

function testRecordDestructuringWithOptionalFieldBindingPattern() {
record {int a; int b?;} r1 = {a: 1, b: 2};
int a;
int b;
{a, b} = r1;
record {|int a; record {|int b?;|} bb;|} r2 = {a: 1, bb: {b: 2}};
{a, bb: {b}} = r2;
}

function testRecordDestructuringWithUnspecifiedFields() {
record {int a;} r1 = {a: 1};
int a;
int b;
{a, b} = r1;
record {|int a; record {||} bb;|} r2 = {a: 1, bb: {}};
{a, bb: {b}} = r2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,14 @@ function testRecordTypedBindingPatternAgainstRecordLiteralInRecordDestructuring(
assertEquality("Highway 61", street);
assertEquality({lat: 37.30, lon: -90.40}, coordinates);

record {|
string street;
float lat;
float lon;
|} {street: street1, ...otherdata} = {street: "Highway 61", "lat": 37.30, "lon": -90.40};
assertEquality("Highway 61", street1);
assertEquality({lat: 37.30, lon: -90.40}, otherdata);

record {string state;} {state: state2, ...other} = {state: "Colorado", "postalCode": 80001};
assertEquality("Colorado", state2);
assertEquality({postalCode: 80001}, other);
Expand Down

0 comments on commit edebb78

Please sign in to comment.