Skip to content

Commit e01b1c9

Browse files
authored
Merge pull request SQLab#142 from Highwaist/511558025
[LAB2] 511558025
2 parents a3380b2 + fcf7db7 commit e01b1c9

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

lab2/main_test.js

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,64 @@
1-
const test = require('node:test');
1+
const testUtil = require('node:test');
22
const assert = require('assert');
3+
const fs = require('fs');
4+
5+
// Mock the file containing names
6+
testUtil.mock.method(fs, 'readFile', (file, options, callback) => {
7+
callback(null, 'Alice\nBob\ncat');
8+
});
9+
310
const { Application, MailSystem } = require('./main');
411

5-
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
12+
testUtil('MailSystem_write()', () => {
13+
const mailSystem = new MailSystem();
14+
assert.strictEqual(mailSystem.write('Alice'), 'Congrats, Alice!');
15+
assert.strictEqual(mailSystem.write(202), 'Congrats, 202!');
16+
assert.strictEqual(mailSystem.write(null), 'Congrats, null!');
17+
});
18+
19+
testUtil('MailSystem_send()', () => {
20+
const mailSystem = new MailSystem();
21+
const name = 'Alice';
22+
testUtil.mock.method(Math, 'random', () => 0.9);
23+
assert.strictEqual(mailSystem.send(name, 'success'), true);
24+
testUtil.mock.method(Math, 'random', () => 0.2);
25+
assert.strictEqual(mailSystem.send(name, 'fail'), false);
26+
});
27+
28+
testUtil('Application_getNames()', async () => {
29+
const app = new Application();
30+
const nameList = ['Alice', 'Bob', 'cat'];
31+
const [names, selected] = await app.getNames();
32+
assert.deepStrictEqual(names, nameList);
33+
assert.deepStrictEqual(selected, []);
34+
});
35+
36+
testUtil('Application_getRandomPerson()', async () => {
37+
const app = new Application();
38+
const [names] = await app.getNames();
39+
const randomPerson = app.getRandomPerson();
40+
});
41+
42+
testUtil('Application_selectNextPerson()', async () => {
43+
const app = new Application();
44+
const [names] = await app.getNames();
45+
app.selected = ['Alice'];
46+
let count = 0;
47+
testUtil.mock.method(app, 'getRandomPerson', () => names[count++]);
48+
assert.strictEqual(app.selectNextPerson(), 'Bob');
49+
assert.deepStrictEqual(app.selected, ['Alice', 'Bob']);
50+
assert.strictEqual(app.selectNextPerson(), 'cat');
51+
assert.deepStrictEqual(app.selected, ['Alice', 'Bob', 'cat']);
52+
assert.strictEqual(app.selectNextPerson(), null);
53+
});
54+
55+
testUtil('Application_notifySelected()', async () => {
56+
const app = new Application();
57+
const [names] = await app.getNames();
58+
app.selected = names.slice(); // Select all names initially
59+
app.mailSystem.send = testUtil.mock.fn(app.mailSystem.send);
60+
app.mailSystem.write = testUtil.mock.fn(app.mailSystem.write);
61+
app.notifySelected();
62+
assert.strictEqual(app.mailSystem.send.mock.calls.length, names.length);
63+
assert.strictEqual(app.mailSystem.write.mock.calls.length, names.length);
64+
});

0 commit comments

Comments
 (0)