Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LAB2] b122521 #111

Merged
merged 4 commits into from
Mar 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 86 additions & 1 deletion lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,91 @@
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
// 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 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();
assert.strictEqual(application.selectNextPerson(), 'John');
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);
}).then(() => {
fs.unlinkSync('name_list.txt');
});
Loading