@@ -2,22 +2,77 @@ const test = require('node:test');
22const assert = require ( 'assert' ) ;
33const { MyClass, Student } = require ( './main' ) ;
44
5+
56test ( "Test MyClass's addStudent" , ( ) => {
67 // 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." )
825} ) ;
926
1027test ( "Test MyClass's getStudentById" , ( ) => {
1128 // 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+
1350} ) ;
1451
1552test ( "Test Student's setName" , ( ) => {
1653 // 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" ) ;
1864} ) ;
1965
2066test ( "Test Student's getName" , ( ) => {
2167 // 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+
2378} ) ;
0 commit comments