Skip to content

Commit 96dbe9d

Browse files
authored
Merge pull request #61 from LichtLiu/512558015
[LAB2] 512558015
2 parents 4cc41ac + f289473 commit 96dbe9d

File tree

2 files changed

+202
-15
lines changed

2 files changed

+202
-15
lines changed

lab1/main_test.js

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,69 @@ const test = require('node:test');
22
const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

5-
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
5+
// Test MyClass addStudent functionality
6+
test('Test MyClass addStudent', () => {
7+
const myClass = new MyClass();
8+
const student = new Student();
9+
10+
//check if student is instance of Student
11+
const notAStudent = 123
12+
const notAStudentInstance = myClass.addStudent(notAStudent);
13+
assert.strictEqual(notAStudentInstance, -1, "If it's not a Student instance, it should return -1.");
14+
15+
// push student to Student list
16+
student.setName("Joy");
17+
const addStudentResult = myClass.addStudent(student);
18+
assert.strictEqual(myClass.students[0], student, "The student has not been added to the student list. Add success");
19+
20+
// return id
21+
assert.strictEqual(addStudentResult, 0, "Id should be 0 or positive number.")
822
});
923

10-
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
24+
// Test MyClass getStudentById functionality
25+
test('Test MyClass getStudentById', () => {
26+
const myClass = new MyClass();
27+
const student = new Student();
28+
student.setName('Tiffany');
29+
myClass.addStudent(student);
30+
31+
// Id is negative number
32+
assert.strictEqual(myClass.getStudentById(-1), null, "Id should not be negative number, otherwise it will return null");
33+
34+
// Id is not exist
35+
assert.strictEqual(myClass.getStudentById(myClass.students.length+1), null, "Id is not exist");
36+
37+
// student exist
38+
assert.strictEqual(myClass.getStudentById(0), student, "return student object");
39+
1340
});
1441

15-
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
42+
// Test Student setName functionality
43+
test('Test Student setName', () => {
44+
const student1 = new Student();
45+
const student2 = new Student();
46+
47+
// pass a value to setName that is a string type
48+
student1.setName('Alice');
49+
assert.strictEqual(student1.getName(), 'Alice', 'Student name set correctly');
50+
51+
// pass a value to setName that is not a string type
52+
student2.setName(123);
53+
assert.strictEqual(student2.getName(), '', "userName should be in type of a string value");
54+
55+
1856
});
1957

20-
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
58+
// Test Student getName functionality
59+
test('Test Student getName', () => {
60+
const student = new Student();
61+
62+
// Test getting the name of a student without setting a name
63+
assert.strictEqual(student.getName(), '', 'Student name should be an empty string if not set');
64+
65+
// Test after set a name to student can return name
66+
student.setName('Bob');
67+
assert.strictEqual(student.getName(), 'Bob', 'Student name retrieved correctly');
68+
69+
70+
});

lab2/main_test.js

Lines changed: 142 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,146 @@
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+
});
146+

0 commit comments

Comments
 (0)