Skip to content
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

[LAB7] 311552005 #602

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ec49344
Update main_test.js
patty5531998 Mar 8, 2024
cfe2098
Merge pull request #91 from patty5531998/311552005
TaiYou-TW Mar 8, 2024
386181d
Update main_test.js
patty5531998 Mar 12, 2024
3e58e56
feat: new workflows
AlaRduTP Mar 14, 2024
e73574e
Update main_test.js
patty5531998 Mar 14, 2024
a594f1a
Update main_test.js
patty5531998 Mar 14, 2024
d94c552
Update main_test.js
patty5531998 Mar 14, 2024
f503783
Update main_test.js
patty5531998 Mar 14, 2024
b7cbdde
fix: wrong GITHUB SHA when checkout in workflow
AlaRduTP Mar 14, 2024
b8b9293
Merge branch '311552005' into 311552005
patty5531998 Mar 14, 2024
57f2fa0
hotfix: remove changed files check
AlaRduTP Mar 14, 2024
16cb378
Update main_test.js
patty5531998 Mar 14, 2024
15dccae
Merge pull request #138 from patty5531998/311552005
AlaRduTP Mar 20, 2024
dead294
feat: lab4
AlaRduTP Mar 21, 2024
662b073
Update main_test.js
patty5531998 Mar 27, 2024
a958c0e
Update main_test.js
patty5531998 Mar 27, 2024
9d48927
Update main_test.js
patty5531998 Mar 27, 2024
cc7af22
Update main_test.js
patty5531998 Mar 27, 2024
2fe9cc8
Update main_test.js
patty5531998 Mar 28, 2024
14b0af6
Update main_test.js
patty5531998 Mar 28, 2024
d65ec16
Update main_test.js
patty5531998 Mar 28, 2024
06c21f9
Merge pull request #186 from patty5531998/lab3
AlaRduTP Apr 9, 2024
193fe53
Merge pull request #383 from patty5531998/lab4
AlaRduTP Apr 9, 2024
8072b3f
feat: merge lab5
YingMuo Apr 18, 2024
90d84fa
Merge: lab6
YingMuo Apr 25, 2024
89cf768
feat: lab7
AlaRduTP May 2, 2024
5ae4ea4
Update sol.py
patty5531998 May 8, 2024
6200877
Update sol.py
patty5531998 May 8, 2024
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
34 changes: 19 additions & 15 deletions .github/workflows/PR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,31 @@ jobs:
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+)\]/;
const titleRegex = /^\[LAB\d+\] [\da-zA-Z]+$/;

if (!titleRegex.test(title)) {
core.setFailed('PR title does not match the required format. Please use the format [LAB#] student#.');
const titleRegex = /^\[LAB(\d+)\] [a-zA-Z]?\d+$/;
const match = title.match(titleRegex);

let labNumberStr = undefined;
if (match) {
labNumberStr = match[1];
} else {
core.setFailed('PR title does not match the required format. Please use the format: [LAB#] <studentId>.');
}

if (pr.data.head.ref !== pr.data.base.ref) {
core.setFailed('The source branch and target branch must be the same.');
const labelToAdd = `lab${labNumberStr}`;
if (labNumberStr) {
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] });
}

if (pr.data.base.ref === 'main') {
core.setFailed('The target branch cannot be main.');
}

const match = title.match(labRegex);
if (match) {
const labelToAdd = 'lab' + match[1];
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [labelToAdd] });
} else {
core.setFailed('No match found in PR title. Please add a label in the format [LAB#] to the PR title.');
if (labNumberStr < 3 && pr.data.head.ref !== pr.data.base.ref) {
core.setFailed('The source branch and target branch must be the same.');
}
if (labNumberStr >= 3 && pr.data.head.ref !== labelToAdd) {
core.setFailed(`The source branch must be '${labelToAdd}'`);
}
checklist-check:
runs-on: ubuntu-latest
Expand All @@ -49,12 +53,12 @@ jobs:
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
const body = pr.data.body;

const checkboxes = body.match(/\- \[[x ]\]/g);
const checkboxes = body.match(/^ ?(-|\*) \[[x ]\]/gmi);
if (!checkboxes || checkboxes.length !== 5) {
core.setFailed('The PR description must contain exactly 5 checkboxes.');
}

const unchecked = body.match(/\- \[ \]/g);
const unchecked = body.match(/^ ?(-|\*) \[ \]/gm);
if (unchecked && unchecked.length > 0) {
core.setFailed(`There are ${unchecked.length} unchecked items in the PR description.`);
core.setFailed(`There are ${unchecked.length} unchecked item(s) in the PR description.`);
}
56 changes: 56 additions & 0 deletions .github/workflows/lab-autograding.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Autograding

on:
pull_request_target:
types: [labeled, synchronize, opened, reopened, ready_for_review]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04]
fail-fast: false
steps:
- uses: actions/checkout@v4
with:
ref: "${{ github.event.pull_request.merge_commit_sha }}"
fetch-depth: 1
- uses: actions/setup-node@v4
with:
node-version: latest
- name: Extract lab number and Check no changes other than specific files
uses: actions/github-script@v5
id: lab
with:
result-encoding: string
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const { owner, repo, number: issue_number } = context.issue;
const pr = await github.rest.pulls.get({ owner, repo, pull_number: issue_number });
const labels = pr.data.labels;
const lab = labels.find((label) => label.name.startsWith('lab'));
if (!lab) {
core.setFailed('No lab label found on the PR.');
return { number: 0 };
}
const labNumberMatch = lab.name.match(/lab(\d+)/);
if (!labNumberMatch) {
core.setFailed('Invalid lab label found on the PR.');
return { number: 0 };
}
const labNumber = labNumberMatch[1];
console.log(`Lab number: ${labNumber}`)

