Permalink
Browse files

isEqual stuff works for nested objects

git-svn-id: file:///Users/jyurek/Backups/repositories/jocha/trunk@11 5317845c-832e-0410-b2c0-db6c5bc7c761
  • Loading branch information...
1 parent 0922c03 commit 45b07736bf430240458aafac07803b41bfb1b05d jyurek committed Apr 20, 2007
Showing with 31 additions and 11 deletions.
  1. +31 −11 jocha.js
View
@@ -1,14 +1,32 @@
// Thanks http://www.svendtofte.com/code/usefull_prototypes/
-Array.prototype.isEqual = function(arr) {
- if (this.length != arr.length) return false;
- for (var i = 0; i < arr.length; i++) {
- if (this[i].isEqual) { //likely nested array
- if (!this[i].isEqual(arr[i])) return false;
- else continue;
- }
- if (this[i] != arr[i]) return false;
- }
- return true;
+Array.prototype.isEqual = function(other) {
+ if (this.length != other.length) return false;
+ for (var p = 0; p < other.length; p++) {
+ if(Object.equal( this[p], other[p] )) return false;
+ }
+ return true;
+}
+
+Object.prototype.isEqual = function(other) {
+ if(this.attributes().length != other.attributes().length) return false;
+ for(var p in this){
+ if(Object.equal( this[p], other[p] )) return false;
+ }
+ return true;
+}
+
+Object.equal = function(one, two){
+ if(typeof(one) == "object" && one != null && one.isEqual){
+ if(!one.isEqual(two)) return false;
+ } else {
+ if(one != two) return false;
+ }
+}
+
+Object.prototype.attributes = function(){
+ var attributes = [];
+ for(var p in this){ attributes.push(p); }
+ return attributes;
}
Mock = Class.create();
@@ -52,7 +70,9 @@ Object.extend(Object.prototype, {
return false;
},
methodMocked : function(functionName, args) {
- expectation = this.mock.expectations.find(function(e){return e.functionName == functionName && e.params.isEqual($A(args))});
+ expectation = this.mock.expectations.find(function(e){
+ return e.functionName == functionName && e.params.isEqual($A(args))
+ });
if (expectation) {
expectation.invoked = true;
return expectation.returnVal;

0 comments on commit 45b0773

Please sign in to comment.