~keith-hughitt/helioviewer.org/2.0

« back to all changes in this revision

Viewing changes to scripts/jsunit/tests/jsUnitAssertionTests.html

  • Committer: Keith Hughitt
  • Date: 2010-03-24 11:14:04 UTC
  • Revision ID: hughitt1@io-20100324111404-tjat3xqy09nqvwik
Helioviewer.orgĀ 2.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 
2
<html>
 
3
<head>
 
4
<title>JsUnit Assertion Tests</title>
 
5
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 
6
<link rel="stylesheet" type="text/css" href="../css/jsUnitStyle.css">
 
7
<script type="text/javascript" src="../app/jsUnitCore.js"></script>
 
8
<script type="text/javascript">
 
9
 
 
10
function testAssert() {
 
11
    assert("true should be true", true);
 
12
    assert(true);
 
13
}
 
14
 
 
15
function testAssertTrue() {
 
16
    assertTrue("true should be true", true);
 
17
    assertTrue(true);
 
18
}
 
19
 
 
20
function testAssertFalse() {
 
21
    assertFalse("false should be false", false);
 
22
    assertFalse(false);
 
23
}
 
24
 
 
25
function testAssertEquals() {
 
26
    assertEquals("1 should equal 1", 1, 1);
 
27
    assertEquals(1, 1);
 
28
}
 
29
 
 
30
function testAssertNotEquals() {
 
31
    assertNotEquals("1 should not equal 2", 1, 2);
 
32
    assertNotEquals(1, 2);
 
33
}
 
34
 
 
35
function testAssertNull() {
 
36
    assertNull("null should be null", null);
 
37
    assertNull(null);
 
38
}
 
39
 
 
40
function testAssertNotNull() {
 
41
    assertNotNull("1 should not be null", 1);
 
42
    assertNotNull(1);
 
43
}
 
44
 
 
45
function testAssertUndefined() {
 
46
    var myVar;
 
47
    assertUndefined("A declared but unassigned variable should have the undefined value", myVar);
 
48
    assertUndefined(myVar);
 
49
}
 
50
 
 
51
function testAssertNotUndefined() {
 
52
    assertNotUndefined("1 should not be undefined", 1);
 
53
    assertNotUndefined(1);
 
54
}
 
55
 
 
56
function testAssertNaN() {
 
57
    assertNaN("a string should not be a number", "string");
 
58
    assertNaN("string");
 
59
}
 
60
 
 
61
function testAssertNotNaN() {
 
62
    assertNotNaN("1 should not be not a number", 1);
 
63
    assertNotNaN(1);
 
64
}
 
65
 
 
66
function testFail() {
 
67
    var excep = null;
 
68
    try {
 
69
        fail("Failure message");
 
70
    } catch (e) {
 
71
        excep = e;
 
72
    }
 
73
    assertJsUnitFailure("fail(string) should throw a JsUnit.Exception", excep);
 
74
}
 
75
 
 
76
function testTooFewArguments() {
 
77
    var excep = null;
 
78
    try {
 
79
        assert();
 
80
    } catch (e1) {
 
81
        excep = e1;
 
82
    }
 
83
    assertNonJsUnitFailure("Calling an assertion function with too few arguments should throw an exception", excep);
 
84
}
 
85
 
 
86
function testTooManyArguments() {
 
87
    var exception = null;
 
88
    try {
 
89
        assertEquals("A comment", true, true, true);
 
90
    } catch (e) {
 
91
        exception = e;
 
92
    }
 
93
    assertNonJsUnitFailure("Calling an assertion function with too many arguments should throw an exception", exception);
 
94
}
 
95
 
 
96
function testInvalidCommentArgumentType() {
 
97
    var excep = null;
 
98
    try {
 
99
        assertNull(1, true);
 
100
    } catch (e3) {
 
101
        excep = e3;
 
102
    }
 
103
    assertNonJsUnitFailure("Calling an assertion function with a non-string comment should throw an exception", excep);
 
104
}
 
105
 
 
106
function testInvalidArgumentType() {
 
107
    var exception = null;
 
108
    try {
 
109
        assert("string");
 
110
    } catch (e) {
 
111
        exception = e;
 
112
    }
 
113
    assertNonJsUnitFailure("Calling an assertion function with an invalid argument should throw an exception", exception);
 
114
}
 
115
 
 
116
function testAssertArrayEquals() {
 
117
    var array1 = Array();
 
118
    array1[0] = "foo";
 
119
    array1[1] = "bar";
 
120
    array1[2] = "foobar";
 
121
    var array2 = Array();
 
122
    array2[0] = "foo";
 
123
    array2[1] = "bar";
 
124
    array2[2] = "foobar";
 
125
    var array3 = Array();
 
126
    array3[0] = "foo";
 
127
    array3[1] = "bar";
 
128
    var array4 = Array();
 
129
    array4[0] = "bar";
 
130
    array4[1] = "foo";
 
131
    array4[2] = "foobar";
 
132
 
 
133
    assertArrayEquals(array1, array1);
 
134
    assertArrayEquals(array1, array2);
 
135
    try {
 
136
        assertArrayEquals(array1, array3);
 
137
        fail("Should not be equal");
 
138
    } catch (e) {
 
139
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
140
        if (e.comment == "Call to fail()")
 
141
            fail(e.comment + e.jsUnitMessage); //tried fail is also caught
 
142
    }
 
143
    try {
 
144
        assertArrayEquals(array1, array4);
 
145
        fail("Should not be equal");
 
146
    } catch (e) {
 
147
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
148
        if (e.comment == "Call to fail()")
 
149
            fail(e.comment + e.jsUnitMessage); //tried fail is also caught
 
150
    }
 
151
    var array5 = ['foo', 'bar', ['nested', 'bar'], 'foobar'];
 
152
    var array6 = ['foo', 'bar', ['nested', 'bar'], 'foobar'];
 
153
    var array7 = ['foo', 'bar', ['nested', 'foo'], 'foobar'];
 
154
    assertArrayEquals('Equal nested arrays', array5, array6);
 
155
    try
 
156
    {
 
157
        assertArrayEquals(array5, array7);
 
158
        var failure = 'Differing nested arrays found to be equal';
 
159
        fail(failure);
 
160
    }
 
161
    catch (e)
 
162
    {
 
163
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
164
        if (e.jsUnitMessage == failure)
 
165
            fail(e.jsUnitMessage);
 
166
    }
 
167
}
 
168
 
 
169
function testAssertObjectEquals_hash() {
 
170
    var failure;
 
171
    var hash1 = {foo:'bar'};
 
172
    var hash2 = {foo:'bar'};
 
173
    assertObjectEquals('Single object', hash1, hash1);
 
174
    assertObjectEquals('Same objects', hash1, hash2);
 
175
    var hash3 = {foo:'foo'};
 
176
    var hash4 = {foo:'foo',
 
177
        bar: function () {
 
178
            this.foo = 'bar';
 
179
            delete this.bar
 
180
        }};
 
181
    var hash5 = {foo:'foo',
 
182
        bar: function () {
 
183
            this.foo = 'foo';
 
184
            delete this.bar
 
185
        }};
 
186
    try {
 
187
        assertObjectEquals(hash1, hash3);
 
188
        fail(failure = 'Simple differing objects found to be the same');
 
189
    }
 
190
    catch (e) {
 
191
        assertJsUnitFailure("Should be a JsUnit.Failure", e);
 
192
        if (e.jsUnitMessage == failure)
 
193
            fail(e.jsUnitMessage);
 
194
    }
 
195
    try {
 
196
        assertObjectEquals(hash4, hash5);
 
197
        fail(failure = 'Objects with different methods found to be the same');
 
198
    }
 
199
    catch (e) {
 
200
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
201
        if (e.jsUnitMessage == failure)
 
202
            fail(e.jsUnitMessage);
 
203
    }
 
204
 
 
205
    hash4.bar();
 
206
    assertObjectEquals('Different objects, made to be the same', hash1, hash4);
 
207
 
 
208
    try {
 
209
        assertObjectEquals({ts:new Date()}, {ts:new Date()});
 
210
        fail(failure = 'Objects with different Date attributes found to be the same');
 
211
        var d1 = new Date(1);
 
212
        var d2 = new Date(10000);
 
213
        assertObjectEquals(d1, d2);
 
214
        fail(failure = 'Different Date objects (' + d1 + ' and ' + d2 + ') found to be the same');
 
215
    }
 
216
    catch (e) {
 
217
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
218
        if (e.jsUnitMessage == failure)
 
219
            fail(e.jsUnitMessage);
 
220
    }
 
221
}
 
