diff --git a/framework/src/source/BaseTestSuite.bs b/framework/src/source/BaseTestSuite.bs index 88089c31..d8d55780 100644 --- a/framework/src/source/BaseTestSuite.bs +++ b/framework/src/source/BaseTestSuite.bs @@ -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) @@ -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) @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/framework/src/source/CommonUtils.bs b/framework/src/source/CommonUtils.bs index de7f61ef..64477014 100755 --- a/framework/src/source/CommonUtils.bs +++ b/framework/src/source/CommonUtils.bs @@ -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" @@ -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 + "}" @@ -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 @@ -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 @@ -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) @@ -633,17 +641,17 @@ 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 @@ -651,14 +659,37 @@ namespace rooibos.Common 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 @@ -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() @@ -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 @@ -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 @@ -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 diff --git a/tests/src/source/Basic.spec.bs b/tests/src/source/Basic.spec.bs index 043f5b66..4ade40d4 100644 --- a/tests/src/source/Basic.spec.bs +++ b/tests/src/source/Basic.spec.bs @@ -181,5 +181,34 @@ namespace tests m.assertEqual(msg, "[one, two, three] != [2one, 2two, 2three]") end function + '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + @describe("assertLike") + '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + @it("does not fail on like values") + @params({ id: 10 }, { id: 10.0 }) + @params([10], [10.0]) + @params(10, 10.0) + function _(value1, value2) + m.assertLike(value1, value2) + end function + + @it("does fail on really unalike values") + @params(10, invalid) + @params(invalid, 10) + @params(invalid, "invalid") + @params(false, "false") + @params(true, "true") + @params(10, "10.0") + @params({ id: "10" }, { id: 10 }) + @params({ id: "10" }, { id: 10.0 }) + function _(value1, value2) + m.assertLike(value1, value2) + isFail = m.currentResult.isFail + msg = m.currentResult.getMessage() + m.currentResult.Reset() + m.assertTrue(isFail) + end function + end class end namespace \ No newline at end of file diff --git a/tests/src/source/NewExpectSyntax.spec.bs b/tests/src/source/NewExpectSyntax.spec.bs index ff0c69f7..3e81f8bf 100644 --- a/tests/src/source/NewExpectSyntax.spec.bs +++ b/tests/src/source/NewExpectSyntax.spec.bs @@ -1,6 +1,5 @@ namespace tests - @only @suite class NewExpectSyntaxTests extends rooibos.BaseTestSuite