diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml new file mode 100644 index 00000000..eb4d9d56 --- /dev/null +++ b/.github/workflows/node.js.yml @@ -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 diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..45817f96 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -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"); -}); \ No newline at end of file + const student = new Student(); + assert.strictEqual(student.getName(), ''); // 如果名字未設置應返回空字串 + + student.setName('Jane Doe'); + assert.strictEqual(student.getName(), 'Jane Doe'); // setName 後 getName 應該返回設置的名字 +});