Skip to content

Commit f64699e

Browse files
committed
main_test.js_test2
1 parent 0532aba commit f64699e

File tree

1 file changed

+61
-39
lines changed

1 file changed

+61
-39
lines changed

lab1/main_test.js

Lines changed: 61 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,47 +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" , async () =>{
5+
// Test MyClass addStudent functionality
6+
test('Test MyClass addStudent', () => {
67
const myClass = new MyClass();
78
const student = new Student();
8-
student.setName("Tingegg");
9-
const index = myClass.addStudent(student);
10-
assert.strictEqual(myClass.students.includes(student), true);
11-
assert.strictEqual(index, 0);
12-
13-
const faultresult = myClass.addStudent("NotAStudent");
14-
assert.strictEqual(faultresult, -1);
15-
16-
});
17-
18-
test("Test MyClass's getStudentById", async () => {
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', () => {
1926
const myClass = new MyClass();
2027
const student = new Student();
21-
student.setName("Bob");
22-
const index = myClass.addStudent(student);
23-
const foundStudent = myClass.getStudentById(index);
24-
assert.strictEqual(foundStudent, student);
25-
26-
const faultfoundStudent = myClass.getStudentById(999);
27-
assert.strictEqual(faultfoundStudent, null);
28-
29-
});
30-
31-
test("Test Student's setName", async () => {
32-
const student = new Student();
33-
student.setName("Charlie");
34-
assert.strictEqual(student.getName(), "Charlie");
35-
student.setName(null);
36-
assert.strictEqual(student.getName(), '');
37-
});
38-
39-
test("Test Student's getName", async () => {
40-
const myClass = new MyClass();
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', () => {
4160
const student = new Student();
42-
student.setName("David");
43-
assert.strictEqual(student.getName(), "David");
44-
student.setName("Eve");
45-
await myClass.addStudent(student);
46-
assert.strictEqual(myClass.students.length, 1);
47-
});
48-
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)