@@ -2,34 +2,43 @@ const test = require('node:test');
2
2
const assert = require ( 'assert' ) ;
3
3
const { MyClass, Student } = require ( './main' ) ;
4
4
5
- test ( ' Test MyClass\ 's addStudent' , ( ) => {
5
+ test ( " Test MyClass's addStudent" , ( ) => {
6
6
const myClass = new MyClass ( ) ;
7
7
const student = new Student ( ) ;
8
- student . setName ( "Test Student" ) ;
8
+ student . setName ( 'John Doe' ) ;
9
9
const index = myClass . addStudent ( student ) ;
10
- assert . strictEqual ( index , 0 ) ; // 應該正確添加學生並返回索引0
10
+ assert . strictEqual ( index , 0 ) ; // 應該成功添加並返回索引 0
11
11
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
15
15
} ) ;
16
16
17
-
18
- test ( 'Test Student\'s setName' , ( ) => {
17
+ test ( "Test MyClass's getStudentById" , ( ) => {
18
+ const myClass = new MyClass ( ) ;
19
19
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 ) ; // 應該返回相同的學生實例
22
24
23
- student . setName ( 123 ) ; // 傳入非字符串應不修改名字
24
- assert . strictEqual ( student . name , "Test Student" ) ; // 名字應該保持不變
25
+ const nullStudent = myClass . getStudentById ( - 1 ) ; // 錯誤的索引
26
+ assert . strictEqual ( nullStudent , null ) ; // 應該返回 null
25
27
} ) ;
26
28
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 應該返回設置的名字
27
33
34
+ student . setName ( 123 ) ; // 嘗試設置非字串
35
+ assert . strictEqual ( student . getName ( ) , 'John Doe' ) ; // 名字應該保持不變
36
+ } ) ;
28
37
29
- test ( ' Test Student\ 's getName' , ( ) => {
38
+ test ( " Test Student's getName" , ( ) => {
30
39
const student = new Student ( ) ;
31
- assert . strictEqual ( student . getName ( ) , '' ) ; // 應該返回空字符串,因為名字未設置
40
+ assert . strictEqual ( student . getName ( ) , '' ) ; // 如果名字未設置應返回空字串
32
41
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 應該返回設置的名字
35
44
} ) ;
0 commit comments