@@ -6,59 +6,70 @@ test("Test MyClass's addStudent", () => {
6
6
const myClass = new MyClass ( ) ;
7
7
const student = new Student ( ) ;
8
8
student . setName ( 'Alice' ) ;
9
-
9
+
10
10
const result = myClass . addStudent ( student ) ;
11
-
11
+
12
12
assert . strictEqual ( result , 0 ) ;
13
13
assert . strictEqual ( myClass . students . length , 1 ) ;
14
14
} ) ;
15
15
16
- test ( "Test MyClass's getStudentById" , ( ) => {
16
+ test ( "Test MyClass's addStudent with invalid student" , ( ) => {
17
+ const myClass = new MyClass ( ) ;
18
+ const result = myClass . addStudent ( { } ) ;
19
+ assert . strictEqual ( result , - 1 ) ;
20
+ assert . strictEqual ( myClass . students . length , 0 ) ;
21
+ } ) ;
22
+
23
+ test ( "Test MyClass's getStudentById with valid id" , ( ) => {
17
24
const myClass = new MyClass ( ) ;
18
25
const student = new Student ( ) ;
19
26
student . setName ( 'Bob' ) ;
20
27
myClass . addStudent ( student ) ;
21
-
28
+
22
29
const result = myClass . getStudentById ( 0 ) ;
23
-
30
+
24
31
assert . strictEqual ( result , student ) ;
25
32
} ) ;
26
33
34
+ test ( "Test MyClass's getStudentById with invalid id" , ( ) => {
35
+ const myClass = new MyClass ( ) ;
36
+ const result = myClass . getStudentById ( 0 ) ;
37
+ assert . strictEqual ( result , null ) ;
38
+ } ) ;
39
+
40
+ test ( "Test MyClass's getStudentById with negative id" , ( ) => {
41
+ const myClass = new MyClass ( ) ;
42
+ const result = myClass . getStudentById ( - 1 ) ;
43
+ assert . strictEqual ( result , null ) ;
44
+ } ) ;
45
+
46
+ test ( "Test MyClass's getStudentById with id beyond length" , ( ) => {
47
+ const myClass = new MyClass ( ) ;
48
+ const result = myClass . getStudentById ( 1 ) ;
49
+ assert . strictEqual ( result , null ) ;
50
+ } ) ;
51
+
27
52
test ( "Test Student's setName" , ( ) => {
28
53
const student = new Student ( ) ;
29
54
student . setName ( 'John' ) ;
30
-
31
55
assert . strictEqual ( student . name , 'John' ) ;
32
56
} ) ;
33
57
34
- test ( "Test Student's getName" , ( ) => {
58
+ test ( "Test Student's setName with invalid input" , ( ) => {
59
+ const student = new Student ( ) ;
60
+ student . setName ( 123 ) ; // Passing invalid input
61
+ assert . strictEqual ( student . name , undefined ) ;
62
+ } ) ;
63
+
64
+ test ( "Test Student's getName when name is set" , ( ) => {
35
65
const student = new Student ( ) ;
36
66
student . setName ( 'Alice' ) ;
37
-
38
67
const name = student . getName ( ) ;
39
-
40
68
assert . strictEqual ( name , 'Alice' ) ;
41
69
} ) ;
42
- const test = require ( 'node:test' ) ;
43
- const assert = require ( 'assert' ) ;
44
- const { MyClass, Student } = require ( './main' ) ;
45
-
46
- test ( "Test MyClass's addStudent" , ( ) => {
47
- // TODO
48
- throw new Error ( "Test not implemented" ) ;
49
- } ) ;
50
-
51
- test ( "Test MyClass's getStudentById" , ( ) => {
52
- // TODO
53
- throw new Error ( "Test not implemented" ) ;
54
- } ) ;
55
-
56
- test ( "Test Student's setName" , ( ) => {
57
- // TODO
58
- throw new Error ( "Test not implemented" ) ;
59
- } ) ;
60
70
61
- test ( "Test Student's getName" , ( ) => {
62
- // TODO
63
- throw new Error ( "Test not implemented" ) ;
71
+ test ( "Test Student's getName when name is not set" , ( ) => {
72
+ const student = new Student ( ) ;
73
+ const name = student . getName ( ) ;
74
+ assert . strictEqual ( name , '' ) ;
64
75
} ) ;
0 commit comments