Skip to content

Commit 711ed6a

Browse files
authored
Merge pull request #41 from Kyle-12/312553021
[LAB1] 312553021
2 parents 2d41d26 + 2873532 commit 711ed6a

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

lab1/main_test.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,59 @@ const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
66
// TODO
7-
throw new Error("Test not implemented");
7+
const myClass = new MyClass();
8+
const student = new Student();
9+
10+
student.setName("John");
11+
const index = myClass.addStudent(student);
12+
assert.strictEqual(index, 0); //check if the student index is added to the class
13+
14+
const notAStudent = {};
15+
const invalidIndex = myClass.addStudent(notAStudent);
16+
assert.strictEqual(invalidIndex, -1); //check if the student's name is not a string
17+
18+
//throw new Error("Test not implemented");
819
});
920

1021
test("Test MyClass's getStudentById", () => {
1122
// TODO
12-
throw new Error("Test not implemented");
23+
const myClass = new MyClass();
24+
const student = new Student();
25+
student.setName("Jane");
26+
const id = myClass.addStudent(student);
27+
28+
const retrievedStudent = myClass.getStudentById(id);
29+
assert.strictEqual(retrievedStudent, student); // use the id to get the student
30+
31+
const invalidStudent = myClass.getStudentById(999);
32+
assert.strictEqual(invalidStudent, null); // check if the id is invalid
33+
34+
const invalidStudent1 = myClass.getStudentById(-1);
35+
assert.strictEqual(invalidStudent1, null); // check if the id is invalid
36+
37+
//throw new Error("Test not implemented");
1338
});
1439

1540
test("Test Student's setName", () => {
1641
// TODO
17-
throw new Error("Test not implemented");
42+
const student = new Student();
43+
student.setName("Doe");
44+
assert.strictEqual(student.name, "Doe"); //check if the name is set to Doe
45+
46+
student.setName(123);
47+
assert.strictEqual(student.name, "Doe"); //check if the name is not a string
48+
49+
//throw new Error("Test not implemented");
1850
});
1951

2052
test("Test Student's getName", () => {
2153
// TODO
22-
throw new Error("Test not implemented");
54+
const student = new Student();
55+
student.setName("Smith");
56+
assert.strictEqual(student.getName(), "Smith"); //check if the name is Smith
57+
58+
const newStudent = new Student();
59+
assert.strictEqual(newStudent.getName(), ''); //check if the name is empty
60+
61+
//throw new Error("Test not implemented");
2362
});

0 commit comments

Comments
 (0)