Skip to content

Commit

Permalink
Merge pull request #16013 from SupunS/enable-tests
Browse files Browse the repository at this point in the history
Enable typecast tests
  • Loading branch information
lankavitharana authored Jun 27, 2019
2 parents 94f9e2a + fc0b4b2 commit 9580039
Show file tree
Hide file tree
Showing 34 changed files with 452 additions and 322 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ illegal.format.conversion = illegal format conversion ''{0}''
incompatible.stamp.operation = incompatible stamp operation: ''{0}'' value cannot be stamped as ''{1}''
cannot.stamp.null = cannot stamp ''null'' value to type ''{0}''
cannot.convert.null = cannot convert ''null'' value to type ''{0}''
incompatible.convert.operation = incompatible convert operation: ''{0}'' value cannot be converted as ''{1}''
incompatible.simple.type.convert.operation = incompatible convert operation: ''{0}'' value ''{1}'' cannot be converted as ''{2}''
incompatible.convert.operation = ''{0}'' value cannot be converted to ''{1}''
incompatible.simple.type.convert.operation = ''{0}'' value ''{1}'' cannot be converted to ''{2}''
unsupported.clone.operation = ''clone()'' not allowed on ''{0}''
invalid.record.field.access = invalid field access: field ''{0}'' not found in record type ''{1}''
invalid.record.field.addition = invalid value for record field ''{0}'': expected value of type ''{1}'', found ''{2}''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ illegal.format.conversion = illegal format conversion ''{0}''
incompatible.stamp.operation = incompatible stamp operation: ''{0}'' value cannot be stamped as ''{1}''
cannot.stamp.null = cannot stamp ''null'' value to type ''{0}''
cannot.convert.null = cannot convert ''null'' value to type ''{0}''
incompatible.convert.operation = incompatible convert operation: ''{0}'' value cannot be converted as ''{1}''
incompatible.simple.type.convert.operation = incompatible convert operation: ''{0}'' value ''{1}'' cannot be converted as ''{2}''
incompatible.convert.operation = ''{0}'' value cannot be converted to ''{1}''
incompatible.simple.type.convert.operation = ''{0}'' value ''{1}'' cannot be converted to ''{2}''
unsupported.clone.operation = ''clone()'' not allowed on ''{0}''
invalid.record.field.access = invalid field access: field ''{0}'' not found in record type ''{1}''
invalid.record.field.addition = invalid value for record field ''{0}'': expected value of type ''{1}'', found ''{2}''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,25 @@ public static ErrorValue createConversionError(Object inputValue, BType targetTy
TypeChecker.getType(inputValue), targetType));
}

static ErrorValue createTypeCastError(Object sourceVal, BType targetType) {
public static ErrorValue createTypeCastError(Object sourceVal, BType targetType) {
throw createError(BallerinaErrorReasons.TYPE_CAST_ERROR,
BLangExceptionHelper.getErrorMessage(RuntimeErrors.TYPE_CAST_ERROR,
TypeChecker.getType(sourceVal), targetType));

}

static ErrorValue createNumericConversionError(Object inputValue, BType targetType) {
public static ErrorValue createNumericConversionError(Object inputValue, BType targetType) {
throw createError(BallerinaErrorReasons.NUMBER_CONVERSION_ERROR,
BLangExceptionHelper.getErrorMessage(
RuntimeErrors.INCOMPATIBLE_SIMPLE_TYPE_CONVERT_OPERATION,
TypeChecker.getType(inputValue), inputValue, targetType));
}

public static ErrorValue createNumericConversionError(Object inputValue, BType inputType, BType targetType) {
throw createError(BallerinaErrorReasons.NUMBER_CONVERSION_ERROR, BLangExceptionHelper.getErrorMessage(
RuntimeErrors.INCOMPATIBLE_SIMPLE_TYPE_CONVERT_OPERATION, inputType, inputValue, targetType));
}

static String getErrorMessageFromDetail(MapValueImpl<String, Object> detailMap) {
return (String) detailMap.get(ERROR_MESSAGE_FIELD);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,43 @@ public static Object checkCast(Object sourceVal, BType targetType) {
return sourceVal;
}

BType sourceType = getType(sourceVal);
if (sourceType.getTag() <= TypeTags.BOOLEAN_TAG && targetType.getTag() <= TypeTags.BOOLEAN_TAG) {
return TypeConverter.castValues(targetType, sourceVal);
}

// 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()) {
try {
return TypeConverter.castValues(memberType, sourceVal);
} catch (Exception e) {
continue;
}
}
}

throw BallerinaErrors.createTypeCastError(sourceVal, 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 @@ -1122,20 +1140,57 @@ private static boolean isEqual(Object lhsValue, Object rhsValue, List<ValuePair>

switch (lhsValTypeTag) {
case TypeTags.STRING_TAG:
return lhsValue.equals(rhsValue);
case TypeTags.FLOAT_TAG:
if (rhsValTypeTag <= TypeTags.FLOAT_TAG) {
return lhsValue.equals(((Number) rhsValue).doubleValue());
}

if (rhsValTypeTag == TypeTags.DECIMAL_TAG) {
return DecimalValue.valueOf((double) lhsValue).equals(rhsValue);
}

return false;
case TypeTags.DECIMAL_TAG:
case TypeTags.BOOLEAN_TAG:
return lhsValue.equals(rhsValue);
if (rhsValTypeTag <= TypeTags.FLOAT_TAG) {
return DecimalValue.valueOf(((Number) rhsValue).doubleValue()).equals(lhsValue);
}

if (rhsValTypeTag == TypeTags.DECIMAL_TAG) {
return ((DecimalValue) rhsValue).equals(lhsValue);
}

return false;
case TypeTags.INT_TAG:
if (rhsValTypeTag != TypeTags.BYTE_TAG && rhsValTypeTag != TypeTags.INT_TAG) {
return false;
if (rhsValTypeTag <= TypeTags.FLOAT_TAG) {
return lhsValue.equals(((Number) rhsValue).longValue());
}
return lhsValue.equals(((Number) rhsValue).longValue());

if (rhsValTypeTag == TypeTags.DECIMAL_TAG) {
return DecimalValue.valueOf((long) lhsValue).equals(rhsValue);
}

return false;
case TypeTags.BYTE_TAG:
if (rhsValTypeTag != TypeTags.BYTE_TAG && rhsValTypeTag != TypeTags.INT_TAG) {
return false;
if (rhsValTypeTag <= TypeTags.FLOAT_TAG) {
return ((Number) lhsValue).byteValue() == ((Number) rhsValue).byteValue();
}

if (rhsValTypeTag == TypeTags.DECIMAL_TAG) {
return DecimalValue.valueOf((int) lhsValue).equals(rhsValue);
}
return ((Number) lhsValue).byteValue() == ((Number) rhsValue).byteValue();

return false;
case TypeTags.BOOLEAN_TAG:
if (rhsValTypeTag <= TypeTags.FLOAT_TAG) {
return ((boolean) lhsValue) == (((Number) rhsValue).longValue() == 1);
}

if (rhsValTypeTag == TypeTags.DECIMAL_TAG) {
return ((boolean) lhsValue) == ((DecimalValue) rhsValue).booleanValue();
}

return false;
case TypeTags.XML_TAG:
return XMLFactory.isEqual((XMLValue) lhsValue, (XMLValue) rhsValue);
case TypeTags.TABLE_TAG:
Expand Down
Loading

0 comments on commit 9580039

Please sign in to comment.