generated from classroom-resources/autograding-example-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.test.js
187 lines (138 loc) · 6.02 KB
/
todo.test.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
const { EOL } = require("os");
const fs = require("fs");
const { execSync, exec } = require("child_process");
let ddMmYyyy = () => {
const o_date = new Intl.DateTimeFormat();
const f_date = (m_ca, m_it) => Object({ ...m_ca, [m_it.type]: m_it.value });
const { day, month, year } = o_date.formatToParts().reduce(f_date, {});
return `${day}/${month}/${year}`;
};
let deleteFile = (path) => {
try {
fs.unlinkSync(path);
} catch (err) {}
};
beforeEach(() => {
deleteFile(`${__dirname}/todo.txt`);
deleteFile(`${__dirname}/done.txt`);
});
let todoTxtCli = (...args) => ["node", `${__dirname}/todo.js`, ...args].join(" ");
let usage = `Usage :-
$ node todo.js add "todo item" # Add a new todo
$ node todo.js ls # Show remaining todos
$ node todo.js del NUMBER # Delete a todo
$ node todo.js done NUMBER # Complete a todo
$ node todo.js help # Show usage
$ node todo.js report # Statistics
`;
test("prints help when no additional args are provided", () => {
let received = execSync(todoTxtCli()).toString("utf8");
expect(received).toBe(usage);
});
test("prints help", () => {
let received = execSync(todoTxtCli("help")).toString("utf8");
expect(received).toBe(usage);
});
test("add a single todo", () => {
let expected = `Added todo: "the thing i need to do"${EOL}`;
let received = execSync(todoTxtCli("add", '"the thing i need to do"')).toString("utf8");
expect(received).toBe(expected);
});
test("show error message when add is not followed by a todo", () => {
let expected = "Error: Missing todo string. Nothing added!" + EOL;
let received = execSync(todoTxtCli("add")).toString("utf8");
expect(received).toBe(expected);
});
test("add multiple todos", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo, i) => {
let expected = `Added todo: "${todo}"${EOL}`;
let received = execSync(todoTxtCli("add", `"${todo}"`)).toString("utf8");
expect(received).toBe(expected);
});
});
test("list todos in reverse order (added latest first)", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = `[3] find needle in the haystack
[2] water the plants
[1] the thing i need to do
`;
let received = execSync(todoTxtCli("ls")).toString("utf8");
expect(received).toBe(expected);
});
test("list when there are no remaining todos", () => {
let expected = `There are no pending todos!`;
let received = execSync(todoTxtCli("ls")).toString("utf8");
expect(received).toBe(expected);
});
test("delete a todo", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = `Deleted todo #2${EOL}`;
let received = execSync(todoTxtCli("del", "2")).toString("utf8");
expect(received).toBe(expected);
});
test("delete todos numbered 3, 2 & 1", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
[3, 2, 1].forEach((n) => {
let expected = `Deleted todo #${n}${EOL}`;
let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");
expect(received).toBe(expected);
});
});
test("delete first todo item 3 times", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
[1, 1, 1].forEach((n) => {
let expected = `Deleted todo #${n}${EOL}`;
let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");
expect(received).toBe(expected);
});
});
test("delete non-existent todos", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
[0, 4, 5].forEach((n) => {
let expected = `Error: todo #${n} does not exist. Nothing deleted.${EOL}`;
let received = execSync(todoTxtCli("del", n.toString())).toString("utf8");
expect(received).toBe(expected);
});
});
test("delete does not have enough arguments", () => {
let expected = `Error: Missing NUMBER for deleting todo.${EOL}`;
let received = execSync(todoTxtCli("del")).toString("utf8");
expect(received).toBe(expected);
});
test("mark a todo as done", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = `Marked todo #2 as done.${EOL}`;
let received = execSync(todoTxtCli("done", "2")).toString("utf8");
expect(received).toBe(expected);
});
test("mark as done a todo which does not exist", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = `Error: todo #0 does not exist.${EOL}`;
let received = execSync(todoTxtCli("done", "0")).toString("utf8");
expect(received).toBe(expected);
});
test("mark as done without providing a todo number", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
let expected = `Error: Missing NUMBER for marking todo as done.${EOL}`;
let received = execSync(todoTxtCli("done")).toString("utf8");
expect(received).toBe(expected);
});
test("report pending & completed todos", () => {
let todos = ["the thing i need to do", "water the plants", "find needle in the haystack"];
todos.forEach((todo) => execSync(todoTxtCli("add", `"${todo}"`)));
execSync(todoTxtCli("done", "1"));
execSync(todoTxtCli("done", "2"));
let date = ddMmYyyy();
let expected = `${date} Pending : 1 Completed : 2${EOL}`;
let received = execSync(todoTxtCli("report")).toString("utf8");
expect(received).toBe(expected);
});