222
function testAssertObjectEquals_date() {
 
223
    var failure;
 
224
    try
 
225
    {
 
226
        assertObjectEquals(new Date(), new Date());
 
227
        fail(failure = 'Different Date objects found to be the same');
 
228
        assertObjectEquals({ts:new Date(1)}, {ts:new Date(2)});
 
229
        fail(failure = 'Objects with different Date attributes found to be the same');
 
230
    }
 
231
    catch (e)
 
232
    {
 
233
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
234
        if (e.jsUnitMessage == failure)
 
235
            fail(e.jsUnitMessage);
 
236
    }
 
237
}
 
238
function testAssertObjectEquals_regularExpression() {
 
239
    assertObjectEquals(/a/, new RegExp('a'));
 
240
    assertObjectEquals(/a/i, new RegExp('a', 'i'));
 
241
 
 
242
    var failure;
 
243
    try
 
244
    {
 
245
        assertObjectEquals(/a/i, new RegExp('a', 'g'));
 
246
        fail(failure = 'RegExp with different flags found to be the same');
 
247
    }
 
248
    catch (e)
 
249
    {
 
250
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
251
        if (e.jsUnitMessage == failure)
 
252
            fail(e.jsUnitMessage);
 
253
    }
 
254
    try
 
255
    {
 
256
        assertObjectEquals(/a/, new RegExp('b'));
 
257
        fail(failure = 'RegExp with different patterns found to be the same');
 
258
    }
 
259
    catch (e)
 
260
    {
 
261
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
262
        if (e.jsUnitMessage == failure)
 
263
            fail(e.jsUnitMessage);
 
264
    }
 
265
}
 
266
 
 
267
function testAssertObjectEquals_strings() {
 
268
    var s1 = 'string1';
 
269
    var s2 = 'string1';
 
270
    var newS1 = new String('string1');
 
271
    assertObjectEquals('Same Strings', s1, s2);
 
272
    assertObjectEquals('Same Strings 1 with new', s1, newS1);
 
273
 
 
274
    var failure;
 
275
    try {
 
276
        failure = "Different Strings, different length";
 
277
        assertObjectEquals(failure, "foo", "fooXXX");
 
278
        fail(failure);
 
279
    } catch (e) {
 
280
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
281
        if (e.jsUnitMessage == failure)
 
282
            fail(e.jsUnitMessage);
 
283
    }
 
284
 
 
285
    try {
 
286
        failure = "Different Strings, same length";
 
287
        assertObjectEquals(failure, "foo", "bar");
 
288
        fail(failure);
 
289
    } catch (e) {
 
290
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
291
        if (e.jsUnitMessage == failure)
 
292
            fail(e.jsUnitMessage);
 
293
    }
 
294
}
 
