Skip to content

Commit 18c9229

Browse files
Zayn1029Zayn1029
authored andcommitted
main_test.js
1 parent 9d0813d commit 18c9229

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

lab1/main_test.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,53 @@ 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+
8+
const student1 = new Student();
9+
student1.setName("Alice");
10+
const id1 = myClass.addStudent(student1);
11+
assert.strictEqual(id1, 0, "First student ID should be 0");
12+
13+
const student2 = new Student();
14+
student2.setName("Bob");
15+
const id2 = myClass.addStudent(student2);
16+
assert.strictEqual(id2, 1, "Second student ID should be 1");
17+
18+
const invalidId = myClass.addStudent({ name: "FakeStudent" });
19+
assert.strictEqual(invalidId, -1, "Non-Student object should return -1");
820
});
921

1022
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
23+
const myClass = new MyClass();
24+
const student = new Student();
25+
student.setName("Charlie");
26+
27+
const id = myClass.addStudent(student);
28+
const retrievedStudent = myClass.getStudentById(id);
29+
assert.ok(retrievedStudent, "Student should be retrieved");
30+
assert.strictEqual(retrievedStudent.getName(), "Charlie", "Retrieved student's name should match");
31+
32+
assert.strictEqual(myClass.getStudentById(-1), null, "Negative ID should return null");
33+
assert.strictEqual(myClass.getStudentById(99), null, "Out of range ID should return null");
1334
});
1435

1536
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
37+
const student = new Student();
38+
39+
student.setName("David");
40+
assert.strictEqual(student.getName(), "David", "Should set name correctly");
41+
42+
student.setName(12345);
43+
assert.strictEqual(student.getName(), "David", "Non-string name should be ignored");
44+
45+
student.setName("");
46+
assert.strictEqual(student.getName(), "", "Empty string should be a valid name");
1847
});
1948

2049
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
50+
const student = new Student();
51+
assert.strictEqual(student.getName(), "", "Default name should be empty string");
52+
53+
student.setName("Eve");
54+
assert.strictEqual(student.getName(), "Eve", "getName should return the correct name");
2355
});

0 commit comments

Comments
 (0)