diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md index 78b908d9..31e90c3d 100644 --- a/.github/pull_request_template.md +++ b/.github/pull_request_template.md @@ -1,5 +1,8 @@ ## Description + + + --- @@ -7,9 +10,10 @@ -- [ ] A clear title + +- [ ] A clear title (name your pr "[LAB{lab_number}] {your_student_id}") - [ ] A meaningful message for PR, as well as its commits -- [ ] From your specific branch (***not master***) merging to your branch +- [ ] From your specific branch (***not main or other's branch***) merging to your branch - [ ] Excluding any irrelevant files, such as binaries, text files, or dot files - [ ] Passing tests/CI -- [ ] Add proper label for this PR + diff --git a/.github/workflows/label.yml b/.github/workflows/label.yml new file mode 100644 index 00000000..4e7902a5 --- /dev/null +++ b/.github/workflows/label.yml @@ -0,0 +1,24 @@ +name: Automatic PR Labeler +on: + pull_request_target: + types: [opened, reopened, edited] + +jobs: + labeler: + runs-on: ubuntu-latest + steps: + - name: Label PR + uses: actions/github-script@v5 + with: + github-token: ${{ secrets.PAT }} + script: | + const { owner, repo, number: issue_number } = context.issue; + const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number }); + const title = pr.data.title; + const labRegex = /\[LAB(\d+)\]/i; + + const match = title.match(labRegex); + if (match) { + const labelToAdd = 'lab' + match[1]; + await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] }); + } diff --git a/lab1/README.md b/lab1/README.md index f205b0b9..779166ca 100644 --- a/lab1/README.md +++ b/lab1/README.md @@ -6,8 +6,7 @@ In this lab, you will write unit tests for functions implemented in `main.js`. Y ## Requirement -1. Write test cases in `main_test.js` and achieve 100% code coverage. (90%) -2. Add a badge and make it show `passing` in `README.md` in the root folder. (10%) +1. Write test cases in `main_test.js` and achieve 100% code coverage. (100%) You can run `validate.sh` in your local to test if you satisfy the requirements. diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..58d28cbb 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,23 +1,47 @@ -const test = require('node:test'); -const assert = require('assert'); -const { MyClass, Student } = require('./main'); - -test("Test MyClass's addStudent", () => { - // TODO - throw new Error("Test not implemented"); -}); - -test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); -}); - -test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); -}); - -test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file +const test = require('node:test'); +const assert = require('assert'); +const { MyClass, Student } = require('./main'); + +test("Test MyClass's addStudent", () => { + const mc = new MyClass(); + const st = new Student(); + assert.strictEqual(mc.addStudent(mc), -1); // not Student class + assert.strictEqual(mc.addStudent(st), 0); // 1 students + assert.strictEqual(mc.addStudent(st), 1); // 2 students +}); + +test("Test MyClass's getStudentById", () => { + const mc = new MyClass(); + + const st1 = new Student(); + st1.setName("st1"); + const st2 = new Student(); + st2.setName("st2"); + mc.addStudent(st1); + mc.addStudent(st2); + + assert.strictEqual(mc.getStudentById(-1), null); // id < 0 + assert.strictEqual(mc.getStudentById(2), null); // id >= length + assert.strictEqual(mc.getStudentById(0), st1); // st1 + assert.strictEqual(mc.getStudentById(1), st2); // st2 +}); + +test("Test Student's setName", () => { + const st1 = new Student(); + st1.setName("st1"); + const st2 = new Student(); + st2.setName(1); + + assert.strictEqual(st1.name, "st1"); // st1 + assert.strictEqual(st2.name, undefined); // st2 not a string +}); + +test("Test Student's getName", () => { + const st1 = new Student(); + st1.setName("st1"); + const st2 = new Student(); + + assert.strictEqual(st1.getName(), "st1"); // st1 + assert.strictEqual(st2.getName(), ''); // st2 +}); + diff --git a/lab1/validate.sh b/lab1/validate.sh index dca233b1..6eb7ac2c 100755 --- a/lab1/validate.sh +++ b/lab1/validate.sh @@ -9,7 +9,9 @@ cd $tmp_dir rm -rf * cp $solution_path/*.js . -result=$($node --test --experimental-test-coverage) ; ret=$? + +result=$($"node" --test --experimental-test-coverage) ; ret=$? + if [ $ret -ne 0 ] ; then echo "[!] testing fails" exit 1