Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jun 26, 2019
1 parent 1a0547a commit 5300696
Show file tree
Hide file tree
Showing 15 changed files with 63 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public static Object checkCast(Object sourceVal, BType targetType) {
// if the source is a numeric value and the target type is a union, try to find a matching
// member.
if (sourceType.getTag() <= TypeTags.BOOLEAN_TAG && targetType.getTag() == TypeTags.UNION_TAG) {
for(BType memberType : ((BUnionType) targetType).getMemberTypes()) {
for (BType memberType : ((BUnionType) targetType).getMemberTypes()) {
try {
return TypeConverter.castValues(memberType, sourceVal);
} catch (Exception e) {
Expand All @@ -99,21 +99,22 @@ public static Object checkCast(Object sourceVal, BType targetType) {
}

public static long anyToInt(Object sourceVal) {
return TypeConverter.anyToInt(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal, BTypes.typeInt));
return TypeConverter.anyToIntCast(sourceVal,
() -> BallerinaErrors.createTypeCastError(sourceVal, BTypes.typeInt));
}

public static double anyToFloat(Object sourceVal) {
return TypeConverter.anyToFloat(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal,
return TypeConverter.anyToFloatCast(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal,
BTypes.typeFloat));
}

public static boolean anyToBoolean(Object sourceVal) {
return TypeConverter.anyToBoolean(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal,
return TypeConverter.anyToBooleanCast(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal,
BTypes.typeBoolean));
}

public static int anyToByte(Object sourceVal) {
return TypeConverter.anyToByte(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal,
return TypeConverter.anyToByteCast(sourceVal, () -> BallerinaErrors.createTypeCastError(sourceVal,
BTypes.typeByte));
}

Expand Down Expand Up @@ -1134,10 +1135,6 @@ private static boolean isEqual(Object lhsValue, Object rhsValue, List<ValuePair>
return false;
}

if (lhsValue.equals(rhsValue)) {
return true;
}

int lhsValTypeTag = getType(lhsValue).getTag();
int rhsValTypeTag = getType(rhsValue).getTag();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static long anyToInt(Object sourceVal, Supplier<ErrorValue> errorFunc) {
throw errorFunc.get();
}

private static long anyToIntCast(Object sourceVal, Supplier<ErrorValue> errorFunc) {
static long anyToIntCast(Object sourceVal, Supplier<ErrorValue> errorFunc) {
if (sourceVal instanceof Long) {
return (Long) sourceVal;
} else if (sourceVal instanceof Double) {
Expand Down Expand Up @@ -162,7 +162,7 @@ static double anyToFloat(Object sourceVal, Supplier<ErrorValue> errorFunc) {
throw errorFunc.get();
}

private static double anyToFloatCast(Object sourceVal, Supplier<ErrorValue> errorFunc) {
static double anyToFloatCast(Object sourceVal, Supplier<ErrorValue> errorFunc) {
if (sourceVal instanceof Long) {
return ((Long) sourceVal).doubleValue();
} else if (sourceVal instanceof Double) {
Expand Down Expand Up @@ -269,7 +269,7 @@ static int anyToByte(Object sourceVal, Supplier<ErrorValue> errorFunc) {
throw errorFunc.get();
}

private static int anyToByteCast(Object sourceVal, Supplier<ErrorValue> errorFunc) {
static int anyToByteCast(Object sourceVal, Supplier<ErrorValue> errorFunc) {
if (sourceVal instanceof Long) {
return intToByte((Long) sourceVal);
} else if (sourceVal instanceof Double) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
import org.ballerinalang.jvm.types.BType;
import org.ballerinalang.jvm.types.BTypes;
import org.ballerinalang.jvm.util.BLangConstants;
import org.ballerinalang.jvm.util.exceptions.BallerinaErrorReasons;
import org.ballerinalang.jvm.util.exceptions.BallerinaException;

import java.math.BigDecimal;
import java.math.BigInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ function generateCheckCastToFiniteType(jvm:MethodVisitor mv, bir:BType sourceTyp
// ------------------------------------------------------------------
// Generate Cast Methods - Performs cast without type checking
// ------------------------------------------------------------------

function generateCast(jvm:MethodVisitor mv, bir:BType sourceType, bir:BType targetType) {
if (targetType is bir:BTypeInt) {
generateCastToInt(mv, sourceType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2941,9 +2941,7 @@ public void visit(BLangFunctionVarRef functionVarRef) {

@Override
public void visit(BLangStructFieldAccessExpr fieldAccessExpr) {
BType expType = fieldAccessExpr.type;
fieldAccessExpr.type = symTable.anyType;
result = addConversionExprIfRequired(fieldAccessExpr, expType);
result = fieldAccessExpr;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,14 @@ public void testNonExistentEnvVarLookupForBoolean() {
}

@Test(expectedExceptions = BLangRuntimeException.class,
expectedExceptionsMessageRegExp = ".*incompatible convert operation: 'string' value 'b7auser' cannot be "
+ "converted as 'int'.*")
expectedExceptionsMessageRegExp = ".*'string' value 'b7auser' cannot be converted to 'int'.*")
public void testInvalidIntEnvVarLookup() {
BString key = new BString("user.name");
BRunUtil.invoke(compileResult, "testGetAsInt", new BValue[]{key});
}

@Test(expectedExceptions = BLangRuntimeException.class,
expectedExceptionsMessageRegExp = ".*incompatible convert operation: 'string' value 'b7auser' cannot be "
+ "converted as 'float'.*")
expectedExceptionsMessageRegExp = ".*'string' value 'b7auser' cannot be converted to 'float'.*")
public void testInvalidFloatEnvVarLookup() {
BString key = new BString("user.name");
BRunUtil.invoke(compileResult, "testGetAsFloat", new BValue[]{key});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.ballerinalang.test.expressions.typecast;

import org.ballerinalang.model.types.ValueType;
import org.ballerinalang.model.values.BBoolean;
import org.ballerinalang.model.values.BByte;
import org.ballerinalang.model.values.BDecimal;
Expand Down Expand Up @@ -188,7 +187,7 @@ public void testIntAsByte(String functionName, int i) {
Assert.assertEquals(returns.length, 2);
Assert.assertSame(returns[0].getClass(), BBoolean.class);
Assert.assertTrue(((BBoolean) returns[0]).booleanValue(), "expected bytes to be the same");
Assert.assertEquals(((ValueType) returns[1]).byteValue(), (new BInteger(i)).byteValue(), "incorrect int " +
Assert.assertEquals(((BValueType) returns[1]).byteValue(), (new BInteger(i)).byteValue(), "incorrect int " +
"representation as byte");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import org.ballerinalang.test.util.BCompileUtil;
import org.ballerinalang.test.util.BRunUtil;
import org.ballerinalang.test.util.CompileResult;
import org.ballerinalang.util.exceptions.BLangRuntimeException;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -56,16 +55,15 @@ public void testBinaryOrExprWithLeftMostExprTrue() {
Assert.assertEquals(actualResult, expectedResult);
}

//TODO this expected NullPointerException should be properly handled at ballerina layer when
//TODO accessing non existing JSON elements.
@Test(description = "Test binary OR expression with " +
"left most expr evaluated to false expression.", expectedExceptions = {BLangRuntimeException.class})
@Test(description = "Test binary OR expression with left most expr evaluated to false expression.")
public void testBinaryOrExprWithLeftMostExprFalseNegativeCase() {
boolean one = false;
boolean two = false;
boolean three = false;
BValue[] args = {new BBoolean(one), new BBoolean(two), new BBoolean(three)};
BRunUtil.invoke(result, "binaryOrExprWithLeftMostSubExprTrue", args);
BValue[] returns = BRunUtil.invoke(result, "binaryOrExprWithLeftMostSubExprTrue", args);
boolean actualResult = ((BBoolean) returns[0]).booleanValue();
Assert.assertEquals(actualResult, one);
}

@Test(description = "Test binary AND expression with left most expr evaluated to false expression.")
Expand All @@ -85,16 +83,15 @@ public void testBinaryAndExprWithLeftMostExprTrue() {
Assert.assertEquals(actualResult, expectedResult);
}

//TODO this expected NullPointerException should be properly handled at ballerina layer when
//TODO accessing non existing JSON elements.
@Test(description = "Test binary AND expression with " +
"left most expr evaluated to true expression.", expectedExceptions = {BLangRuntimeException.class})
@Test(description = "Test binary AND expression with left most expr evaluated to true expression.")
public void testBinaryAndExprWithLeftMostExprFalseNegativeCase() {
boolean one = true;
boolean two = false;
boolean three = false;
BValue[] args = {new BBoolean(one), new BBoolean(two), new BBoolean(three)};
BRunUtil.invoke(result, "binaryANDExprWithLeftMostSubExprFalse", args);
BValue[] returns = BRunUtil.invoke(result, "binaryANDExprWithLeftMostSubExprFalse", args);
boolean actualResult = ((BBoolean) returns[0]).booleanValue();
Assert.assertEquals(actualResult, false);
}

@Test(description = "Test multi binary expression with OR sub expressions inside If condition.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ public void testIncompatibleJsonToStructWithErrors() {
// check the error
Assert.assertTrue(returns[0] instanceof BError);
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: 'json' value cannot be converted as 'Person'");
Assert.assertEquals(errorMsg, "'json' value cannot be converted to 'Person'");
}

@Test
public void testEmptyJSONtoStructWithoutDefaults() {
BValue[] returns = BRunUtil.invoke(negativeResult, "testEmptyJSONtoStructWithoutDefaults");
Assert.assertTrue(returns[0] instanceof BError);
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: 'json' value cannot be converted as "
Assert.assertEquals(errorMsg, "'json' value cannot be converted to "
+ "'StructWithoutDefaults'");
}

Expand All @@ -77,7 +77,7 @@ public void testEmptyMaptoStructWithDefaults() {
BValue[] returns = BRunUtil.invoke(negativeResult, "testEmptyMaptoStructWithDefaults");
Assert.assertTrue(returns[0] instanceof BError);
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: 'map' value cannot be converted as "
Assert.assertEquals(errorMsg, "'map' value cannot be converted to "
+ "'StructWithDefaults'");
}

Expand All @@ -86,15 +86,15 @@ public void testEmptyMaptoStructWithoutDefaults() {
BValue[] returns = BRunUtil.invoke(negativeResult, "testEmptyMaptoStructWithoutDefaults");
Assert.assertTrue(returns[0] instanceof BError);
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: 'map' value cannot be converted as "
Assert.assertEquals(errorMsg, "'map' value cannot be converted to "
+ "'StructWithoutDefaults'");
}

@Test(description = "Test performing an invalid tuple conversion")
public void testTupleConversionFail() {
BValue[] returns = BRunUtil.invoke(negativeResult, "testTupleConversionFail");
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: '(T1,T1)' value cannot be converted as '(T1,"
Assert.assertEquals(errorMsg, "'(T1,T1)' value cannot be converted to '(T1,"
+ "T2)'");
}

Expand All @@ -103,7 +103,7 @@ public void testArrayToJsonFail() {
BValue[] returns = BRunUtil.invoke(negativeResult, "testArrayToJsonFail");
Assert.assertTrue(returns[0] instanceof BError);
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: 'TX[]' value cannot be converted as 'json'");
Assert.assertEquals(errorMsg, "'TX[]' value cannot be converted to 'json'");
}

@Test(description = "Test passing tainted value with convert")
Expand Down Expand Up @@ -140,7 +140,7 @@ public void testIncompatibleImplicitConversion() {
BValue[] returns = BRunUtil.invoke(negativeResult, "testIncompatibleImplicitConversion");
Assert.assertTrue(returns[0] instanceof BError);
String errorMsg = ((BMap<String, BValue>) ((BError) returns[0]).getDetails()).get("message").stringValue();
Assert.assertEquals(errorMsg, "incompatible convert operation: 'string' value 'abjd' cannot be converted as "
Assert.assertEquals(errorMsg, "'string' value 'abjd' cannot be converted to "
+ "'int'");
}

Expand Down
Loading

0 comments on commit 5300696

Please sign in to comment.