Permalink
Browse files

Merge branch 'master' of git://github.com/kristopher/jsunittest into …

…integration
  • Loading branch information...
2 parents 14444b9 + fbf5443 commit a774b95645b368f9aa36d448954a48206cad82b5 @drnic committed Mar 21, 2009
Showing with 54 additions and 54 deletions.
  1. +7 −50 src/assertions.js
  2. +46 −3 src/common.js
  3. +1 −1 test/unit/test_case_test.html
View
@@ -4,7 +4,7 @@ JsUnitTest.Unit.Assertions = {
return (message ? message + '\n' : '') +
new JsUnitTest.Unit.MessageTemplate(template).evaluate(args);
},
-
+
flunk: function(message) {
this.assertBlock(message || 'Flunked', function() { return false });
},
@@ -32,65 +32,22 @@ JsUnitTest.Unit.Assertions = {
assertEnumEqual: function(expected, actual, message) {
message = this.buildMessage(message || 'assertEnumEqual', 'expected <?>, actual: <?>', expected, actual);
- var expected_array = JsUnitTest.flattenArray(expected);
- var actual_array = JsUnitTest.flattenArray(actual);
- this.assertBlock(message, function() {
- if (expected_array.length == actual_array.length) {
- for (var i=0; i < expected_array.length; i++) {
- if (expected_array[i] != actual_array[i]) return false;
- };
- return true;
- }
- return false;
- });
+ this.assertBlock(message, function() { return JsUnitTest.areArraysEqual(expected, actual) });
},
assertEnumNotEqual: function(expected, actual, message) {
message = this.buildMessage(message || 'assertEnumNotEqual', '<?> was the same as <?>', expected, actual);
- var expected_array = JsUnitTest.flattenArray(expected);
- var actual_array = JsUnitTest.flattenArray(actual);
- this.assertBlock(message, function() {
- if (expected_array.length == actual_array.length) {
- for (var i=0; i < expected_array.length; i++) {
- if (expected_array[i] != actual_array[i]) return true;
- };
- return false;
- }
- return true;
- });
+ this.assertBlock(message, function() { return JsUnitTest.areArraysNotEqual(expected, actual) });
},
assertHashEqual: function(expected, actual, message) {
- message = this.buildMessage(message || 'assertHashEqual', 'expected <?>, actual: <?>', expected, actual);
- var expected_array = JsUnitTest.flattenArray(JsUnitTest.hashToSortedArray(expected));
- var actual_array = JsUnitTest.flattenArray(JsUnitTest.hashToSortedArray(actual));
- var block = function() {
- if (expected_array.length == actual_array.length) {
- for (var i=0; i < expected_array.length; i++) {
- if (expected_array[i] != actual_array[i]) return false;
- };
- return true;
- }
- return false;
- };
- this.assertBlock(message, block);
+ message = this.buildMessage(message || 'assertHashEqual', 'expected <?>, actual: <?>', JsUnitTest.inspect(expected), JsUnitTest.inspect(actual));
+ this.assertBlock(message, function() { return JsUnitTest.areHashesEqual(expected, actual) });
},
assertHashNotEqual: function(expected, actual, message) {
- message = this.buildMessage(message || 'assertHashNotEqual', '<?> was the same as <?>', expected, actual);
- var expected_array = JsUnitTest.flattenArray(JsUnitTest.hashToSortedArray(expected));
- var actual_array = JsUnitTest.flattenArray(JsUnitTest.hashToSortedArray(actual));
- // from now we recursively zip & compare nested arrays
- var block = function() {
- if (expected_array.length == actual_array.length) {
- for (var i=0; i < expected_array.length; i++) {
- if (expected_array[i] != actual_array[i]) return true;
- };
- return false;
- }
- return true;
- };
- this.assertBlock(message, block);
+ message = this.buildMessage(message || 'assertHashNotEqual', '<?> was the same as <?>', JsUnitTest.inspect(expected), JsUnitTest.inspect(actual));
+ this.assertBlock(message, function() { return JsUnitTest.areHashesNotEqual(expected, actual) });
},
assertIdentical: function(expected, actual, message) {
View
@@ -20,13 +20,26 @@ var JsUnitTest = {
if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
return "'" + escapedString.replace(/'/g, '\\\'') + "'";
};
+ if (JsUnitTest.getClass(object) === 'Object') {
+ var keys_values = new Array(), prefix = 'Object: { ';
+ for (property in object) {
+ keys_values.push(property + ': ' + object[property]);
+ }
+ return (prefix + keys_values.join(', ') + ' }');
+ }
return String(object);
} catch (e) {
if (e instanceof RangeError) return '...';
throw e;
}
},
- $: function(element) {
+
+ getClass: function(object) {
+ return Object.prototype.toString.call(object)
+ .match(/^\[object\s(.*)\]$/)[1];
+ },
+
+ $: function(element) {
if (arguments.length > 1) {
for (var i = 0, elements = [], length = arguments.length; i < length; i++)
elements.push(this.$(arguments[i]));
@@ -36,7 +49,8 @@ var JsUnitTest = {
element = document.getElementById(element);
return element;
},
- gsub: function(source, pattern, replacement) {
+
+ gsub: function(source, pattern, replacement) {
var result = '', match;
replacement = arguments.callee.prepareReplacement(replacement);
@@ -71,6 +85,34 @@ var JsUnitTest = {
return myarray;
},
+
+ // from now we recursively zip & compare nested arrays
+ areArraysEqual: function(expected, actual) {
+ var expected_array = JsUnitTest.flattenArray(expected);
+ var actual_array = JsUnitTest.flattenArray(actual);
+ if (expected_array.length == actual_array.length) {
+ for (var i=0; i < expected_array.length; i++) {
+ if (expected_array[i] != actual_array[i]) return false;
+ };
+ return true;
+ }
+ return false;
+ },
+
+ areArraysNotEqual: function(expected, actual) {
+ return !this.areArraysEqual(expected, actual);
+ },
+
+ areHashesEqual: function(expected, actual) {
+ var expected_array = JsUnitTest.hashToSortedArray(expected);
+ var actual_array = JsUnitTest.hashToSortedArray(actual);
+ return this.areArraysEqual(expected_array, actual_array);
+ },
+
+ areHashesNotEqual: function(expected, actual) {
+ return !this.areHashesEqual(expected, actual);
+ },
+
hashToSortedArray: function(hash) {
var results = [];
for (key in hash) {
@@ -161,7 +203,8 @@ var JsUnitTest = {
return match;
},
- toQueryParams: function(query, separator) {
+
+ toQueryParams: function(query, separator) {
var query = query || window.location.search;
var match = query.replace(/^\s+/, '').replace(/\s+$/, '').match(/([^?#]*)(#.*)?$/);
if (!match) return { };
@@ -86,7 +86,7 @@
testcase.error({name: "name", message: "An Error"}, testcase);
assertEqual(0, testcase.assertions, "Assertions");
assertEqual(1, testcase.errors, "Errors");
- assertEqual("name: An Error([object Object])", testcase.messages[0], 'Should be equal');
+ assertEqual("name: An Error(Object: { name: name, message: An Error })", testcase.messages[0], 'Should be equal');
assertEqual('error', testcase.status(), 'Should be equal');
}},

0 comments on commit a774b95

Please sign in to comment.