Skip to content

[LAB1] 511558014 #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs

name: Node.js CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: npm test
39 changes: 30 additions & 9 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,42 @@ const assert = require('assert');
const { MyClass, Student } = require('./main');

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
const student = new Student();
student.setName('John Doe');
const index = myClass.addStudent(student);
assert.strictEqual(index, 0); // 應該成功添加並返回索引 0

const nonStudent = {}; // 非 Student 實例
const wrongIndex = myClass.addStudent(nonStudent);
assert.strictEqual(wrongIndex, -1); // 嘗試添加非 Student 實例應返回 -1
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
const student = new Student();
student.setName('Jane Doe');
const index = myClass.addStudent(student);
const retrievedStudent = myClass.getStudentById(index);
assert.strictEqual(retrievedStudent, student); // 應該返回相同的學生實例

const nullStudent = myClass.getStudentById(-1); // 錯誤的索引
assert.strictEqual(nullStudent, null); // 應該返回 null
});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
const student = new Student();
student.setName('John Doe');
assert.strictEqual(student.getName(), 'John Doe'); // setName 後 getName 應該返回設置的名字

student.setName(123); // 嘗試設置非字串
assert.strictEqual(student.getName(), 'John Doe'); // 名字應該保持不變
});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});
const student = new Student();
assert.strictEqual(student.getName(), ''); // 如果名字未設置應返回空字串

student.setName('Jane Doe');
assert.strictEqual(student.getName(), 'Jane Doe'); // setName 後 getName 應該返回設置的名字
});