@@ -2,40 +2,42 @@ 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" , ( ) => {
6
- // TODO
7
- const myClass = new MyClass ( ) ;
8
- const student = new Student ( "John" ) ;
9
- myClass . addStudent ( student ) ;
10
- assert . strictEqual ( myClass . students . length , 1 ) ;
11
- assert . strictEqual ( myClass . students [ 0 ] , student ) ;
12
- //throw new Error("Test not implemented");
5
+ test ( "Test MyClass's addStudent method" , async ( t ) => {
6
+ const myClass = new MyClass ( ) ;
7
+ const student = new Student ( ) ;
8
+ student . setName ( "John" ) ;
9
+ const index = myClass . addStudent ( student ) ;
10
+ assert . strictEqual ( index , 0 ) ;
13
11
} ) ;
14
12
15
- test ( "Test MyClass's getStudentById" , ( ) => {
16
- // TODO
17
- const myClass = new MyClass ( ) ;
18
- const student1 = new Student ( "John" ) ;
19
- const student2 = new Student ( "Jane" ) ;
20
- const student3 = new Student ( "Doe" ) ;
21
- myClass . addStudent ( student1 ) ;
22
- myClass . addStudent ( student2 ) ;
23
- myClass . addStudent ( student3 ) ;
24
- const foundStudent = myClass . getStudentById ( student2 . id ) ;
25
- assert . strictEqual ( foundStudent , student2 ) ;
26
- //throw new Error("Test not implemented");
13
+ test ( "Test MyClass's getStudentById method" , async ( t ) => {
14
+ const myClass = new MyClass ( ) ;
15
+ const student = new Student ( ) ;
16
+ student . setName ( "Jane" ) ;
17
+ const index = myClass . addStudent ( student ) ;
18
+ const fetchedStudent = myClass . getStudentById ( index ) ;
19
+ assert . strictEqual ( fetchedStudent . getName ( ) , "Jane" ) ;
27
20
} ) ;
28
21
29
- test ( "Test Student's setName" , ( ) => {
30
- // TODO
31
- const student = new Student ( "John" ) ;
32
- student . setName ( "Smith" ) ;
33
- assert . strictEqual ( student . getName ( ) , "Smith" ) ;
22
+ test ( "Test Student's setName and getName methods" , async ( t ) => {
23
+ const student = new Student ( ) ;
24
+ student . setName ( "Doe" ) ;
25
+ assert . strictEqual ( student . getName ( ) , "Doe" ) ;
34
26
} ) ;
35
27
36
- test ( "Test Student's getName" , ( ) => {
37
- // TODO
38
- const student = new Student ( "John" ) ;
39
- assert . strictEqual ( student . getName ( ) , "John" ) ;
40
- //throw new Error("Test not implemented");
28
+ test ( "Test handling non-Student instance in addStudent" , async ( t ) => {
29
+ const myClass = new MyClass ( ) ;
30
+ const notAStudent = { } ;
31
+ const index = myClass . addStudent ( notAStudent ) ;
32
+ assert . strictEqual ( index , - 1 ) ;
33
+ } ) ;
34
+
35
+ test ( "Test getStudentById with invalid id" , async ( t ) => {
36
+ const myClass = new MyClass ( ) ;
37
+ const student = new Student ( ) ;
38
+ student . setName ( "Smith" ) ;
39
+ myClass . addStudent ( student ) ;
40
+ const invalidId = - 1 ;
41
+ const fetchedStudent = myClass . getStudentById ( invalidId ) ;
42
+ assert . strictEqual ( fetchedStudent , null ) ;
41
43
} ) ;
0 commit comments