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