-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.js
71 lines (57 loc) · 2.2 KB
/
main_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');
// 测试 MyClass 的 addStudent 方法
test("Test MyClass's addStudent", async () => {
const myClass = new MyClass();
const student = new Student();
student.setName('John Doe');
const index = myClass.addStudent(student);
assert.strictEqual(index, 0); // 第一个学生的索引应该是 0
});
// 测试 MyClass 的 getStudentById 方法
test("Test MyClass's getStudentById", async () => {
const myClass = new MyClass();
const student = new Student();
student.setName('John Doe');
const index = myClass.addStudent(student);
// 有效的 ID
const retrievedStudent = myClass.getStudentById(index);
assert.strictEqual(retrievedStudent, student);
// 无效的 ID
const invalidRetrieval = myClass.getStudentById(-1);
assert.strictEqual(invalidRetrieval, null);
});
// 测试 Student 的 setName 方法
test("Test Student's setName", async () => {
const student = new Student();
// 有效的名称
student.setName('Jane Doe');
assert.strictEqual(student.getName(), 'Jane Doe');
// 无效的名称(非字符串)
student.setName(123);
assert.notStrictEqual(student.getName(), 123); // 名称不应该设置为一个数字
});
// 测试 Student 的 getName 方法
test("Test Student's getName", async () => {
const student = new Student();
// 名称未设置
assert.strictEqual(student.getName(), ''); // 应该返回一个空字符串
// 名称已设置
student.setName('Jane Doe');
assert.strictEqual(student.getName(), 'Jane Doe');
});
// 测试 MyClass 的 removeStudent 方法
test("Test MyClass's removeStudent", async () => {
const myClass = new MyClass();
const student = new Student();
student.setName('John Doe');
const index = myClass.addStudent(student);
// 移除学生
const removedStudent = myClass.removeStudent(index);
assert.strictEqual(removedStudent, student);
// 尝试移除不存在的学生
const result = myClass.removeStudent(999);
assert.strictEqual(result, null);
});
// 添加更多测试用例以覆盖你预期的所有场景和边界情况