@@ -2,22 +2,53 @@ const test = require('node:test');
22const assert = require ( 'assert' ) ;
33const { MyClass, Student } = require ( './main' ) ;
44
5- test ( "Test MyClass's addStudent" , ( ) => {
6- // TODO
7- throw new Error ( "Test not implemented" ) ;
5+ test ( "Test MyClass\'s addStudent" , ( ) => {
6+ const myClass = new MyClass ( ) ;
7+ const student = new Student ( ) ;
8+
9+ const id = myClass . addStudent ( student ) ;
10+ assert . strictEqual ( id , 0 ) ;
11+
12+ const invalidId = myClass . addStudent ( "invalid student" ) ;
13+ assert . strictEqual ( invalidId , - 1 ) ;
814} ) ;
915
10- test ( "Test MyClass's getStudentById" , ( ) => {
11- // TODO
12- throw new Error ( "Test not implemented" ) ;
16+ test ( "Test MyClass\'s getStudentById" , ( ) => {
17+ const myClass = new MyClass ( ) ;
18+ const student = new Student ( ) ;
19+ student . setName ( "Ann" ) ;
20+
21+ const id = myClass . addStudent ( student ) ;
22+ const retrievedStudent = myClass . getStudentById ( id ) ;
23+
24+ assert . ok ( retrievedStudent instanceof Student ) ;
25+ assert . strictEqual ( retrievedStudent . getName ( ) , "Ann" ) ;
26+
27+ assert . strictEqual ( myClass . getStudentById ( - 1 ) , null ) ;
28+ assert . strictEqual ( myClass . getStudentById ( 100 ) , null ) ;
1329} ) ;
1430
1531test ( "Test Student's setName" , ( ) => {
16- // TODO
17- throw new Error ( "Test not implemented" ) ;
32+ const student = new Student ( ) ;
33+
34+ student . setName ( "Anton" ) ;
35+ assert . strictEqual ( student . getName ( ) , "Anton" ) ;
36+
37+ student . setName ( 1 ) ;
38+ assert . strictEqual ( student . getName ( ) , "Anton" ) ;
39+
40+ student . setName ( null ) ;
41+ assert . strictEqual ( student . getName ( ) , "Anton" ) ;
42+
43+ student . setName ( undefined ) ;
44+ assert . strictEqual ( student . getName ( ) , "Anton" ) ;
1845} ) ;
1946
2047test ( "Test Student's getName" , ( ) => {
21- // TODO
22- throw new Error ( "Test not implemented" ) ;
48+ const student = new Student ( ) ;
49+
50+ assert . strictEqual ( student . getName ( ) , "" ) ;
51+
52+ student . setName ( "Cheri" ) ;
53+ assert . strictEqual ( student . getName ( ) , "Cheri" ) ;
2354} ) ;
0 commit comments