1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
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">
10
function testAssert() {
11
assert("true should be true", true);
15
function testAssertTrue() {
16
assertTrue("true should be true", true);
20
function testAssertFalse() {
21
assertFalse("false should be false", false);
25
function testAssertEquals() {
26
assertEquals("1 should equal 1", 1, 1);
30
function testAssertNotEquals() {
31
assertNotEquals("1 should not equal 2", 1, 2);
32
assertNotEquals(1, 2);
35
function testAssertNull() {
36
assertNull("null should be null", null);
40
function testAssertNotNull() {
41
assertNotNull("1 should not be null", 1);
45
function testAssertUndefined() {
47
assertUndefined("A declared but unassigned variable should have the undefined value", myVar);
48
assertUndefined(myVar);
51
function testAssertNotUndefined() {
52
assertNotUndefined("1 should not be undefined", 1);
53
assertNotUndefined(1);
56
function testAssertNaN() {
57
assertNaN("a string should not be a number", "string");
61
function testAssertNotNaN() {
62
assertNotNaN("1 should not be not a number", 1);
69
fail("Failure message");
73
assertJsUnitFailure("fail(string) should throw a JsUnit.Exception", excep);
76
function testTooFewArguments() {
83
assertNonJsUnitFailure("Calling an assertion function with too few arguments should throw an exception", excep);
86
function testTooManyArguments() {
89
assertEquals("A comment", true, true, true);
93
assertNonJsUnitFailure("Calling an assertion function with too many arguments should throw an exception", exception);
96
function testInvalidCommentArgumentType() {
103
assertNonJsUnitFailure("Calling an assertion function with a non-string comment should throw an exception", excep);
106
function testInvalidArgumentType() {
107
var exception = null;
113
assertNonJsUnitFailure("Calling an assertion function with an invalid argument should throw an exception", exception);
116
function testAssertArrayEquals() {
117
var array1 = Array();
120
array1[2] = "foobar";
121
var array2 = Array();
124
array2[2] = "foobar";
125
var array3 = Array();
128
var array4 = Array();
131
array4[2] = "foobar";
133
assertArrayEquals(array1, array1);
134
assertArrayEquals(array1, array2);
136
assertArrayEquals(array1, array3);
137
fail("Should not be equal");
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
144
assertArrayEquals(array1, array4);
145
fail("Should not be equal");
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
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);
157
assertArrayEquals(array5, array7);
158
var failure = 'Differing nested arrays found to be equal';
163
assertJsUnitFailure("Should be a JsUnit.Exception", e);
164
if (e.jsUnitMessage == failure)
165
fail(e.jsUnitMessage);
169
function testAssertObjectEquals_hash() {
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',
181
var hash5 = {foo:'foo',
187
assertObjectEquals(hash1, hash3);
188
fail(failure = 'Simple differing objects found to be the same');
191
assertJsUnitFailure("Should be a JsUnit.Failure", e);
192
if (e.jsUnitMessage == failure)
193
fail(e.jsUnitMessage);
196
assertObjectEquals(hash4, hash5);
197
fail(failure = 'Objects with different methods found to be the same');
200
assertJsUnitFailure("Should be a JsUnit.Exception", e);
201
if (e.jsUnitMessage == failure)
202
fail(e.jsUnitMessage);
206
assertObjectEquals('Different objects, made to be the same', hash1, hash4);
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');
217
assertJsUnitFailure("Should be a JsUnit.Exception", e);
218
if (e.jsUnitMessage == failure)
219
fail(e.jsUnitMessage);
222
function testAssertObjectEquals_date() {
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');
233
assertJsUnitFailure("Should be a JsUnit.Exception", e);
234
if (e.jsUnitMessage == failure)
235
fail(e.jsUnitMessage);
238
function testAssertObjectEquals_regularExpression() {
239
assertObjectEquals(/a/, new RegExp('a'));
240
assertObjectEquals(/a/i, new RegExp('a', 'i'));
245
assertObjectEquals(/a/i, new RegExp('a', 'g'));
246
fail(failure = 'RegExp with different flags found to be the same');
250
assertJsUnitFailure("Should be a JsUnit.Exception", e);
251
if (e.jsUnitMessage == failure)
252
fail(e.jsUnitMessage);
256
assertObjectEquals(/a/, new RegExp('b'));
257
fail(failure = 'RegExp with different patterns found to be the same');
261
assertJsUnitFailure("Should be a JsUnit.Exception", e);
262
if (e.jsUnitMessage == failure)
263
fail(e.jsUnitMessage);
267
function testAssertObjectEquals_strings() {
270
var newS1 = new String('string1');
271
assertObjectEquals('Same Strings', s1, s2);
272
assertObjectEquals('Same Strings 1 with new', s1, newS1);
276
failure = "Different Strings, different length";
277
assertObjectEquals(failure, "foo", "fooXXX");
280
assertJsUnitFailure("Should be a JsUnit.Exception", e);
281
if (e.jsUnitMessage == failure)
282
fail(e.jsUnitMessage);
286
failure = "Different Strings, same length";
287
assertObjectEquals(failure, "foo", "bar");
290
assertJsUnitFailure("Should be a JsUnit.Exception", e);
291
if (e.jsUnitMessage == failure)
292
fail(e.jsUnitMessage);
296
function testAssertObjectEquals_numbers() {
300
var newN1 = new Number(1);
301
assertObjectEquals('Same Numbers', n1, n2);
302
assertObjectEquals('Same Numbers 1 with new', n1, newN1);
304
var newN3 = new Number(2);
307
assertObjectEquals(n1, n3);
308
fail(failure = 'Different Numbers');
312
assertJsUnitFailure("Should be a JsUnit.Exception", e);
313
if (e.jsUnitMessage == failure)
314
fail(e.jsUnitMessage);
318
assertObjectEquals(newN1, newN3);
319
fail(failure = 'Different New Numbers');
323
assertJsUnitFailure("Should be a JsUnit.Exception", e);
324
if (e.jsUnitMessage == failure)
325
fail(e.jsUnitMessage);
329
function testAssertObjectEquals_differentTypes() {
332
assertObjectEquals(1, "foo");
333
fail(failure = "different types");
335
assertJsUnitFailure("Should be a JsUnit.Exception", e);
336
if (e.jsUnitMessage == failure)
337
fail(e.jsUnitMessage);
341
function testAssertObjectEquals_tooManyEntriesInActual() {
344
assertObjectEquals({a: "b"}, {a: "b", c: "d"});
345
fail(failure = "too many entries in actual");
347
assertJsUnitFailure("Should be a JsUnit.Exception", e);
348
if (e.jsUnitMessage == failure)
349
fail(e.jsUnitMessage);
351
assertEquals("Expected keys \"a\" but found \"a, c\"", e.jsUnitMessage);
355
function testAssertEvaluatesToTrue() {
356
assertEvaluatesToTrue("foo");
357
assertEvaluatesToTrue(true);
358
assertEvaluatesToTrue(1);
360
assertEvaluatesToTrue(null);
361
fail("Should have thrown a JsUnitException");
363
assertJsUnitFailure("Should be a JsUnit.Exception", e);
367
function testAssertEvaluatesToFalse() {
368
assertEvaluatesToFalse("");
369
assertEvaluatesToFalse(null);
370
assertEvaluatesToFalse(false);
371
assertEvaluatesToFalse(0);
373
assertEvaluatesToFalse("foo");
374
fail("Should have thrown a JsUnitException");
376
assertJsUnitFailure("Should be a JsUnit.Exception", e);
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>");
387
function testAssertHashEquals() {
388
var hash1 = new Array();
389
hash1["key1"] = "value1";
390
hash1["key2"] = "value2";
392
var hash2 = new Array();
394
assertHashEquals(hash1, hash2);
397
assertJsUnitFailure("hash2 is empty", e);
399
hash2["key1"] = "value1";
401
assertHashEquals(hash1, hash2);
404
assertJsUnitFailure("hash2 is a of a different size", e);
406
hash2["key2"] = "foo";
408
assertHashEquals(hash1, hash2);
411
assertJsUnitFailure("hash2 has different values", e);
413
hash2["key2"] = "value2";
414
assertHashEquals(hash1, hash2);
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);
423
assertRoughlyEquals(1, 2, 0.5);
425
assertJsUnitFailure("1 and 2 are more than 0.5 apart", e);
429
function testAssertContains() {
430
assertContains("foo", "foobar");
431
assertContains("ooba", "foobar");
432
assertContains("bar", "foobar");
435
function testAssertEqualsIgnoringOrder() {
437
assertEqualsIgnoringOrder("a", ["b", "c"]);
440
assertJsUnitFailure("both args should be arrays", e);
441
assertEquals("Expected arguments <a> (String) and <b,c> (Array) to be arrays", e.jsUnitMessage);
444
assertEqualsIgnoringOrder(["a", "b"], ["a", "b"]);
445
assertEqualsIgnoringOrder(["a", "b"], ["b", "a"]);
448
assertEqualsIgnoringOrder(["a"], ["b"]);
451
assertJsUnitFailure("arrays are different", e);
452
assertEquals("Expected arrays <a> (Array) and <b> (Array) to be equal (ignoring order)", e.jsUnitMessage);
456
assertEqualsIgnoringOrder(["a", "b", "c"], ["b", "c", "d"]);
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);
464
function assertJsUnitFailure(comment, allegedJsUnitFailure) {
465
assertNotNull(comment, allegedJsUnitFailure);
466
assert(comment, allegedJsUnitFailure.isJsUnitFailure);
467
assertNotUndefined(comment, allegedJsUnitFailure.comment);
470
function assertNonJsUnitFailure(comment, allegedNonJsUnitFailure) {
471
assertNotNull(comment, allegedNonJsUnitFailure);
472
assertUndefined(comment, allegedNonJsUnitFailure.isJsUnitFailure);
473
assertNotUndefined(comment, allegedNonJsUnitFailure.description);
479
<h1>JsUnit Assertion Tests</h1>
481
<p>This page contains tests for the JsUnit Assertion functions. To see them, take a look at the source.</p>