@@ -3,21 +3,58 @@ const assert = require('assert');
33const { MyClass, Student } = require ( './main' ) ;
44
55test ( "Test MyClass's addStudent" , ( ) => {
6- // TODO
7- throw new Error ( "Test not implemented" ) ;
6+ const myClass = new MyClass ( ) ;
7+ const student = new Student ( ) ;
8+
9+ // Valid student case
10+ const studentId = myClass . addStudent ( student ) ;
11+ assert . strictEqual ( studentId , 0 ) ;
12+
13+ // Invalid input case
14+ const invalidId = myClass . addStudent ( { } ) ;
15+ assert . strictEqual ( invalidId , - 1 ) ;
816} ) ;
917
1018test ( "Test MyClass's getStudentById" , ( ) => {
11- // TODO
12- throw new Error ( "Test not implemented" ) ;
19+ const myClass = new MyClass ( ) ;
20+ const student = new Student ( ) ;
21+ student . setName ( "John" ) ;
22+
23+ const studentId = myClass . addStudent ( student ) ;
24+
25+ // Valid retrieval
26+ const retrievedStudent = myClass . getStudentById ( studentId ) ;
27+ assert . ok ( retrievedStudent instanceof Student ) ;
28+ assert . strictEqual ( retrievedStudent . getName ( ) , "John" ) ;
29+
30+ // Invalid ID cases
31+ assert . strictEqual ( myClass . getStudentById ( - 1 ) , null ) ;
32+ assert . strictEqual ( myClass . getStudentById ( 101 ) , null ) ;
1333} ) ;
1434
35+
1536test ( "Test Student's setName" , ( ) => {
16- // TODO
17- throw new Error ( "Test not implemented" ) ;
37+ const student = new Student ( ) ;
38+
39+ // Valid name setting
40+ student . setName ( "Jane" ) ;
41+ assert . strictEqual ( student . getName ( ) , "Jane" ) ;
42+
43+ // Invalid input cases
44+ student . setName ( 123 ) ;
45+ assert . strictEqual ( student . getName ( ) , "Jane" ) ; // Should not change
46+
47+ student . setName ( null ) ;
48+ assert . strictEqual ( student . getName ( ) , "Jane" ) ; // Should not change
1849} ) ;
1950
2051test ( "Test Student's getName" , ( ) => {
21- // TODO
22- throw new Error ( "Test not implemented" ) ;
23- } ) ;
52+ const student = new Student ( ) ;
53+
54+ // Default value case
55+ assert . strictEqual ( student . getName ( ) , '' ) ;
56+
57+ // After setting a valid name
58+ student . setName ( "Alice" ) ;
59+ assert . strictEqual ( student . getName ( ) , "Alice" ) ;
60+ } ) ;
0 commit comments