const files = await github.rest.pulls.listFiles({ owner, repo, pull_number: issue_number });
const changedFiles = files.data.map((file) => file.filename);
const allowedFileRegex = /^lab\d+\/main_test.js$/;
const specialChangedFiles = ["lab5/Answer.md", "lab5/antiasan.c", "lab6/Answer.md", "lab7/sol.py"];
if (!changedFiles.every((file) => (allowedFileRegex.test(file) || specialChangedFiles.includes(file)))) {
core.setFailed('The PR contains changes to files other than the allowed files.');
}
return labNumber;
- name: Grading
run: |
cd lab${{ steps.lab.outputs.result }}
./validate.sh
26 changes: 0 additions & 26 deletions .github/workflows/lab1.yml

This file was deleted.

26 changes: 0 additions & 26 deletions .github/workflows/lab2.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
51 changes: 42 additions & 9 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,54 @@ const assert = require('assert');
const { MyClass, Student } = require('./main');

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");

//case 1
const myclass = new MyClass();
assert.strictEqual(myclass.addStudent("for_test"), -1);

//case 2
const student = new Student();
assert.strictEqual(myclass.addStudent(student), 0);

});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");

//case 1
const myclass = new MyClass();
assert.strictEqual(myclass.getStudentById(-1), null);

//case 2
assert.strictEqual(myclass.getStudentById(1),null);

//case 3
const student1 = new Student();
myclass.addStudent(student1);
assert.strictEqual(myclass.getStudentById(0),student1);

});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");

const student = new Student();

//case 1
assert.strictEqual(student.setName(1), undefined);

//case 2
assert.strictEqual(student.setName('Amy'), undefined);

});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});

const student = new Student();

//case 1
assert.strictEqual(student.getName(),'');

//case 2
student.setName('Amy');
assert.strictEqual(student.getName(),'Amy');

});
102 changes: 100 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,104 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');

test.mock.method(fs, "readFile", (path, encoding, callback) => {
callback(null, "Amy\nJack");
});

const names = ['Amy', 'Jack'];
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
test("Test MailSystem's write", () => {

const mailSystem = new MailSystem();
assert.strictEqual(mailSystem.write(names[0]), 'Congrats, ' + names[0] + '!');

});

test("Test MailSystem's send", () => {

const mailSystem = new MailSystem();

//case 1
test.mock.method(Math, "random", () => true);
for(let i = 0; i < names.length; i++) {
assert.strictEqual(mailSystem.send(names[i]), true);
}

//case 2
test.mock.method(Math, "random", () => false);
for(let i = 0; i < names.length; i++) {
assert.strictEqual(mailSystem.send(names[i]), false);
}

});

test("Test Application's getNames", async () => {

const application = new Application();
application.people = names;
const [people, selected] = await application.getNames();
assert.deepStrictEqual(people, names);
assert.deepStrictEqual(selected, []);

});

test("Test Application's getRandomPerson", () => {

const application = new Application();
application.people = names;

for(let i = 0; i < names.length; i++) {
test.mock.method(Math, "floor", () => i);
assert.strictEqual(application.getRandomPerson(), names[i]);
}

});

test("Test Application's selectNextPerson", async () => {

const application = new Application();
application.people = names;

application.getRandomPerson = () => "Amy";
assert.strictEqual(application.selectNextPerson(),'Amy');
assert.deepStrictEqual(application.selected,["Amy"]);

let count = 1;
application.getRandomPerson = () => {
if (count % 2){
count++;
return "Amy";
}
return "Jack";
};
assert.strictEqual(application.selectNextPerson(),'Jack');
assert.deepStrictEqual(application.selected,["Amy","Jack"]);

assert.strictEqual(application.selectNextPerson(),null);

});

test("Test Application's notifySelected", () => {

const application = new Application();

application.mailSystem.write = test.mock.fn(application.mailSystem.write);
application.mailSystem.send = test.mock.fn(() => true);

application.selected = names;
application.notifySelected();


//check call arguments
assert.strictEqual(application.mailSystem.write.mock.calls[0].arguments[0], names[0]);
assert.strictEqual(application.mailSystem.send.mock.calls[0].arguments[0], names[0]);
assert.strictEqual(application.mailSystem.write.mock.calls[1].arguments[0], names[1]);
assert.strictEqual(application.mailSystem.send.mock.calls[1].arguments[0], names[1]);

// //check call times
assert.strictEqual(application.mailSystem.write.mock.calls.length, names.length);
assert.strictEqual(application.mailSystem.send.mock.calls.length, names.length);

});
29 changes: 29 additions & 0 deletions lab3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Lab3

## Introduction

In this lab, you will write unit tests for functions implemented in `main.js`. You can learn how to use classes and functions in it by uncommenting the code in it. (But remember don't commit them on GitHub)

## Preparation (Important!!!)

1. Sync fork on GitHub
2. `git checkout -b lab3` (**NOT** your student ID !!!)

## Requirement

1. (40%) Write test cases in `main_test.js` and achieve 100% code coverage.
2. (30%) For each function, parameterize their testcases to test the error-results.
3. (30%) For each function, use at least 3 parameterized testcases to test the non-error-results.

You can run `validate.sh` in your local to test if you satisfy the requirements.

Please note that you must not alter files other than `main_test.js`. You will get 0 points if

1. you modify other files to achieve requirements.
2. you can't pass all CI on your PR.

## Submission

You need to open a pull request to your branch (e.g. 311XXXXXX, your student number) and contain the code that satisfies the abovementioned requirements.

Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.
Loading
Loading