Skip to content

Commit e9aad93

Browse files
authored
Update main_test.js
1 parent 24f6d06 commit e9aad93

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

lab1/main_test.js

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,42 @@ 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-
const myClass = new MyClass();
8-
const student = new Student("John");
9-
myClass.addStudent(student);
10-
assert.strictEqual(myClass.students.length, 1);
11-
assert.strictEqual(myClass.students[0], student);
12-
//throw new Error("Test not implemented");
5+
test("Test MyClass's addStudent method", async (t) => {
6+
const myClass = new MyClass();
7+
const student = new Student();
8+
student.setName("John");
9+
const index = myClass.addStudent(student);
10+
assert.strictEqual(index, 0);
1311
});
1412

15-
test("Test MyClass's getStudentById", () => {
16-
// TODO
17-
const myClass = new MyClass();
18-
const student1 = new Student("John");
19-
const student2 = new Student("Jane");
20-
const student3 = new Student("Doe");
21-
myClass.addStudent(student1);
22-
myClass.addStudent(student2);
23-
myClass.addStudent(student3);
24-
const foundStudent = myClass.getStudentById(student2.id);
25-
assert.strictEqual(foundStudent, student2);
26-
//throw new Error("Test not implemented");
13+
test("Test MyClass's getStudentById method", async (t) => {
14+
const myClass = new MyClass();
15+
const student = new Student();
16+
student.setName("Jane");
17+
const index = myClass.addStudent(student);
18+
const fetchedStudent = myClass.getStudentById(index);
19+
assert.strictEqual(fetchedStudent.getName(), "Jane");
2720
});
2821

29-
test("Test Student's setName", () => {
30-
// TODO
31-
const student = new Student("John");
32-
student.setName("Smith");
33-
assert.strictEqual(student.getName(), "Smith");
22+
test("Test Student's setName and getName methods", async (t) => {
23+
const student = new Student();
24+
student.setName("Doe");
25+
assert.strictEqual(student.getName(), "Doe");
3426
});
3527

36-
test("Test Student's getName", () => {
37-
// TODO
38-
const student = new Student("John");
39-
assert.strictEqual(student.getName(), "John");
40-
//throw new Error("Test not implemented");
28+
test("Test handling non-Student instance in addStudent", async (t) => {
29+
const myClass = new MyClass();
30+
const notAStudent = {};
31+
const index = myClass.addStudent(notAStudent);
32+
assert.strictEqual(index, -1);
33+
});
34+
35+
test("Test getStudentById with invalid id", async (t) => {
36+
const myClass = new MyClass();
37+
const student = new Student();
38+
student.setName("Smith");
39+
myClass.addStudent(student);
40+
const invalidId = -1;
41+
const fetchedStudent = myClass.getStudentById(invalidId);
42+
assert.strictEqual(fetchedStudent, null);
4143
});

0 commit comments

Comments
 (0)