blob: d8d546a9011fdbaf3b85edec50ef16f1d1ea4e22 [file] [log] [blame]
Karl Guertin4f679fd2009-11-30 02:20:371process.mixin(require("./common"));
2
Karl Guertin4f679fd2009-11-30 02:20:373var a = require('assert');
4
Ryan Dahldd356372009-12-29 18:37:405function makeBlock (f) {
6 var args = Array.prototype.slice.call(arguments,1);
7 return function () {
8 return f.apply(this,args);
9 };
Karl Guertin4f679fd2009-11-30 02:20:3710}
11
Ryan Dahldd356372009-12-29 18:37:4012assert.ok(a.AssertionError instanceof Error,
13 "a.AssertionError instanceof Error");
Karl Guertin4f679fd2009-11-30 02:20:3714
Ryan Dahldd356372009-12-29 18:37:4015assert.throws(makeBlock(a.ok, false),
16 a.AssertionError, "ok(false)");
17
18assert.doesNotThrow(makeBlock(a.ok, true),
19 a.AssertionError, "ok(true)");
20
21assert.doesNotThrow(makeBlock(a.ok, "test"), "ok('test')");
Karl Guertin4f679fd2009-11-30 02:20:3722
23assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal');
Ryan Dahldd356372009-12-29 18:37:4024
Karl Guertin4f679fd2009-11-30 02:20:3725assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal');
Ryan Dahldd356372009-12-29 18:37:4026
Karl Guertin4f679fd2009-11-30 02:20:3727assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal');
Ryan Dahldd356372009-12-29 18:37:4028
Karl Guertin4f679fd2009-11-30 02:20:3729assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal');
Ryan Dahldd356372009-12-29 18:37:4030
Karl Guertin4f679fd2009-11-30 02:20:3731assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal');
Ryan Dahldd356372009-12-29 18:37:4032
Karl Guertin4f679fd2009-11-30 02:20:3733assert.doesNotThrow(makeBlock(a.equal, 2, "2"), 'equal');
34
35assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual');
Ryan Dahldd356372009-12-29 18:37:4036
Karl Guertin4f679fd2009-11-30 02:20:3737assert.throws(makeBlock(a.notEqual, true, true), a.AssertionError, 'notEqual');
38
39assert.throws(makeBlock(a.strictEqual, 2, "2"), a.AssertionError, 'strictEqual');
Ryan Dahldd356372009-12-29 18:37:4040
Karl Guertin4f679fd2009-11-30 02:20:3741assert.throws(makeBlock(a.strictEqual, null, undefined), a.AssertionError, 'strictEqual');
42
43assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, "2"), 'notStrictEqual');
44
Ryan Dahldd356372009-12-29 18:37:4045// deepEquals joy!
46// 7.2
47assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000,3,14), new Date(2000,3,14)),
48 'deepEqual date');
49
50assert.throws(makeBlock(a.deepEqual, new Date(), new Date(2000,3,14)),
51 a.AssertionError,
52 'deepEqual date');
53
54// 7.3
Karl Guertin4f679fd2009-11-30 02:20:3755assert.doesNotThrow(makeBlock(a.deepEqual, 4, "4"), 'deepEqual == check');
56assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
Ryan Dahldd356372009-12-29 18:37:4057assert.throws(makeBlock(a.deepEqual, 4, "5"),
58 a.AssertionError,
59 'deepEqual == check');
60
61// 7.4
Karl Guertin4f679fd2009-11-30 02:20:3762// having the same number of owned properties && the same set of keys
63assert.doesNotThrow(makeBlock(a.deepEqual, {a:4}, {a:4}));
64assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"2"}, {a:4,b:"2"}));
65assert.doesNotThrow(makeBlock(a.deepEqual, [4], ["4"]));
Ryan Dahldd356372009-12-29 18:37:4066assert.throws(makeBlock(a.deepEqual, {a:4}, {a:4,b:true}), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:3767assert.doesNotThrow(makeBlock(a.deepEqual, ["a"], {0:"a"}));
68//(although not necessarily the same order),
69assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"1"}, {b:"1",a:4}));
70var a1 = [1,2,3];
71var a2 = [1,2,3];
72a1.a = "test";
73a1.b = true;
74a2.b = true;
Ryan Dahldd356372009-12-29 18:37:4075a2.a = "test";
76assert.throws(makeBlock(a.deepEqual,
77 Object.keys(a1),
78 Object.keys(a2)),
79 a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:3780assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
81
82// having an identical prototype property
83var nbRoot = {
Ryan Dahldd356372009-12-29 18:37:4084 toString: function () { return this.first+' '+this.last; }
85};
86
87function nameBuilder (first,last) {
88 this.first = first;
89 this.last = last;
90 return this;
Karl Guertin4f679fd2009-11-30 02:20:3791}
92nameBuilder.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:4093
94function nameBuilder2 (first,last) {
95 this.first = first;
96 this.last = last;
97 return this;
Karl Guertin4f679fd2009-11-30 02:20:3798}
99nameBuilder2.prototype = nbRoot;
Ryan Dahldd356372009-12-29 18:37:40100
Karl Guertin4f679fd2009-11-30 02:20:37101var nb1 = new nameBuilder('Ryan', 'Dahl');
102var nb2 = new nameBuilder2('Ryan','Dahl');
103
104assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
105
106nameBuilder2.prototype = Object;
107nb2 = new nameBuilder2('Ryan','Dahl');
Ryan Dahldd356372009-12-29 18:37:40108assert.throws(makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37109
Ryan Dahldd356372009-12-29 18:37:40110// String literal + object blew up my implementation...
111assert.throws(makeBlock(a.deepEqual, 'a', {}), a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37112
Ryan Dahldd356372009-12-29 18:37:40113// Testing the throwing
114function thrower (errorConstructor){
115 throw new errorConstructor('test');
Karl Guertin4f679fd2009-11-30 02:20:37116}
117aethrow = makeBlock(thrower, a.AssertionError);
118aethrow = makeBlock(thrower, a.AssertionError);
Ryan Dahldd356372009-12-29 18:37:40119
120// the basic calls work
121assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError, 'message');
122assert.throws(makeBlock(thrower, a.AssertionError), a.AssertionError);
123assert.throws(makeBlock(thrower, a.AssertionError));
124
125// if not passing an error, catch all.
126assert.throws(makeBlock(thrower, TypeError));
127
128// when passing a type, only catch errors of the appropriate type
Karl Guertin4f679fd2009-11-30 02:20:37129var threw = false;
Ryan Dahldd356372009-12-29 18:37:40130try {
131 a.throws(makeBlock(thrower, TypeError), a.AssertionError);
132} catch (e) {
133 threw = true;
134 assert.ok(e instanceof TypeError, 'type');
Karl Guertin4f679fd2009-11-30 02:20:37135}
136assert.equal(true,threw,'a.throws with an explicit error is eating extra errors', a.AssertionError);
137threw = false;
Ryan Dahldd356372009-12-29 18:37:40138
139// doesNotThrow should pass through all errors
140try {
141 a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
142} catch(e) {
143 threw = true
144 assert.ok(e instanceof TypeError);
Karl Guertin4f679fd2009-11-30 02:20:37145}
146assert.equal(true,threw,'a.doesNotThrow with an explicit error is eating extra errors');
Ryan Dahldd356372009-12-29 18:37:40147
148// key difference is that throwing our correct error makes an assertion error
149try {
150 a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
151} catch(e) {
152 threw = true
153 assert.ok(e instanceof a.AssertionError);
Karl Guertin4f679fd2009-11-30 02:20:37154}
155assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');
Ryan Dahldd356372009-12-29 18:37:40156