Skip to content

Commit 804b083

Browse files
authored
Merge pull request #49 from Yyrff/511558014
[LAB1] 511558014
2 parents dfaa7dc + 43c319a commit 804b083

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

Diff for: .github/workflows/node.js.yml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ "main" ]
9+
pull_request:
10+
branches: [ "main" ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [14.x, 16.x, 18.x]
20+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21+
22+
steps:
23+
- uses: actions/checkout@v3
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v3
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
cache: 'npm'
29+
- run: npm ci
30+
- run: npm run build --if-present
31+
- run: npm test

Diff for: lab1/main_test.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,42 @@ 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+
student.setName('John Doe');
9+
const index = myClass.addStudent(student);
10+
assert.strictEqual(index, 0); // 應該成功添加並返回索引 0
11+
12+
const nonStudent = {}; // 非 Student 實例
13+
const wrongIndex = myClass.addStudent(nonStudent);
14+
assert.strictEqual(wrongIndex, -1); // 嘗試添加非 Student 實例應返回 -1
815
});
916

1017
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
18+
const myClass = new MyClass();
19+
const student = new Student();
20+
student.setName('Jane Doe');
21+
const index = myClass.addStudent(student);
22+
const retrievedStudent = myClass.getStudentById(index);
23+
assert.strictEqual(retrievedStudent, student); // 應該返回相同的學生實例
24+
25+
const nullStudent = myClass.getStudentById(-1); // 錯誤的索引
26+
assert.strictEqual(nullStudent, null); // 應該返回 null
1327
});
1428

1529
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
30+
const student = new Student();
31+
student.setName('John Doe');
32+
assert.strictEqual(student.getName(), 'John Doe'); // setName 後 getName 應該返回設置的名字
33+
34+
student.setName(123); // 嘗試設置非字串
35+
assert.strictEqual(student.getName(), 'John Doe'); // 名字應該保持不變
1836
});
1937

2038
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
39+
const student = new Student();
40+
assert.strictEqual(student.getName(), ''); // 如果名字未設置應返回空字串
41+
42+
student.setName('Jane Doe');
43+
assert.strictEqual(student.getName(), 'Jane Doe'); // setName 後 getName 應該返回設置的名字
44+
});

0 commit comments

Comments
 (0)