Skip to content

Commit b327aef

Browse files
authored
Merge pull request #18 from tingegg/510558006
[LAB1] 510558006
2 parents 21f567d + ac1d5fc commit b327aef

File tree

1 file changed

+66
-19
lines changed

1 file changed

+66
-19
lines changed

lab1/main_test.js

+66-19
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");
8-
});
9-
10-
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
13-
});
14-
15-
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
18-
});
19-
20-
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
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.")
22+
});
23+
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+
40+
});
41+
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+
56+
});
57+
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+
});

0 commit comments

Comments
 (0)