@@ -3,39 +3,60 @@ const assert = require('assert');
3
3
const { MyClass, Student } = require ( './main' ) ;
4
4
5
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");
6
+ const myClass = new MyClass ( ) ;
7
+ const student = new Student ( ) ;
8
+
9
+ // Test adding a student
10
+ myClass . addStudent ( student ) ;
11
+ assert . strictEqual ( myClass . students . length , 1 ) ;
12
+ assert . strictEqual ( myClass . students [ 0 ] , student ) ;
13
+
14
+ // Test adding multiple students
15
+ const student2 = new Student ( ) ;
16
+ const student3 = new Student ( ) ;
17
+ myClass . addStudent ( student2 ) ;
18
+ myClass . addStudent ( student3 ) ;
19
+ assert . strictEqual ( myClass . students . length , 3 ) ;
13
20
} ) ;
14
21
15
22
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");
23
+ const myClass = new MyClass ( ) ;
24
+ const student1 = new Student ( ) ;
25
+ const student2 = new Student ( ) ;
26
+ const student3 = new Student ( ) ;
27
+ myClass . addStudent ( student1 ) ;
28
+ myClass . addStudent ( student2 ) ;
29
+ myClass . addStudent ( student3 ) ;
30
+
31
+ // Test getting a student by ID
32
+ const foundStudent = myClass . getStudentById ( 1 ) ;
33
+ assert . strictEqual ( foundStudent , student2 ) ;
34
+
35
+ // Test getting a non-existing student by ID
36
+ const nonExistingStudent = myClass . getStudentById ( 5 ) ;
37
+ assert . strictEqual ( nonExistingStudent , null ) ;
27
38
} ) ;
28
39
29
40
test ( "Test Student's setName" , ( ) => {
30
- // TODO
31
- const student = new Student ( "John" ) ;
32
- student . setName ( "Smith" ) ;
33
- assert . strictEqual ( student . getName ( ) , "Smith" ) ;
41
+ const student = new Student ( ) ;
42
+
43
+ // Test setting a valid name
44
+ student . setName ( "John" ) ;
45
+ assert . strictEqual ( student . getName ( ) , "John" ) ;
46
+
47
+ // Test setting an invalid name
48
+ student . setName ( 123 ) ; // Passing a number instead of a string
49
+ assert . strictEqual ( student . getName ( ) , "" ) ; // Name should remain unchanged
34
50
} ) ;
35
51
36
52
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");
53
+ const student = new Student ( ) ;
54
+
55
+ // Test getting name when name is set
56
+ student . setName ( "John" ) ;
57
+ assert . strictEqual ( student . getName ( ) , "John" ) ;
58
+
59
+ // Test getting name when name is not set
60
+ assert . strictEqual ( student . getName ( ) , "" ) ; // Should return an empty string
41
61
} ) ;
62
+
0 commit comments