Skip to content

Commit 460acdf

Browse files
authored
Merge pull request #358 from soymilk05/512559005
[LAB2] 512559005
2 parents 70a99ee + 8b316c0 commit 460acdf

File tree

2 files changed

+65
-4
lines changed

2 files changed

+65
-4
lines changed

lab2/main_test.js

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,68 @@
1-
const test = require('node:test');
1+
const {test,mock} = require('node:test');
22
const assert = require('assert');
3+
const fs = require('fs')
4+
mock.method(fs, 'readFile', (file, options, callback) => {
5+
callback(null, 'sam\njack\njim');
6+
});
37
const { Application, MailSystem } = require('./main');
48

5-
// TODO: write your tests here
6-
// Remember to use Stub, Mock, and Spy when necessary
9+
10+
11+
test('write', () => {
12+
const mailSystem = new MailSystem()
13+
assert.strictEqual(mailSystem.write('sam'),'Congrats, sam!')
14+
assert.strictEqual(mailSystem.write(123),'Congrats, 123!')
15+
assert.strictEqual(mailSystem.write(null),'Congrats, null!')
16+
17+
});
18+
19+
20+
test('send',()=>{
21+
const mailSystem = new MailSystem()
22+
mock.method(Math, 'random', () => 0.9);
23+
assert.strictEqual(mailSystem.send('sam','hello world'),true)
24+
mock.method(Math, 'random', () => 0.1);
25+
assert.strictEqual(mailSystem.send('sam','hello world'),false)
26+
})
27+
28+
29+
test('getNames', async () => {
30+
const app = new Application();
31+
const names = await app.getNames();
32+
assert.deepStrictEqual(names, [['sam', 'jack','jim'],[]]);
33+
});
34+
35+
36+
test('getRandomPerson',async ()=>{
37+
const app = new Application();
38+
await app.getNames();
39+
mock.method(Math,'random',()=>0.5)
40+
const person = app.getRandomPerson()
41+
assert.strictEqual(person,app.people[Math.floor(0.5*app.people.length)])
42+
})
43+
44+
45+
46+
test('selectNextPerson', async ()=>{
47+
const app = new Application();
48+
await app.getNames();
49+
app.selected = ['sam']
50+
let i = 0
51+
mock.method(app, 'getRandomPerson', () => app.people[i++]);
52+
assert.strictEqual(app.selectNextPerson(),'jack')
53+
assert.strictEqual(app.selectNextPerson(),'jim')
54+
assert.strictEqual(app.selectNextPerson(),null)
55+
})
56+
57+
58+
59+
test('notifySelected',async ()=>{
60+
const app = new Application();
61+
const [names] = await app.getNames();
62+
app.selected = names.slice();
63+
app.mailSystem.send = mock.fn(app.mailSystem.send);
64+
app.mailSystem.write = mock.fn(app.mailSystem.write);
65+
app.notifySelected();
66+
assert.strictEqual(app.mailSystem.send.mock.calls.length, names.length);
67+
assert.strictEqual(app.mailSystem.write.mock.calls.length, names.length);
68+
})

lab3/main_test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ const { describe, it } = require('node:test');
22
const assert = require('assert');
33
const { Calculator } = require('./main');
44

5-
// TODO: write your tests here

0 commit comments

Comments
 (0)