|
1 | 1 | const test = require('node:test');
|
2 | 2 | const assert = require('assert');
|
3 | 3 | const fs = require('fs');
|
4 |
| -const { Application, MailSystem } = require('./main'); |
5 |
| - |
6 |
| -// Mock fs.readFile |
7 | 4 | test.mock.method(fs, 'readFile', (file, options, callback) => {
|
8 |
| - callback(null, 'Guan\nChen\nGala'); |
| 5 | + callback(null, 'Guan\nChen\nGala'); |
9 | 6 | });
|
| 7 | +const { Application, MailSystem } = require('./main'); |
10 | 8 |
|
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!") |
14 | 14 | });
|
15 | 15 |
|
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) |
23 | 24 | });
|
24 | 25 |
|
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, []) |
30 | 31 | });
|
31 | 32 |
|
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"); |
41 | 42 | });
|
42 | 43 |
|
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) |
59 | 57 | });
|
60 | 58 |
|
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); |
70 | 68 | });
|
0 commit comments