Skip to content

Commit 44d657c

Browse files
authored
Update main_test.js
1 parent 617f910 commit 44d657c

File tree

1 file changed

+153
-1
lines changed

1 file changed

+153
-1
lines changed

lab2/main_test.js

+153-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,158 @@
11
const test = require('node:test');
22
const assert = require('assert');
3+
const fs = require('fs');
4+
const util = require('util');
5+
const writeFile = util.promisify(fs.writeFile);
36
const { Application, MailSystem } = require('./main');
7+
const path = require('node:path');
48

59
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
10+
// Remember to use Stub, Mock, and Spy when necessary
11+
// Remember to use Stub, Mock, and Spy when necessary
12+
13+
test("Test MailSystem's write", () => {
14+
const ms = new MailSystem();
15+
const context = ms.write("Jerry");
16+
assert.strictEqual(context, "Congrats, Jerry!");
17+
});
18+
19+
test("Test MailSystem's send", () => {
20+
const ms = new MailSystem();
21+
const orgrdm = Math.random;
22+
Math.random = () => 0.4;
23+
let context = ms.send("Jerry", "Test Message");
24+
assert.strictEqual(context, false);
25+
26+
Math.random = () => 0.6;
27+
context = ms.send("Jerry", "Test Message");
28+
assert.strictEqual(context, true);
29+
30+
Math.random = orgrdm;
31+
});
32+
33+
test("Test Application's getNames", async () => {
34+
const fn_ = path.join(process.cwd(), 'name_list.txt');
35+
const data_ = "JJ\nEE\nRR\nYY";
36+
await writeFile(fn_, data_, "utf-8");
37+
38+
const app = new Application();
39+
40+
let ctx = new Array([],[]);
41+
ctx = await app.getNames();
42+
assert.deepStrictEqual(ctx[0], ["JJ", "EE", "RR", "YY"]);
43+
assert.deepStrictEqual(ctx[1], []);
44+
45+
fs.unlinkSync(fn_);
46+
});
47+
48+
test("Test Application's getRandomPerson", async () => {
49+
const fn_ = path.join(process.cwd(), 'name_list.txt');
50+
const data_ = "JJ\nEE";
51+
await writeFile(fn_, data_, "utf-8");
52+
53+
const app = new Application();
54+
55+
let ctx = new Array([],[]);
56+
ctx = await app.getNames();
57+
assert.deepStrictEqual(ctx[0], ["JJ", "EE"]);
58+
assert.deepStrictEqual(ctx[1], []);
59+
60+
let rdmPeople = await app.getRandomPerson();
61+
assert.ok(app.people.includes(rdmPeople));
62+
63+
rdmPeople = await app.getRandomPerson();
64+
assert.ok(app.people.includes(rdmPeople));
65+
66+
fs.unlinkSync(fn_);
67+
});
68+
69+
test("Test Application's selectNextPerson", async () => {
70+
const fn_ = path.join(process.cwd(), 'name_list.txt');
71+
const data_ = "JJ\nEE";
72+
await writeFile(fn_, data_, "utf-8");
73+
74+
const app = new Application();
75+
let ctx = new Array([],[]);
76+
ctx = await app.getNames();
77+
assert.deepStrictEqual(ctx[0], ["JJ", "EE"]);
78+
assert.deepStrictEqual(ctx[1], []);
79+
80+
const orgsnp = app.getRandomPerson;
81+
app.getRandomPerson = () => "JJ";
82+
let rdperson = app.selectNextPerson();
83+
assert.strictEqual(rdperson, "JJ");
84+
assert.ok(app.people.includes(rdperson));
85+
assert.ok(app.selected.includes(rdperson));
86+
assert.deepStrictEqual(app.selected, ["JJ"]);
87+
assert.strictEqual(app.selected.length, 1);
88+
89+
app.getRandomPerson = () => "EE";
90+
rdperson = app.selectNextPerson();
91+
assert.strictEqual(rdperson, "EE");
92+
assert.ok(app.people.includes(rdperson));
93+
assert.ok(app.selected.includes(rdperson));
94+
assert.deepStrictEqual(app.selected, ["JJ","EE"]);
95+
assert.strictEqual(app.selected.length, 2);
96+
97+
app.getRandomPerson = orgsnp;
98+
rdperson = app.selectNextPerson();
99+
assert.strictEqual(rdperson, null);
100+
assert.strictEqual(rdperson, null);
101+
assert.strictEqual(app.selected.length, 2);
102+
103+
let cnt = 0;
104+
app.getRandomPerson = () => {
105+
if (cnt++ % 2 === 0) {
106+
return 'JJ';
107+
} else {
108+
return 'EE';
109+
}
110+
}
111+
app.selected = ['JJ'];
112+
rdperson = app.selectNextPerson();
113+
assert.strictEqual(rdperson, 'EE');
114+
assert.deepStrictEqual(app.selected, ['JJ', 'EE']);
115+
fs.unlinkSync(fn_);
116+
});
117+
118+
test("Test Application's notifySelected", async () => {
119+
120+
const fn_ = 'name_list.txt';
121+
const data_ = "JJ\nEE";
122+
await writeFile(fn_, data_, "utf-8");
123+
124+
const app = new Application();
125+
let ctx = new Array([],[]);
126+
ctx = await app.getNames();
127+
assert.deepStrictEqual(ctx[0], ["JJ", "EE"]);
128+
assert.deepStrictEqual(ctx[1], []);
129+
130+
app.getRandomPerson = () => "JJ";
131+
let person = app.selectNextPerson();
132+
assert.strictEqual(person, "JJ");
133+
assert.deepStrictEqual(app.selected, ["JJ"]);
134+
135+
app.getRandomPerson = () => "EE";
136+
person = app.selectNextPerson();
137+
assert.strictEqual(person, "EE");
138+
assert.deepStrictEqual(app.selected, ["JJ", "EE"]);
139+
140+
test.mock.method(app.mailSystem, 'write', (name) => "Congrats, " + name + "!");
141+
test.mock.method(app.mailSystem, 'send', () => true);
142+
143+
app.notifySelected();
144+
145+
assert.strictEqual(app.mailSystem.write.mock.calls.length, 2);
146+
assert.strictEqual(app.mailSystem.send.mock.calls.length, 2);
147+
148+
let call = app.mailSystem.write.mock.calls[0];
149+
assert.deepStrictEqual(call.arguments, ["JJ"]);
150+
assert.deepStrictEqual(call.result, "Congrats, JJ!");
151+
assert.deepStrictEqual(call.error, undefined);
152+
153+
call = app.mailSystem.write.mock.calls[1];
154+
assert.deepStrictEqual(call.arguments, ["EE"]);
155+
assert.deepStrictEqual(call.result, "Congrats, EE!");
156+
assert.deepStrictEqual(call.error, undefined);
157+
fs.unlinkSync(fn_);
158+
});

0 commit comments

Comments
 (0)