@@ -2,34 +2,43 @@ const test = require('node:test');
22const assert = require ( 'assert' ) ;
33const { MyClass, Student } = require ( './main' ) ;
44
5- test ( ' Test MyClass\ 's addStudent' , ( ) => {
5+ test ( " Test MyClass's addStudent" , ( ) => {
66 const myClass = new MyClass ( ) ;
77 const student = new Student ( ) ;
8- student . setName ( "Test Student" ) ;
8+ student . setName ( 'John Doe' ) ;
99 const index = myClass . addStudent ( student ) ;
10- assert . strictEqual ( index , 0 ) ; // 應該正確添加學生並返回索引0
10+ assert . strictEqual ( index , 0 ) ; // 應該成功添加並返回索引 0
1111
12- const invalidStudent = { } ;
13- const invalidIndex = myClass . addStudent ( invalidStudent ) ;
14- assert . strictEqual ( invalidIndex , - 1 ) ; // 傳入非Student實例應返回 -1
12+ const nonStudent = { } ; // 非 Student 實例
13+ const wrongIndex = myClass . addStudent ( nonStudent ) ;
14+ assert . strictEqual ( wrongIndex , - 1 ) ; // 嘗試添加非 Student 實例應返回 -1
1515} ) ;
1616
17-
18- test ( 'Test Student\'s setName' , ( ) => {
17+ test ( "Test MyClass's getStudentById" , ( ) => {
18+ const myClass = new MyClass ( ) ;
1919 const student = new Student ( ) ;
20- student . setName ( "Test Student" ) ;
21- assert . strictEqual ( student . name , "Test Student" ) ; // 設置的名字應該符合預期
20+ student . setName ( 'Jane Doe' ) ;
21+ const index = myClass . addStudent ( student ) ;
22+ const retrievedStudent = myClass . getStudentById ( index ) ;
23+ assert . strictEqual ( retrievedStudent , student ) ; // 應該返回相同的學生實例
2224
23- student . setName ( 123 ) ; // 傳入非字符串應不修改名字
24- assert . strictEqual ( student . name , "Test Student" ) ; // 名字應該保持不變
25+ const nullStudent = myClass . getStudentById ( - 1 ) ; // 錯誤的索引
26+ assert . strictEqual ( nullStudent , null ) ; // 應該返回 null
2527} ) ;
2628
29+ test ( "Test Student's setName" , ( ) => {
30+ const student = new Student ( ) ;
31+ student . setName ( 'John Doe' ) ;
32+ assert . strictEqual ( student . getName ( ) , 'John Doe' ) ; // setName 後 getName 應該返回設置的名字
2733
34+ student . setName ( 123 ) ; // 嘗試設置非字串
35+ assert . strictEqual ( student . getName ( ) , 'John Doe' ) ; // 名字應該保持不變
36+ } ) ;
2837
29- test ( ' Test Student\ 's getName' , ( ) => {
38+ test ( " Test Student's getName" , ( ) => {
3039 const student = new Student ( ) ;
31- assert . strictEqual ( student . getName ( ) , '' ) ; // 應該返回空字符串,因為名字未設置
40+ assert . strictEqual ( student . getName ( ) , '' ) ; // 如果名字未設置應返回空字串
3241
33- student . setName ( "Test Student" ) ;
34- assert . strictEqual ( student . getName ( ) , "Test Student" ) ; // 設置名字後應該返回設置的名字
42+ student . setName ( 'Jane Doe' ) ;
43+ assert . strictEqual ( student . getName ( ) , 'Jane Doe' ) ; // setName 後 getName 應該返回設置的名字
3544} ) ;
0 commit comments