blob: 67b1a5e0974caeb0b27234a7acbd147660c47e5d [file] [log] [blame]
Karl Guertin4f679fd2009-11-30 02:20:371process.mixin(require("./common"));
2
3//strangely meta, no?
4
5var a = require('assert');
6
7
8function makeBlock(f){
9 var args = Array.prototype.slice.call(arguments,1);
10 return function(){
11 return f.apply(this,args);
12 }
13}
14
15assert.ok(a.AssertionError instanceof Error, "a.AssertionError instanceof Error")
16
17assert.throws(makeBlock(a.ok, false), a.AssertionError, "ok(false)");
18assert.doesNotThrow(makeBlock(a.ok, true), a.AssertionError, "ok(true)");
19assert.doesNotThrow(makeBlock(a.ok, "test"),"ok('test')");
20
21assert.throws(makeBlock(a.equal, true, false), a.AssertionError, 'equal');
22assert.doesNotThrow(makeBlock(a.equal, null, null), 'equal');
23assert.doesNotThrow(makeBlock(a.equal, undefined, undefined), 'equal');
24assert.doesNotThrow(makeBlock(a.equal, null, undefined), 'equal');
25assert.doesNotThrow(makeBlock(a.equal, true, true), 'equal');
26assert.doesNotThrow(makeBlock(a.equal, 2, "2"), 'equal');
27
28assert.doesNotThrow(makeBlock(a.notEqual, true, false), 'notEqual');
29assert.throws(makeBlock(a.notEqual, true, true), a.AssertionError, 'notEqual');
30
31assert.throws(makeBlock(a.strictEqual, 2, "2"), a.AssertionError, 'strictEqual');
32assert.throws(makeBlock(a.strictEqual, null, undefined), a.AssertionError, 'strictEqual');
33
34assert.doesNotThrow(makeBlock(a.notStrictEqual, 2, "2"), 'notStrictEqual');
35
36//deepEquals joy!
37//7.2
38assert.doesNotThrow(makeBlock(a.deepEqual, new Date(2000,3,14), new Date(2000,3,14)), 'deepEqual date');
39assert.throws( makeBlock(a.deepEqual, new Date(), new Date(2000,3,14)), a.AssertionError, 'deepEqual date');
40//7.3
41assert.doesNotThrow(makeBlock(a.deepEqual, 4, "4"), 'deepEqual == check');
42assert.doesNotThrow(makeBlock(a.deepEqual, true, 1), 'deepEqual == check');
43assert.throws( makeBlock(a.deepEqual, 4, "5"), a.AssertionError, 'deepEqual == check');
44//7.4
45// having the same number of owned properties && the same set of keys
46assert.doesNotThrow(makeBlock(a.deepEqual, {a:4}, {a:4}));
47assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"2"}, {a:4,b:"2"}));
48assert.doesNotThrow(makeBlock(a.deepEqual, [4], ["4"]));
49assert.throws( makeBlock(a.deepEqual, {a:4}, {a:4,b:true}), a.AssertionError);
50assert.doesNotThrow(makeBlock(a.deepEqual, ["a"], {0:"a"}));
51//(although not necessarily the same order),
52assert.doesNotThrow(makeBlock(a.deepEqual, {a:4,b:"1"}, {b:"1",a:4}));
53var a1 = [1,2,3];
54var a2 = [1,2,3];
55a1.a = "test";
56a1.b = true;
57a2.b = true;
58a2.a = "test"
59assert.throws( makeBlock(a.deepEqual, Object.keys(a1), Object.keys(a2)), a.AssertionError);
60assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
61
62// having an identical prototype property
63var nbRoot = {
64 toString: function(){return this.first+' '+this.last;}
65}
66var nameBuilder = function(first,last){
67 this.first = first;
68 this.last = last;
69 return this;
70}
71nameBuilder.prototype = nbRoot;
72var nameBuilder2 = function(first,last){
73 this.first = first;
74 this.last = last;
75 return this;
76}
77nameBuilder2.prototype = nbRoot;
78var nb1 = new nameBuilder('Ryan', 'Dahl');
79var nb2 = new nameBuilder2('Ryan','Dahl');
80
81assert.doesNotThrow(makeBlock(a.deepEqual, nb1, nb2));
82
83nameBuilder2.prototype = Object;
84nb2 = new nameBuilder2('Ryan','Dahl');
85assert.throws( makeBlock(a.deepEqual, nb1, nb2), a.AssertionError);
86
87//String literal + object blew up my implementation...
88assert.throws( makeBlock(a.deepEqual, 'a', {}), a.AssertionError);
89
90//Testing the throwing
91function thrower(errorConstructor){
92 throw new errorConstructor('test');
93}
94aethrow = makeBlock(thrower, a.AssertionError);
95aethrow = makeBlock(thrower, a.AssertionError);
96//the basic calls work
97assert.throws( makeBlock(thrower, a.AssertionError), a.AssertionError, 'message');
98assert.throws( makeBlock(thrower, a.AssertionError), a.AssertionError);
99assert.throws( makeBlock(thrower, a.AssertionError));
100//if not passing an error, catch all.
101assert.throws( makeBlock(thrower, TypeError));
102//when passing a type, only catch errors of the appropriate type
103var threw = false;
104try{
105 a.throws( makeBlock(thrower, TypeError), a.AssertionError);
106}catch (e){
107 threw = true;
108 assert.ok(e instanceof TypeError, 'type');
109}
110assert.equal(true,threw,'a.throws with an explicit error is eating extra errors', a.AssertionError);
111threw = false;
112//doesNotThrow should pass through all errors
113try{
114 a.doesNotThrow(makeBlock(thrower, TypeError), a.AssertionError);
115}catch(e){
116 threw = true
117 assert.ok(e instanceof TypeError);
118}
119assert.equal(true,threw,'a.doesNotThrow with an explicit error is eating extra errors');
120//the key difference is that throwing our correct error makes an assertion error
121try{
122 a.doesNotThrow(makeBlock(thrower, TypeError), TypeError);
123}catch(e){
124 threw = true
125 assert.ok(e instanceof a.AssertionError);
126}
127assert.equal(true,threw,'a.doesNotThrow is not catching type matching errors');