Skip to content

Commit f3176ed

Browse files
committed
Add more UTs.
Signed-off-by: Eric Wang <[email protected]>
1 parent 9d96f70 commit f3176ed

File tree

4 files changed

+214
-6
lines changed

4 files changed

+214
-6
lines changed

Diff for: test/test_cache.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
1+
var execSync = require('child_process').execSync;
2+
13
var assert = require('chai').assert;
4+
var rewire = require('rewire');
25

3-
var cache = require('../lib/cache');
6+
var cache = rewire('../lib/cache');
7+
var h = rewire('../lib/helper');
48

59
describe('cache', function() {
610
var k = '.test';
711
var v = {test: 'data'};
812

13+
before(function() {
14+
var cachedir = './tmp';
15+
execSync('rm -rf ' + cachedir);
16+
17+
h.getCacheDir = function() {
18+
return cachedir;
19+
};
20+
cache.__set__('h', h);
21+
});
22+
923
it('should ok when not cached', function() {
1024
cache.del(k);
1125

Diff for: test/test_core.js

+62-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
var assert = require('chai').assert;
1+
var execSync = require('child_process').execSync;
22
var fs = require('fs');
3+
4+
var _ = require('underscore');
5+
var assert = require('chai').assert;
36
var rewire = require('rewire');
47

58
// mock depedencies
@@ -12,7 +15,8 @@ var h = rewire('../lib/helper');
1215
describe('core', function() {
1316
before(function() {
1417
var home = './tmp';
15-
if (!fs.existsSync(home)) fs.mkdirSync(home);
18+
execSync('rm -rf ' + home);
19+
fs.mkdirSync(home);
1620

1721
h.getHomeDir = function() {
1822
return home;
@@ -278,6 +282,15 @@ describe('core', function() {
278282
done();
279283
});
280284
});
285+
286+
it('should starProblem ok if already unstarred', function(done) {
287+
assert.equal(PROBLEMS[0].starred, false);
288+
core.starProblem(PROBLEMS[0], false, function(e, starred) {
289+
assert.equal(e, null);
290+
assert.equal(starred, false);
291+
done();
292+
});
293+
});
281294
}); // #starProblem
282295

283296
// dummy test
@@ -366,6 +379,53 @@ describe('core', function() {
366379
var problem = require('./mock/add-two-numbers.20161015.json');
367380
core.exportProblem(problem, 'test.cpp', false);
368381
});
382+
383+
it('should ok w/ detailed comments, 2nd', function(done) {
384+
var expected = [
385+
'#',
386+
'# [2] Add Two Numbers',
387+
'#',
388+
'# https://leetcode.com/problems/add-two-numbers',
389+
'#',
390+
'# Medium (25.37%)',
391+
'# Total Accepted: 195263',
392+
'# Total Submissions: 769711',
393+
'# Testcase Example: \'\'',
394+
'#',
395+
'# You are given two linked lists representing two non-negative numbers. The',
396+
'# digits are stored in reverse order and each of their nodes contain a single',
397+
'# digit. Add the two numbers and return it as a linked list.',
398+
'# ',
399+
'# Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)',
400+
'# Output: 7 -> 0 -> 8',
401+
'#',
402+
'# Definition for singly-linked list.',
403+
'# class ListNode',
404+
'# attr_accessor :val, :next',
405+
'# def initialize(val)',
406+
'# @val = val',
407+
'# @next = nil',
408+
'# end',
409+
'# end',
410+
'',
411+
'# @param {ListNode} l1',
412+
'# @param {ListNode} l2',
413+
'# @return {ListNode}',
414+
'def add_two_numbers(l1, l2)',
415+
' ',
416+
'end',
417+
''
418+
].join('\r\n');
419+
420+
injectVerify(expected, done);
421+
422+
var problem = require('./mock/add-two-numbers.20161015.json');
423+
problem.testcase = null;
424+
problem.code = _.find(problem.templates, function(template) {
425+
return template.value === 'ruby';
426+
}).defaultCode;
427+
core.exportProblem(problem, 'test.rb', false);
428+
});
369429
}); // #exportProblem
370430
}); // #problems
371431

Diff for: test/test_helper.js

+13
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ describe('helper', function() {
133133
assert.equal(h.getCacheFile('xxx'), '/home/skygragon/.lc/xxx.json');
134134
assert.equal(h.getConfigFile(), '/home/skygragon/.lcconfig');
135135
assert.equal(h.getFilename('/home/skygragon/.lc/xxx.json'), 'xxx');
136+
137+
process.env.HOME = '';
138+
process.env.USERPROFILE = 'C:\\Users\\skygragon';
139+
assert.equal(h.getHomeDir(), 'C:\\Users\\skygragon');
136140
});
137141
}); // #dirAndFiles
138142

