Initial commit
[yaffs-website] / node_modules / verror / tests / tst.inherit.js
1 /*
2  * tst.inherit.js: test that inheriting from VError and WError work as expected.
3  */
4
5 var mod_assert = require('assert');
6 var mod_util = require('util');
7
8 var mod_verror = require('../lib/verror');
9
10 var VError = mod_verror.VError;
11 var WError = mod_verror.WError;
12 var err, suberr;
13
14 function VErrorChild()
15 {
16         VError.apply(this, Array.prototype.slice.call(arguments));
17 }
18
19 mod_util.inherits(VErrorChild, VError);
20 VErrorChild.prototype.name = 'VErrorChild';
21
22
23 function WErrorChild()
24 {
25         WError.apply(this, Array.prototype.slice.call(arguments));
26 }
27
28 mod_util.inherits(WErrorChild, WError);
29 WErrorChild.prototype.name = 'WErrorChild';
30
31
32 suberr = new Error('root cause');
33 err = new VErrorChild(suberr, 'top');
34 mod_assert.ok(err instanceof Error);
35 mod_assert.ok(err instanceof VError);
36 mod_assert.ok(err instanceof VErrorChild);
37 mod_assert.equal(err.cause(), suberr);
38 mod_assert.equal(err.message, 'top: root cause');
39 mod_assert.equal(err.toString(), 'VErrorChild: top: root cause');
40 mod_assert.equal(err.stack.split('\n')[0], 'VErrorChild: top: root cause');
41
42 suberr = new Error('root cause');
43 err = new WErrorChild(suberr, 'top');
44 mod_assert.ok(err instanceof Error);
45 mod_assert.ok(err instanceof WError);
46 mod_assert.ok(err instanceof WErrorChild);
47 mod_assert.equal(err.cause(), suberr);
48 mod_assert.equal(err.message, 'top');
49 mod_assert.equal(err.toString(),
50         'WErrorChild: top; caused by Error: root cause');
51 mod_assert.equal(err.stack.split('\n')[0],
52         'WErrorChild: top; caused by Error: root cause');
53
54
55 // Test that `<Ctor>.toString()` uses the ctor name. I.e. setting
56 // `<Ctor>.prototype.name` isn't necessary.
57 function VErrorChildNoName() {
58         VError.apply(this, Array.prototype.slice.call(arguments));
59 }
60 mod_util.inherits(VErrorChildNoName, VError);
61 err = new VErrorChildNoName('top');
62 mod_assert.equal(err.toString(), 'VErrorChildNoName: top');
63
64 function WErrorChildNoName() {
65         WError.apply(this, Array.prototype.slice.call(arguments));
66 }
67 mod_util.inherits(WErrorChildNoName, WError);
68 err = new WErrorChildNoName('top');
69 mod_assert.equal(err.toString(), 'WErrorChildNoName: top');
70
71
72 // Test that `<Ctor>.prototype.name` can be used for the `.toString()`
73 // when the ctor is anonymous.
74 var VErrorChildAnon = function () {
75         VError.apply(this, Array.prototype.slice.call(arguments));
76 };
77 mod_util.inherits(VErrorChildAnon, VError);
78 VErrorChildAnon.prototype.name = 'VErrorChildAnon';
79 err = new VErrorChildAnon('top');
80 mod_assert.equal(err.toString(), 'VErrorChildAnon: top');
81
82 var WErrorChildAnon = function () {
83         WError.apply(this, Array.prototype.slice.call(arguments));
84 };
85 mod_util.inherits(WErrorChildAnon, WError);
86 WErrorChildAnon.prototype.name = 'WErrorChildAnon';
87 err = new WErrorChildAnon('top');
88 mod_assert.equal(err.toString(), 'WErrorChildAnon: top');
89
90
91 // Test get appropriate exception name in `.toString()` when reconstituting
92 // an error instance a la:
93 //    https://github.com/mcavage/node-fast/blob/master/lib/client.js#L215
94 err = new VError('top');
95 err.name = 'CustomNameError';
96 mod_assert.equal(err.toString(), 'CustomNameError: top');
97
98 err = new WError('top');
99 err.name = 'CustomNameError';
100 mod_assert.equal(err.toString(), 'CustomNameError: top');