diff --git a/lab2/README.md b/lab2/README.md deleted file mode 100644 index 60a9c805..00000000 --- a/lab2/README.md +++ /dev/null @@ -1,22 +0,0 @@ -# Lab2 - -## 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) - -## Requirement - -1. Write test cases in `main_test.js` and achieve 100% code coverage. Remember to use Mock, Spy, or Stub when necessary, you need to at least use one of them in your test cases. (100%) - -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. diff --git a/lab2/main.js b/lab2/main.js deleted file mode 100644 index 2e159e75..00000000 --- a/lab2/main.js +++ /dev/null @@ -1,81 +0,0 @@ -const fs = require('fs'); -const util = require('util'); -const readFile = util.promisify(fs.readFile); - -class MailSystem { - write(name) { - console.log('--write mail for ' + name + '--'); - const context = 'Congrats, ' + name + '!'; - return context; - } - - send(name, context) { - console.log('--send mail to ' + name + '--'); - // Interact with mail system and send mail - // random success or failure - const success = Math.random() > 0.5; - if (success) { - console.log('mail sent'); - } else { - console.log('mail failed'); - } - return success; - } -} - -class Application { - constructor() { - this.people = []; - this.selected = []; - this.mailSystem = new MailSystem(); - this.getNames().then(([people, selected]) => { - this.people = people; - this.selected = selected; - }); - } - - async getNames() { - const data = await readFile('name_list.txt', 'utf8'); - const people = data.split('\n'); - const selected = []; - return [people, selected]; - } - - getRandomPerson() { - const i = Math.floor(Math.random() * this.people.length); - return this.people[i]; - } - - selectNextPerson() { - console.log('--select next person--'); - if (this.people.length === this.selected.length) { - console.log('all selected'); - return null; - } - let person = this.getRandomPerson(); - while (this.selected.includes(person)) { - person = this.getRandomPerson(); - } - this.selected.push(person); - return person; - } - - notifySelected() { - console.log('--notify selected--'); - for (const x of this.selected) { - const context = this.mailSystem.write(x); - this.mailSystem.send(x, context); - } - } -} - -// const app = new Application(); -// app.selectNextPerson(); -// app.selectNextPerson(); -// app.selectNextPerson(); -// app.notifySelected(); - -module.exports = { - Application, - MailSystem, -}; \ No newline at end of file diff --git a/lab2/main_test.js b/lab2/main_test.js deleted file mode 100644 index 5034468e..00000000 --- a/lab2/main_test.js +++ /dev/null @@ -1,6 +0,0 @@ -const test = require('node:test'); -const assert = require('assert'); -const { Application, MailSystem } = require('./main'); - -// TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary \ No newline at end of file diff --git a/lab2/validate.sh b/lab2/validate.sh deleted file mode 100755 index 13b53ed8..00000000 --- a/lab2/validate.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -# Check for unwanted files -for file in *; do - if [[ $file != "main.js" && $file != "main_test.js" && $file != "README.md" && $file != "validate.sh" ]]; then - echo "[!] Unwanted file detected: $file." - exit 1 - fi -done - -node=$(which node) -test_path="${BASH_SOURCE[0]}" -solution_path="$(realpath .)" -tmp_dir=$(mktemp -d -t lab2-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: \ No newline at end of file diff --git a/lab3/main_test.js b/lab3/main_test.js index 096fd421..2ef844ec 100644 --- a/lab3/main_test.js +++ b/lab3/main_test.js @@ -3,3 +3,4 @@ const assert = require('assert'); const { Calculator } = require('./main'); // TODO: write your tests here +});