@@ -2,22 +2,77 @@ const test = require('node:test');
2
2
const assert = require ( 'assert' ) ;
3
3
const { MyClass, Student } = require ( './main' ) ;
4
4
5
+
5
6
test ( "Test MyClass's addStudent" , ( ) => {
6
7
// TODO
7
- throw new Error ( "Test not implemented" ) ;
8
+ //throw new Error("Test not implemented");
9
+ const myClass = new MyClass ( ) ;
10
+ const student = new Student ( ) ;
11
+
12
+
13
+ //!(student instanceof Student)
14
+ const notAStudent = { name : "Rex" }
15
+ const notAStudentInstance = myClass . addStudent ( notAStudent ) ;
16
+ assert . strictEqual ( notAStudentInstance , - 1 , "If it's not a Student instance, it should return -1." ) ;
17
+
18
+ // push result
19
+ student . setName ( "Rex" ) ;
20
+ const addStudentResult = myClass . addStudent ( student ) ;
21
+ assert . strictEqual ( myClass . students [ 0 ] , student , "The student has not been added to the student list." ) ;
22
+
23
+ // return id
24
+ assert . strictEqual ( addStudentResult , 0 , "Return wrong id." )
8
25
} ) ;
9
26
10
27
test ( "Test MyClass's getStudentById" , ( ) => {
11
28
// TODO
12
- throw new Error ( "Test not implemented" ) ;
29
+ //throw new Error("Test not implemented");
30
+ const myClass = new MyClass ( ) ;
31
+ const student1 = new Student ( ) ;
32
+ const student2 = new Student ( ) ;
33
+ student1 . setName ( "Alice" ) ;
34
+ student2 . setName ( "Bob" ) ;
35
+ myClass . addStudent ( student1 ) ;
36
+ myClass . addStudent ( student2 ) ;
37
+
38
+
39
+ // id < 0
40
+ assert . strictEqual ( myClass . getStudentById ( - 1 ) , null , "get(-1) should be null" ) ;
41
+
42
+ // id >= this.students.length
43
+ assert . strictEqual ( myClass . getStudentById ( 2 ) , null , "Out of range should be null" ) ;
44
+
45
+
46
+ // return id
47
+ assert . strictEqual ( myClass . getStudentById ( 0 ) , student1 , "Abnormal return of ID value" ) ;
48
+ assert . strictEqual ( myClass . getStudentById ( 1 ) , student2 , "Abnormal return of ID value" ) ;
49
+
13
50
} ) ;
14
51
15
52
test ( "Test Student's setName" , ( ) => {
16
53
// TODO
17
- throw new Error ( "Test not implemented" ) ;
54
+ //throw new Error("Test not implemented");
55
+ const student = new Student ( ) ;
56
+
57
+ // return userName
58
+ student . setName ( "Alice" ) ;
59
+ assert . strictEqual ( student . getName ( ) , "Alice" , "setName Fail" ) ;
60
+
61
+ // typeof userName !== 'string'
62
+ student . setName ( 123 ) ;
63
+ assert . strictEqual ( student . getName ( ) , "Alice" , "userName should not be changed to anything other than a String" ) ;
18
64
} ) ;
19
65
20
66
test ( "Test Student's getName" , ( ) => {
21
67
// TODO
22
- throw new Error ( "Test not implemented" ) ;
68
+ //throw new Error("Test not implemented");
69
+
70
+ // this.name === undefined
71
+ const student = new Student ( ) ;
72
+ assert . strictEqual ( student . getName ( ) , "" , "When the name is not set, it should return an empty string" ) ;
73
+
74
+ // return name
75
+ student . setName ( "Bob" ) ;
76
+ assert . strictEqual ( student . getName ( ) , "Bob" , "Get the incorrect name" ) ;
77
+
23
78
} ) ;
0 commit comments