From 0efda56ad03d3dea0cb7bf767ec90b57c3f84a49 Mon Sep 17 00:00:00 2001 From: forbis <32932288+forbis@users.noreply.github.com> Date: Mon, 18 Mar 2024 14:07:58 +0800 Subject: [PATCH] Update main_test.js --- lab2/main_test.js | 63 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 2 deletions(-) diff --git a/lab2/main_test.js b/lab2/main_test.js index 38c11eb6..b2c9b0f3 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -6,5 +6,64 @@ test.mock.method(fs, 'readFile', (file, options, callback) => { }); const { Application, MailSystem } = require('./main'); -// TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary +test('Test MailSystem : write()', () => { + const ms = new MailSystem(); + assert.strictEqual(ms.write('alpha'), 'Congrats, alpha!'); + assert.strictEqual(ms.write(null), 'Congrats, null!'); + assert.strictEqual(ms.write(48763), 'Congrats, 48763!'); +}); + +test('Test MailSystem : send()', () => { + const ms = new MailSystem(); + const name = 'alpha'; + test.mock.method(Math, 'random', () => 0.6); + assert.strictEqual(ms.send(name, 'success'), true); + test.mock.method(Math, 'random', () => 0.4); + assert.strictEqual(ms.send(name, 'fail'), false); +}); + +test('Test Application : getNames()', async () => { + const app = new Application(); + const name_list = ['alpha', 'beta', 'gama']; + const names = await app.getNames(); + assert.deepStrictEqual(names, [name_list, []]) +}); + +test('Test Application : getRandomPerson()', async (test) => { + const app = new Application(); + const names = await app.getNames(); + test.mock.method(Math, 'random', () => 0); + assert.strictEqual(app.getRandomPerson(), 'alpha'); + test.mock.method(Math, 'random', () => 0.4); + assert.strictEqual(app.getRandomPerson(), 'beta'); + test.mock.method(Math, 'random', () => 0.7); + assert.strictEqual(app.getRandomPerson(), 'gama'); +}); + +test('Test Application : selectNextPerson()', async (test) => { + const app = new Application(); + const names = await app.getNames(); + app.selected = ['alpha']; + let cnt = 0; + test.mock.method(app, 'getRandomPerson', () => { + if (cnt <= names.length) { + return names[0][cnt++]; + } + }); + assert.strictEqual(app.selectNextPerson(), 'beta'); + assert.deepStrictEqual(app.selected, ['alpha', 'beta']); + assert.strictEqual(app.selectNextPerson(), 'gama'); + assert.deepStrictEqual(app.selected, ['alpha', 'beta', 'gama']); + assert.strictEqual(app.selectNextPerson(), null); +}); + +test('Test Application : notifySelected()', async (test) => { + const app = new Application(); + app.people = ['alpha', 'beta', 'gama']; + app.selected = ['alpha', 'beta', 'gama']; + app.mailSystem.send = test.mock.fn(app.mailSystem.send); + app.mailSystem.write = test.mock.fn(app.mailSystem.write); + app.notifySelected(); + assert.strictEqual(app.mailSystem.send.mock.calls.length, 3); + assert.strictEqual(app.mailSystem.write.mock.calls.length, 3); +});