Skip to content

Commit 6c3bef3

Browse files
authored
Merge pull request #35 from tsaiet/312552050
[LAB1] 312552050
2 parents 2d41d26 + 5cfb80f commit 6c3bef3

File tree

1 file changed

+46
-23
lines changed

1 file changed

+46
-23
lines changed

lab1/main_test.js

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,46 @@
1-
const test = require('node:test');
2-
const assert = require('assert');
3-
const { MyClass, Student } = require('./main');
4-
5-
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
8-
});
9-
10-
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
13-
});
14-
15-
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
18-
});
19-
20-
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
1+
const test = require('node:test');
2+
const assert = require('assert');
3+
const { MyClass, Student } = require('./main');
4+
5+
test("Test MyClass's addStudent", () => {
6+
const mc = new MyClass();
7+
const st = new Student();
8+
assert.strictEqual(mc.addStudent(mc), -1); // not Student class
9+
assert.strictEqual(mc.addStudent(st), 0); // 1 students
10+
assert.strictEqual(mc.addStudent(st), 1); // 2 students
11+
});
12+
13+
test("Test MyClass's getStudentById", () => {
14+
const mc = new MyClass();
15+
16+
const st1 = new Student();
17+
st1.setName("st1");
18+
const st2 = new Student();
19+
st2.setName("st2");
20+
mc.addStudent(st1);
21+
mc.addStudent(st2);
22+
23+
assert.strictEqual(mc.getStudentById(-1), null); // id < 0
24+
assert.strictEqual(mc.getStudentById(2), null); // id >= length
25+
assert.strictEqual(mc.getStudentById(0), st1); // st1
26+
assert.strictEqual(mc.getStudentById(1), st2); // st2
27+
});
28+
29+
test("Test Student's setName", () => {
30+
const st1 = new Student();
31+
st1.setName("st1");
32+
const st2 = new Student();
33+
st2.setName(1);
34+
35+
assert.strictEqual(st1.name, "st1"); // st1
36+
assert.strictEqual(st2.name, undefined); // st2 not a string
37+
});
38+
39+
test("Test Student's getName", () => {
40+
const st1 = new Student();
41+
st1.setName("st1");
42+
const st2 = new Student();
43+
44+
assert.strictEqual(st1.getName(), "st1"); // st1
45+
assert.strictEqual(st2.getName(), ''); // st2
46+
});

0 commit comments

Comments
 (0)