forked from skygragon/leetcode-cli
-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathtest_core.js
256 lines (230 loc) · 7.22 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
var assert = require('chai').assert;
var rewire = require('rewire');
var log = require('../lib/log');
var session = rewire('../lib/session');
var plugin = rewire('../lib/core');
describe('core', function() {
var PROBLEMS = [
{id: 0, name: 'name0', slug: 'slug0', starred: false, category: 'algorithms'},
{id: 1, name: 'name1', slug: 'slug1', starred: true, category: 'algorithms'}
];
var USER = {};
var NEXT = {};
before(function() {
log.init();
session.getUser = function() {
return USER;
};
plugin.__set__('session', session);
plugin.setNext(NEXT);
});
beforeEach(function() {
NEXT.getProblems = function(cb) {
return cb(null, PROBLEMS);
};
NEXT.getProblem = function(problem, cb) {
return cb(null, problem);
};
});
describe('#starProblem', function() {
it('should starProblem ok', function(done) {
NEXT.starProblem = function(problem, starred, cb) {
return cb(null, starred);
};
assert.equal(PROBLEMS[0].starred, false);
plugin.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);
plugin.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);
plugin.starProblem(PROBLEMS[0], false, function(e, starred) {
assert.equal(e, null);
assert.equal(starred, false);
done();
});
});
}); // #starProblem
describe('#exportProblem', function() {
it('should codeonly ok', function() {
var expected = [
'/**',
' * Definition for singly-linked list.',
' * struct ListNode {',
' * int val;',
' * ListNode *next;',
' * ListNode(int x) : val(x), next(NULL) {}',
' * };',
' */',
'class Solution {',
'public:',
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
' ',
' }',
'};',
''
].join('\n');
var problem = require('./mock/add-two-numbers.20161015.json');
var opts = {
lang: 'cpp',
code: problem.templates[0].defaultCode,
tpl: 'codeonly'
};
assert.equal(plugin.exportProblem(problem, opts), expected);
});
it('should detailed ok', function() {
var expected = [
'/*',
' * [2] Add Two Numbers',
' *',
' * https://leetcode.com/problems/add-two-numbers',
' *',
' * algorithms',
' * 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',
' */',
'/**',
' * Definition for singly-linked list.',
' * struct ListNode {',
' * int val;',
' * ListNode *next;',
' * ListNode(int x) : val(x), next(NULL) {}',
' * };',
' */',
'class Solution {',
'public:',
' ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {',
' ',
' }',
'};',
''
].join('\n');
var problem = require('./mock/add-two-numbers.20161015.json');
var opts = {
lang: 'cpp',
code: problem.templates[0].defaultCode,
tpl: 'detailed'
};
assert.equal(plugin.exportProblem(problem, opts), expected);
});
it('should detailed ok, 2nd', function() {
var expected = [
'#',
'# [2] Add Two Numbers',
'#',
'# https://leetcode.com/problems/add-two-numbers',
'#',
'# algorithms',
'# 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('\n');
var problem = require('./mock/add-two-numbers.20161015.json');
problem.testcase = null;
var opts = {
lang: 'ruby',
code: problem.templates[6].defaultCode,
tpl: 'detailed'
};
assert.equal(plugin.exportProblem(problem, opts), expected);
});
}); // #exportProblem
describe('#getProblem', function() {
it('should getProblem by id ok', function(done) {
plugin.getProblem(0, function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem, PROBLEMS[0]);
done();
});
});
it('should getProblem by key ok', function(done) {
plugin.getProblem('slug0', function(e, problem) {
assert.equal(e, null);
assert.deepEqual(problem, PROBLEMS[0]);
done();
});
});
it('should getProblem error if not found', function(done) {
plugin.getProblem(3, function(e, problem) {
assert.equal(e, 'Problem not found!');
done();
});
});
it('should getProblem fail if client error', function(done) {
NEXT.getProblem = function(problem, cb) {
return cb('client getProblem error');
};
plugin.getProblem(0, function(e, problem) {
assert.equal(e, 'client getProblem error');
done();
});
});
it('should getProblem random ok', function(done) {
NEXT.getProblems = function(cb) {
return cb(null, [
{id: 0, state: 'ac', locked: false},
{id: 1, state: 'none', locked: true},
{id: 2, state: 'none', locked: false}
]);
};
plugin.getProblem(undefined, function(e, problem) {
assert.equal(e, null);
assert.equal(problem.id, 2);
done();
});
});
it('should getProblem fail if getProblems error', function(done) {
NEXT.getProblems = function(cb) {
return cb('getProblems error');
};
plugin.getProblem(0, function(e, problem) {
assert.equal(e, 'getProblems error');
done();
});
});
}); // #getProblem
});