Skip to content

Commit b148ebc

Browse files
authored
Update main_test.js
1 parent 7a26e96 commit b148ebc

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

lab1/main_test.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,40 @@ const test = require('node:test');
22
const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

5-
test("Test MyClass's addStudent", async (t) => {
5+
test("Test MyClass's addStudent", () => {
66
const myClass = new MyClass();
77
const student = new Student();
8-
student.setName("Test Student");
8+
student.setName("John");
99
const index = myClass.addStudent(student);
1010
assert.strictEqual(index, 0, "addStudent should return index 0 for the first student");
11+
const notAStudent = {};
12+
const indexForNotAStudent = myClass.addStudent(notAStudent);
13+
assert.strictEqual(indexForNotAStudent, -1, "addStudent should return -1 when adding a non-Student instance");
1114
});
1215

13-
test("Test MyClass's getStudentById", async (t) => {
16+
test("Test MyClass's getStudentById", () => {
1417
const myClass = new MyClass();
1518
const student = new Student();
16-
student.setName("Test Student");
19+
student.setName("Jane");
1720
const index = myClass.addStudent(student);
1821
const fetchedStudent = myClass.getStudentById(index);
19-
assert.strictEqual(fetchedStudent.getName(), "Test Student", "getStudentById should return the correct student by id");
22+
assert.strictEqual(fetchedStudent.getName(), "Jane", "getStudentById should retrieve the student with the correct name");
23+
const invalidFetchedStudent = myClass.getStudentById(-1);
24+
assert.strictEqual(invalidFetchedStudent, null, "getStudentById should return null for an invalid id");
2025
});
2126

22-
test("Test Student's setName", async (t) => {
27+
test("Test Student's setName", () => {
2328
const student = new Student();
24-
student.setName("Another Student");
25-
assert.strictEqual(student.getName(), "Another Student", "setName should successfully set the student's name");
29+
student.setName("Doe");
30+
assert.strictEqual(student.name, "Doe", "setName should correctly set the student's name");
31+
student.setName(123);
32+
assert.strictEqual(student.name, "Doe", "setName should not set name when the input is not a string");
2633
});
2734

28-
test("Test Student's getName", async (t) => {
35+
test("Test Student's getName", () => {
2936
const student = new Student();
30-
student.setName("Another Test Student");
31-
assert.strictEqual(student.getName(), "Another Test Student", "getName should return the name of the student");
37+
student.setName("Smith");
38+
assert.strictEqual(student.getName(), "Smith", "getName should return the correct name");
39+
const newStudent = new Student();
40+
assert.strictEqual(newStudent.getName(), '', "getName should return an empty string for a student without a name");
3241
});

0 commit comments

Comments
 (0)