|
1 | 1 | const test = require('node:test'); |
2 | 2 | const assert = require('assert'); |
3 | | -const fs = require('fs'); |
4 | | -test.mock.method(fs, 'readFile', (file, options, callback) => { |
5 | | - callback(null, 'Guan\nChen\nGala'); |
6 | | -}); |
7 | 3 | const { Application, MailSystem } = require('./main'); |
8 | 4 |
|
9 | 5 | // TODO: write your tests here |
10 | 6 | // 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!") |
| 7 | +test.mock.method(fs, 'readFile', (file, options, callback) => { |
| 8 | + callback(null, 'Guan\nChen\nGala'); |
| 9 | +}); |
| 10 | + |
| 11 | +test("MailSystem write", () => { |
| 12 | + const mailSystem = new MailSystem(); |
| 13 | + assert.strictEqual(mailSystem.write("Guan"), "Congrats, Guan!"); |
14 | 14 | }); |
15 | 15 |
|
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) |
| 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); |
24 | 23 | }); |
25 | 24 |
|
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, []) |
| 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, []); |
31 | 30 | }); |
32 | 31 |
|
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"); |
| 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"); |
42 | 41 | }); |
43 | 42 |
|
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) |
| 43 | +test("Application selectNextPerson", async () => { |
| 44 | + const application = new Application(); |
| 45 | + await application.getNames(); |
| 46 | + application.selected = ["Guan"]; |
| 47 | + let count = 0; |
| 48 | + test.mock.method(application, 'getRandomPerson', () => { |
| 49 | + const name_list = application.people; |
| 50 | + if (count <= name_list.length) { |
| 51 | + return name_list[count++]; |
| 52 | + } |
| 53 | + }); |
| 54 | + assert.strictEqual(application.selectNextPerson(), "Chen"); |
| 55 | + assert.strictEqual(application.selectNextPerson(), "Gala"); |
| 56 | + assert.strictEqual(application.selectNextPerson(), null); |
57 | 57 | }); |
58 | 58 |
|
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); |
| 59 | +test("Application 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); |
68 | 68 | }); |
0 commit comments