4 var Comparator = function() {};
5 Comparator
.prototype = {
6 compare: function(actual
, expected
) {
10 this.internal_compare('root', actual
, expected
);
13 internal_compare: function(context
, actual
, expected
) {
14 if (expected
instanceof Array
) {
15 return this.internal_compare_array(context
, actual
, expected
);
16 } else if (expected
instanceof Object
) {
17 return this.internal_compare_object(context
, actual
, expected
);
19 return this.internal_compare_value(context
, actual
, expected
);
23 internal_compare_array: function(context
, actual
, expected
) {
24 if (!(actual
instanceof Array
)) {
25 this.result
.pass
= false;
26 this.result
.message
= "The expected data has an array at " + context
+ ", but the actual data has something else (" + actual
+ ")";
29 if (expected
.length
!= actual
.length
) {
30 this.result
.pass
= false;
31 this.result
.message
= "The expected data has an array with " + expected
.length
+ " items in it, but the actual data has " + actual
.length
+ " items.";
34 for (var i
= 0; i
< expected
.length
; i
++) {
35 var still_matches
= this.internal_compare(context
+ "[" + i
+ "]", actual
[i
], expected
[i
]);
42 internal_compare_object: function(context
, actual
, expected
) {
43 if (!(actual
instanceof Object
) || actual
instanceof Array
) {
44 this.result
.pass
= false;
45 this.result
.message
= "The expected data has an object at root, but the actual data has something else (" + actual
+ ")";
48 for (var key
in expected
) {
49 if (!(key
in actual
)) {
50 this.result
.pass
= false;
51 this.result
.message
= "Could not find key '" + key
+ "' in actual data at " + context
+ ".";
54 var still_matches
= this.internal_compare(context
+ "[" + key
+ "]", actual
[key
], expected
[key
]);
59 for (var key
in actual
) {
60 if (!(key
in expected
)) {
61 this.result
.pass
= false;
62 this.result
.message
= "Did not expect key " + key
+ " in actual data at " + context
+ ".";
68 internal_compare_value: function(context
, actual
, expected
) {
69 if (expected
=== actual
) {
72 this.result
.pass
= false;
73 this.result
.message
= "Expected '" + actual
+ "' to be '" + expected
+ "' at " + context
+ ".";
76 register: function(jasmine
) {
77 var comparator
= this;
79 toEqualData: function(expected
) {
81 compare
: $.proxy(comparator
.compare
, comparator
)
87 var module
= angular
.module('crmJsonComparator', []);
88 module
.service('crmJsonComparator', Comparator
);