Skip to content

Commit 61d7de6

Browse files
committedJan 8, 2018
Refactor UTs.
Signed-off-by: Eric Wang <skygragon@gmail.com>
·
2.6.72.5.1
1 parent 917855b commit 61d7de6

13 files changed

+385
-268
lines changed
 

‎test/helper.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use_strict';
2+
const fs = require('fs');
3+
4+
const h = {
5+
DIR: './tmp/'
6+
};
7+
8+
h.clean = function() {
9+
for (let f of fs.readdirSync(this.DIR))
10+
fs.unlinkSync(this.DIR + f);
11+
};
12+
13+
module.exports = h;

‎test/plugins/test_cache.js

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,50 @@
11
'use strict';
2-
const execSync = require('child_process').execSync;
3-
const fs = require('fs');
4-
52
const _ = require('underscore');
63
const assert = require('chai').assert;
74
const rewire = require('rewire');
85

96
const log = require('../../lib/log');
107
const config = require('../../lib/config');
11-
12-
const cache = rewire('../../lib/cache');
13-
const h = rewire('../../lib/helper');
14-
const session = rewire('../../lib/session');
15-
const plugin = rewire('../../lib/plugins/cache');
16-
17-
const HOME = './tmp';
8+
const th = require('../helper');
189

1910
describe('plugin:cache', function() {
11+
let plugin;
12+
let next;
13+
let cache;
14+
let h;
15+
let session;
16+
2017
const PROBLEMS = [
2118
{id: 0, fid: 0, name: 'name0', slug: 'slug0', starred: false, category: 'algorithms'},
2219
{id: 1, fid: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
2320
];
2421
const PROBLEM = {id: 0, fid: 0, slug: 'slug0', category: 'algorithms'};
2522

26-
const NEXT = {};
27-
2823
before(function() {
2924
log.init();
3025
config.init();
31-
plugin.init();
26+
});
3227

33-
h.getCacheDir = () => HOME;
28+
beforeEach(function() {
29+
th.clean();
30+
next = {};
31+
32+
h = rewire('../../lib/helper');
33+
h.getCacheDir = () => th.DIR;
34+
35+
cache = rewire('../../lib/cache');
3436
cache.__set__('h', h);
3537
cache.init();
3638

39+
session = rewire('../../lib/session');
3740
session.__set__('cache', cache);
41+
42+
plugin = rewire('../../lib/plugins/cache');
3843
plugin.__set__('cache', cache);
3944
plugin.__set__('session', session);
40-
plugin.setNext(NEXT);
41-
});
45+
plugin.init();
4246

43-
beforeEach(function() {
44-
execSync('rm -rf ' + HOME);
45-
fs.mkdirSync(HOME);
47+
plugin.setNext(next);
4648
});
4749

4850
describe('#getProblems', function() {
@@ -58,7 +60,7 @@ describe('plugin:cache', function() {
5860

5961
it('should getProblems w/o cache ok', function(done) {
6062
cache.del('problems');
61-
NEXT.getProblems = cb => cb(null, PROBLEMS);
63+
next.getProblems = cb => cb(null, PROBLEMS);
6264

6365
plugin.getProblems(function(e, problems) {
6466
assert.equal(e, null);
@@ -69,7 +71,7 @@ describe('plugin:cache', function() {
6971

7072
it('should getProblems w/o cache fail if client error', function(done) {
7173
cache.del('problems');
72-
NEXT.getProblems = cb => cb('client getProblems error');
74+
next.getProblems = cb => cb('client getProblems error');
7375

7476
plugin.getProblems(function(e, problems) {
7577
assert.equal(e, 'client getProblems error');
@@ -93,7 +95,7 @@ describe('plugin:cache', function() {
9395
it('should getProblem w/o cache ok', function(done) {
9496
cache.set('problems', PROBLEMS);
9597
cache.del('0.slug0.algorithms');
96-
NEXT.getProblem = (problem, cb) => cb(null, PROBLEMS[0]);
98+
next.getProblem = (problem, cb) => cb(null, PROBLEMS[0]);
9799

98100
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
99101
assert.equal(e, null);
@@ -105,7 +107,7 @@ describe('plugin:cache', function() {
105107
it('should getProblem fail if client error', function(done) {
106108
cache.set('problems', PROBLEMS);
107109
cache.del('0.slug0.algorithms');
108-
NEXT.getProblem = (problem, cb) => cb('client getProblem error');
110+
next.getProblem = (problem, cb) => cb('client getProblem error');
109111

110112
plugin.getProblem(_.clone(PROBLEM), function(e, problem) {
111113
assert.equal(e, 'client getProblem error');
@@ -171,7 +173,7 @@ describe('plugin:cache', function() {
171173
assert.equal(session.getUser(), null);
172174
assert.equal(session.isLogin(), false);
173175

174-
NEXT.login = (user, cb) => cb(null, user);
176+
next.login = (user, cb) => cb(null, user);
175177

176178
plugin.login(USER, function(e, user) {
177179
assert.equal(e, null);
@@ -188,7 +190,7 @@ describe('plugin:cache', function() {
188190
config.autologin.enable = false;
189191
cache.del(h.KEYS.user);
190192

191-
NEXT.login = (user, cb) => cb(null, user);
193+
next.login = (user, cb) => cb(null, user);
192194

193195
plugin.login(USER, function(e, user) {
194196
assert.equal(e, null);
@@ -200,7 +202,7 @@ describe('plugin:cache', function() {
200202
});
201203

202204
it('should login fail if client login error', function(done) {
203-
NEXT.login = (user, cb) => cb('client login error');
205+
next.login = (user, cb) => cb('client login error');
204206

205207
plugin.login(USER, function(e, user) {
206208
assert.equal(e, 'client login error');
@@ -220,5 +222,18 @@ describe('plugin:cache', function() {
220222
assert.equal(session.isLogin(), false);
221223
done();
222224
});
225+
226+
it('should logout ok', function(done) {
227+
// before logout
228+
cache.set(h.KEYS.user, USER);
229+
assert.deepEqual(session.getUser(), USER);
230+
assert.equal(session.isLogin(), true);
231+
232+
// after logout
233+
plugin.logout(null, true);
234+
assert.equal(session.getUser(), null);
235+
assert.equal(session.isLogin(), false);
236+
done();
237+
});
223238
}); // #user
224239
});

‎test/plugins/test_leetcode.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const nock = require('nock');
55
const rewire = require('rewire');
66

77
const config = require('../../lib/config');
8+
const chalk = require('../../lib/chalk');
89
const log = require('../../lib/log');
910

1011
const plugin = rewire('../../lib/plugins/leetcode');
@@ -31,6 +32,7 @@ describe('plugin:leetcode', function() {
3132
before(function() {
3233
log.init();
3334
config.init();
35+
chalk.init();
3436
plugin.init();
3537

3638
session.getUser = () => USER;

‎test/test_cache.js

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
'use strict';
2-
const execSync = require('child_process').execSync;
3-
42
const assert = require('chai').assert;
53
const rewire = require('rewire');
64

7-
const cache = rewire('../lib/cache');
8-
const h = rewire('../lib/helper');
5+
const th = require('./helper');
96

107
describe('cache', function() {
11-
const k = '.test';
12-
const v = {test: 'data'};
8+
let cache;
9+
10+
const K = '.test';
11+
const V = {test: 'data'};
1312

14-
before(function() {
15-
const cachedir = './tmp';
16-
execSync('rm -rf ' + cachedir);
13+
beforeEach(function() {
14+
th.clean();
1715

18-
h.getCacheDir = () => cachedir;
16+
const h = rewire('../lib/helper');
17+
h.getCacheDir = () => th.DIR;
18+
19+
cache = rewire('../lib/cache');
1920
cache.__set__('h', h);
2021
cache.init();
2122
});
2223

23-
it('should ok when not cached', function() {
24-
cache.del(k);
25-
26-
assert.equal(cache.get(k), null);
27-
assert.equal(cache.del(k), false);
24+
it('should get ok when not cached', function() {
25+
cache.del(K);
26+
assert.equal(cache.get(K), null);
27+
assert.equal(cache.del(K), false);
2828
});
2929

30-
it('should ok when cached', function() {
31-
assert.equal(cache.set(k, v), true);
32-
33-
assert.deepEqual(cache.get(k), v);
34-
assert.equal(cache.del(k), true);
30+
it('should get ok when cached', function() {
31+
assert.equal(cache.set(K, V), true);
32+
assert.deepEqual(cache.get(K), V);
33+
assert.equal(cache.del(K), true);
3534
});
3635

3736
it('should list ok when no cached', function() {
@@ -40,12 +39,10 @@ describe('cache', function() {
4039
});
4140

4241
it('should list ok when cached', function() {
43-
assert.equal(cache.set(k, v), true);
44-
42+
assert.equal(cache.set(K, V), true);
4543
const items = cache.list();
4644
assert.equal(items.length, 1);
47-
48-
assert.equal(items[0].name, k);
49-
assert.equal(items[0].size, JSON.stringify(v).length);
45+
assert.equal(items[0].name, K);
46+
assert.equal(items[0].size, JSON.stringify(V).length);
5047
});
5148
});

‎test/test_chalk.js

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@ const rewire = require('rewire');
44

55
// refer to https://en.wikipedia.org/wiki/ANSI_escape_code
66
describe('chalk', function() {
7-
it('should ok w/ 256 colors', function() {
8-
const chalk = rewire('../lib/chalk');
7+
let chalk;
8+
9+
beforeEach(function() {
10+
chalk = rewire('../lib/chalk');
911
chalk.enabled = true;
1012
chalk.use256 = true;
13+
});
14+
15+
it('should ok w/ 256 colors', function() {
1116
chalk.init();
1217
chalk.setTheme('default');
1318

@@ -29,8 +34,6 @@ describe('chalk', function() {
2934
});
3035

3136
it('should ok w/ 8 colors', function() {
32-
const chalk = rewire('../lib/chalk');
33-
chalk.enabled = true;
3437
chalk.use256 = false;
3538
chalk.init();
3639
chalk.setTheme('default');
@@ -46,7 +49,6 @@ describe('chalk', function() {
4649
});
4750

4851
it('should ok w/o colors', function() {
49-
const chalk = rewire('../lib/chalk');
5052
chalk.enabled = false;
5153
chalk.init();
5254
chalk.setTheme('default');
@@ -61,36 +63,28 @@ describe('chalk', function() {
6163
assert.equal(chalk.white(' '), ' ');
6264
});
6365

64-
it('should sprint ok', function() {
65-
const chalk = rewire('../lib/chalk');
66-
chalk.enabled = true;
67-
chalk.use256 = true;
66+
it('should sprint w/ 256 colors ok', function() {
6867
chalk.init();
6968
chalk.setTheme('default');
70-
7169
assert.equal(chalk.sprint(' ', '#00ff00'), '\u001b[38;5;46m \u001b[39m');
70+
});
7271

72+
it('should sprint w/ 8 colors ok', function() {
7373
chalk.use256 = false;
74+
chalk.init();
75+
chalk.setTheme('default');
7476
assert.equal(chalk.sprint(' ', '#00ff00'), '\u001b[92m \u001b[39m');
7577
});
7678

7779
it('should set theme ok', function() {
78-
const chalk = rewire('../lib/chalk');
79-
chalk.enabled = true;
80-
chalk.use256 = true;
8180
chalk.init();
8281
chalk.setTheme('dark');
83-
8482
assert.equal(chalk.sprint(' ', '#009900'), chalk.green(' '));
8583
});
8684

8785
it('should set unknown theme ok', function() {
88-
const chalk = rewire('../lib/chalk');
89-
chalk.enabled = true;
90-
chalk.use256 = true;
9186
chalk.init();
9287
chalk.setTheme('unknown');
93-
9488
assert.equal(chalk.sprint(' ', '#00ff00'), chalk.green(' '));
9589
});
9690
});

‎test/test_config.js

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
11
'use strict';
2-
const fs = require('fs');
3-
42
const assert = require('chai').assert;
53
const rewire = require('rewire');
64
const _ = require('underscore');
75

6+
const th = require('./helper');
7+
88
describe('config', function() {
99
let config;
10-
const f = './tmp/config.json';
10+
const FILE = './tmp/config.json';
1111

1212
beforeEach(function() {
13-
config = rewire('../lib/config');
13+
th.clean();
14+
1415
const h = rewire('../lib/helper');
15-
h.getConfigFile = () => f;
16+
h.getConfigFile = () => FILE;
17+
18+
config = rewire('../lib/config');
1619
config.__set__('h', h);
1720
});
1821

19-
afterEach(function() {
20-
if (fs.existsSync(f)) fs.unlinkSync(f);
21-
});
22+
function createConfigFile(data) {
23+
const fs = require('fs');
24+
fs.writeFileSync(FILE, JSON.stringify(data));
25+
}
2226

2327
it('should ok w/o local config', function() {
24-
if (fs.existsSync(f)) fs.unlinkSync(f);
25-
2628
const DEFAULT_CONFIG = config.__get__('DEFAULT_CONFIG');
2729
config.init();
2830

@@ -36,29 +38,24 @@ describe('config', function() {
3638
});
3739

3840
it('should ok w/ local config', function() {
39-
const data = {
41+
createConfigFile({
4042
autologin: {enable: false},
4143
code: {lang: 'ruby'},
4244
color: {enable: false}
43-
};
44-
fs.writeFileSync(f, JSON.stringify(data));
45-
45+
});
4646
config.init();
4747

4848
assert.equal(config.autologin.enable, false);
4949
assert.equal(config.code.lang, 'ruby');
5050
assert.equal(config.color.enable, false);
51-
5251
assert.equal(config.code.editor, 'vim');
5352
});
5453

5554
it('should remove legacy keys', function() {
56-
const data = {
55+
createConfigFile({
5756
USE_COLOR: true,
5857
code: {lang: 'ruby'}
59-
};
60-
fs.writeFileSync(f, JSON.stringify(data));
61-
58+
});
6259
config.init();
6360

6461
assert.equal(config.USE_COLOR, undefined);

‎test/test_core.js

Lines changed: 91 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22
const assert = require('chai').assert;
33
const rewire = require('rewire');
44

5-
const log = require('../lib/log');
6-
7-
const plugin = rewire('../lib/core');
8-
95
describe('core', function() {
6+
let core;
7+
let next;
8+
109
const PROBLEMS = [
1110
{
1211
category: 'algorithms',
@@ -33,16 +32,19 @@ describe('core', function() {
3332
state: 'none'
3433
}
3534
];
36-
const NEXT = {};
3735

3836
before(function() {
37+
const log = require('../lib/log');
3938
log.init();
40-
plugin.setNext(NEXT);
4139
});
4240

4341
beforeEach(function() {
44-
NEXT.getProblems = cb => cb(null, PROBLEMS);
45-
NEXT.getProblem = (problem, cb) => cb(null, problem);
42+
next = {};
43+
next.getProblems = cb => cb(null, PROBLEMS);
44+
next.getProblem = (p, cb) => cb(null, p);
45+
46+
core = rewire('../lib/core');
47+
core.setNext(next);
4648
});
4749

4850
describe('#filterProblems', function() {
@@ -64,10 +66,12 @@ describe('core', function() {
6466
['Dh', []]
6567
];
6668
let n = cases.length;
69+
6770
for (let x of cases) {
68-
plugin.filterProblems({query: x[0]}, function(e, problems) {
69-
assert.equal(e, null);
71+
core.filterProblems({query: x[0]}, function(e, problems) {
72+
assert.notExists(e);
7073
assert.equal(problems.length, x[1].length);
74+
7175
for (let i = 0; i < problems.length; ++i)
7276
assert.equal(problems[i], PROBLEMS[x[1][i]]);
7377
if (--n === 0) done();
@@ -84,45 +88,53 @@ describe('core', function() {
8488
[['apple'], []],
8589
];
8690
let n = cases.length;
91+
8792
for (let x of cases) {
88-
plugin.filterProblems({tag: x[0]}, function(e, problems) {
89-
assert.equal(e, null);
93+
core.filterProblems({tag: x[0]}, function(e, problems) {
94+
assert.notExists(e);
9095
assert.equal(problems.length, x[1].length);
96+
9197
for (let i = 0; i < problems.length; ++i)
9298
assert.equal(problems[i], PROBLEMS[x[1][i]]);
9399
if (--n === 0) done();
94100
});
95101
}
96102
});
97-
});
103+
104+
it('should fail if getProblems error', function(done) {
105+
next.getProblems = cb => cb('getProblems error');
106+
core.filterProblems({}, function(e) {
107+
assert.equal(e, 'getProblems error');
108+
done();
109+
});
110+
});
111+
}); // #filterProblems
98112

99113
describe('#starProblem', function() {
100-
it('should starProblem ok', function(done) {
101-
NEXT.starProblem = function(problem, starred, cb) {
102-
return cb(null, starred);
103-
};
114+
it('should ok', function(done) {
115+
next.starProblem = (p, starred, cb) => cb(null, starred);
104116

105117
assert.equal(PROBLEMS[0].starred, false);
106-
plugin.starProblem(PROBLEMS[0], true, function(e, starred) {
107-
assert.equal(e, null);
118+
core.starProblem(PROBLEMS[0], true, function(e, starred) {
119+
assert.notExists(e);
108120
assert.equal(starred, true);
109121
done();
110122
});
111123
});
112124

113-
it('should starProblem ok if already starred', function(done) {
125+
it('should ok if already starred', function(done) {
114126
assert.equal(PROBLEMS[1].starred, true);
115-
plugin.starProblem(PROBLEMS[1], true, function(e, starred) {
116-
assert.equal(e, null);
127+
core.starProblem(PROBLEMS[1], true, function(e, starred) {
128+
assert.notExists(e);
117129
assert.equal(starred, true);
118130
done();
119131
});
120132
});
121133

122-
it('should starProblem ok if already unstarred', function(done) {
134+
it('should ok if already unstarred', function(done) {
123135
assert.equal(PROBLEMS[0].starred, false);
124-
plugin.starProblem(PROBLEMS[0], false, function(e, starred) {
125-
assert.equal(e, null);
136+
core.starProblem(PROBLEMS[0], false, function(e, starred) {
137+
assert.notExists(e);
126138
assert.equal(starred, false);
127139
done();
128140
});
@@ -155,10 +167,42 @@ describe('core', function() {
155167
code: problem.templates[0].defaultCode,
156168
tpl: 'codeonly'
157169
};
158-
assert.equal(plugin.exportProblem(problem, opts), expected);
170+
assert.equal(core.exportProblem(problem, opts), expected);
171+
});
172+
173+
it('should codeonly ok in windows', function() {
174+
const h = rewire('../lib/helper');
175+
h.isWindows = () => true;
176+
core.__set__('h', h);
177+
178+
const expected = [
179+
'/**',
180+
' * Definition for singly-linked list.',
181+
' * struct ListNode {',
182+
' * int val;',
183+
' * ListNode *next;',
184+
' * ListNode(int x) : val(x), next(NULL) {}',
185+
' * };',
186+
' */',
187+
'class Solution {',
188+
'public:',
189+
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
190+
' ',
191+
' }',
192+
'};',
193+
''
194+
].join('\r\n');
195+
196+
const problem = require('./mock/add-two-numbers.20161015.json');
197+
const opts = {
198+
lang: 'cpp',
199+
code: problem.templates[0].defaultCode,
200+
tpl: 'codeonly'
201+
};
202+
assert.equal(core.exportProblem(problem, opts), expected);
159203
});
160204

161-
it('should detailed ok', function() {
205+
it('should detailed ok with cpp', function() {
162206
const expected = [
163207
'/*',
164208
' * [2] Add Two Numbers',
@@ -201,10 +245,10 @@ describe('core', function() {
201245
code: problem.templates[0].defaultCode,
202246
tpl: 'detailed'
203247
};
204-
assert.equal(plugin.exportProblem(problem, opts), expected);
248+
assert.equal(core.exportProblem(problem, opts), expected);
205249
});
206250

207-
it('should detailed ok, 2nd', function() {
251+
it('should detailed ok with ruby', function() {
208252
const expected = [
209253
'#',
210254
'# [2] Add Two Numbers',
@@ -249,55 +293,55 @@ describe('core', function() {
249293
code: problem.templates[6].defaultCode,
250294
tpl: 'detailed'
251295
};
252-
assert.equal(plugin.exportProblem(problem, opts), expected);
296+
assert.equal(core.exportProblem(problem, opts), expected);
253297
});
254298
}); // #exportProblem
255299

256300
describe('#getProblem', function() {
257-
it('should getProblem by id ok', function(done) {
258-
plugin.getProblem(0, function(e, problem) {
259-
assert.equal(e, null);
301+
it('should get by id ok', function(done) {
302+
core.getProblem(0, function(e, problem) {
303+
assert.notExists(e);
260304
assert.deepEqual(problem, PROBLEMS[0]);
261305
done();
262306
});
263307
});
264308

265-
it('should getProblem by key ok', function(done) {
266-
plugin.getProblem('slug0', function(e, problem) {
267-
assert.equal(e, null);
309+
it('should get by key ok', function(done) {
310+
core.getProblem('slug0', function(e, problem) {
311+
assert.notExists(e);
268312
assert.deepEqual(problem, PROBLEMS[0]);
269313
done();
270314
});
271315
});
272316

273-
it('should getProblem error if not found', function(done) {
274-
plugin.getProblem(3, function(e, problem) {
317+
it('should fail if not found', function(done) {
318+
core.getProblem(3, function(e, problem) {
275319
assert.equal(e, 'Problem not found!');
276320
done();
277321
});
278322
});
279323

280-
it('should getProblem fail if client error', function(done) {
281-
NEXT.getProblem = (problem, cb) => cb('client getProblem error');
324+
it('should fail if client error', function(done) {
325+
next.getProblem = (problem, cb) => cb('client getProblem error');
282326

283-
plugin.getProblem(0, function(e, problem) {
327+
core.getProblem(0, function(e, problem) {
284328
assert.equal(e, 'client getProblem error');
285329
done();
286330
});
287331
});
288332

289-
it('should getProblem ok if problem is already there', function(done) {
290-
plugin.getProblem(PROBLEMS[1], function(e, problem) {
291-
assert.equal(e, null);
333+
it('should ok if problem is already there', function(done) {
334+
core.getProblem(PROBLEMS[1], function(e, problem) {
335+
assert.notExists(e);
292336
assert.deepEqual(problem, PROBLEMS[1]);
293337
done();
294338
});
295339
});
296340

297-
it('should getProblem fail if getProblems error', function(done) {
298-
NEXT.getProblems = cb => cb('getProblems error');
341+
it('should fail if getProblems error', function(done) {
342+
next.getProblems = cb => cb('getProblems error');
299343

300-
plugin.getProblem(0, function(e, problem) {
344+
core.getProblem(0, function(e, problem) {
301345
assert.equal(e, 'getProblems error');
302346
done();
303347
});

‎test/test_helper.js

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22
const path = require('path');
33

44
const assert = require('chai').assert;
5+
const rewire = require('rewire');
6+
const _ = require('underscore');
57

68
const chalk = require('../lib/chalk');
7-
const h = require('../lib/helper');
8-
9-
chalk.init();
109

1110
describe('helper', function() {
11+
let h;
12+
13+
before(function() {
14+
chalk.init();
15+
});
16+
17+
beforeEach(function() {
18+
h = rewire('../lib/helper');
19+
});
20+
1221
describe('#prettyState', function() {
1322
it('should ok w/ color', function() {
1423
chalk.enabled = true;
@@ -172,7 +181,7 @@ describe('helper', function() {
172181
}); // #langToCommentStyle
173182

174183
describe('#dirAndFiles', function() {
175-
const root = path.join(__dirname, '..');
184+
const HOME = path.join(__dirname, '..');
176185

177186
it('should ok', function() {
178187
process.env.HOME = '/home/skygragon';
@@ -190,10 +199,10 @@ describe('helper', function() {
190199
});
191200

192201
it('should getCodeDir ok', function() {
193-
assert.equal(h.getCodeDir(), root);
194-
assert.equal(h.getCodeDir('.'), root);
195-
assert.equal(h.getCodeDir('icons'), path.join(root, 'icons'));
196-
assert.equal(h.getCodeDir('lib/plugins'), path.join(root, 'lib', 'plugins'));
202+
assert.equal(h.getCodeDir(), HOME);
203+
assert.equal(h.getCodeDir('.'), HOME);
204+
assert.equal(h.getCodeDir('icons'), path.join(HOME, 'icons'));
205+
assert.equal(h.getCodeDir('lib/plugins'), path.join(HOME, 'lib', 'plugins'));
197206
});
198207

199208
it('should getCodeDirData ok', function() {
@@ -205,11 +214,15 @@ describe('helper', function() {
205214
});
206215

207216
it('should getPluginFile ok', function() {
208-
const expect = path.join(root, 'lib/plugins/cache.js');
217+
const expect = path.join(HOME, 'lib/plugins/cache.js');
209218
assert.equal(h.getPluginFile('cache.js'), expect);
210219
assert.equal(h.getPluginFile('./cache.js'), expect);
211220
assert.equal(h.getPluginFile('https://github.com/skygragon/cache.js'), expect);
212221
});
222+
223+
it('should getFileData ok with missing file', function() {
224+
assert.equal(h.getFileData('non-exist'), null);
225+
});
213226
}); // #dirAndFiles
214227

215228
describe('#getSetCookieValue', function() {
@@ -284,5 +297,15 @@ describe('helper', function() {
284297
assert.equal(h.badge('x'), chalk.white.bgBlue(' x '));
285298
assert.equal(h.badge('x', 'green'), chalk.black.bgGreen(' x '));
286299
});
300+
301+
it('should ok with random', function() {
302+
const badges = _.values(h.__get__('COLORS'))
303+
.map(function(x) {
304+
return chalk[x.fg][x.bg](' random ');
305+
});
306+
307+
const i = badges.indexOf(h.badge('random', 'random'));
308+
assert.equal(i >= 0, true);
309+
});
287310
}); // #badge
288311
});

‎test/test_icon.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,27 @@
22
const assert = require('chai').assert;
33
const rewire = require('rewire');
44

5-
const h = rewire('../lib/helper');
6-
75
describe('icon', function() {
8-
let icon = null;
6+
let icon;
7+
let h;
98

10-
before(function() {
9+
beforeEach(function() {
10+
h = rewire('../lib/helper');
1111
h.getCodeDirData = function() {
1212
return [
13-
{
14-
name: 'word',
15-
data: {
16-
yes: 'yes',
17-
no: 'no',
18-
lock: 'lock',
19-
like: 'like',
20-
unlike: 'unlike'
21-
}
22-
}
13+
{name: 'mac', data: {yes: 'yes', no: 'no', lock: 'lock', like: 'like', unlike: 'unlike'}},
14+
{name: 'win7', data: {yes: 'YES', no: 'NO', lock: 'LOCK', like: 'LIKE', unlike: 'UNLIKE'}}
2315
];
2416
};
25-
});
2617

27-
beforeEach(function() {
2818
icon = rewire('../lib/icon');
2919
icon.__set__('h', h);
3020
icon.init();
3121
});
3222

3323
describe('#setTheme', function() {
3424
it('should ok with known theme', function() {
35-
icon.setTheme('word');
25+
icon.setTheme('mac');
3626
assert.equal(icon.yes, 'yes');
3727
assert.equal(icon.no, 'no');
3828
assert.equal(icon.lock, 'lock');
@@ -48,5 +38,16 @@ describe('icon', function() {
4838
assert.equal(icon.like, '★');
4939
assert.equal(icon.unlike, '☆');
5040
});
51-
});
41+
42+
it('should ok with unknown theme on windows', function() {
43+
h.isWindows = () => true;
44+
45+
icon.setTheme('non-exist');
46+
assert.equal(icon.yes, 'YES');
47+
assert.equal(icon.no, 'NO');
48+
assert.equal(icon.lock, 'LOCK');
49+
assert.equal(icon.like, 'LIKE');
50+
assert.equal(icon.unlike, 'UNLIKE');
51+
});
52+
}); // #setTheme
5253
});

‎test/test_log.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,29 @@
11
'use strict';
22
const assert = require('chai').assert;
3+
const rewire = require('rewire');
34

45
const chalk = require('../lib/chalk');
5-
const log = require('../lib/log');
66

77
describe('log', function() {
8-
let _output = null;
9-
let result = '';
8+
let log;
9+
let savedOutput;
10+
let expected;
1011

1112
before(function() {
1213
chalk.init();
13-
_output = log.output;
14-
log.output = function(s) {
15-
result = s;
16-
};
17-
});
18-
19-
after(function() {
20-
log.output = _output;
2114
});
2215

2316
beforeEach(function() {
17+
log = rewire('../lib/log');
18+
savedOutput = log.output;
19+
log.output = x => expected = x;
20+
2421
log.init();
25-
result = '';
22+
expected = '';
23+
});
24+
25+
afterEach(function() {
26+
log.output = savedOutput;
2627
});
2728

2829
describe('#setLevel', function() {
@@ -43,7 +44,7 @@ describe('log', function() {
4344
log.setLevel('');
4445
assert.deepEqual(log.level, log.levels.get('INFO'));
4546
});
46-
});
47+
}); // #setLevel
4748

4849
describe('#isEnabled', function() {
4950
it('should ok', function() {
@@ -54,55 +55,55 @@ describe('log', function() {
5455
assert.equal(log.isEnabled('WARN'), true);
5556
assert.equal(log.isEnabled('ERROR'), true);
5657
});
57-
});
58+
}); // #isEnabled
5859

5960
describe('#levels', function() {
6061
it('should ok with log.trace', function() {
6162
log.trace('some error');
62-
assert.equal(result, '');
63+
assert.equal(expected, '');
6364

6465
log.setLevel('TRACE');
6566
log.trace('some error');
66-
assert.equal(result, chalk.gray('[TRACE] some error'));
67+
assert.equal(expected, chalk.gray('[TRACE] some error'));
6768
});
6869

6970
it('should ok with log.debug', function() {
7071
log.debug('some error');
71-
assert.equal(result, '');
72+
assert.equal(expected, '');
7273

7374
log.setLevel('DEBUG');
7475
log.debug('some error');
75-
assert.equal(result, chalk.gray('[DEBUG] some error'));
76+
assert.equal(expected, chalk.gray('[DEBUG] some error'));
7677
});
7778

7879
it('should ok with log.info', function() {
7980
log.info('some error');
80-
assert.equal(result, 'some error');
81+
assert.equal(expected, 'some error');
8182
});
8283

8384
it('should ok with log.warn', function() {
8485
log.warn('some error');
85-
assert.equal(result, chalk.yellow('[WARN] some error'));
86+
assert.equal(expected, chalk.yellow('[WARN] some error'));
8687
});
8788

8889
it('should ok with log.error', function() {
8990
log.error('some error');
90-
assert.equal(result, chalk.red('[ERROR] some error'));
91+
assert.equal(expected, chalk.red('[ERROR] some error'));
9192
});
9293

9394
it('should ok with log.fail', function() {
9495
log.fail({msg: 'some error', statusCode: 500});
95-
assert.equal(result, chalk.red('[ERROR] some error [500]'));
96+
assert.equal(expected, chalk.red('[ERROR] some error [500]'));
9697

9798
log.fail('some error');
98-
assert.equal(result, chalk.red('[ERROR] some error [0]'));
99+
assert.equal(expected, chalk.red('[ERROR] some error [0]'));
99100
});
100-
});
101+
}); // #levels
101102

102103
describe('#printf', function() {
103104
it('should ok', function() {
104105
log.printf('%s and %d and %%', 'string', 100);
105-
assert.equal(result, 'string and 100 and %');
106+
assert.equal(expected, 'string and 100 and %');
106107
});
107-
});
108+
}); // #printf
108109
});

‎test/test_plugin.js

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -8,126 +8,128 @@ const rewire = require('rewire');
88
const chalk = require('../lib/chalk');
99
const config = require('../lib/config');
1010
const log = require('../lib/log');
11+
const th = require('./helper');
1112

1213
const Plugin = rewire('../lib/plugin');
1314

1415
describe('plugin', function() {
15-
const noop = () => {};
16+
let h;
17+
let cache;
18+
19+
const NOOP = () => {};
1620

1721
before(function() {
1822
log.init();
1923
chalk.init();
2024
config.init();
2125

22-
const h = rewire('../lib/helper');
26+
h = rewire('../lib/helper');
27+
cache = rewire('../lib/cache');
2328
Plugin.__set__('h', h);
24-
Plugin.__set__('cache', {get: noop});
29+
Plugin.__set__('cache', cache);
2530
});
2631

27-
function clean() {
28-
for (let f of fs.readdirSync('./tmp'))
29-
fs.unlinkSync('./tmp/' + f);
30-
}
31-
beforeEach(clean);
32-
afterEach(clean);
32+
beforeEach(function() {
33+
th.clean();
34+
cache.get = NOOP;
35+
});
3336

3437
describe('#Plugin.init', function() {
35-
const leetcode = new Plugin(0, 'Leetcode', '2.0');
36-
const cache = new Plugin(1, 'Cache', '1.0');
37-
const retry = new Plugin(2, 'Retry', '3.0');
38-
const core = new Plugin(3, 'Core', '4.0');
38+
const p1 = new Plugin(0, 'Leetcode', '2.0');
39+
const p2 = new Plugin(1, 'Cache', '1.0');
40+
const p3 = new Plugin(2, 'Retry', '3.0');
41+
const p4 = new Plugin(3, 'Core', '4.0');
3942

4043
before(function() {
41-
cache.init = leetcode.init = retry.init = core.init = noop;
42-
Plugin.__get__('h').getCodeDirData = function() {
44+
p1.init = p2.init = p3.init = p4.init = NOOP;
45+
h.getCodeDirData = function() {
4346
return [
44-
{name: 'cache', data: cache, file: 'cache.js'},
45-
{name: 'leetcode', data: leetcode, file: '.leetcode.js'}, // disabled
46-
{name: 'retry', data: retry, file: 'retry.js'},
47+
{name: 'cache', data: p2, file: 'cache.js'},
48+
{name: 'leetcode', data: p1, file: '.leetcode.js'}, // disabled
49+
{name: 'retry', data: p3, file: 'retry.js'},
4750
{name: 'bad', data: null}
4851
];
4952
};
5053
});
5154

5255
it('should init ok', function() {
5356
assert.deepEqual(Plugin.plugins, []);
54-
const res = Plugin.init(core);
57+
58+
const res = Plugin.init(p4);
5559
assert.equal(res, true);
5660
assert.deepEqual(Plugin.plugins.length, 3);
5761

5862
const names = Plugin.plugins.map(p => p.name);
5963
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode']);
6064

61-
assert.equal(core.next, retry);
62-
assert.equal(retry.next, cache);
63-
assert.equal(cache.next, null);
64-
assert.equal(leetcode.next, null);
65+
assert.equal(p4.next, p3);
66+
assert.equal(p3.next, p2);
67+
assert.equal(p2.next, null);
68+
assert.equal(p1.next, null);
6569
});
6670

6771
it('should find missing ok', function() {
68-
Plugin.__set__('cache', {
69-
get: () => {
70-
return {company: true, solution: true};
71-
}
72-
});
72+
cache.get = () => {
73+
return {company: true, solution: true};
74+
};
7375

74-
const res = Plugin.init(core);
76+
const res = Plugin.init(p4);
7577
assert.equal(res, false);
7678
assert.deepEqual(Plugin.plugins.length, 5);
7779

7880
const names = Plugin.plugins.map(p => p.name);
7981
assert.deepEqual(names, ['Retry', 'Cache', 'Leetcode', 'company', 'solution']);
8082

81-
assert.equal(core.next, retry);
82-
assert.equal(retry.next, cache);
83-
assert.equal(cache.next, null);
84-
assert.equal(leetcode.next, null);
83+
assert.equal(p4.next, p3);
84+
assert.equal(p3.next, p2);
85+
assert.equal(p2.next, null);
86+
assert.equal(p1.next, null);
8587
});
8688
}); // #Plugin.init
8789

8890
describe('#install', function() {
8991
let expected;
92+
9093
before(function() {
91-
const cp = {
94+
Plugin.__set__('cp', {
9295
exec: function(cmd, opts, cb) {
9396
expected = cmd;
9497
return cb();
9598
}
96-
};
97-
Plugin.__set__('cp', cp);
99+
});
98100
});
99101

100102
it('should install no deps ok', function(done) {
101103
expected = '';
102-
const plugin = new Plugin(100, 'test', '2017.12.26', 'desc', []);
103-
plugin.install(function() {
104+
const p = new Plugin(100, 'test', '2017.12.26', 'desc', []);
105+
p.install(function() {
104106
assert.equal(expected, '');
105107
done();
106108
});
107109
});
108110

109111
it('should install deps ok', function(done) {
110112
const deps = ['a', 'b:linux', 'b:darwin', 'b:win32', 'c:bad', 'd'];
111-
const plugin = new Plugin(100, 'test', '2017.12.26', 'desc', deps);
112-
plugin.install(function() {
113+
const p = new Plugin(100, 'test', '2017.12.26', 'desc', deps);
114+
p.install(function() {
113115
assert.equal(expected, 'npm install --save a b d');
114116
done();
115117
});
116118
});
117119
}); // #install
118120

119121
describe('#Plugin.copy', function() {
120-
const src = path.resolve('./tmp/copy.src.js');
121-
const dst = path.resolve('./tmp/copy.test.js');
122+
const SRC = path.resolve(th.DIR, 'copy.src.js');
123+
const DST = path.resolve(th.DIR, 'copy.test.js');
122124

123-
beforeEach(function() {
124-
Plugin.__get__('h').getPluginFile = () => dst;
125+
before(function() {
126+
h.getPluginFile = () => DST;
125127
});
126128

127129
it('should copy from http error', function(done) {
128130
Plugin.copy('non-exists', function(e, fullpath) {
129131
assert.equal(e, 'HTTP Error: 404');
130-
assert.equal(fs.existsSync(dst), false);
132+
assert.equal(fs.existsSync(DST), false);
131133
done();
132134
});
133135
}).timeout(5000);
@@ -139,12 +141,12 @@ describe('plugin', function() {
139141
' install: function(cb) { cb(); }',
140142
'};'
141143
];
142-
fs.writeFileSync(src, data.join('\n'));
144+
fs.writeFileSync(SRC, data.join('\n'));
143145

144-
Plugin.install(src, function(e, plugin) {
146+
Plugin.install(SRC, function(e, p) {
145147
assert.notExists(e);
146-
assert.equal(plugin.x, 123);
147-
assert.equal(fs.existsSync(dst), true);
148+
assert.equal(p.x, 123);
149+
assert.equal(fs.existsSync(DST), true);
148150
done();
149151
});
150152
});
@@ -156,10 +158,11 @@ describe('plugin', function() {
156158
new Plugin(1, '1', '2018.01.01'),
157159
new Plugin(2, '2', 'missing'),
158160
];
159-
let expected = [];
161+
let expected;
160162

161163
beforeEach(function() {
162-
Plugin.__get__('h').getPluginFile = x => './tmp/' + x;
164+
expected = [];
165+
h.getPluginFile = x => th.DIR + x;
163166
Plugin.install = (name, cb) => {
164167
expected.push(name);
165168
return cb(null, PLUGINS[+name]);
@@ -168,7 +171,6 @@ describe('plugin', function() {
168171

169172
it('should ok', function(done) {
170173
Plugin.plugins = PLUGINS;
171-
expected = [];
172174
Plugin.installMissings(function(e) {
173175
assert.notExists(e);
174176
assert.deepEqual(expected, ['0', '2']);
@@ -178,53 +180,56 @@ describe('plugin', function() {
178180
}); // #Plugin.installMissings
179181

180182
describe('#enable', function() {
181-
const file = path.resolve('./tmp/leetcode.js');
182-
beforeEach(function() {
183-
Plugin.__get__('h').getPluginFile = () => file;
183+
const FILE = path.resolve(th.DIR, 'leetcode.js');
184+
185+
before(function() {
186+
h.getPluginFile = () => FILE;
184187
});
185188

186189
it('should ok', function() {
187190
const p = new Plugin(0, 'Leetcode', '2.0', '');
188191
assert.equal(p.enabled, true);
189192

190193
p.setFile('.leetcode.js');
191-
fs.writeFileSync(file, '');
194+
fs.writeFileSync(FILE, '');
192195
assert.equal(p.enabled, false);
193196
assert.equal(p.file, '.leetcode.js');
194-
195197
p.enable(false);
196198
assert.equal(p.enabled, false);
197199
assert.equal(p.file, '.leetcode.js');
198200
p.enable(true);
199201
assert.equal(p.enabled, true);
200202
assert.equal(p.file, 'leetcode.js');
203+
p.enable(false);
204+
assert.equal(p.enabled, false);
205+
assert.equal(p.file, '.leetcode.js');
201206
});
202207
}); // #enable
203208

204209
describe('#delete', function() {
205210
it('should ok', function() {
206-
Plugin.__get__('h').getPluginFile = x => './tmp/' + x;
211+
h.getPluginFile = x => th.DIR + x;
207212

208213
const p = new Plugin(0, '0', '2018.01.01');
209214
p.file = '0.js';
210215
fs.writeFileSync('./tmp/0.js', '');
211216

212217
assert.equal(p.deleted, false);
213-
assert.deepEqual(fs.readdirSync('./tmp'), ['0.js']);
218+
assert.deepEqual(fs.readdirSync(th.DIR), ['0.js']);
214219
p.delete();
215220
assert.equal(p.deleted, true);
216-
assert.deepEqual(fs.readdirSync('./tmp'), []);
221+
assert.deepEqual(fs.readdirSync(th.DIR), []);
217222
p.delete();
218223
assert.equal(p.deleted, true);
219-
assert.deepEqual(fs.readdirSync('./tmp'), []);
224+
assert.deepEqual(fs.readdirSync(th.DIR), []);
220225
});
221226
}); // #delete
222227

223228
describe('#save', function() {
224229
it('should ok', function() {
225230
let data = {};
226-
Plugin.__get__('cache').get = () => data;
227-
Plugin.__get__('cache').set = (k, x) => data = x;
231+
cache.get = () => data;
232+
cache.set = (k, x) => data = x;
228233

229234
const p = new Plugin(0, '0', '2018.01.01');
230235
p.save();

‎test/test_queue.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
'use strict';
22
const assert = require('chai').assert;
3+
const rewire = require('rewire');
4+
35

4-
const Queue = require('../lib/queue');
56

67
describe('queue', function() {
8+
let Queue;
9+
10+
beforeEach(function() {
11+
Queue = rewire('../lib/queue');
12+
});
13+
714
it('should ok', function(done) {
815
function doTask(x, q, cb) {
916
++q.ctx.n;
@@ -19,10 +26,33 @@ describe('queue', function() {
1926
q.addTasks([3, 4, 5]);
2027

2128
q.run(5, function(e, ctx) {
22-
assert.equal(e, null);
29+
assert.notExists(e);
2330
assert.equal(ctx.n, 5);
2431
assert.equal(ctx.sum, 15);
2532
done();
2633
});
2734
});
35+
36+
it('should ok in sequence', function(done) {
37+
const config = {network: {}};
38+
Queue.__set__('config', config);
39+
40+
function doTask(x, q, cb) {
41+
if (!q.ctx.list) q.ctx.list = [];
42+
q.ctx.list.push(x);
43+
return cb();
44+
}
45+
46+
const q = new Queue(null, null, doTask);
47+
q.addTask(1);
48+
q.addTasks([2, 3]);
49+
q.addTasks([4]);
50+
q.addTask(5);
51+
52+
q.run(null, function(e, ctx) {
53+
assert.notExists(e);
54+
assert.deepEqual(ctx.list, [1, 2, 3, 4, 5]);
55+
done();
56+
});
57+
});
2858
});

‎test/test_session.js

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,24 @@
22
const assert = require('chai').assert;
33
const rewire = require('rewire');
44

5-
const session = rewire('../lib/session');
6-
75
describe('session', function() {
8-
let stats = null;
9-
let now = '';
6+
let session;
7+
let stats;
8+
let now;
109

11-
before(function() {
10+
beforeEach(function() {
11+
stats = null;
1212
const cache = {
1313
get: (k) => stats,
1414
set: (k, v) => stats = v
1515
};
16-
session.__set__('cache', cache);
17-
1816
const moment = () => {
19-
return {
20-
format: () => now
21-
}
17+
return {format: () => now}
2218
};
23-
session.__set__('moment', moment);
24-
});
2519

26-
beforeEach(function() {
27-
stats = null;
20+
session = rewire('../lib/session');
21+
session.__set__('cache', cache);
22+
session.__set__('moment', moment);
2823
});
2924

3025
describe('#updateStat', function() {
@@ -53,5 +48,5 @@ describe('session', function() {
5348
session.updateStat('ac.set', 101);
5449
assert.deepEqual(stats, {'2017.12.13': {'ac.set': [101, 100]}});
5550
});
56-
});
51+
}); // #updateStat
5752
});

0 commit comments

Comments
 (0)
Please sign in to comment.