forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest_core.js
476 lines (405 loc) · 13.7 KB
/
test_core.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
var execSync = require('child_process').execSync;
var fs = require('fs');
var _ = require('underscore');
var assert = require('chai').assert;
var rewire = require('rewire');
// mock depedencies
var cache = rewire('../lib/cache');
var client = rewire('../lib/leetcode_client');
var config = rewire('../lib/config');
var core = rewire('../lib/core');
var h = rewire('../lib/helper');
describe('core', function() {
before(function() {
var home = './tmp';
execSync('rm -rf ' + home);
fs.mkdirSync(home);
h.getHomeDir = function() {
return home;
};
cache.__set__('h', h);
core.__set__('cache', cache);
core.__set__('client', client);
core.__set__('config', config);
});
describe('#user', function() {
var USER = {name: 'test-user', pass: 'password'};
var USER_AFTER = {name: 'test-user', pass: 'password', hash: 'abcdef'};
var USER_AFTER_SAFE = {name: 'test-user', hash: 'abcdef'};
var FAVORITES = {
'favorites': {
'private_favorites': [{
'id_hash': 'abcdef',
'name': 'Favorite'
}]
}
};
it('should login ok', function(done) {
config.AUTO_LOGIN = true;
// before login
cache.del('.user');
assert.equal(core.getUser(), null);
assert.equal(core.isLogin(), false);
client.login = function(user, cb) {
return cb(null, user);
};
client.getFavorites = function(cb) {
return cb(null, FAVORITES);
};
core.login(USER, function(e, user) {
assert.equal(e, null);
assert.deepEqual(user, USER_AFTER);
// after login
assert.deepEqual(core.getUser(), USER_AFTER);
assert.equal(core.isLogin(), true);
done();
});
});
it('should login ok w/ auto login', function(done) {
config.AUTO_LOGIN = false;
cache.del('.user');
client.login = function(user, cb) {
return cb(null, user);
};
core.login(USER, function(e, user) {
assert.equal(e, null);
assert.deepEqual(user, USER_AFTER);
assert.deepEqual(core.getUser(), USER_AFTER_SAFE);
assert.equal(core.isLogin(), true);
done();
});
});
it('should login fail if client login error', function(done) {
client.login = function(user, cb) {
return cb('client login error');
};
core.login(USER, function(e, user) {
assert.equal(e, 'client login error');
done();
});
});
it('should logout ok', function(done) {
// before logout
cache.set('.user', USER);
assert.deepEqual(core.getUser(), USER);
assert.equal(core.isLogin(), true);
// after logout
core.logout(USER);
assert.equal(core.getUser(), null);
assert.equal(core.isLogin(), false);
done();
});
}); // #user
describe('#problems', function() {
var PROBLEMS = [
{id: 0, name: 'name0', slug: 'slug0', starred: false, category: 'algorithms'},
{id: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
];
var RESULTS = [
{name: 'result0'},
{name: 'result1'}
];
describe('#getProblems', function() {
it('should getProblems w/ cache ok', function(done) {
cache.set('problems', PROBLEMS);
core.getProblems(function(e, problems) {
assert.equal(e, null);
assert.deepEqual(problems, PROBLEMS);
done();
});
});
it('should getProblems w/o cache ok', function(done) {
cache.del('problems');
client.getProblems = function(category, user, cb) {
return cb(null, PROBLEMS);
};
core.getProblems(function(e, problems) {
assert.equal(e, null);
assert.deepEqual(problems, PROBLEMS);
done();
});
});
it('should getProblems w/o cache fail if client error', function(done) {
cache.del('problems');
client.getProblems = function(category, user, cb) {
return cb('client getProblems error');
};
core.getProblems(function(e, problems) {
assert.equal(e, 'client getProblems error');
done();
});
});
}); // #getProblems
describe('#getProblem', function() {
it('should getProblem by id w/ cache ok', function(done) {
cache.set('problems', PROBLEMS);
cache.set('0.slug0.algorithms', PROBLEMS[0]);
core.getProblem(0, function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem, PROBLEMS[0]);
done();
});
});
it('should getProblem by name w/ cache ok', function(done) {
cache.set('problems', PROBLEMS);
cache.set('0.slug0.algorithms', PROBLEMS[0]);
core.getProblem('name0', function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem, PROBLEMS[0]);
done();
});
});
it('should getProblem by key w/ cache ok', function(done) {
cache.set('problems', PROBLEMS);
cache.set('0.slug0.algorithms', PROBLEMS[0]);
core.getProblem('slug0', function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem, PROBLEMS[0]);
done();
});
});
it('should getProblem by id w/o cache ok', function(done) {
cache.set('problems', PROBLEMS);
cache.del('0.slug0.algorithms');
client.getProblem = function(user, problem, cb) {
return cb(null, problem);
};
core.getProblem(0, function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem, PROBLEMS[0]);
done();
});
});
it('should getProblem error if not found', function(done) {
cache.set('problems', PROBLEMS);
core.getProblem(3, function(e, problem) {
assert.equal(e, 'Problem not found!');
done();
});
});
it('should getProblem fail if client error', function(done) {
cache.set('problems', PROBLEMS);
cache.del('0.slug0.algorithms');
client.getProblem = function(user, problem, cb) {
return cb('client getProblem error');
};
core.getProblem(0, function(e, problem) {
assert.equal(e, 'client getProblem error');
done();
});
});
it('should getProblem fail if getProblems error', function(done) {
cache.del('problems');
client.getProblems = function(category, user, cb) {
return cb('getProblems error');
};
core.getProblem(0, function(e, problem) {
assert.equal(e, 'getProblems error');
done();
});
});
}); // #getProblem
describe('#updateProblem', function() {
it('should updateProblem ok', function(done) {
cache.set('problems', PROBLEMS);
cache.del('0.slug0.algorithms');
var kv = {value: 'value00'};
var ret = core.updateProblem(PROBLEMS[0], kv);
assert.equal(ret, true);
core.getProblem(0, function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem,
{id: 0, name: 'name0', slug: 'slug0', value: 'value00', starred: false, category: 'algorithms'});
done();
});
});
it('should updateProblem fail if no problems found', function() {
cache.del('problems');
var ret = core.updateProblem(PROBLEMS[0], {});
assert.equal(ret, false);
});
it('should updateProblem fail if unknown problem', function() {
cache.set('problems', [PROBLEMS[1]]);
var ret = core.updateProblem(PROBLEMS[0], {});
assert.equal(ret, false);
});
}); // #updateProblem
describe('#starProblem', function() {
it('should starProblem ok', function(done) {
client.starProblem = function(user, problem, starred, cb) {
return cb(null, starred);
};
assert.equal(PROBLEMS[0].starred, false);
core.starProblem(PROBLEMS[0], true, function(e, starred) {
assert.equal(e, null);
assert.equal(starred, true);
done();
});
});
it('should starProblem ok if already starred', function(done) {
assert.equal(PROBLEMS[1].starred, true);
core.starProblem(PROBLEMS[1], true, function(e, starred) {
assert.equal(e, null);
assert.equal(starred, true);
done();
});
});
it('should starProblem ok if already unstarred', function(done) {
assert.equal(PROBLEMS[0].starred, false);
core.starProblem(PROBLEMS[0], false, function(e, starred) {
assert.equal(e, null);
assert.equal(starred, false);
done();
});
});
}); // #starProblem
// dummy test
it('should testProblem ok', function(done) {
client.testProblem = function(problem, cb) {
return cb(null, RESULTS);
};
core.testProblem(PROBLEMS[0], function(e, results) {
assert.equal(e, null);
assert.deepEqual(results, RESULTS);
done();
});
});
// dummy test
it('should submitProblem ok', function(done) {
client.submitProblem = function(problem, cb) {
return cb(null, RESULTS);
};
core.submitProblem(PROBLEMS[1], function(e, results) {
assert.equal(e, null);
assert.deepEqual(results, RESULTS);
done();
});
});
describe('#exportProblem', function() {
function injectVerify(expected, done) {
core.__set__('fs', {
writeFileSync: function(f, data) {
assert.equal(data, expected);
done();
},
readFileSync: fs.readFileSync
});
}
it('should ok w/ code only', function(done) {
var expected = [
'class Solution {',
'public:',
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
'',
' }',
'};'
].join('\r\n');
injectVerify(expected, done);
var problem = require('./mock/add-two-numbers.20161015.json');
core.exportProblem(problem, 'test.cpp', true);
});
it('should ok w/ detailed comments', function(done) {
var expected = [
'/*',
' * [2] Add Two Numbers',
' *',
' * https://leetcode.com/problems/add-two-numbers',
' *',
' * Medium (25.37%)',
' * Total Accepted: 195263',
' * Total Submissions: 769711',
' * Testcase Example: \'[2,4,3]\\n[5,6,4]\'',
' *',
' * You are given two linked lists representing two non-negative numbers. The',
' * digits are stored in reverse order and each of their nodes contain a single',
' * digit. Add the two numbers and return it as a linked list.',
' * ',
' * Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
' * Output: 7 -> 0 -> 8',
' */',
'class Solution {',
'public:',
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
'',
' }',
'};',
''
].join('\r\n');
injectVerify(expected, done);
var problem = require('./mock/add-two-numbers.20161015.json');
core.exportProblem(problem, 'test.cpp', false);
});
it('should ok w/ detailed comments, 2nd', function(done) {
var expected = [
'#',
'# [2] Add Two Numbers',
'#',
'# https://leetcode.com/problems/add-two-numbers',
'#',
'# Medium (25.37%)',
'# Total Accepted: 195263',
'# Total Submissions: 769711',
'# Testcase Example: \'\'',
'#',
'# You are given two linked lists representing two non-negative numbers. The',
'# digits are stored in reverse order and each of their nodes contain a single',
'# digit. Add the two numbers and return it as a linked list.',
'# ',
'# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
'# Output: 7 -> 0 -> 8',
'#',
'# Definition for singly-linked list.',
'# class ListNode',
'# attr_accessor :val, :next',
'# def initialize(val)',
'# @val = val',
'# @next = nil',
'# end',
'# end',
'',
'# @param {ListNode} l1',
'# @param {ListNode} l2',
'# @return {ListNode}',
'def add_two_numbers(l1, l2)',
' ',
'end',
''
].join('\r\n');
injectVerify(expected, done);
var problem = require('./mock/add-two-numbers.20161015.json');
problem.testcase = null;
problem.code = _.find(problem.templates, function(template) {
return template.value === 'ruby';
}).defaultCode;
core.exportProblem(problem, 'test.rb', false);
});
}); // #exportProblem
}); // #problems
describe('#submission', function() {
var SUBMISSIONS = [
{id: 1234, state: 'Accepted'}
];
// dummy test
it('should getSubmissions ok', function(done) {
client.getSubmissions = function(problem, cb) {
return cb(null, SUBMISSIONS);
};
core.getSubmissions({}, function(e, submissions) {
assert.equal(e, null);
assert.deepEqual(submissions, SUBMISSIONS);
done();
});
});
// dummy test
it('should getSubmission ok', function(done) {
client.getSubmission = function(submission, cb) {
return cb(null, SUBMISSIONS[0]);
};
core.getSubmission({}, function(e, submission) {
assert.equal(e, null);
assert.deepEqual(submission, SUBMISSIONS[0]);
done();
});
});
}); // #submission
});