@@ -2,31 +2,40 @@ const test = require('node:test');
2
2
const assert = require ( 'assert' ) ;
3
3
const { MyClass, Student } = require ( './main' ) ;
4
4
5
- test ( "Test MyClass's addStudent" , async ( t ) => {
5
+ test ( "Test MyClass's addStudent" , ( ) => {
6
6
const myClass = new MyClass ( ) ;
7
7
const student = new Student ( ) ;
8
- student . setName ( "Test Student " ) ;
8
+ student . setName ( "John " ) ;
9
9
const index = myClass . addStudent ( student ) ;
10
10
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" ) ;
11
14
} ) ;
12
15
13
- test ( "Test MyClass's getStudentById" , async ( t ) => {
16
+ test ( "Test MyClass's getStudentById" , ( ) => {
14
17
const myClass = new MyClass ( ) ;
15
18
const student = new Student ( ) ;
16
- student . setName ( "Test Student " ) ;
19
+ student . setName ( "Jane " ) ;
17
20
const index = myClass . addStudent ( student ) ;
18
21
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" ) ;
20
25
} ) ;
21
26
22
- test ( "Test Student's setName" , async ( t ) => {
27
+ test ( "Test Student's setName" , ( ) => {
23
28
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" ) ;
26
33
} ) ;
27
34
28
- test ( "Test Student's getName" , async ( t ) => {
35
+ test ( "Test Student's getName" , ( ) => {
29
36
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" ) ;
32
41
} ) ;
0 commit comments