@@ -4,20 +4,59 @@ const { MyClass, Student } = require('./main');
4
4
5
5
test ( "Test MyClass's addStudent" , ( ) => {
6
6
// 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");
8
19
} ) ;
9
20
10
21
test ( "Test MyClass's getStudentById" , ( ) => {
11
22
// 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");
13
38
} ) ;
14
39
15
40
test ( "Test Student's setName" , ( ) => {
16
41
// 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");
18
50
} ) ;
19
51
20
52
test ( "Test Student's getName" , ( ) => {
21
53
// 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");
23
62
} ) ;
0 commit comments