Skip to content

Commit

Permalink
fix(asserts): improves like,and imrpoves output for types, across the…
Browse files Browse the repository at this point in the history
… board (#171)
  • Loading branch information
georgejecook authored May 21, 2022
1 parent f2047a7 commit e526c26
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 46 deletions.
48 changes: 24 additions & 24 deletions framework/src/source/BaseTestSuite.bs
Original file line number Diff line number Diff line change
Expand Up @@ -263,8 +263,8 @@ namespace rooibos
try
if not Rooibos.Common.eqValues(first, second)
if msg = ""
first_as_string = Rooibos.Common.asString(first)
second_as_string = Rooibos.Common.asString(second)
first_as_string = Rooibos.Common.asString(first, true)
second_as_string = Rooibos.Common.asString(second, true)
msg = first_as_string + " != " + second_as_string
end if
m.currentResult.fail(msg, m.currentAssertLineNumber)
Expand Down Expand Up @@ -295,7 +295,7 @@ namespace rooibos
return false
end if
try
if first <> second
if not Rooibos.Common.eqValues(first, second, true)
if msg = ""
first_as_string = Rooibos.Common.asString(first)
second_as_string = Rooibos.Common.asString(second)
Expand Down Expand Up @@ -331,8 +331,8 @@ namespace rooibos
try
if Rooibos.Common.eqValues(first, second)
if msg = ""
first_as_string = Rooibos.Common.asString(first)
second_as_string = Rooibos.Common.asString(second)
first_as_string = Rooibos.Common.asString(first, true)
second_as_string = Rooibos.Common.asString(second, true)
msg = first_as_string + " == " + second_as_string
end if
m.currentResult.fail(msg, m.currentAssertLineNumber)
Expand Down Expand Up @@ -365,7 +365,7 @@ namespace rooibos
try
if value <> invalid
if msg = ""
expr_as_string = Rooibos.Common.asString(value)
expr_as_string = Rooibos.Common.asString(value, true)
msg = expr_as_string + " <> Invalid"
end if
m.currentResult.fail(msg, m.currentAssertLineNumber)
Expand Down Expand Up @@ -590,7 +590,7 @@ namespace rooibos
try
if Rooibos.Common.isAssociativeArray(array) or Rooibos.Common.isArray(array)
if not Rooibos.Common.arrayContains(array, value, key)
msg = "Array doesn't have the '" + Rooibos.Common.asString(value) + "' value."
msg = "Array doesn't have the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -634,7 +634,7 @@ namespace rooibos
for each value in values
isMatched = false
if not Rooibos.Common.isAssociativeArray(value)
msg = "Value to search for was not associativeArray " + Rooibos.Common.asString(value)
msg = "Value to search for was not associativeArray " + Rooibos.Common.asString(value, true)
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand All @@ -657,7 +657,7 @@ namespace rooibos
end for ' items in array

if not isMatched
msg = "array missing value: " + Rooibos.Common.asString(value)
msg = "array missing value: " + Rooibos.Common.asString(value, true)
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -697,7 +697,7 @@ namespace rooibos
try
if Rooibos.Common.isAssociativeArray(array) or Rooibos.Common.isArray(array)
if Rooibos.Common.arrayContains(array, value, key)
msg = "Array has the '" + Rooibos.Common.asString(value) + "' value."
msg = "Array has the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -741,7 +741,7 @@ namespace rooibos
value = subset[key]
end if
if not Rooibos.Common.arrayContains(array, value, key)
msg = "Array doesn't have the '" + Rooibos.Common.asString(value) + "' value."
msg = "Array doesn't have the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -786,7 +786,7 @@ namespace rooibos
value = item[key]
end if
if Rooibos.Common.arrayContains(array, value, key)
msg = "Array has the '" + Rooibos.Common.asString(value) + "' value."
msg = "Array has the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -988,7 +988,7 @@ namespace rooibos
if typeCheckFunction <> invalid
for each item in array
if not typeCheckFunction(item)
msg = Rooibos.Common.asString(item) + "is not a '" + typeStr + "' type."
msg = Rooibos.Common.asString(item, true) + " is not a '" + typeStr + "' type."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -1066,7 +1066,7 @@ namespace rooibos
try
if type(value) <> typeStr
if msg = ""
expr_as_string = Rooibos.Common.asString(value)
expr_as_string = Rooibos.Common.asString(value, true)
msg = expr_as_string + " was not expected type " + typeStr
end if
m.currentResult.fail(msg, m.currentAssertLineNumber)
Expand Down Expand Up @@ -1099,14 +1099,14 @@ namespace rooibos
try
if type(value) <> "roSGNode"
if msg = ""
expr_as_string = Rooibos.Common.asString(value)
expr_as_string = Rooibos.Common.asString(value, true)
msg = expr_as_string + " was not a node, so could not match subtype " + typeStr
end if
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
else if value.subType() <> typeStr
if msg = ""
expr_as_string = Rooibos.Common.asString(value)
expr_as_string = Rooibos.Common.asString(value, true)
msg = expr_as_string + "( type : " + value.subType() + ") was not of subType " + typeStr
end if
m.currentResult.fail(msg, m.currentAssertLineNumber)
Expand Down Expand Up @@ -1308,7 +1308,7 @@ namespace rooibos
try
if type(node) = "roSGNode"
if not Rooibos.Common.nodeContains(node, value)
msg = "Node doesn't have the '" + Rooibos.Common.asString(value) + "' value."
msg = "Node doesn't have the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -1344,7 +1344,7 @@ namespace rooibos
try
if type(node) = "roSGNode"
if not Rooibos.Common.nodeContains(node, value)
msg = "Node doesn't have the '" + Rooibos.Common.asString(value) + "' value."
msg = "Node doesn't have the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
else if node.getChildCount() <> 1
Expand Down Expand Up @@ -1385,7 +1385,7 @@ namespace rooibos
try
if type(node) = "roSGNode"
if Rooibos.Common.nodeContains(node, value)
msg = "Node has the '" + Rooibos.Common.asString(value) + "' value."
msg = "Node has the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -1427,7 +1427,7 @@ namespace rooibos
subsetValue = subset[key]
nodeValue = node[key]
if not Rooibos.Common.eqValues(nodeValue, subsetValue)
msg = key + ": Expected '" + Rooibos.Common.asString(subsetValue) + "', got '" + Rooibos.Common.asString(nodeValue) + "'"
msg = key + ": Expected '" + Rooibos.Common.asString(subsetValue, true) + "', got '" + Rooibos.Common.asString(nodeValue, true) + "'"
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -1476,7 +1476,7 @@ namespace rooibos
value = item[key]
end if
if Rooibos.Common.nodeContains(node, value)
msg = "Node has the '" + Rooibos.Common.asString(value) + "' value."
msg = "Node has the '" + Rooibos.Common.asString(value, true) + "' value."
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -1524,7 +1524,7 @@ namespace rooibos
subsetValue = subset[key]
arrayValue = array[key]
if not Rooibos.Common.eqValues(arrayValue, subsetValue)
msg = key + ": Expected '" + Rooibos.Common.asString(subsetValue) + "', got '" + Rooibos.Common.asString(arrayValue) + "'"
msg = key + ": Expected '" + Rooibos.Common.asString(subsetValue, true) + "', got '" + Rooibos.Common.asString(arrayValue, true) + "'"
m.currentResult.fail(msg, m.currentAssertLineNumber)
return false
end if
Expand Down Expand Up @@ -2022,7 +2022,7 @@ namespace rooibos

if isUsingMatcher
if not expected.matcher(value)
m.mockFail(mock.lineNumbers[invocationIndex], methodName, "on Invocation #" + stri(invocationIndex).trim() + ", expected arg #" + stri(i).trim() + " to match matching function '" + Rooibos.Common.asString(expected.matcher) + "' got '" + Rooibos.Common.asString(value) + "')")
m.mockFail(mock.lineNumbers[invocationIndex], methodName, "on Invocation #" + stri(invocationIndex).trim() + ", expected arg #" + stri(i).trim() + " to match matching function '" + Rooibos.Common.asString(expected.matcher) + "' got '" + Rooibos.Common.asString(value, true) + "')")
m.cleanMocks()
end if
else
Expand All @@ -2031,7 +2031,7 @@ namespace rooibos
expected = "[INVALID]"
end if

m.mockFail(mock.lineNumbers[invocationIndex], methodName, "on Invocation #" + stri(invocationIndex).trim() + ", expected arg #" + stri(i).trim() + " to be '" + Rooibos.Common.asString(expected) + "' got '" + Rooibos.Common.asString(value) + "')")
m.mockFail(mock.lineNumbers[invocationIndex], methodName, "on Invocation #" + stri(invocationIndex).trim() + ", expected arg #" + stri(i).trim() + " to be '" + Rooibos.Common.asString(expected, true) + "' got '" + Rooibos.Common.asString(value, true) + "')")
m.cleanMocks()
return
end if
Expand Down
73 changes: 52 additions & 21 deletions framework/src/source/CommonUtils.bs
Original file line number Diff line number Diff line change
Expand Up @@ -303,15 +303,23 @@ namespace rooibos.Common
' * @param {Dynamic} input - value to check
' * @returns {String} - converted string
' */
function asString(input) as string
function asString(input, includeType = false) as string
if Rooibos.Common.isValid(input) = false
return "Invalid"
else if Rooibos.Common.isString(input)
return input
return """" + input + """"
else if Rooibos.Common.isInteger(input) or Rooibos.Common.isLongInteger(input) or Rooibos.Common.isBoolean(input)
return input.ToStr()
if includeType
return input.ToStr() + " (" + Rooibos.Common.getSafeType(input) + ")"
else
return input.ToStr()
end if
else if Rooibos.Common.isFloat(input) or Rooibos.Common.isDouble(input)
return Str(input).Trim()
if includeType
return Str(input).Trim() + " (" + Rooibos.Common.getSafeType(input) + ")"
else
return Str(input).Trim()
end if
else if type(input) = "roSGNode"
return "Node(" + input.subType() + ")"
else if type(input) = "roAssociativeArray"
Expand All @@ -324,7 +332,7 @@ namespace rooibos.Common
end if
for each key in input
if rooibos.Common.canSafelyIterateAAKey(input, key)
text = text + key + ":" + Rooibos.Common.asString(input[key])
text = text + key + ":" + Rooibos.Common.asString(input[key], includeType)
end if
end for
text = text + "}"
Expand All @@ -335,7 +343,7 @@ namespace rooibos.Common
maxLen = 500
for each v in input
if len(text) < maxLen
text += join + rooibos.Common.asString(v)
text += join + rooibos.Common.asString(v, includeType)
join = ", "
end if
end for
Expand All @@ -345,7 +353,7 @@ namespace rooibos.Common
text = text + "]"
return text
else if Rooibos.Common.isFunction(input)
return input.toStr()
return input.toStr() + "(function)"
else
return ""
end if
Expand Down Expand Up @@ -617,7 +625,7 @@ namespace rooibos.Common
' * @param {Dynamic} Value2 - second item to compare
' * @returns {boolean} - True if values are equal or False in other case.
' */
function eqValues(Value1, Value2) as dynamic
function eqValues(Value1, Value2, fuzzy = false) as dynamic
' Workaraund for bug with string boxing, and box everything else
val1Type = Rooibos.Common.getSafeType(Value1)
val2Type = Rooibos.Common.getSafeType(Value2)
Expand All @@ -633,32 +641,55 @@ namespace rooibos.Common
Value1 = cdbl(Value1)
end if

if val1Type <> val2Type
if val1Type <> val2Type and fuzzy <> true
return false
else
valtype = val1Type

if valtype = "List"
return Rooibos.Common.eqArray(Value1, Value2)
if val1Type = "List"
return Rooibos.Common.eqArray(Value1, Value2, fuzzy)
else if valtype = "roAssociativeArray"
return Rooibos.Common.eqAssocArray(Value1, Value2)
return Rooibos.Common.eqAssocArray(Value1, Value2, fuzzy)
else if valtype = "roArray"
return Rooibos.Common.eqArray(Value1, Value2)
return Rooibos.Common.eqArray(Value1, Value2, fuzzy)
else if valtype = "roSGNode"
if val2Type <> "roSGNode"
return false
else
return Value1.isSameNode(Value2)
end if
else
'If you crashed on this line, then you're trying to compare
'2 things which can't be compared - check what value1 and value2
'are in your debug log
return Value1 = Value2
if fuzzy = true
return Rooibos.Common.asString(Value1) = Rooibos.Common.asString(Value2)
else
'If you crashed on this line, then you're trying to compare
'2 things which can't be compared - check what value1 and value2
'are in your debug log
return Value1 = Value2
end if
end if
end if
end function

function eqTypes(Value1, Value2) as dynamic
val1Type = Rooibos.Common.getSafeType(Value1)
val2Type = Rooibos.Common.getSafeType(Value2)
if val1Type = invalid or val2Type = invalid
? "ERROR!!!! - undefined value passed"
return false
end if

'Upcast int to float, if other is float
if val1Type = "Float" and val2Type = "Integer"
Value2 = cdbl(Value2)
else if val2Type = "Float" and val1Type = "Integer"
Value1 = cdbl(Value1)
end if

return val1Type <> val2Type
end function


' /**
' * @memberof module:CommonUtils
' * @name EqAssocArray
Expand All @@ -669,7 +700,7 @@ namespace rooibos.Common
' * @param {Dynamic} Value2 - second associative array
' * @returns {boolean} - True if arrays are equal or False in other case.
' */
function eqAssocArray(Value1, Value2) as dynamic
function eqAssocArray(Value1, Value2, fuzzy = false) as dynamic
l1 = Value1.Count()
l2 = Value2.Count()

Expand All @@ -683,7 +714,7 @@ namespace rooibos.Common
if rooibos.common.canSafelyIterateAAKey(Value1, k) and rooibos.common.canSafelyIterateAAKey(Value2, k)
v1 = Value1[k]
v2 = Value2[k]
if not Rooibos.Common.eqValues(v1, v2)
if not Rooibos.Common.eqValues(v1, v2, fuzzy)
return false
end if
end if
Expand Down Expand Up @@ -713,7 +744,7 @@ namespace rooibos.Common
' * @param {Dynamic} Value2 - second array
' * @returns {boolean} - True if arrays are equal or False in other case.
' */
function eqArray(Value1, Value2) as dynamic
function eqArray(Value1, Value2, fuzzy = false) as dynamic
if not (Rooibos.Common.isArray(Value1)) or not Rooibos.Common.isArray(Value2)
return false
end if
Expand All @@ -727,7 +758,7 @@ namespace rooibos.Common
for i = 0 to l1 - 1
v1 = Value1[i]
v2 = Value2[i]
if not Rooibos.Common.eqValues(v1, v2)
if not Rooibos.Common.eqValues(v1, v2, fuzzy)
return false
end if
end for
Expand Down
Loading

0 comments on commit e526c26

Please sign in to comment.