-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
203 lines (156 loc) · 6.08 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
describe('warnings', function () {
'use strict';
var assume = require('assume')
, Stream = require('stream')
, Warnings = require('./');
it('is exported as a function', function () {
assume(Warnings).is.a('function');
});
it('can be constructed without new keyword', function () {
assume(Warnings()).is.instanceOf(Warnings);
});
describe('#set', function () {
it('adds a new warnings', function () {
var warn = new Warnings();
assume(warn.set).is.a('function');
assume(warn.warnings).has.length(0);
assume(warn.set('foo', 'bar')).equals(warn);
warn.set('bar', { message: 'bar' });
assume(warn.warnings).has.length(2);
assume(warn.warnings).contain('foo');
assume(warn.warnings).contain('bar');
assume(warn.warnings.foo.message).equals('bar');
assume(warn.warnings.bar.message).equals('bar');
assume(warn.warnings.foo.name).equals('foo');
assume(warn.warnings.bar.name).equals('bar');
});
it('can set a warning message using an array', function () {
var warn = new Warnings();
warn.set('foo', ['bar', 'pez']);
assume(warn.warnings.foo.message).is.a('array');
assume(warn.warnings.foo.message).deep.equals(['bar', 'pez']);
});
});
describe('#read', function () {
it('includes the warnings from the JS file', function () {
var warn = new Warnings();
assume(warn.warnings).has.length(0);
assume(warn.read(__dirname +'/warnings.js')).equals(warn);
assume(warn.warnings).has.length(7);
assume(warn.warnings).to.contain('pass');
assume(warn.warnings).to.contain('fail');
assume(warn.warnings).to.contain('regexp');
assume(warn.warnings).to.contain('number');
assume(warn.warnings).to.contain('string');
assume(warn.warnings).to.contain('regular');
assume(warn.warnings).to.contain('array');
});
it('includes the warnings from the JSON file', function () {
var warn = new Warnings();
assume(warn.warnings).has.length(0);
assume(warn.read(__dirname +'/warnings.json')).equals(warn);
assume(warn.warnings).has.length(2);
assume(warn.warnings).to.contain('foo');
assume(warn.warnings).to.contain('hello');
});
});
describe('#disable', function () {
it('can disable warnings using multiple arguments', function () {
var warn = new Warnings();
assume(warn.disabled).has.length(0);
assume(warn.disable('foo', 'bar')).equals(warn);
assume(warn.disabled).has.length(2);
assume(warn.disabled).to.contain('foo');
assume(warn.disabled).to.contain('bar');
});
it('can disable warnings using arrays', function () {
var warn = new Warnings();
assume(warn.disabled).has.length(0);
assume(warn.disable(['foo', 'bar'])).equals(warn);
assume(warn.disabled).has.length(2);
assume(warn.disabled).to.contain('foo');
assume(warn.disabled).to.contain('bar');
});
it('can disable warnings using objects', function () {
var warn = new Warnings();
assume(warn.disabled).has.length(0);
assume(warn.disable({ foo: 1, bar: 1 })).equals(warn);
assume(warn.disabled).has.length(2);
assume(warn.disabled).to.contain('foo');
assume(warn.disabled).to.contain('bar');
});
it('can disable warnings using strings', function () {
var warn = new Warnings();
assume(warn.disabled).has.length(0);
assume(warn.disable('foo').disable('bar')).equals(warn);
assume(warn.disabled).has.length(2);
assume(warn.disabled).to.contain('foo');
assume(warn.disabled).to.contain('bar');
});
});
describe('#about', function () {
var messages = []
, warn, stream;
beforeEach(function () {
stream = new Stream();
stream.write = function write(msg) {
messages.push(msg);
return true;
};
warn = new Warnings('test', { stream: stream });
warn.read(__dirname +'/warnings.js');
});
afterEach(function () {
messages.length = 0;
warn.destroy();
});
it('returns false when we want to warn about an unknown topic', function () {
assume(warn.about('you momo')).is.false();
assume(messages).has.length(0);
assume(warn.about('regular')).is.true();
assume(messages).has.length(1);
});
it('does not write disabled messages', function () {
warn.disable('regular');
assume(warn.about('regular')).is.false();
assume(messages).has.length(0);
});
it('only writes warnings once', function () {
assume(warn.about('regular')).is.true();
assume(messages).has.length(1);
assume(warn.about('regular')).is.false();
assume(messages).has.length(1);
});
it('does not write if the conditional (function) test fails', function () {
assume(warn.about('pass')).is.true();
assume(messages).has.length(1);
assume(warn.about('fail')).is.false();
assume(messages).has.length(1);
warn.set('foo', { message: 'bar', conditional: function (topic) {
assume(topic).equals('hello world');
return false;
}});
assume(warn.about('foo', 'hello world')).is.false();
});
it('does not write if the conditional (regex) test fails', function () {
assume(warn.about('regexp', 'foo')).is.false();
assume(messages).has.length(0);
assume(warn.about('regexp', process.version)).is.true();
assume(messages).has.length(1);
});
it('does === matches against other conditionals', function () {
assume(warn.about('number', 14447)).is.false();
assume(warn.about('number', 1447)).is.true();
assume(warn.about('number', 1447)).is.false();
assume(warn.about('string', 'no')).is.false();
assume(warn.about('string', 'no')).is.false();
assume(warn.about('string', 'yes')).is.true();
assume(warn.about('string', 'yes')).is.false();
});
it('prefixes the output with the namespace', function () {
warn.about('array');
assume(messages[0]).contains(warn.namespace);
assume(messages[0]).contains('manually');
});
});
});