Skip to content

Commit 3a45202

Browse files
committed
add tests for commands.test() and commands.tests()
1 parent 64bc21a commit 3a45202

File tree

2 files changed

+358
-2
lines changed

2 files changed

+358
-2
lines changed

tests/__tests__/__snapshots__/commands.ts.snap

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`Commands contest() contest id is given contest found, try login and success 1`] = `
4+
"AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987
5+
"
6+
`;
7+
8+
exports[`Commands contest() contest id is given contest found, try login and success 2`] = `""`;
9+
310
exports[`Commands contest() contest id is given contest found, with id 1`] = `
411
"aic987 AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987
512
"
@@ -21,6 +28,13 @@ exports[`Commands contest() contest id is given contest not found 2`] = `
2128
"
2229
`;
2330

31+
exports[`Commands contest() contest id is given try login and fail, but (public) contest found 1`] = `
32+
"AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987
33+
"
34+
`;
35+
36+
exports[`Commands contest() contest id is given try login and fail, but (public) contest found 2`] = `""`;
37+
2438
exports[`Commands contest() contest id is not given find project json found, with id 1`] = `
2539
"aic987 AtCoder Imaginary Contest 987 https://atcoder.jp/contests/aic987
2640
"
@@ -72,3 +86,140 @@ not login
7286
`;
7387

7488
exports[`Commands session() not logged-in 2`] = `""`;
89+
90+
exports[`Commands task() contest id and task id are given contest found, try login and success 1`] = `
91+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
92+
"
93+
`;
94+
95+
exports[`Commands task() contest id and task id are given contest found, try login and success 2`] = `""`;
96+
97+
exports[`Commands task() contest id and task id are given contest not found 1`] = `""`;
98+
99+
exports[`Commands task() contest id and task id are given contest not found 2`] = `
100+
"task \\"aic987_z\\" of contest \\"aic987\\" not found.
101+
"
102+
`;
103+
104+
exports[`Commands task() contest id and task id are given task found, with id 1`] = `
105+
"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
106+
"
107+
`;
108+
109+
exports[`Commands task() contest id and task id are given task found, with id 2`] = `""`;
110+
111+
exports[`Commands task() contest id and task id are given task found, without id 1`] = `
112+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
113+
"
114+
`;
115+
116+
exports[`Commands task() contest id and task id are given task found, without id 2`] = `""`;
117+
118+
exports[`Commands task() contest id and task id are given try login and fail, but (public) task found 1`] = `
119+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
120+
"
121+
`;
122+
123+
exports[`Commands task() contest id and task id are given try login and fail, but (public) task found 2`] = `""`;
124+
125+
exports[`Commands task() contest id and task id are not given error occured in searching path 1`] = `""`;
126+
127+
exports[`Commands task() contest id and task id are not given error occured in searching path 2`] = `
128+
"something wrong
129+
"
130+
`;
131+
132+
exports[`Commands task() contest id and task id are not given task found, with id 1`] = `
133+
"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
134+
"
135+
`;
136+
137+
exports[`Commands task() contest id and task id are not given task found, with id 2`] = `""`;
138+
139+
exports[`Commands task() contest id and task id are not given task found, without id 1`] = `
140+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
141+
"
142+
`;
143+
144+
exports[`Commands task() contest id and task id are not given task found, without id 2`] = `""`;
145+
146+
exports[`Commands task() contest id and task id are not given task not found 1`] = `""`;
147+
148+
exports[`Commands task() contest id and task id are not given task not found 2`] = `
149+
"failed to find the task.
150+
"
151+
`;
152+
153+
exports[`Commands tasks() contest id and task id are given contest found, try login and success 1`] = `
154+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
155+
B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b
156+
C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c
157+
D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d
158+
"
159+
`;
160+
161+
exports[`Commands tasks() contest id and task id are given contest found, try login and success 2`] = `""`;
162+
163+
exports[`Commands tasks() contest id and task id are given contest not found 1`] = `""`;
164+
165+
exports[`Commands tasks() contest id and task id are given contest not found 2`] = `
166+
"contest \\"aic987\\" not found.
167+
"
168+
`;
169+
170+
exports[`Commands tasks() contest id and task id are given task found, with id 1`] = `
171+
"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
172+
aic987_b B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b
173+
aic987_c C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c
174+
aic987_d D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d
175+
"
176+
`;
177+
178+
exports[`Commands tasks() contest id and task id are given task found, with id 2`] = `""`;
179+
180+
exports[`Commands tasks() contest id and task id are given tasks found, without id 1`] = `
181+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
182+
B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b
183+
C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c
184+
D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d
185+
"
186+
`;
187+
188+
exports[`Commands tasks() contest id and task id are given tasks found, without id 2`] = `""`;
189+
190+
exports[`Commands tasks() contest id and task id are given try login and fail, but (public) task found 1`] = `
191+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
192+
B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b
193+
C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c
194+
D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d
195+
"
196+
`;
197+
198+
exports[`Commands tasks() contest id and task id are given try login and fail, but (public) task found 2`] = `""`;
199+
200+
exports[`Commands tasks() contest id and task id are not given tasks found, with id 1`] = `
201+
"aic987_a A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
202+
aic987_b B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b
203+
aic987_c C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c
204+
aic987_d D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d
205+
"
206+
`;
207+
208+
exports[`Commands tasks() contest id and task id are not given tasks found, with id 2`] = `""`;
209+
210+
exports[`Commands tasks() contest id and task id are not given tasks found, without id 1`] = `
211+
"A This is Problem https://atcoder.jp/contests/aic987/tasks/aic987_a
212+
B Next Problem https://atcoder.jp/contests/aic987/tasks/aic987_b
213+
C The Test Cases https://atcoder.jp/contests/aic987/tasks/aic987_c
214+
D Imaginary Problem https://atcoder.jp/contests/aic987/tasks/aic987_d
215+
"
216+
`;
217+
218+
exports[`Commands tasks() contest id and task id are not given tasks found, without id 2`] = `""`;
219+
220+
exports[`Commands tasks() contest id and task id are not given tasks not found 1`] = `""`;
221+
222+
exports[`Commands tasks() contest id and task id are not given tasks not found 2`] = `
223+
"contest.acc.json not found.
224+
"
225+
`;

tests/__tests__/commands.ts

Lines changed: 207 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { login, logout, session, contest } from "../../src/commands";
1+
import { login, logout, session, contest, task, tasks } from "../../src/commands";
22
import { AtCoder } from "../../src/atcoder";
33
import { getAtCoder } from "../../src/di";
4-
import { Contest, ContestProject, findProjectJSON, Task } from "../../src/project"
4+
import { Contest, ContestProject, findProjectJSON, Task, detectTaskByPath } from "../../src/project"
55
jest.mock("../../src/di");
66
jest.mock("../../src/project")
77

@@ -152,6 +152,30 @@ describe("Commands", () => {
152152
expect(atcoder.contest).toBeCalledTimes(1);
153153
expect(atcoder.contest).toBeCalledWith("aic987");
154154
});
155+
test("contest found, try login and success", async () => {
156+
atcoder.checkSession = jest.fn(async () => false);
157+
atcoder.login = jest.fn(async () => true);
158+
atcoder.contest = jest.fn(async _ => dummy_contest);
159+
await contest("aic987", {});
160+
expect(log).toMatchSnapshot();
161+
expect(elog).toMatchSnapshot();
162+
expect(findProjectJSON).toBeCalledTimes(0);
163+
expect(atcoder.login).toBeCalledTimes(1);
164+
expect(atcoder.contest).toBeCalledTimes(1);
165+
expect(atcoder.contest).toBeCalledWith("aic987");
166+
});
167+
test("try login and fail, but (public) contest found", async () => {
168+
atcoder.checkSession = jest.fn(async () => false);
169+
atcoder.login = jest.fn(async () => false);
170+
atcoder.contest = jest.fn(async _ => dummy_contest);
171+
await contest("aic987", {});
172+
expect(log).toMatchSnapshot();
173+
expect(elog).toMatchSnapshot();
174+
expect(findProjectJSON).toBeCalledTimes(0);
175+
expect(atcoder.login).toBeCalledTimes(1);
176+
expect(atcoder.contest).toBeCalledTimes(1);
177+
expect(atcoder.contest).toBeCalledWith("aic987");
178+
});
155179
test("contest not found", async () => {
156180
atcoder.checkSession = jest.fn(async () => true);
157181
atcoder.contest = jest.fn(async _ => {
@@ -166,4 +190,185 @@ describe("Commands", () => {
166190
});
167191
});
168192
});
193+
describe("task()", () => {
194+
describe("contest id and task id are not given", () => {
195+
test("task found, without id", async () => {
196+
// @ts-ignore: dynamically added method for test
197+
detectTaskByPath.mockResolvedValueOnce({ contest: dummy_contest, task: dummy_task01 });
198+
await task(undefined, undefined, {});
199+
expect(log).toMatchSnapshot();
200+
expect(elog).toMatchSnapshot();
201+
expect(detectTaskByPath).toBeCalledTimes(1);
202+
})
203+
test("task found, with id", async () => {
204+
// @ts-ignore: dynamically added method for test
205+
detectTaskByPath.mockResolvedValueOnce({ contest: dummy_contest, task: dummy_task01 });
206+
await task(undefined, undefined, { id: true });
207+
expect(log).toMatchSnapshot();
208+
expect(elog).toMatchSnapshot();
209+
expect(detectTaskByPath).toBeCalledTimes(1);
210+
})
211+
test("task not found", async () => {
212+
// @ts-ignore: dynamically added method for test
213+
detectTaskByPath.mockResolvedValueOnce({ contest: null, task: null });
214+
await task(undefined, undefined, {});
215+
expect(log).toMatchSnapshot();
216+
expect(elog).toMatchSnapshot();
217+
expect(detectTaskByPath).toBeCalledTimes(1);
218+
});
219+
test("error occured in searching path", async () => {
220+
// TODO: this path seems to be never reached
221+
// @ts-ignore: dynamically added method for test
222+
detectTaskByPath.mockRejectedValueOnce(new Error("something wrong"));
223+
await task(undefined, undefined, {});
224+
expect(log).toMatchSnapshot();
225+
expect(elog).toMatchSnapshot();
226+
expect(detectTaskByPath).toBeCalledTimes(1);
227+
});
228+
});
229+
describe("contest id and task id are given", () => {
230+
test("task found, without id", async () => {
231+
atcoder.checkSession = jest.fn(async () => true);
232+
atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01);
233+
await task("aic987", "aic987_a", {});
234+
expect(log).toMatchSnapshot();
235+
expect(elog).toMatchSnapshot();
236+
expect(detectTaskByPath).toBeCalledTimes(0);
237+
expect(atcoder.task).toBeCalledTimes(1);
238+
expect(atcoder.task).toBeCalledWith("aic987", "aic987_a");
239+
});
240+
test("task found, with id", async () => {
241+
atcoder.checkSession = jest.fn(async () => true);
242+
atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01);
243+
await task("aic987", "aic987_a", { id: true });
244+
expect(log).toMatchSnapshot();
245+
expect(elog).toMatchSnapshot();
246+
expect(detectTaskByPath).toBeCalledTimes(0);
247+
expect(atcoder.task).toBeCalledTimes(1);
248+
expect(atcoder.task).toBeCalledWith("aic987", "aic987_a");
249+
});
250+
test("contest found, try login and success", async () => {
251+
atcoder.checkSession = jest.fn(async () => false);
252+
atcoder.login = jest.fn(async () => true);
253+
atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01);
254+
await task("aic987", "aic987_a", {});
255+
expect(log).toMatchSnapshot();
256+
expect(elog).toMatchSnapshot();
257+
expect(detectTaskByPath).toBeCalledTimes(0);
258+
expect(atcoder.login).toBeCalledTimes(1);
259+
expect(atcoder.task).toBeCalledTimes(1);
260+
expect(atcoder.task).toBeCalledWith("aic987", "aic987_a");
261+
});
262+
test("try login and fail, but (public) task found", async () => {
263+
atcoder.checkSession = jest.fn(async () => false);
264+
atcoder.login = jest.fn(async () => false);
265+
atcoder.task = jest.fn(async (contest_id, task_id) => dummy_task01);
266+
await task("aic987", "aic987_a", {});
267+
expect(log).toMatchSnapshot();
268+
expect(elog).toMatchSnapshot();
269+
expect(detectTaskByPath).toBeCalledTimes(0);
270+
expect(atcoder.login).toBeCalledTimes(1);
271+
expect(atcoder.task).toBeCalledTimes(1);
272+
expect(atcoder.task).toBeCalledWith("aic987", "aic987_a");
273+
});
274+
test("contest not found", async () => {
275+
atcoder.checkSession = jest.fn(async () => true);
276+
atcoder.task = jest.fn(async _ => {
277+
throw new Error();
278+
});
279+
await task("aic987", "aic987_z", {});
280+
expect(log).toMatchSnapshot();
281+
expect(elog).toMatchSnapshot();
282+
expect(detectTaskByPath).toBeCalledTimes(0);
283+
expect(atcoder.task).toBeCalledTimes(1);
284+
expect(atcoder.task).toBeCalledWith("aic987", "aic987_z");
285+
});
286+
});
287+
});
288+
describe("tasks()", () => {
289+
describe("contest id and task id are not given", () => {
290+
test("tasks found, without id", async () => {
291+
// @ts-ignore: dynamically added method for test
292+
findProjectJSON.mockResolvedValueOnce({ path: "/dummy/path", data: dummy_contest_project });
293+
await tasks(undefined, {});
294+
expect(log).toMatchSnapshot();
295+
expect(elog).toMatchSnapshot();
296+
expect(findProjectJSON).toBeCalledTimes(1);
297+
})
298+
test("tasks found, with id", async () => {
299+
// @ts-ignore: dynamically added method for test
300+
findProjectJSON.mockResolvedValueOnce({ path: "/dummy/path", data: dummy_contest_project });
301+
await tasks(undefined, { id: true });
302+
expect(log).toMatchSnapshot();
303+
expect(elog).toMatchSnapshot();
304+
expect(findProjectJSON).toBeCalledTimes(1);
305+
})
306+
test("tasks not found", async () => {
307+
// @ts-ignore: dynamically added method for test
308+
findProjectJSON.mockRejectedValueOnce(new Error("contest.acc.json not found."));
309+
await tasks(undefined, {});
310+
expect(log).toMatchSnapshot();
311+
expect(elog).toMatchSnapshot();
312+
expect(findProjectJSON).toBeCalledTimes(1);
313+
});
314+
});
315+
describe("contest id and task id are given", () => {
316+
test("tasks found, without id", async () => {
317+
atcoder.checkSession = jest.fn(async () => true);
318+
atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]);
319+
await tasks("aic987", {});
320+
expect(log).toMatchSnapshot();
321+
expect(elog).toMatchSnapshot();
322+
expect(findProjectJSON).toBeCalledTimes(0);
323+
expect(atcoder.tasks).toBeCalledTimes(1);
324+
expect(atcoder.tasks).toBeCalledWith("aic987");
325+
});
326+
test("task found, with id", async () => {
327+
atcoder.checkSession = jest.fn(async () => true);
328+
atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]);
329+
await tasks("aic987", { id: true });
330+
expect(log).toMatchSnapshot();
331+
expect(elog).toMatchSnapshot();
332+
expect(findProjectJSON).toBeCalledTimes(0);
333+
expect(atcoder.tasks).toBeCalledTimes(1);
334+
expect(atcoder.tasks).toBeCalledWith("aic987");
335+
});
336+
test("contest found, try login and success", async () => {
337+
atcoder.checkSession = jest.fn(async () => false);
338+
atcoder.login = jest.fn(async () => true);
339+
atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]);
340+
await tasks("aic987", {});
341+
expect(log).toMatchSnapshot();
342+
expect(elog).toMatchSnapshot();
343+
expect(findProjectJSON).toBeCalledTimes(0);
344+
expect(atcoder.login).toBeCalledTimes(1);
345+
expect(atcoder.tasks).toBeCalledTimes(1);
346+
expect(atcoder.tasks).toBeCalledWith("aic987");
347+
});
348+
test("try login and fail, but (public) task found", async () => {
349+
atcoder.checkSession = jest.fn(async () => false);
350+
atcoder.login = jest.fn(async () => false);
351+
atcoder.tasks = jest.fn(async (contest_id) => [dummy_task01, dummy_task02, dummy_task03, dummy_task04]);
352+
await tasks("aic987", {});
353+
expect(log).toMatchSnapshot();
354+
expect(elog).toMatchSnapshot();
355+
expect(findProjectJSON).toBeCalledTimes(0);
356+
expect(atcoder.login).toBeCalledTimes(1);
357+
expect(atcoder.tasks).toBeCalledTimes(1);
358+
expect(atcoder.tasks).toBeCalledWith("aic987");
359+
});
360+
test("contest not found", async () => {
361+
atcoder.checkSession = jest.fn(async () => true);
362+
atcoder.tasks = jest.fn(async _ => {
363+
throw new Error();
364+
});
365+
await tasks("aic987", {});
366+
expect(log).toMatchSnapshot();
367+
expect(elog).toMatchSnapshot();
368+
expect(findProjectJSON).toBeCalledTimes(0);
369+
expect(atcoder.tasks).toBeCalledTimes(1);
370+
expect(atcoder.tasks).toBeCalledWith("aic987");
371+
});
372+
});
373+
});
169374
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy