Skip to content

Commit a2cf440

Browse files
authored
Update main_test.js
1 parent 775a3ab commit a2cf440

File tree

1 file changed

+51
-53
lines changed

1 file changed

+51
-53
lines changed

lab2/main_test.js

+51-53
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,68 @@
11
const test = require('node:test');
22
const assert = require('assert');
33
const fs = require('fs');
4-
const { Application, MailSystem } = require('./main');
5-
6-
// Mock fs.readFile
74
test.mock.method(fs, 'readFile', (file, options, callback) => {
8-
callback(null, 'Guan\nChen\nGala');
5+
callback(null, 'Guan\nChen\nGala');
96
});
7+
const { Application, MailSystem } = require('./main');
108

11-
test("MailSystem write", () => {
12-
const mailSystem = new MailSystem();
13-
assert.strictEqual(mailSystem.write("Guan"), "Congrats, Guan!");
9+
// TODO: write your tests here
10+
// Remember to use Stub, Mock, and Spy when necessary
11+
test("Test MailSystem's write", () => {
12+
const myClass = new MailSystem()
13+
assert.strictEqual( myClass.write("Guan"),"Congrats, Guan!")
1414
});
1515

16-
test("MailSystem send", () => {
17-
const mailSystem = new MailSystem();
18-
Math.random = () => 0.6;
19-
const context = mailSystem.write("Guan");
20-
assert.strictEqual(mailSystem.send("Guan", context), true);
21-
Math.random = () => 0.5;
22-
assert.strictEqual(mailSystem.send("Chen", context), false);
16+
test("Test MailSystem's send(name, context)", () => {
17+
Math.random = () => 0.6
18+
const mailSystem = new MailSystem()
19+
const context = mailSystem.write("Guan")
20+
const success = mailSystem.send("Guan",context)
21+
assert.strictEqual(success,true)
22+
Math.random = () => 0.5
23+
assert.strictEqual(mailSystem.send("Chen",context),false)
2324
});
2425

25-
test("Application getNames", async () => {
26-
const application = new Application();
27-
const [people, selected] = await application.getNames();
28-
assert.deepStrictEqual(people, ["Guan", "Chen", "Gala"]);
29-
assert.deepStrictEqual(selected, []);
26+
test("Test Application's getNames", async () => {
27+
const application = new Application
28+
const [people, selected] = await application.getNames()
29+
assert.deepStrictEqual(people, ["Guan", "Chen", "Gala"])
30+
assert.deepStrictEqual(selected, [])
3031
});
3132

32-
test("Application getRandomPerson", () => {
33-
const application = new Application();
34-
application.people = ["Guan", "Chen", "Gala"];
35-
Math.random = () => 0;
36-
assert.deepStrictEqual(application.getRandomPerson(), "Guan");
37-
Math.random = () => 0.6;
38-
assert.deepStrictEqual(application.getRandomPerson(), "Chen");
39-
Math.random = () => 0.9;
40-
assert.deepStrictEqual(application.getRandomPerson(), "Gala");
33+
test("Test Application's getRandomPerson()", (t) => {
34+
const application = new Application
35+
application.people = ["Guan", "Chen", "Gala"]
36+
Math.random = () => 0
37+
assert.deepStrictEqual(application.getRandomPerson(), "Guan");
38+
Math.random = () => 0.6
39+
assert.deepStrictEqual(application.getRandomPerson(), "Chen");
40+
Math.random = () => 0.9
41+
assert.deepStrictEqual(application.getRandomPerson(), "Gala");
4142
});
4243

43-
test("Application selectNextPerson", async () => {
44-
const application = new Application();
45-
await application.getNames();
46-
application.selected = ["Guan"];
47-
let count = 1;
48-
test.mock.method(application, 'getRandomPerson', () => {
49-
const name_list = application.people;
50-
if (count < name_list.length) {
51-
return name_list[count++];
52-
} else {
53-
return null;
54-
}
55-
});
56-
assert.strictEqual(application.selectNextPerson(), "Chen");
57-
assert.strictEqual(application.selectNextPerson(), "Gala");
58-
assert.strictEqual(application.selectNextPerson(), null);
44+
test("Test Application's selectNextPerson", async() => {
45+
const application = new Application
46+
const name_list = await application.getNames();
47+
application.selected = ["Guan"]
48+
let count = 0;
49+
test.mock.method(application, 'getRandomPerson', () => {
50+
if (count <= name_list.length) {
51+
return name_list[0][count++];
52+
}
53+
});
54+
assert.strictEqual(application.selectNextPerson(), "Chen")
55+
assert.strictEqual(application.selectNextPerson(), "Gala")
56+
assert.strictEqual(application.selectNextPerson(), null)
5957
});
6058

61-
test("Application notifySelected", () => {
62-
const application = new Application();
63-
application.people = ["Guan", "Chen", "Gala"];
64-
application.selected = ["Guan", "Chen", "Gala"];
65-
application.mailSystem.write = test.mock.fn(application.mailSystem.write);
66-
application.mailSystem.send = test.mock.fn(application.mailSystem.send);
67-
application.notifySelected();
68-
assert.strictEqual(application.mailSystem.send.mock.calls.length, application.people.length);
69-
assert.strictEqual(application.mailSystem.write.mock.calls.length, application.people.length);
59+
test("Test Application's notifySelected() ", () => {
60+
const application = new Application
61+
application.people = ["Guan", "Chen", "Gala"]
62+
application.selected = ["Guan", "Chen", "Gala"]
63+
application.mailSystem.write = test.mock.fn(application.mailSystem.write)
64+
application.mailSystem.send = test.mock.fn(application.mailSystem.send)
65+
application.notifySelected()
66+
assert.strictEqual(application.mailSystem.send.mock.calls.length, application.people.length);
67+
assert.strictEqual(application.mailSystem.write.mock.calls.length, application.people.length);
7068
});

0 commit comments

Comments
 (0)