Skip to content

Commit fe005ce

Browse files
authored
Merge pull request #134 from vivian921207/111550064
[LAB1] 111550064
2 parents 610a903 + bf2fe43 commit fe005ce

1 file changed

Lines changed: 31 additions & 9 deletions

File tree

lab1/main_test.js

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,43 @@ const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
6+
const myClass = new MyClass();
7+
const student1 = new Student();
8+
student1.setName("Alice");
9+
10+
const studentId = myClass.addStudent(student1);
11+
assert.strictEqual(studentId, 0, "First student ID should be 0");
12+
13+
14+
const invalidStudent = myClass.addStudent({});
15+
assert.strictEqual(invalidStudent, -1, "Adding a non-Student object should return -1");
816
});
917

1018
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
19+
const myClass = new MyClass();
20+
const student1 = new Student();
21+
student1.setName("Alice");
22+
myClass.addStudent(student1);
23+
24+
assert.strictEqual(myClass.getStudentById(0).getName(), "Alice", "Should return Alice");
25+
assert.strictEqual(myClass.getStudentById(2), null, "ID out of range should return null");
1326
});
1427

1528
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
29+
const student = new Student();
30+
31+
student.setName("Bob");
32+
assert.strictEqual(student.getName(), "Bob", "Name should be Bob");
33+
34+
student.setName(12345);
35+
assert.strictEqual(student.getName(), "Bob", "Setting name to non-string should not change it");
1836
});
1937

2038
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
39+
const student = new Student();
40+
41+
assert.strictEqual(student.getName(), "", "Default name should be an empty string");
42+
43+
student.setName("David");
44+
assert.strictEqual(student.getName(), "David", "getName() should return 'David'");
45+
});

0 commit comments

Comments
 (0)