@@ -2,22 +2,62 @@ const test = require('node:test');
22const assert = require ( 'assert' ) ;
33const { MyClass, Student } = require ( './main' ) ;
44
5+ // const myClass = new MyClass();
6+ // const names = ['John', 'Jane', 'Doe', 'Smith'];
7+ // names.forEach(name => {
8+ // const student = new Student();
9+ // student.setName(name);
10+ // const newStudentId = myClass.addStudent(student);
11+ // const newStudentName = myClass.getStudentById(newStudentId).getName();
12+ // console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
13+ // });
14+
515test ( "Test MyClass's addStudent" , ( ) => {
6- // TODO
7- throw new Error ( "Test not implemented" ) ;
16+ const myClass = new MyClass ( ) ;
17+ const names = [ 'John' , 'Jane' , 'Doe' , 'Smith' ] ;
18+ for ( let i = 0 ; i < names . length ; i ++ ) {
19+ const student = new Student ( ) ;
20+ const validStudentId = myClass . addStudent ( student ) ;
21+ assert . strictEqual ( validStudentId , i ) ;
22+ }
23+
24+ const invalidStudentId = myClass . addStudent ( { } ) ;
25+ assert . strictEqual ( invalidStudentId , - 1 ) ;
826} ) ;
927
1028test ( "Test MyClass's getStudentById" , ( ) => {
11- // TODO
12- throw new Error ( "Test not implemented" ) ;
29+ const myClass = new MyClass ( ) ;
30+ const names = [ 'John' , 'Jane' , 'Doe' , 'Smith' ] ;
31+ names . forEach ( name => {
32+ const student = new Student ( ) ;
33+ student . setName ( name ) ;
34+ const newStudentId = myClass . addStudent ( student ) ;
35+ const newStudentName = myClass . getStudentById ( newStudentId ) . getName ( ) ;
36+ assert . strictEqual ( newStudentName , name ) ;
37+ } ) ;
38+
39+ const nonExistingStudent = myClass . getStudentById ( 4 ) ;
40+ assert . strictEqual ( nonExistingStudent , null ) ;
41+
42+ const nonExistingStudent_2 = myClass . getStudentById ( - 1 ) ;
43+ assert . strictEqual ( nonExistingStudent_2 , null ) ;
1344} ) ;
1445
1546test ( "Test Student's setName" , ( ) => {
16- // TODO
17- throw new Error ( "Test not implemented" ) ;
47+ const student = new Student ( ) ;
48+
49+ student . setName ( 'John' ) ;
50+ assert . strictEqual ( student . getName ( ) , 'John' ) ;
51+
52+ student . setName ( 123 ) ;
53+ assert . strictEqual ( student . getName ( ) , 'John' ) ;
1854} ) ;
1955
2056test ( "Test Student's getName" , ( ) => {
21- // TODO
22- throw new Error ( "Test not implemented" ) ;
57+ const student = new Student ( ) ;
58+
59+ assert . strictEqual ( student . getName ( ) , '' ) ;
60+
61+ student . setName ( 'John' ) ;
62+ assert . strictEqual ( student . getName ( ) , 'John' ) ;
2363} ) ;
0 commit comments