Permalink
Browse files

feat #327 improve error messages in assertEquals/NotEquals/JsonEquals

1. Allow usage of %1 %2 in assertEquals messages

%1 and %2 are shortcuts for value1 and value2 params passed to
%assertEquals and can be used as placeholders in the error message.

This commit implements the feature (Assert.js) and changes some of the
unit tests to use the new capability.

2. Fix the obvious issue with the error message of
'assertNotEquals'.

3. Write serialized versions of JSON entities in assertJsonEquals
in case there's no default error message passed.

Close #327. Close #328.
  • Loading branch information...
jakub-g authored and Fabio Crisci committed Jan 14, 2013
1 parent 4c3b221 commit 48b49da866a7efc5967e3238d107351770aef3f7
View
@@ -175,27 +175,40 @@
* @param {String} optMsg optional message to add to the failure description
*/
assertFalse : function (value, optMsg) {
- this.assertTrue((value === false), optMsg || ("Expected false. Got : " + value));
+ this.assertTrue((value === false), optMsg || ("Expected false. Got: " + value));
},
/**
* AssertEquals: Check that value1 is equal to value2
* @param {object} value1 the value to compare
* @param {object} value2 the value to compare
- * @param {String} optMsg optional message to add to the failure description
+ * @param {String} optMsg optional message to add to the failure description. It can contain placeholders %1
+ * and %2 which will be replaced accordingly with: %1 - value1, %2 - value2.
*/
assertEquals : function (value1, value2, optMsg) {
- this.assertTrue((value1 === value2), optMsg || ("Expected : " + value1 + ". Got : " + value2));
+ var msg;
+
+ if (optMsg) {
+ msg = aria.utils.String.substitute(optMsg, ['"' + value1 + '"', '"' + value2 + '"']);
+ } else {
+ msg = 'First value: "' + value1 + '" differs from the second: "' + value2 + '".';
+ }
+
+ this.assertTrue((value1 === value2), msg);
},
/**
* AssertNotEquals: Check that value1 is not! equal to value2
* @param {object} value1 the value to compare
* @param {object} value2 the value to compare
* @param {String} optMsg optional message to add to the failure description
+ * @param {String} optAssertId optional ID to quickly locate the failed assertion (in case of many
+ * assertions with the same, or default, message). Could be e.g. a line where the assertion is put at
+ * creation time.
*/
- assertNotEquals : function (value1, value2, optMsg) {
- this.assertTrue((value1 !== value2), optMsg || ("Expected : " + value1 + ". Got : " + value2));
+ assertNotEquals : function (value1, value2, optMsg, optAssertId) {
+ var msg = optMsg || ("First and second value shouldn't be equal but they are both equal to: " + value1);
+ this.assertTrue((value1 !== value2), msg);
},
/**
@@ -302,6 +315,12 @@
* @param {String} optMsg optional message to add to the failure description
*/
assertJsonEquals : function (obj1, obj2, optMsg) {
+ if (!optMsg) {
+ var s1 = aria.utils.Json.convertToJsonString(obj1);
+ var s2 = aria.utils.Json.convertToJsonString(obj2);
+ optMsg = "JSON comparison failed. First object: <code>" + s1
+ + "</code> differs from the second: <code>" + s2 + "</code>.";
+ }
this.assertTrue(aria.utils.Json.equals(obj1, obj2), optMsg);
},
@@ -51,10 +51,8 @@ Aria.classDefinition({
var suggestions = controller.data.suggestions;
this.assertEquals(suggestions.length, 2, "ByInstance should have two suggestions");
- this.assertEquals(suggestions[0], "ByInstance-", "Suggestion 0 expecting ByInstance- got "
- + suggestions[0]);
- this.assertEquals(suggestions[1], "ByInstance-suggestion", "Suggestion 1 expecting ByInstance-suggestion got "
- + suggestions[1]);
+ this.assertEquals(suggestions[0], "ByInstance-", "Suggestion 0 expecting %2, got %1");
+ this.assertEquals(suggestions[1], "ByInstance-suggestion", "Suggestion 1 expecting %2, got %1");
controller.$dispose();
@@ -93,10 +91,8 @@ Aria.classDefinition({
var suggestions = controller.data.suggestions;
this.assertEquals(suggestions.length, 2, "ByClasspath should have two suggestions");
- this.assertEquals(suggestions[0], "ByClasspath-", "Suggestion 0 expecting ByClasspath- got "
- + suggestions[0]);
- this.assertEquals(suggestions[1], "ByClasspath-suggestion", "Suggestion 1 expecting ByClasspath-suggestion got "
- + suggestions[1]);
+ this.assertEquals(suggestions[0], "ByClasspath-", "Suggestion 0 expecting %2, got %1");
+ this.assertEquals(suggestions[1], "ByClasspath-suggestion", "Suggestion 1 expecting %2, got %1");
controller.$dispose();
@@ -134,9 +130,8 @@ Aria.classDefinition({
var suggestions = controller.data.suggestions;
this.assertEquals(suggestions.length, 2, "Dynamic should have two suggestions");
- this.assertEquals(suggestions[0], "Dynamic-", "Suggestion 0 expecting Dynamic- got " + suggestions[0]);
- this.assertEquals(suggestions[1], "Dynamic-suggestion", "Suggestion 1 expecting Dynamic-suggestion got "
- + suggestions[1]);
+ this.assertEquals(suggestions[0], "Dynamic-", "Suggestion 0 expecting expecting %2, got %1");
+ this.assertEquals(suggestions[1], "Dynamic-suggestion", "Suggestion 1 expecting expecting %2, got %1");
// Do another getSuggestion just in case, we do have some function rewrite in the class
controller.suggestValue("+");
@@ -156,9 +151,8 @@ Aria.classDefinition({
var suggestions = controller.data.suggestions;
this.assertEquals(suggestions.length, 2, "Dynamic should have two suggestions");
- this.assertEquals(suggestions[0], "Dynamic+", "Suggestion 0 expecting Dynamic+ got " + suggestions[0]);
- this.assertEquals(suggestions[1], "Dynamic+suggestion", "Suggestion 1 expecting Dynamic+suggestion got "
- + suggestions[1]);
+ this.assertEquals(suggestions[0], "Dynamic+", "Suggestion 0 expecting %2, got %1");
+ this.assertEquals(suggestions[1], "Dynamic+suggestion", "Suggestion 1 expecting expecting %2, got %1");
controller.$dispose();
@@ -203,4 +197,4 @@ Aria.classDefinition({
this.notifyTestEnd(testName);
}
}
-})
+})
@@ -69,42 +69,42 @@ Aria.classDefinition({
// Change a value with listener
aria.utils.Json.setValue(basket, "apples", 3);
- this.assertEquals(name, "apple", "Expecting name apple, got " + name);
- this.assertEquals(value, 3, "Expecting value 3, got " + value);
+ this.assertEquals(name, "apple", "Expecting name %2, got %1");
+ this.assertEquals(value, 3, "Expecting value %2, got %1");
this.assertTrue(old == null, "Expecting old value null, got " + old);
// Change it again
aria.utils.Json.setValue(basket, "apples", 0);
- this.assertEquals(name, "apple", "Expecting name apple, got " + name);
- this.assertEquals(value, 0, "Expecting value 0, got " + value);
- this.assertEquals(old, 3, "Expecting old value 3, got " + old);
+ this.assertEquals(name, "apple", "Expecting name %2, got %1");
+ this.assertEquals(value, 0, "Expecting value %2, got %1");
+ this.assertEquals(old, 3, "Expecting old value %2, got %1");
// Change a different value
aria.utils.Json.setValue(basket, "pears", ["abate"]);
- this.assertEquals(name, "pear", "Expecting name pear, got " + name);
- this.assertEquals(value.length, 1, "Expecting value length 1, got " + value.length);
- this.assertEquals(value[0], "abate", "Expecting value abate, got " + value[0]);
+ this.assertEquals(name, "pear", "Expecting name %2, got %1");
+ this.assertEquals(value.length, 1, "Expecting value length %2, got %1");
+ this.assertEquals(value[0], "abate", "Expecting value %2, got %1");
this.assertTrue(old == null, "Expecting old value null, got " + old);
// And cahnge again the previous one
aria.utils.Json.setValue(basket, "apples", {
type : "golden"
});
- this.assertEquals(name, "apple", "Expecting name apple, got " + name);
- this.assertEquals(value.type, "golden", "Expecting value golden, got " + value.type);
- this.assertEquals(old, 0, "Expecting old value 0, got " + old);
+ this.assertEquals(name, "apple", "Expecting name %2, got %1");
+ this.assertEquals(value.type, "golden", "Expecting value %2, got %1");
+ this.assertEquals(old, 0, "Expecting old value %2, got %1");
// Change a value with no listeners
aria.utils.Json.setValue(shopping.bags[0], "hand", "middle");
- this.assertEquals(name, "apple", "Expecting name apple, got " + name);
- this.assertEquals(value.type, "golden", "Expecting value golden, got " + value.type);
- this.assertEquals(old, 0, "Expecting old value 0, got " + old);
+ this.assertEquals(name, "apple", "Expecting name %2, got %1");
+ this.assertEquals(value.type, "golden", "Expecting value %2, got %1");
+ this.assertEquals(old, 0, "Expecting old value %2, got %1");
// And now one with a listener
aria.utils.Json.setValue(shopping.bags[1], "content", false);
- this.assertEquals(name, "sugar", "Expecting name sugar, got " + name);
- this.assertEquals(value, false, "Expecting value false, got " + value);
- this.assertEquals(old, null, "Expecting old value null, got " + old);
+ this.assertEquals(name, "sugar", "Expecting name %2, got %1");
+ this.assertEquals(value, false, "Expecting value false, got %1");
+ this.assertEquals(old, null, "Expecting old value null, got %1");
widget.$dispose();
},
@@ -256,4 +256,4 @@ Aria.classDefinition({
three.$dispose();
}
}
-});
+});
@@ -347,8 +347,7 @@ Aria.classDefinition({
if (tests.hasOwnProperty(check)) {
var got = aria.utils.Number.interpretNumber(check);
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -372,8 +371,7 @@ Aria.classDefinition({
if (tests.hasOwnProperty(check)) {
var got = aria.utils.Number.interpretNumber(check);
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -397,8 +395,7 @@ Aria.classDefinition({
if (tests.hasOwnProperty(check)) {
var got = aria.utils.Number.interpretNumber(check);
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -422,8 +419,7 @@ Aria.classDefinition({
if (tests.hasOwnProperty(check)) {
var got = aria.utils.Number.interpretNumber(check);
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -452,8 +448,7 @@ Aria.classDefinition({
groupingSeparator : "\\"
});
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -481,8 +476,7 @@ Aria.classDefinition({
groupingSeparator : " "
});
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -507,8 +501,7 @@ Aria.classDefinition({
if (tests.hasOwnProperty(check)) {
var got = aria.utils.Number.interpretNumber(check);
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -537,8 +530,7 @@ Aria.classDefinition({
strictGrouping : false
});
- this.assertEquals(got, tests[check], "Comparing " + check + " expected " + tests[check] + " got "
- + got);
+ this.assertEquals(got, tests[check], "Comparing " + check + " expected %2, got %1");
}
}
},
@@ -603,4 +595,4 @@ Aria.classDefinition({
}
}
-});
+});
@@ -448,7 +448,7 @@ Aria.classDefinition({
if (holders.hasOwnProperty(key)) {
got = aria.utils.String.substitute(holders[key], "thisString");
- this.assertEquals(got, holders[key], "Expecting " + holders[key] + ", got " + got);
+ this.assertEquals(got, holders[key], "Expecting %2, got %1");
}
}
},
@@ -471,7 +471,7 @@ Aria.classDefinition({
if (holders.hasOwnProperty(key)) {
got = aria.utils.String.substitute(holders[key], ["thisString", "another"]);
- this.assertEquals(got, holders[key], "Expecting " + holders[key] + ", got " + got);
+ this.assertEquals(got, holders[key], "Expecting %2, got %1");
}
}
},
@@ -493,7 +493,7 @@ Aria.classDefinition({
if (holders.hasOwnProperty(key)) {
got = aria.utils.String.substitute(holders[key], ["one", "two"]);
- this.assertEquals(got, holders[key], "Expecting " + holders[key] + ", got " + got);
+ this.assertEquals(got, holders[key], "Expecting %2, got %1");
}
}
}
@@ -80,7 +80,7 @@ Aria.classDefinition({
input.value = "invalid";
widget._dom_onblur();
- this.assertEquals(input.value, "invalid", "Input should be 'invalid' got '" + input.value + "'");
+ this.assertEquals(input.value, "invalid", "Input value should be %2, got %1.");
// Now try to have a valid value
var today = new Date();
@@ -90,7 +90,7 @@ Aria.classDefinition({
input.value = formatted.replace(/\//g, "-");
widget._dom_onblur();
- this.assertEquals(input.value, formatted, "Input should be '" + formatted + "' got '" + input.value + "'");
+ this.assertEquals(input.value, formatted, "Input value should be %2, got %1.");
widget.$dispose();
},
@@ -122,7 +122,7 @@ Aria.classDefinition({
input.value = formatted;
widget._dom_onblur();
- this.assertEquals(input.value, formatted, "Input should be '" + formatted + "' got '" + input.value + "'");
+ this.assertEquals(input.value, formatted, "Input value should be %2, got %1.");
// Now set another valid date
var tomorrow = aria.utils.Date.interpret("+1");
@@ -132,7 +132,7 @@ Aria.classDefinition({
input.value = formatted.replace(/\//g, "-");
widget._dom_onblur();
- this.assertEquals(input.value, formatted, "Input should be '" + formatted + "' got '" + input.value + "'");
+ this.assertEquals(input.value, formatted, "Input value should be %2, got %1.");
widget.$dispose();
},
@@ -164,18 +164,18 @@ Aria.classDefinition({
input.value = formatted;
widget._dom_onblur();
- this.assertEquals(input.value, formatted, "Input should be '" + formatted + "' got '" + input.value + "'");
+ this.assertEquals(input.value, formatted, "Input value should be %2, got %1.");
// Now set it invalid
widget._dom_onfocus();
input.value = "invalid";
widget._dom_onblur();
- this.assertEquals(input.value, "invalid", "Input should be 'invalid' got '" + input.value + "'");
+ this.assertEquals(input.value, "invalid", "Input value should be %2, got %1.");
// but we should also check it's style
var color = aria.utils.Dom.getStyle(input, "color");
var isBlack = color === "black" || color === "inherit";
- this.assertTrue(isBlack,"Input should be black got color '" + input.value + "'");
+ this.assertTrue(isBlack, "Input should be black got color '" + input.value + "'");
widget.$dispose();
}
Oops, something went wrong.

0 comments on commit 48b49da

Please sign in to comment.