@@ -3,21 +3,60 @@ const assert = require('assert');
3
3
const { MyClass, Student } = require ( './main' ) ;
4
4
5
5
test ( "Test MyClass's addStudent" , ( ) => {
6
- // TODO
7
- throw new Error ( "Test not implemented" ) ;
6
+ const myClass = new MyClass ( ) ;
7
+ //create 2 students John & Jane
8
+ const student1 = new Student ( ) ;
9
+ student1 . setName ( 'John' ) ;
10
+ const id1 = myClass . addStudent ( student1 ) ;
11
+ assert . strictEqual ( id1 , 0 ) ;
12
+
13
+ const student2 = new Student ( ) ;
14
+ student2 . setName ( 'Jane' ) ;
15
+ const id2 = myClass . addStudent ( student2 ) ;
16
+ assert . strictEqual ( id2 , 1 ) ;
17
+
18
+ //新增一個TESTING student
19
+ const invalidStudent = 'TESTING' ;
20
+ const invalidId = myClass . addStudent ( invalidStudent ) ;
21
+ assert . strictEqual ( invalidId , - 1 ) ;
22
+
8
23
} ) ;
9
24
10
25
test ( "Test MyClass's getStudentById" , ( ) => {
11
- // TODO
12
- throw new Error ( "Test not implemented" ) ;
26
+ const myClass = new MyClass ( ) ;
27
+ const student1 = new Student ( ) ;
28
+ student1 . setName ( 'John' ) ;
29
+ myClass . addStudent ( student1 ) ;
30
+
31
+ const retrievedStudent = myClass . getStudentById ( 0 ) ;
32
+ assert . strictEqual ( retrievedStudent . getName ( ) , 'John' ) ;
33
+
34
+ const invalidStudent = myClass . getStudentById ( - 1 ) ;
35
+ assert . strictEqual ( invalidStudent , null ) ;
36
+
37
+ const outOfBoundsStudent = myClass . getStudentById ( 1 ) ;
38
+ assert . strictEqual ( outOfBoundsStudent , null ) ;
39
+
13
40
} ) ;
14
41
15
42
test ( "Test Student's setName" , ( ) => {
16
- // TODO
17
- throw new Error ( "Test not implemented" ) ;
43
+ const student = new Student ( ) ;
44
+ //測試變數是否有效
45
+ student . setName ( 'Alice' ) ;
46
+ assert . strictEqual ( student . getName ( ) , 'Alice' ) ;
47
+
48
+ student . setName ( 123 ) ;
49
+ assert . strictEqual ( student . getName ( ) , 'Alice' ) ;
50
+
18
51
} ) ;
19
52
20
53
test ( "Test Student's getName" , ( ) => {
21
- // TODO
22
- throw new Error ( "Test not implemented" ) ;
23
- } ) ;
54
+ const student = new Student ( ) ;
55
+ // 初始化未設置
56
+ assert . strictEqual ( student . getName ( ) , '' ) ;
57
+
58
+ // 設置變數後返回
59
+ student . setName ( 'Bob' ) ;
60
+ assert . strictEqual ( student . getName ( ) , 'Bob' ) ;
61
+
62
+ } ) ;
0 commit comments