Fixed mispelling: comparitor -> comparator.
[civicrm-core.git] / tests / karma / lib / crmJsonComparator.js
CommitLineData
416abe87
PH
1'use strict';
2
3(function(root) {
09a09949
PH
4 var Comparator = function() {};
5 Comparator.prototype = {
416abe87
PH
6 compare: function(actual, expected) {
7 this.result = {
8 'pass': true,
9 };
10 this.internal_compare('root', actual, expected);
11 return this.result;
12 },
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);
18 } else {
19 return this.internal_compare_value(context, actual, expected);
20 }
21 return true;
22 },
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 + ")";
27 return false;
28 }
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.";
32 return false;
33 }
34 for (var i = 0; i < expected.length; i++) {
35 var still_matches = this.internal_compare(context + "[" + i + "]", actual[i], expected[i]);
36 if (!still_matches) {
37 return false;
38 }
39 }
40 return true;
41 },
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 + ")";
46 return false;
47 }
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 + ".";
52 return false;
53 }
54 var still_matches = this.internal_compare(context + "[" + key + "]", actual[key], expected[key]);
55 if (!still_matches) {
56 return false;
57 }
58 }
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 + ".";
63 return false;
64 }
65 }
66 return true;
67 },
68 internal_compare_value: function(context, actual, expected) {
69 if (expected === actual) {
70 return true;
71 }
72 this.result.pass = false;
73 this.result.message = "Expected '" + actual + "' to be '" + expected + "' at " + context + ".";
74 return false;
75 },
76 register: function(jasmine) {
09a09949 77 var comparator = this;
416abe87
PH
78 jasmine.addMatchers({
79 toEqualData: function(expected) {
80 return {
09a09949 81 compare: $.proxy(comparator.compare, comparator)
416abe87
PH
82 }
83 }
84 });
85 }
86 };
09a09949
PH
87 var module = angular.module('crmJsonComparator', []);
88 module.service('crmJsonComparator', Comparator);
416abe87 89})(angular);