Skip to content

[LAB2] 511558013 #68

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

Merged
merged 1 commit 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
63 changes: 61 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Loading