|
1 |
| -// |
2 |
| -// Assertions and In-Test Utilities |
3 |
| -// |
| 1 | +doh.assert = { |
| 2 | + assertTrue:function(/*Object*/ condition, /*String?*/ hint){ |
| 3 | + // summary: |
| 4 | + // is the passed item "truthy"? |
| 5 | + if(arguments.length < 1){ |
| 6 | + throw new doh.assert.Failure("assertTrue failed because it was not passed at least 1 argument"); |
| 7 | + } |
| 8 | + if(!eval(condition)){ |
| 9 | + throw new doh.assert.Failure("assertTrue('" + condition + "') failed", hint); |
| 10 | + } |
| 11 | + }, |
4 | 12 |
|
5 |
| -doh.t = doh.assertTrue = function(/*Object*/ condition, /*String?*/ hint){ |
6 |
| - // summary: |
7 |
| - // is the passed item "truthy"? |
8 |
| - if(arguments.length < 1){ |
9 |
| - throw new doh._AssertFailure("assertTrue failed because it was not passed at least 1 argument"); |
10 |
| - } |
11 |
| - if(!eval(condition)){ |
12 |
| - throw new doh._AssertFailure("assertTrue('" + condition + "') failed", hint); |
13 |
| - } |
14 |
| -} |
| 13 | + assertFalse:function(/*Object*/ condition, /*String?*/ hint){ |
| 14 | + // summary: |
| 15 | + // is the passed item "falsey"? |
| 16 | + if(arguments.length < 1){ |
| 17 | + throw new doh.assert.Failure("assertFalse failed because it was not passed at least 1 argument"); |
| 18 | + } |
| 19 | + if(eval(condition)){ |
| 20 | + throw new doh.assert.Failure("assertFalse('" + condition + "') failed", hint); |
| 21 | + } |
| 22 | + }, |
15 | 23 |
|
16 |
| -doh.f = doh.assertFalse = function(/*Object*/ condition, /*String?*/ hint){ |
17 |
| - // summary: |
18 |
| - // is the passed item "falsey"? |
19 |
| - if(arguments.length < 1){ |
20 |
| - throw new doh._AssertFailure("assertFalse failed because it was not passed at least 1 argument"); |
21 |
| - } |
22 |
| - if(eval(condition)){ |
23 |
| - throw new doh._AssertFailure("assertFalse('" + condition + "') failed", hint); |
24 |
| - } |
25 |
| -} |
| 24 | + assertError:function(/*Error object*/expectedError, /*Object*/scope, /*String*/functionName, /*Array*/args, /*String?*/ hint){ |
| 25 | + // summary: |
| 26 | + // Test for a certain error to be thrown by the given function. |
| 27 | + // example: |
| 28 | + // t.assertError(dojox.data.QueryReadStore.InvalidAttributeError, store, "getValue", [item, "NOT THERE"]); |
| 29 | + // t.assertError(dojox.data.QueryReadStore.InvalidItemError, store, "getValue", ["not an item", "NOT THERE"]); |
| 30 | + try{ |
| 31 | + scope[functionName].apply(scope, args); |
| 32 | + }catch (e){ |
| 33 | + if(e instanceof expectedError){ |
| 34 | + return true; |
| 35 | + }else{ |
| 36 | + throw new doh.assert.Failure("assertError() failed:\n\texpected error\n\t\t"+expectedError+"\n\tbut got\n\t\t"+e+"\n\n", hint); |
| 37 | + } |
| 38 | + } |
| 39 | + throw new doh.assert.Failure("assertError() failed:\n\texpected error\n\t\t"+expectedError+"\n\tbut no error caught\n\n", hint); |
| 40 | + }, |
26 | 41 |
|
27 |
| -doh.e = doh.assertError = function(/*Error object*/expectedError, /*Object*/scope, /*String*/functionName, /*Array*/args, /*String?*/ hint){ |
28 |
| - // summary: |
29 |
| - // Test for a certain error to be thrown by the given function. |
30 |
| - // example: |
31 |
| - // t.assertError(dojox.data.QueryReadStore.InvalidAttributeError, store, "getValue", [item, "NOT THERE"]); |
32 |
| - // t.assertError(dojox.data.QueryReadStore.InvalidItemError, store, "getValue", ["not an item", "NOT THERE"]); |
33 |
| - try{ |
34 |
| - scope[functionName].apply(scope, args); |
35 |
| - }catch (e){ |
36 |
| - if(e instanceof expectedError){ |
| 42 | + assertEqual:function(/*Object*/ expected, /*Object*/ actual, /*String?*/ hint){ |
| 43 | + // summary: |
| 44 | + // are the passed expected and actual objects/values deeply |
| 45 | + // equivalent? |
| 46 | + |
| 47 | + // Compare undefined always with three equal signs, because undefined==null |
| 48 | + // is true, but undefined===null is false. |
| 49 | + if((expected === undefined)&&(actual === undefined)){ |
37 | 50 | return true;
|
38 |
| - }else{ |
39 |
| - throw new doh._AssertFailure("assertError() failed:\n\texpected error\n\t\t"+expectedError+"\n\tbut got\n\t\t"+e+"\n\n", hint); |
40 | 51 | }
|
41 |
| - } |
42 |
| - throw new doh._AssertFailure("assertError() failed:\n\texpected error\n\t\t"+expectedError+"\n\tbut no error caught\n\n", hint); |
43 |
| -} |
44 |
| - |
45 |
| - |
46 |
| -doh.is = doh.assertEqual = function(/*Object*/ expected, /*Object*/ actual, /*String?*/ hint){ |
47 |
| - // summary: |
48 |
| - // are the passed expected and actual objects/values deeply |
49 |
| - // equivalent? |
| 52 | + if(arguments.length < 2){ |
| 53 | + throw doh.assert.Failure("assertEqual failed because it was not passed 2 arguments"); |
| 54 | + } |
| 55 | + if((expected === actual)||(expected == actual)){ |
| 56 | + return true; |
| 57 | + } |
| 58 | + if( (doh.util.isArray(expected) && doh.util.isArray(actual))&& |
| 59 | + (doh.assert._arrayEq(expected, actual)) ){ |
| 60 | + return true; |
| 61 | + } |
| 62 | + if( ((typeof expected == "object")&&((typeof actual == "object")))&& |
| 63 | + (doh.assert._objPropEq(expected, actual)) ){ |
| 64 | + return true; |
| 65 | + } |
| 66 | + throw new doh.assert.Failure("assertEqual() failed:\n\texpected\n\t\t"+expected+"\n\tbut got\n\t\t"+actual+"\n\n", hint); |
| 67 | + }, |
50 | 68 |
|
51 |
| - // Compare undefined always with three equal signs, because undefined==null |
52 |
| - // is true, but undefined===null is false. |
53 |
| - if((expected === undefined)&&(actual === undefined)){ |
54 |
| - return true; |
55 |
| - } |
56 |
| - if(arguments.length < 2){ |
57 |
| - throw doh._AssertFailure("assertEqual failed because it was not passed 2 arguments"); |
58 |
| - } |
59 |
| - if((expected === actual)||(expected == actual)){ |
60 |
| - return true; |
61 |
| - } |
62 |
| - if( (this._isArray(expected) && this._isArray(actual))&& |
63 |
| - (this._arrayEq(expected, actual)) ){ |
64 |
| - return true; |
65 |
| - } |
66 |
| - if( ((typeof expected == "object")&&((typeof actual == "object")))&& |
67 |
| - (this._objPropEq(expected, actual)) ){ |
| 69 | + assertNotEqual:function(/*Object*/ notExpected, /*Object*/ actual, /*String?*/ hint){ |
| 70 | + // summary: |
| 71 | + // are the passed notexpected and actual objects/values deeply |
| 72 | + // not equivalent? |
| 73 | + |
| 74 | + // Compare undefined always with three equal signs, because undefined==null |
| 75 | + // is true, but undefined===null is false. |
| 76 | + if((notExpected === undefined)&&(actual === undefined)){ |
| 77 | + throw new doh.assert.Failure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
| 78 | + } |
| 79 | + if(arguments.length < 2){ |
| 80 | + throw doh.assert.Failure("assertEqual failed because it was not passed 2 arguments"); |
| 81 | + } |
| 82 | + if((notExpected === actual)||(notExpected == actual)){ |
| 83 | + throw new doh.assert.Failure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
| 84 | + } |
| 85 | + if( (doh.util.isArray(notExpected) && doh.util.isArray(actual))&& |
| 86 | + (doh.assert._arrayEq(notExpected, actual)) ){ |
| 87 | + throw new doh.assert.Failure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
| 88 | + } |
| 89 | + if( ((typeof notExpected == "object")&&((typeof actual == "object")))&& |
| 90 | + (doh.assert._objPropEq(notExpected, actual)) ){ |
| 91 | + throw new doh.assert.Failure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
| 92 | + } |
68 | 93 | return true;
|
69 |
| - } |
70 |
| - throw new doh._AssertFailure("assertEqual() failed:\n\texpected\n\t\t"+expected+"\n\tbut got\n\t\t"+actual+"\n\n", hint); |
71 |
| -} |
72 |
| - |
73 |
| -doh.isNot = doh.assertNotEqual = function(/*Object*/ notExpected, /*Object*/ actual, /*String?*/ hint){ |
74 |
| - // summary: |
75 |
| - // are the passed notexpected and actual objects/values deeply |
76 |
| - // not equivalent? |
| 94 | + }, |
77 | 95 |
|
78 |
| - // Compare undefined always with three equal signs, because undefined==null |
79 |
| - // is true, but undefined===null is false. |
80 |
| - if((notExpected === undefined)&&(actual === undefined)){ |
81 |
| - throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
82 |
| - } |
83 |
| - if(arguments.length < 2){ |
84 |
| - throw doh._AssertFailure("assertEqual failed because it was not passed 2 arguments"); |
85 |
| - } |
86 |
| - if((notExpected === actual)||(notExpected == actual)){ |
87 |
| - throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
88 |
| - } |
89 |
| - if( (this._isArray(notExpected) && this._isArray(actual))&& |
90 |
| - (this._arrayEq(notExpected, actual)) ){ |
91 |
| - throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
92 |
| - } |
93 |
| - if( ((typeof notExpected == "object")&&((typeof actual == "object")))&& |
94 |
| - (this._objPropEq(notExpected, actual)) ){ |
95 |
| - throw new doh._AssertFailure("assertNotEqual() failed: not expected |"+notExpected+"| but got |"+actual+"|", hint); |
96 |
| - } |
97 |
| - return true; |
98 |
| -} |
99 |
| - |
100 |
| -doh._arrayEq = function(expected, actual){ |
101 |
| - if(expected.length != actual.length){ return false; } |
102 |
| - // FIXME: we're not handling circular refs. Do we care? |
103 |
| - for(var x=0; x<expected.length; x++){ |
104 |
| - if(!doh.assertEqual(expected[x], actual[x])){ return false; } |
105 |
| - } |
106 |
| - return true; |
107 |
| -} |
108 |
| - |
109 |
| -doh._objPropEq = function(expected, actual){ |
110 |
| - // Degenerate case: if they are both null, then their "properties" are equal. |
111 |
| - if(expected === null && actual === null){ |
112 |
| - return true; |
113 |
| - } |
114 |
| - // If only one is null, they aren't equal. |
115 |
| - if(expected === null || actual === null){ |
116 |
| - return false; |
117 |
| - } |
118 |
| - if(expected instanceof Date){ |
119 |
| - return actual instanceof Date && expected.getTime()==actual.getTime(); |
120 |
| - } |
121 |
| - var x; |
122 |
| - // Make sure ALL THE SAME properties are in both objects! |
123 |
| - for(x in actual){ // Lets check "actual" here, expected is checked below. |
124 |
| - if(expected[x] === undefined){ |
125 |
| - return false; |
| 96 | + _arrayEq:function(expected, actual){ |
| 97 | + if(expected.length != actual.length){ return false; } |
| 98 | + // FIXME: we're not handling circular refs. Do we care? |
| 99 | + for(var x=0; x<expected.length; x++){ |
| 100 | + if(!doh.assert.assertEqual(expected[x], actual[x])){ return false; } |
126 | 101 | }
|
127 |
| - }; |
| 102 | + return true; |
| 103 | + }, |
128 | 104 |
|
129 |
| - for(x in expected){ |
130 |
| - if(!doh.assertEqual(expected[x], actual[x])){ |
| 105 | + _objPropEq:function(expected, actual){ |
| 106 | + // Degenerate case: if they are both null, then their "properties" are equal. |
| 107 | + if(expected === null && actual === null){ |
| 108 | + return true; |
| 109 | + } |
| 110 | + // If only one is null, they aren't equal. |
| 111 | + if(expected === null || actual === null){ |
131 | 112 | return false;
|
132 | 113 | }
|
133 |
| - } |
134 |
| - return true; |
135 |
| -} |
136 |
| - |
137 |
| -doh._isArray = function(it){ |
138 |
| - return (it && it instanceof Array || typeof it == "array" || |
139 |
| - ( |
140 |
| - !!doh.global["dojo"] && |
141 |
| - doh.global["dojo"]["NodeList"] !== undefined && |
142 |
| - it instanceof doh.global["dojo"]["NodeList"] |
143 |
| - ) |
144 |
| - ); |
145 |
| -} |
146 |
| - |
147 |
| - |
148 |
| -doh._AssertFailure = function(msg, hint){ |
149 |
| - // idea for this as way of dis-ambiguating error types is from JUM. |
150 |
| - // The JUM is dead! Long live the JUM! |
| 114 | + if(expected instanceof Date){ |
| 115 | + return actual instanceof Date && expected.getTime()==actual.getTime(); |
| 116 | + } |
| 117 | + var x; |
| 118 | + // Make sure ALL THE SAME properties are in both objects! |
| 119 | + for(x in actual){ // Lets check "actual" here, expected is checked below. |
| 120 | + if(expected[x] === undefined){ |
| 121 | + return false; |
| 122 | + } |
| 123 | + }; |
| 124 | + |
| 125 | + for(x in expected){ |
| 126 | + if(!doh.assert.assertEqual(expected[x], actual[x])){ |
| 127 | + return false; |
| 128 | + } |
| 129 | + } |
| 130 | + return true; |
| 131 | + }, |
151 | 132 |
|
152 |
| - if(!(this instanceof doh._AssertFailure)){ |
153 |
| - return new doh._AssertFailure(msg); |
154 |
| - } |
155 |
| - if(hint){ |
156 |
| - msg = (new String(msg||""))+" with hint: \n\t\t"+(new String(hint)+"\n"); |
| 133 | + Failure:function(msg, hint){ |
| 134 | + // idea for this as way of dis-ambiguating error types is from JUM. |
| 135 | + // The JUM is dead! Long live the JUM! |
| 136 | + |
| 137 | + if(!(this instanceof doh.assert.Failure)){ |
| 138 | + return new doh.assert.Failure(msg); |
| 139 | + } |
| 140 | + if(hint){ |
| 141 | + msg = (new String(msg||""))+" with hint: \n\t\t"+(new String(hint)+"\n"); |
| 142 | + } |
| 143 | + this.message = new String(msg||""); |
| 144 | + return this; |
157 | 145 | }
|
158 |
| - this.message = new String(msg||""); |
159 |
| - return this; |
160 |
| -} |
161 |
| -doh._AssertFailure.prototype = new Error(); |
162 |
| -doh._AssertFailure.prototype.constructor = doh._AssertFailure; |
163 |
| -doh._AssertFailure.prototype.name = "doh._AssertFailure"; |
| 146 | +}; |
| 147 | +doh.assert.Failure.prototype = new Error(); |
| 148 | +doh.assert.Failure.prototype.constructor = doh.assert.Failure; |
| 149 | +doh.assert.Failure.prototype.name = "doh.assert.Failure"; |
0 commit comments