Skip to content

Commit

Permalink
feat: lab1
Browse files Browse the repository at this point in the history
  • Loading branch information
TaiYou-TW committed Feb 28, 2024
1 parent 4e559b6 commit f0fdc31
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Description

<!-- Please briefly describe your change here -->

---

<!-- Please make sure you're satisfy and fill the following checkboxes -->
<!-- A good PR should include the following parts: -->

- [ ] A clear title
- [ ] A meaningful message for PR, as well as its commits
- [ ] From your specific branch (***not master***) 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
27 changes: 27 additions & 0 deletions .github/workflows/lab1.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: lab1

on:
pull_request:
types: [labeled]

jobs:
build:
if: ${{ github.event.label.name == 'lab1' }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-22.04]
fail-fast: false
steps:
- uses: actions/checkout@v1
with:
fetch-depth: 1
- name: dependency (ubuntu)
run: |
curl -fsSL https://deb.nodesource.com/setup_21.x | sudo -E bash - &&\
sudo apt-get install -y nodejs
- name: grading
run: |
echo "cd lab1"
cd lab1
./validate.sh
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# 112-spring-software-testing

Labs for NYCU software testing course in 112 spring
20 changes: 20 additions & 0 deletions lab1/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Lab1

## 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 `main.js.`

## 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%)

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 you modify other files to achieve requirements.

## 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.
55 changes: 55 additions & 0 deletions lab1/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// NOTICE: DO NOT MODIFY THE CODE IN THIS FILE
// But you can uncomment code below and run this file to understand how to use the classes

class MyClass {
constructor() {
this.students = [];
}

addStudent(student) {
if (!(student instanceof Student)) {
return -1;
}
this.students.push(student);
return this.students.length - 1;
}

getStudentById(id) {
if (id < 0 || id >= this.students.length) {
return null;
}
return this.students[id];
}
}

class Student {
constructor() {
this.name = undefined;
}

setName(userName) {
if (typeof userName !== 'string') {
return;
}
this.name = userName;
}

getName() {
if (this.name === undefined) {
return '';
}
return this.name;
}
}

// const myClass = new MyClass();
// const names = ['John', 'Jane', 'Doe', 'Smith'];
// names.forEach(name => {
// const student = new Student();
// student.setName(name);
// const newStudentId = myClass.addStudent(student);
// const newStudentName = myClass.getStudentById(newStudentId).getName();
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
// });

module.exports = { MyClass, Student };
23 changes: 23 additions & 0 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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");
});
30 changes: 30 additions & 0 deletions lab1/validate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

node=$(which node)
test_path="${BASH_SOURCE[0]}"
solution_path="$(realpath .)"
tmp_dir=$(mktemp -d -t lab1-XXXXXXXXXX)

cd $tmp_dir

rm -rf *
cp $solution_path/*.js .
result=$($node --test --experimental-test-coverage) ; ret=$?
if [ $ret -ne 0 ] ; then
echo "[!] testing fails"
exit 1
else
coverage=$(echo "$result" | grep 'all files' | awk -F '|' '{print $2}' | sed 's/ //g')
if (( $(echo "$coverage < 100" | bc -l) )); then
echo "[!] Coverage is only $coverage%"
exit 1
else
echo "[V] Coverage is 100%"
fi
fi

rm -rf $tmp_dir

exit 0

# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2:

0 comments on commit f0fdc31

Please sign in to comment.