295
 
 
296
function testAssertObjectEquals_numbers() {
 
297
    var failure;
 
298
    var n1 = 1;
 
299
    var n2 = 1;
 
300
    var newN1 = new Number(1);
 
301
    assertObjectEquals('Same Numbers', n1, n2);
 
302
    assertObjectEquals('Same Numbers 1 with new', n1, newN1);
 
303
    var n3 = 2;
 
304
    var newN3 = new Number(2);
 
305
    try
 
306
    {
 
307
        assertObjectEquals(n1, n3);
 
308
        fail(failure = 'Different Numbers');
 
309
    }
 
310
    catch (e)
 
311
    {
 
312
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
313
        if (e.jsUnitMessage == failure)
 
314
            fail(e.jsUnitMessage);
 
315
    }
 
316
    try
 
317
    {
 
318
        assertObjectEquals(newN1, newN3);
 
319
        fail(failure = 'Different New Numbers');
 
320
    }
 
321
    catch (e)
 
322
    {
 
323
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
324
        if (e.jsUnitMessage == failure)
 
325
            fail(e.jsUnitMessage);
 
326
    }
 
327
}
 
328
 
 
329
function testAssertObjectEquals_differentTypes() {
 
330
    var failure;
 
331
    try {
 
332
        assertObjectEquals(1, "foo");
 
333
        fail(failure = "different types");
 
334
    } catch (e) {
 
335
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
336
        if (e.jsUnitMessage == failure)
 
337
            fail(e.jsUnitMessage);
 
338
    }
 
339
}
 
340
 
 
341
function testAssertObjectEquals_tooManyEntriesInActual() {
 
342
    var failure;
 
343
    try {
 
344
        assertObjectEquals({a: "b"}, {a: "b", c: "d"});
 
345
        fail(failure = "too many entries in actual");
 
346
    } catch (e) {
 
347
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
348
        if (e.jsUnitMessage == failure)
 
349
            fail(e.jsUnitMessage);
 
350
 
 
351
        assertEquals("Expected keys \"a\" but found \"a, c\"", e.jsUnitMessage);
 
352
    }
 
353
}
 
354
 
 
355
function testAssertEvaluatesToTrue() {
 
356
    assertEvaluatesToTrue("foo");
 
357
    assertEvaluatesToTrue(true);
 
358
    assertEvaluatesToTrue(1);
 
359
    try {
 
360
        assertEvaluatesToTrue(null);
 
361
        fail("Should have thrown a JsUnitException");
 
362
    } catch (e) {
 
363
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
364
    }
 
365
}
 
366
 
 
367
function testAssertEvaluatesToFalse() {
 
368
    assertEvaluatesToFalse("");
 
369
    assertEvaluatesToFalse(null);
 
370
    assertEvaluatesToFalse(false);
 
371
    assertEvaluatesToFalse(0);
 
372
    try {
 
373
        assertEvaluatesToFalse("foo");
 
374
        fail("Should have thrown a JsUnitException");
 
375
    } catch (e) {
 
376
        assertJsUnitFailure("Should be a JsUnit.Exception", e);
 
377
    }
 
378
}
 
379
 
 
380
function testAssertHTMLEquals() {
 
381
    assertHTMLEquals("<div id=mydiv>foobar</div>", "<div id='mydiv'>foobar</div>");
 
382
    assertHTMLEquals("<p/>", "<p></p>");
 
383
    assertHTMLEquals("foo bar", "foo bar");
 
384
    assertHTMLEquals("a comment", "<p id='foo'>foo bar</p>", "<p id=foo>foo bar</p>");
 
385
}
 
