Skip to content

Commit 8e6a34b

Browse files
authored
Merge pull request SQLab#66 from Highwaist/511558025
[LAB1] 511558025
2 parents dfaa7dc + 9735cfb commit 8e6a34b

File tree

1 file changed

+48
-9
lines changed

1 file changed

+48
-9
lines changed

lab1/main_test.js

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,60 @@ 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+
//create 2 students John & Jane
8+
const student1 = new Student();
9+
student1.setName('John');
10+
const id1 = myClass.addStudent(student1);
11+
assert.strictEqual(id1, 0);
12+
13+
const student2 = new Student();
14+
student2.setName('Jane');
15+
const id2 = myClass.addStudent(student2);
16+
assert.strictEqual(id2, 1);
17+
18+
//新增一個TESTING student
19+
const invalidStudent = 'TESTING';
20+
const invalidId = myClass.addStudent(invalidStudent);
21+
assert.strictEqual(invalidId, -1);
22+
823
});
924

1025
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
26+
const myClass = new MyClass();
27+
const student1 = new Student();
28+
student1.setName('John');
29+
myClass.addStudent(student1);
30+
31+
const retrievedStudent = myClass.getStudentById(0);
32+
assert.strictEqual(retrievedStudent.getName(), 'John');
33+
34+
const invalidStudent = myClass.getStudentById(-1);
35+
assert.strictEqual(invalidStudent, null);
36+
37+
const outOfBoundsStudent = myClass.getStudentById(1);
38+
assert.strictEqual(outOfBoundsStudent, null);
39+
1340
});
1441

1542
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
43+
const student = new Student();
44+
//測試變數是否有效
45+
student.setName('Alice');
46+
assert.strictEqual(student.getName(), 'Alice');
47+
48+
student.setName(123);
49+
assert.strictEqual(student.getName(), 'Alice');
50+
1851
});
1952

2053
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
54+
const student = new Student();
55+
// 初始化未設置
56+
assert.strictEqual(student.getName(), '');
57+
58+
// 設置變數後返回
59+
student.setName('Bob');
60+
assert.strictEqual(student.getName(), 'Bob');
61+
62+
});

0 commit comments

Comments
 (0)