From f08b2788af72da51cbe9e02839e15da800b04584 Mon Sep 17 00:00:00 2001 From: zbtion Date: Sat, 9 Mar 2024 23:48:50 +0800 Subject: [PATCH 1/2] mod: lab2/main_test.js new: name_list.txt --- lab2/main_test.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++- name_list.txt | 2 ++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 name_list.txt diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..f3238e5f 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,83 @@ const test = require('node:test'); +const fs = require('fs'); const assert = require('assert'); const { Application, MailSystem } = require('./main'); // TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary \ No newline at end of file +// Remember to use Stub, Mock, and Spy when necessary + +// create name_list.txt for testing +fs.writeFileSync('name_list.txt', 'John\nJane'); + +test('MailSystem write function', () => { + const mailSystem = new MailSystem(); + assert.strictEqual(mailSystem.write('John'), 'Congrats, John!'); + assert.strictEqual(mailSystem.write(123), 'Congrats, 123!'); + assert.strictEqual(mailSystem.write(''), 'Congrats, !'); + assert.strictEqual(mailSystem.write(), 'Congrats, undefined!'); + assert.strictEqual(mailSystem.write(null), 'Congrats, null!'); + assert.strictEqual(mailSystem.write(true), 'Congrats, true!'); +}); + +test('MailSystem send function', (t) => { + // mock Math.random to control the return value + const mailSystem = new MailSystem(); + const mock_random = t.mock.method(Math, 'random'); + mock_random.mock.mockImplementation(() => 0.6); + assert.strictEqual(mailSystem.send('John', 'Congrats, John!'), true); + mock_random.mock.mockImplementation(() => 0.4); + assert.strictEqual(mailSystem.send('John', 'Congrats, John!'), false); +}); + + +test('Application constructor', () => { + const application = new Application(); + assert.strictEqual(application.people.length, 0); + assert.strictEqual(application.selected.length, 0); + assert.strictEqual(application.mailSystem instanceof MailSystem, true); + assert.strictEqual(application.getNames() instanceof Promise, true); +}); + +test('Application getNames function', async () => { + const application = new Application(); + const [people, select] = await application.getNames(); + assert.deepStrictEqual(people, ['John', 'Jane']); + assert.deepStrictEqual(select, []); +}); + +test('Application getRandomPerson function', async (t) => { + // mock Math.floor to control the return value + const mock_floor = t.mock.method(Math, 'floor'); + const application = new Application(); + await application.getNames(); + mock_floor.mock.mockImplementation(() => 0); + assert.strictEqual(application.getRandomPerson(), 'John'); + mock_floor.mock.mockImplementation(() => 1); + assert.strictEqual(application.getRandomPerson(), 'Jane'); + mock_floor.mock.mockImplementation(() => 2); + assert.strictEqual(application.getRandomPerson(), undefined); +}); + +test('Application selectNextPerson function', async (t) => { + // mock Math.floor to control the return value + const mock_floor = t.mock.method(Math, 'floor'); + const application = new Application(); + await application.getNames(); + mock_floor.mock.mockImplementation(() => 0); + assert.strictEqual(application.selectNextPerson(), 'John'); + mock_floor.mock.mockImplementation(() => 1); + assert.strictEqual(application.selectNextPerson(), 'Jane'); + assert.strictEqual(application.selectNextPerson(), null); +}); + +test('Application notifySelected function', (t) => { + // spy on send and write functions + const application = new Application(); + application.selected = ['John', 'Jane']; + const spy_send = t.mock.method(application.mailSystem, 'send'); + const spy_write = t.mock.method(application.mailSystem, 'write'); + application.notifySelected(); + assert.strictEqual(spy_send.mock.calls.length, 2); + assert.strictEqual(spy_write.mock.calls.length, 2); +}); + diff --git a/name_list.txt b/name_list.txt new file mode 100644 index 00000000..4f6ee157 --- /dev/null +++ b/name_list.txt @@ -0,0 +1,2 @@ +John +Jane \ No newline at end of file From 8aeb8f814c6798ca9e572e540853261f86642f08 Mon Sep 17 00:00:00 2001 From: zbtion Date: Sun, 10 Mar 2024 01:00:44 +0800 Subject: [PATCH 2/2] mod: lab2/main_test.js del: name_list.txt --- lab2/main_test.js | 20 ++++++++++++++------ name_list.txt | 2 -- 2 files changed, 14 insertions(+), 8 deletions(-) delete mode 100644 name_list.txt diff --git a/lab2/main_test.js b/lab2/main_test.js index f3238e5f..e11f63be 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -59,13 +59,20 @@ test('Application getRandomPerson function', async (t) => { }); test('Application selectNextPerson function', async (t) => { - // mock Math.floor to control the return value - const mock_floor = t.mock.method(Math, 'floor'); + // mock getRandomPerson to control the return value const application = new Application(); + application.count = 0; + const mock_getRandomPerson = t.mock.method(application, 'getRandomPerson'); + mock_getRandomPerson.mock.mockImplementation(() => { + if (application.count !== 2) { + application.count++; + return 'John'; + } else { + return 'Jane'; + } + }); await application.getNames(); - mock_floor.mock.mockImplementation(() => 0); assert.strictEqual(application.selectNextPerson(), 'John'); - mock_floor.mock.mockImplementation(() => 1); assert.strictEqual(application.selectNextPerson(), 'Jane'); assert.strictEqual(application.selectNextPerson(), null); }); @@ -79,5 +86,6 @@ test('Application notifySelected function', (t) => { application.notifySelected(); assert.strictEqual(spy_send.mock.calls.length, 2); assert.strictEqual(spy_write.mock.calls.length, 2); -}); - +}).then(() => { + fs.unlinkSync('name_list.txt'); +}); \ No newline at end of file diff --git a/name_list.txt b/name_list.txt deleted file mode 100644 index 4f6ee157..00000000 --- a/name_list.txt +++ /dev/null @@ -1,2 +0,0 @@ -John -Jane \ No newline at end of file