386
 
 
387
function testAssertHashEquals() {
 
388
    var hash1 = new Array();
 
389
    hash1["key1"] = "value1";
 
390
    hash1["key2"] = "value2";
 
391
 
 
392
    var hash2 = new Array();
 
393
    try {
 
394
        assertHashEquals(hash1, hash2);
 
395
        fail();
 
396
    } catch (e) {
 
397
        assertJsUnitFailure("hash2 is empty", e);
 
398
    }
 
399
    hash2["key1"] = "value1";
 
400
    try {
 
401
        assertHashEquals(hash1, hash2);
 
402
        fail();
 
403
    } catch (e) {
 
404
        assertJsUnitFailure("hash2 is a of a different size", e);
 
405
    }
 
406
    hash2["key2"] = "foo";
 
407
    try {
 
408
        assertHashEquals(hash1, hash2);
 
409
        fail();
 
410
    } catch (e) {
 
411
        assertJsUnitFailure("hash2 has different values", e);
 
412
    }
 
413
    hash2["key2"] = "value2";
 
414
    assertHashEquals(hash1, hash2);
 
415
}
 
416
 
 
417
function testAssertRoughlyEquals() {
 
418
    assertRoughlyEquals(1, 1.1, 0.5);
 
419
    assertRoughlyEquals(1, 5, 6);
 
420
    assertRoughlyEquals(-4, -5, 2);
 
421
    assertRoughlyEquals(-0.5, 0.1, 0.7);
 
422
    try {
 
423
        assertRoughlyEquals(1, 2, 0.5);
 
424
    } catch (e) {
 
425
        assertJsUnitFailure("1 and 2 are more than 0.5 apart", e);
 
426
    }
 
427
}
 
428
 
 
429
function testAssertContains() {
 
430
    assertContains("foo", "foobar");
 
431
    assertContains("ooba", "foobar");
 
432
    assertContains("bar", "foobar");
 
433
}
 
434
 
 
435
function testAssertEqualsIgnoringOrder() {
 
436
    try {
 
437
        assertEqualsIgnoringOrder("a", ["b", "c"]);
 
438
        fail();
 
439
    } catch (e) {
 
440
        assertJsUnitFailure("both args should be arrays", e);
 
441
        assertEquals("Expected arguments <a> (String) and <b,c> (Array) to be arrays", e.jsUnitMessage);
 
442
    }
 
443
 
 
444
    assertEqualsIgnoringOrder(["a", "b"], ["a", "b"]);
 
445
    assertEqualsIgnoringOrder(["a", "b"], ["b", "a"]);
 
446
 
 
447
    try {
 
448
        assertEqualsIgnoringOrder(["a"], ["b"]);
 
449
        fail();
 
450
    } catch (e) {
 
451
        assertJsUnitFailure("arrays are different", e);
 
452
        assertEquals("Expected arrays <a> (Array) and <b> (Array) to be equal (ignoring order)", e.jsUnitMessage);
 
453
    }
 
454
 
 
455
    try {
 
456
        assertEqualsIgnoringOrder(["a", "b", "c"], ["b", "c", "d"]);
 
457
        fail();
 
458
    } catch (e) {
 
459
        assertJsUnitFailure("arrays are different", e);
 
460
        assertEquals("Expected arrays <a,b,c> (Array) and <b,c,d> (Array) to be equal (ignoring order)", e.jsUnitMessage);
 
461
    }
 
462
}
 
463
 
 
464
function assertJsUnitFailure(comment, allegedJsUnitFailure) {
 
465
    assertNotNull(comment, allegedJsUnitFailure);
 
466
    assert(comment, allegedJsUnitFailure.isJsUnitFailure);
 
467
    assertNotUndefined(comment, allegedJsUnitFailure.comment);
 
468
}
 
469
 
 
470
function assertNonJsUnitFailure(comment, allegedNonJsUnitFailure) {
 
471
    assertNotNull(comment, allegedNonJsUnitFailure);
 
472
    assertUndefined(comment, allegedNonJsUnitFailure.isJsUnitFailure);
 
473
    assertNotUndefined(comment, allegedNonJsUnitFailure.description);
 
474
}
 
475
</script>
 
476
</head>
 
477
 
 
478
<body>
 
479
<h1>JsUnit Assertion Tests</h1>
 
480
 
 
481
<p>This page contains tests for the JsUnit Assertion functions. To see them, take a look at the source.</p>
 
482
</body>
 
483
</html>