Skip to content

Commit e392b30

Browse files
authored
Merge pull request #112 from LichtLiu/lab3
[LAB3] 512558015
2 parents f47a40f + b545da8 commit e392b30

File tree

2 files changed

+204
-2
lines changed

2 files changed

+204
-2
lines changed

lab2/main_test.js

Lines changed: 141 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,145 @@
11
const test = require('node:test');
22
const assert = require('assert');
3+
const fs = require('fs');
4+
const os = require('os');
5+
const path = require('path');
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+
test('should return a message context', () => {
10+
const mailSystem = new MailSystem();
11+
const name = 'John';
12+
13+
const context = mailSystem.write(name);
14+
assert.strictEqual(context, 'Congrats, John!');
15+
16+
});
17+
18+
test('should return true if mail is sent successfully', (t) => {
19+
const mailSystem = new MailSystem();
20+
const name = 'John';
21+
22+
// test send mail success return true
23+
t.mock.method(Math,'random', () => 1);
24+
is_send = mailSystem.send('Joy', "test mail");
25+
assert.strictEqual(is_send, true);
26+
27+
28+
// test send mail fail return false
29+
t.mock.method(Math,'random', () => 0.4);
30+
is_send = mailSystem.send('Joy', "test mail");
31+
assert.strictEqual(is_send, false);
32+
33+
});
34+
35+
36+
37+
38+
39+
test('should return name_list ', async()=>{
40+
41+
// Write mock name list to a temporary file
42+
const nameListContent = 'Alice\nBob\nCharlie';
43+
const tempFilePath = path.join('name_list.txt');
44+
fs.writeFileSync(tempFilePath, nameListContent);
45+
46+
// Attach cleanup handler to the process exit event
47+
process.on('exit', () => {
48+
if (tempFilePath) {
49+
// Clean up: Delete the temporary directory
50+
fs.unlinkSync(tempFilePath);
51+
}
52+
});
53+
54+
// Instantiate Application class and call getNames with the temporary file path
55+
const app = new Application();
56+
const [people, selected] = await app.getNames(tempFilePath);
57+
58+
// Assert the results
59+
assert.deepStrictEqual(people, ['Alice', 'Bob', 'Charlie']);
60+
assert.deepStrictEqual(selected, []);
61+
62+
});
63+
64+
65+
test('should return null if all people are selected', async (t) => {
66+
const app = new Application();
67+
app.people = ['Alice', 'Bob', 'Charlie'];
68+
app.selected = ['Alice', 'Bob', 'Charlie'];
69+
70+
const result = app.selectNextPerson();
71+
assert.strictEqual(result, null);
72+
});
73+
74+
//Test case for getRandomPerson() method
75+
test('should return a random person', () => {
76+
// Stub Math.random() to return a fixed value
77+
Math.random = () => 0.5; // Set Math.random() to always return 0.5
78+
const randomNumber = Math.random();
79+
80+
// Create an instance of the Application class
81+
const app = new Application();
82+
app.people = ['Alice', 'Bob', 'Charlie'];
83+
// Call the getRandomPerson() method
84+
const randomPerson = app.getRandomPerson();
85+
86+
// Ensure that the random person is one of the people in the list
87+
assert(app.people.includes(randomPerson));
88+
89+
});
90+
91+
test('should select and return a person who has not been selected yet', () => {
92+
const app = new Application();
93+
app.people = ['Alice', 'Bob', 'Charlie'];
94+
95+
let getRandomPersonCallCount = 0;
96+
app.getRandomPerson = () => {
97+
switch (getRandomPersonCallCount++) {
98+
case 0:
99+
return 'Alice';
100+
case 1:
101+
return 'Bob';
102+
case 2:
103+
return 'Charlie';
104+
}
105+
};
106+
107+
app.selected = ['Alice', 'Bob'];
108+
109+
const result = app.selectNextPerson();
110+
111+
assert.strictEqual(result, 'Charlie');
112+
assert.strictEqual(getRandomPersonCallCount, 3);
113+
});
114+
115+
class MockMailSystem {
116+
constructor() {
117+
this.writeCallCount = 0;
118+
this.sendCallCount = 0;
119+
}
120+
121+
write() {
122+
this.writeCallCount++;
123+
return 'Message context';
124+
}
125+
126+
send() {
127+
this.sendCallCount++;
128+
return true;
129+
}
130+
}
131+
132+
test('should call write and send methods of MailSystem for each selected person', () => {
133+
const mailSystem = new MockMailSystem();
134+
135+
const app = new Application();
136+
app.mailSystem = mailSystem;
137+
app.selected = ['Alice', 'Bob', 'Charlie'];
138+
139+
app.notifySelected();
140+
141+
assert.strictEqual(mailSystem.writeCallCount, 3);
142+
assert.strictEqual(mailSystem.sendCallCount, 3);
143+
144+
145+
});

lab3/main_test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,66 @@ const assert = require('assert');
33
const { Calculator } = require('./main');
44

55
// TODO: write your tests here
6+
describe('Calculate', (t) =>{
7+
const calculator = new Calculator();
8+
9+
it('test exp() functionality', () => {
10+
const testcases = [
11+
{ param: Infinity, expected: Error, msg: 'unsupported operand type'},
12+
{ param: -Infinity, expected: Error, msg: 'unsupported operand type'},
13+
{ param: NaN, expected: Error, msg: 'unsupported operand type'},
14+
{ param: 'abc', expected: Error, msg: 'unsupported operand type'},
15+
{ param: true, expected: Error, msg: 'unsupported operand type'},
16+
{ param: null, expected: Error, msg: 'unsupported operand type'},
17+
{ param: undefined, expected: Error, msg: 'unsupported operand type'},
18+
{ param: {}, expected: Error, msg: 'unsupported operand type'},
19+
20+
{ param: Number.MAX_VALUE, expected: Error, msg: 'overflow' },
21+
22+
{ param: 42, expected: Math.exp(42)},
23+
{ param: 3.14, expected: Math.exp(3.14)},
24+
{ param: 0, expected: Math.exp(0)}
25+
];
26+
for (const tc of testcases) {
27+
if (tc.expected === Error) {
28+
assert.throws(() => {
29+
calculator.exp(tc.param);
30+
},
31+
{
32+
name: 'Error',
33+
message: tc.msg
34+
});
35+
} else {
36+
assert.strictEqual(calculator.exp(tc.param), tc.expected);
37+
}
38+
}
39+
});
40+
it('test log() functionality', () => {
41+
const testcases = [
42+
{ param: Infinity, expected: Error, msg: 'unsupported operand type'},
43+
{ param: -Infinity, expected: Error, msg: 'unsupported operand type'},
44+
{ param: true, expected: Error, msg: 'unsupported operand type'},
45+
{ param: 'abc', expected: Error, msg: 'unsupported operand type'},
46+
47+
{ param: 0, expected: Error, msg: "math domain error (1)" },
48+
{ param: -1, expected: Error, msg: 'math domain error (2)'},
49+
50+
{ param: 42, expected: Math.log(42)},
51+
{ param: 3.14, expected: Math.log(3.14)},
52+
];
53+
54+
for (const tc of testcases) {
55+
if(tc.expected === Error){
56+
assert.throws(() =>{
57+
calculator.log(tc.param);
58+
},{
59+
name: 'Error',
60+
message: tc.msg
61+
});
62+
}else{
63+
assert.strictEqual(calculator.log(tc.param), tc.expected);
64+
}
65+
}
66+
});
67+
68+
});

0 commit comments

Comments
 (0)