@@ -173,5 +177,14 @@ describe('helper', function() {
173177
done();
174178
});
175179
});
180+
181+
it('should ok w/ empty input', function(done) {
182+
hijackStdin('');
183+
184+
h.readStdin(function(e, data) {
185+
assert.equal(data, '');
186+
done();
187+
});
188+
});
176189
}); // #readStdin
177190
});

Diff for: test/test_leetcode_client.js

+124-3
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,24 @@ describe('leetcode_client', function() {
7878
done();
7979
});
8080
});
81+
82+
it('should fail if http error in relogin', function(done) {
83+
config.AUTO_LOGIN = true;
84+
nock(config.URL_PROBLEMS).get('/').reply(403);
85+
core.login = function(user, cb) {
86+
return cb('unknown error!');
87+
};
88+
89+
// the original error will be returned instead
90+
var expected = {
91+
msg: 'session expired, please login again',
92+
statusCode: 403
93+
};
94+
client.getProblems(function(e, problems) {
95+
assert.deepEqual(e, expected);
96+
done();
97+
});
98+
});
8199
});
82100

83101
describe('#getProblems', function() {
@@ -232,6 +250,17 @@ describe('leetcode_client', function() {
232250
done();
233251
});
234252
});
253+
254+
it('should fail if http error', function(done) {
255+
nock('https://leetcode.com')
256+
.get('/problems/find-the-difference')
257+
.replyWithError('unknown error!');
258+
259+
client.getProblem(PROBLEM, function(e, problem) {
260+
assert.equal(e.message, 'unknown error!');
261+
done();
262+
});
263+
});
235264
}); // #getProblem
236265

237266
describe('#testProblem', function() {
@@ -298,6 +327,9 @@ describe('leetcode_client', function() {
298327
.post('/problems/find-the-difference/submit/')
299328
.reply(200, '{"submission_id": "id1"}');
300329

330+
nock('https://leetcode.com')
331+
.get('/submissions/detail/id1/check/')
332+
.reply(200, '{"state": "STARTED"}');
301333
nock('https://leetcode.com')
302334
.get('/submissions/detail/id1/check/')
303335
.reply(200, '{"state": "SUCCESS"}');
@@ -319,6 +351,21 @@ describe('leetcode_client', function() {
319351
done();
320352
});
321353
});
354+
355+
it('should fail if server error in check result', function(done) {
356+
nock('https://leetcode.com')
357+
.post('/problems/find-the-difference/submit/')
358+
.reply(200, '{"submission_id": "id1"}');
359+
360+
nock('https://leetcode.com')
361+
.get('/submissions/detail/id1/check/')
362+
.replyWithError('unknown error!');
363+
364+
client.submitProblem(PROBLEM, function(e, results) {
365+
assert.equal(e.message, 'unknown error!');
366+
done();
367+
});
368+
});
322369
}); // #submitProblem
323370

