@@ -3,21 +3,53 @@ 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
+ // if student is not an instance of Student, return -1
8
+ assert . strictEqual ( myClass . addStudent ( { } ) , - 1 ) ;
9
+
10
+ // normal case
11
+ const names = [ 'John' , 'Jane' , 'Doe' , 'Smith' ] ;
12
+ names . forEach ( name => {
13
+ const student = new Student ( ) ;
14
+ student . setName ( name ) ;
15
+ const newStudentId = myClass . addStudent ( student ) ;
16
+ const newStudentName = myClass . getStudentById ( newStudentId ) . getName ( ) ;
17
+ assert . strictEqual ( newStudentName , name ) ;
18
+ } ) ;
8
19
} ) ;
9
20
10
21
test ( "Test MyClass's getStudentById" , ( ) => {
11
- // TODO
12
- throw new Error ( "Test not implemented" ) ;
22
+ const myClass = new MyClass ( ) ;
23
+ // if id is less than 0, return null
24
+ assert . strictEqual ( myClass . getStudentById ( - 1 ) , null ) ;
25
+
26
+ // normal case
27
+ const names = [ 'John' , 'Jane' , 'Doe' , 'Smith' ] ;
28
+ names . forEach ( name => {
29
+ const student = new Student ( ) ;
30
+ student . setName ( name ) ;
31
+ const newStudentId = myClass . addStudent ( student ) ;
32
+ const newStudent = myClass . getStudentById ( newStudentId ) ;
33
+ assert . strictEqual ( student , newStudent ) ;
34
+ } ) ;
35
+ // if id is greater than or equal to the length of students, return null
36
+ assert . strictEqual ( myClass . getStudentById ( names . length ) , null ) ;
13
37
} ) ;
14
38
15
39
test ( "Test Student's setName" , ( ) => {
16
- // TODO
17
- throw new Error ( "Test not implemented" ) ;
40
+ const student = new Student ( ) ;
41
+ student . setName ( 'John' ) ;
42
+ assert . strictEqual ( student . getName ( ) , 'John' ) ;
43
+ // Test if setName accepts only string
44
+ student . setName ( 123 ) ;
45
+ assert . strictEqual ( student . getName ( ) , 'John' ) ;
18
46
} ) ;
19
47
20
48
test ( "Test Student's getName" , ( ) => {
21
- // TODO
22
- throw new Error ( "Test not implemented" ) ;
49
+ const student = new Student ( ) ;
50
+ // Test if getName returns empty string if name is undefined
51
+ assert . strictEqual ( student . getName ( ) , '' ) ;
52
+ // Test if getName returns the name set by setName
53
+ student . setName ( 'John' ) ;
54
+ assert . strictEqual ( student . getName ( ) , 'John' ) ;
23
55
} ) ;
0 commit comments