1- const test = require ( ' node:test' ) ;
2- const assert = require ( ' assert' ) ;
3- const { MyClass, Student } = require ( ' ./main' ) ;
1+ const test = require ( " node:test" ) ;
2+ const assert = require ( " assert" ) ;
3+ const { MyClass, Student } = require ( " ./main" ) ;
44
55test ( "Test MyClass's addStudent" , ( ) => {
6- // TODO
7- throw new Error ( "Test not implemented" ) ;
6+
7+ const testClass = new MyClass ( ) ;
8+
9+ // invalid student
10+ const inv_stu = [ - 1 , 0 , 100 , "QAQ" , 3.14 ] ;
11+ for ( let i = 0 ; i < inv_stu . length ; i ++ ) {
12+ assert . strictEqual ( testClass . addStudent ( inv_stu [ i ] ) , - 1 ) ;
13+ }
14+
15+ // valid student
16+ const names = [ "John" , "Jane" , "Doe" , "Smith" ] ;
17+ stuList = [ ] ;
18+ for ( let i = 0 ; i < names . length ; i ++ ) {
19+ stuTmp = new Student ( ) ;
20+ stuTmp . setName ( names [ i ] ) ;
21+ stuList . push ( stuTmp ) ;
22+ }
23+ for ( let i = 0 ; i < stuList . length ; i ++ ) {
24+ assert . strictEqual ( testClass . addStudent ( stuList [ i ] ) , i ) ;
25+ }
26+
827} ) ;
928
1029test ( "Test MyClass's getStudentById" , ( ) => {
11- // TODO
12- throw new Error ( "Test not implemented" ) ;
30+
31+ const testClass = new MyClass ( ) ;
32+ const names = [ "John" , "Jane" , "Doe" , "Smith" ] ;
33+ stuList = [ ] ;
34+ for ( let i = 0 ; i < names . length ; i ++ ) {
35+ stuTmp = new Student ( ) ;
36+ stuTmp . setName ( names [ i ] ) ;
37+ stuList . push ( stuTmp ) ;
38+ }
39+ for ( let i = 0 ; i < stuList . length ; i ++ ) {
40+ testClass . addStudent ( stuList [ i ] ) ;
41+ }
42+
43+ // invalid id
44+ const inv_id = [ - 1 , stuList . length , stuList . length + 1 ] ;
45+ for ( let i = 0 ; i < inv_id . length ; i ++ ) {
46+ assert . strictEqual ( testClass . getStudentById ( inv_id [ i ] ) , null ) ;
47+ }
48+
49+ // valid id
50+ for ( let i = 0 ; i < stuList . length ; i ++ ) {
51+ console . log ( testClass . getStudentById ( i ) ) ;
52+ assert . strictEqual ( testClass . getStudentById ( i ) , stuList [ i ] ) ;
53+ }
54+
55+
1356} ) ;
1457
1558test ( "Test Student's setName" , ( ) => {
16- // TODO
17- throw new Error ( "Test not implemented" ) ;
59+
60+ // invalid username
61+ const inv_names = [ - 1 , 0 , 100 , 1234 ]
62+ for ( let i = 0 ; i < inv_names . length ; i ++ ) {
63+ stuTmp = new Student ( ) ;
64+ assert . strictEqual ( stuTmp . setName ( inv_names [ i ] ) , undefined ) ;
65+ assert . strictEqual ( stuTmp . getName ( ) , "" ) ;
66+ }
67+
68+ // valid username
69+ const v_names = [ "John" , "Jane" , "Doe" , "Smith" ] ;
70+ for ( let i = 0 ; i < v_names . length ; i ++ ) {
71+ stuTmp = new Student ( ) ;
72+ assert . strictEqual ( stuTmp . setName ( v_names [ i ] ) , undefined ) ;
73+ assert . strictEqual ( stuTmp . getName ( ) , v_names [ i ] ) ;
74+ }
75+
1876} ) ;
1977
2078test ( "Test Student's getName" , ( ) => {
21- // TODO
22- throw new Error ( "Test not implemented" ) ;
23- } ) ;
79+
80+ // invalid username
81+ const inv_names = [ - 1 , 0 , 100 , 1234 ]
82+ for ( let i = 0 ; i < inv_names . length ; i ++ ) {
83+ stuTmp = new Student ( ) ;
84+ assert . strictEqual ( stuTmp . setName ( inv_names [ i ] ) , undefined ) ;
85+ assert . strictEqual ( stuTmp . getName ( ) , "" ) ;
86+ }
87+
88+ // valid username
89+ const v_names = [ "John" , "Jane" , "Doe" , "Smith" ] ;
90+ for ( let i = 0 ; i < v_names . length ; i ++ ) {
91+ stuTmp = new Student ( ) ;
92+ assert . strictEqual ( stuTmp . setName ( v_names [ i ] ) , undefined ) ;
93+ assert . strictEqual ( stuTmp . getName ( ) , v_names [ i ] ) ;
94+ }
95+
96+ } ) ;
0 commit comments