Skip to content

Commit f4c7e91

Browse files
authored
Merge pull request #64 from banana1715/313551150
[LAB1] 313551150
2 parents 49da754 + f895388 commit f4c7e91

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

lab0/lab0.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Hello world!");

lab1/main_test.js

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,58 @@ const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
6+
const myClass = new MyClass();
7+
const student = new Student();
8+
9+
// Valid student case
10+
const studentId = myClass.addStudent(student);
11+
assert.strictEqual(studentId, 0);
12+
13+
// Invalid input case
14+
const invalidId = myClass.addStudent({});
15+
assert.strictEqual(invalidId, -1);
816
});
917

1018
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
19+
const myClass = new MyClass();
20+
const student = new Student();
21+
student.setName("John");
22+
23+
const studentId = myClass.addStudent(student);
24+
25+
// Valid retrieval
26+
const retrievedStudent = myClass.getStudentById(studentId);
27+
assert.ok(retrievedStudent instanceof Student);
28+
assert.strictEqual(retrievedStudent.getName(), "John");
29+
30+
// Invalid ID cases
31+
assert.strictEqual(myClass.getStudentById(-1), null);
32+
assert.strictEqual(myClass.getStudentById(101), null);
1333
});
1434

35+
1536
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
37+
const student = new Student();
38+
39+
// Valid name setting
40+
student.setName("Jane");
41+
assert.strictEqual(student.getName(), "Jane");
42+
43+
// Invalid input cases
44+
student.setName(123);
45+
assert.strictEqual(student.getName(), "Jane"); // Should not change
46+
47+
student.setName(null);
48+
assert.strictEqual(student.getName(), "Jane"); // Should not change
1849
});
1950

2051
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
52+
const student = new Student();
53+
54+
// Default value case
55+
assert.strictEqual(student.getName(), '');
56+
57+
// After setting a valid name
58+
student.setName("Alice");
59+
assert.strictEqual(student.getName(), "Alice");
60+
});

0 commit comments

Comments
 (0)