324371
describe('#starProblem', function() {
@@ -345,6 +392,17 @@ describe('leetcode_client', function() {
345392
done();
346393
});
347394
});
395+
396+
it('should star fail if http error', function(done) {
397+
nock('https://leetcode.com')
398+
.post('/problems/favor/')
399+
.replyWithError('unknown error!');
400+
401+
client.starProblem(PROBLEM, true, function(e, starred) {
402+
assert.equal(e.message, 'unknown error!');
403+
done();
404+
});
405+
});
348406
}); // #starProblem
349407

350408
describe('#getSubmissions', function() {
@@ -383,23 +441,38 @@ describe('leetcode_client', function() {
383441
done();
384442
});
385443
});
444+
445+
it('should fail if http error', function(done) {
446+
nock('https://leetcode.com')
447+
.get('/problems/find-the-difference/submissions/')
448+
.replyWithError('unknown error!');
449+
450+
client.getSubmissions(PROBLEM, function(e, submissions) {
451+
assert.equal(e.message, 'unknown error!');
452+
done();
453+
});
454+
});
386455
}); // #getSubmissions
387456

388457
describe('#getSubmission', function() {
389-
it('should ok', function(done) {
390-
var submission = {
458+
var SUBMISSION;
459+
460+
beforeEach(function() {
461+
SUBMISSION = {
391462
id: '73790064',
392463
lang: 'cpp',
393464
runtime: '9 ms',
394465
path: '/submissions/detail/73790064/',
395466
state: 'Accepted'
396467
};
468+
});
397469

470+
it('should ok', function(done) {
398471
nock('https://leetcode.com')
399472
.get('/submissions/detail/73790064/')
400473
.replyWithFile(200, './test/mock/two-sum.submission.73790064.html.20161006');
401474

402-
client.getSubmission(submission, function(e, submission) {
475+
client.getSubmission(SUBMISSION, function(e, submission) {
403476
assert.equal(e, null);
404477
assert.deepEqual(submission.code,
405478
[
@@ -414,6 +487,29 @@ describe('leetcode_client', function() {
414487
done();
415488
});
416489
});
490+
491+
it('should fail if http error', function(done) {
492+
nock('https://leetcode.com')
493+
.get('/submissions/detail/73790064/')
494+
.replyWithError('unknown error!');
495+
496+
client.getSubmission(SUBMISSION, function(e, submission) {
497+
assert.equal(e.message, 'unknown error!');
498+
done();
499+
});
500+
});
501+
502+
it('should fail if no matching submission', function(done) {
503+
nock('https://leetcode.com')
504+
.get('/submissions/detail/73790064/')
505+
.replyWithFile(200, './test/mock/locked.html.20161015');
506+
507+
client.getSubmission(SUBMISSION, function(e, submission) {
508+
assert.equal(e, null);
509+
assert.equal(submission.code, null);
510+
done();
511+
});
512+
});
417513
}); // #getSubmission
418514

419515
describe('#login', function() {
@@ -443,5 +539,30 @@ describe('leetcode_client', function() {
443539
done();
444540
});
445541
});
542+
543+
it('should fail if http error', function(done) {
544+
nock(config.URL_LOGIN).get('/').reply(200, '', {
545+
'Set-Cookie': [
546+
'csrftoken=LOGIN_CSRF_TOKEN; Max-Age=31449600; Path=/; secure'
547+
]
548+
});
549+
nock(config.URL_LOGIN).post('/').replyWithError('unknown error!');
550+
551+
var user = {};
552+
client.login(user, function(e, user) {
553+
assert.equal(e.message, 'unknown error!');
554+
done();
555+
});
556+
});
557+
558+
it('should fail if http error, 2nd', function(done) {
559+
nock(config.URL_LOGIN).get('/').replyWithError('unknown error!');
560+
561+
var user = {};
562+
client.login(user, function(e, user) {
563+
assert.equal(e.message, 'unknown error!');
564+
done();
565+
});
566+
});
446567
}); // #login
447568
});

0 commit comments

Comments
 (0)