Permalink
Browse files

Improved error reporting and fixed an isEqual bug

git-svn-id: file:///Users/jyurek/Backups/repositories/jocha/trunk@23 5317845c-832e-0410-b2c0-db6c5bc7c761
  • Loading branch information...
jyurek
jyurek committed Apr 30, 2007
1 parent 26cd50b commit a5e6bb33b29a7dc7e6e65b542f7bef45bb0f2df9
Showing with 46 additions and 19 deletions.
  1. +46 −19 jocha.js
View
@@ -1,13 +1,22 @@
Jocha = {
mocks : [],
- verifyAll : function(){
+ verifyAll : function()
+ {
this.mocks.each(function(m){
- assert( m.expectations.all(function(e){
- return e.invoked;
- }), "Not all expectations were met." );
+ var unmet = m.expectations.findAll(function(e){
+ return !e.invoked;
+ }).compact();
+
+ var unmet_string = "Not all expectations were met:\n";
+ unmet.each(function(e,i){
+ unmet_string += (i+1) + ": " + e.functionName + "(" + e.params.map(Object.inspect) + ")\n";
+ });
+
+ assert(unmet.size() == 0, unmet_string);
});
},
- reset: function(){
+ reset: function()
+ {
this.mocks = [];
}
}
@@ -16,6 +25,11 @@ Jocha.Mock = Class.create();
Jocha.Mock.prototype = {
initialize : function() {
this.expectations = new Array();
+ },
+ getAllExpectationsNamed: function(name){
+ return this.expectations.findAll( function(e){
+ return e.functionName == name;
+ });
}
}
@@ -66,26 +80,29 @@ Object.extend(Object.prototype, {
return false;
},
methodMocked : function(functionName, args) {
- var expectation = null;
- for(var i=0; i < this.mock.expectations.length; i++)
- {
- var e = this.mock.expectations[i];
- if(e.functionName == functionName && e.params.isEqual($A(args)))
- {
- expectation = e;
- break;
- }
- }
- if (expectation) {
+ var expectation = this.mock.expectations.find( function(e){
+ return e.functionName == functionName && e.params.isEqual($A(args))
+ });
+ if (expectation)
+ {
expectation.invoked = true;
return expectation.returnVal;
- } else {
+ }
+ else
+ {
var st = null;
if(stackTrace){ st = stackTrace(); }
else{ try{ throw new Error() } catch(e) { st = e.stack } }
- throw new Jocha.FailedExpectation(functionName + " was called with the wrong arguments.", st);
+ var expected = this.mock.getAllExpectationsNamed(functionName);
+ var message = functionName + " was called with the wrong arguments. Expected one of the following:\n";
+ expected.each(function(e, i){
+ message += (i+1) + ": " + e.params + "\n";
+ })
+ message += "But was called with:\n" + $A(args);
+
+ throw new Jocha.FailedExpectation(message, st);
}
}
});
@@ -98,6 +115,16 @@ if(typeof(assert) == "undefined") {
// HELPERS
+Object.prototype.toString = function() {
+ return "#<Object:{" + $H(this).map(function(pair) {
+ return pair.map(Object.inspect).join(': ');
+ }).join(', ') + '}>';
+}
+
+Function.prototype.inspect = function() {
+ return "#<Function: " + this.toSource.replace(/\{.*\}/, "{...}") + " >";
+}
+
// Thanks http://www.svendtofte.com/code/usefull_prototypes/
Array.prototype.isEqual = function(other) {
if (this.length != other.length) return false;
@@ -119,7 +146,7 @@ Object.equal = function(one, two){
if(typeof(one) == "object" && one != null && one.isEqual){
return( one.isEqual(two) );
} else {
- return( one != two );
+ return( one == two );
}
}

0 comments on commit a5e6bb3

Please sign in to comment.