From 0464cf425ef6d72f16098444b8543b636f5653c7 Mon Sep 17 00:00:00 2001 From: AxelHowe Date: Wed, 5 Mar 2025 21:38:16 +0800 Subject: [PATCH 1/5] feat: lab1 --- lab1/README.md | 22 +++++++++++++++++++ lab1/main.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++ lab1/main_test.js | 23 ++++++++++++++++++++ lab1/validate.sh | 38 ++++++++++++++++++++++++++++++++ 4 files changed, 138 insertions(+) create mode 100644 lab1/README.md create mode 100644 lab1/main.js create mode 100644 lab1/main_test.js create mode 100755 lab1/validate.sh diff --git a/lab1/README.md b/lab1/README.md new file mode 100644 index 0000000..df0ab9f --- /dev/null +++ b/lab1/README.md @@ -0,0 +1,22 @@ +# 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 it. (But remember don't commit them on GitHub) + +## Requirement + +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. + +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/lab1/main.js b/lab1/main.js new file mode 100644 index 0000000..c9aed9f --- /dev/null +++ b/lab1/main.js @@ -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 }; \ No newline at end of file diff --git a/lab1/main_test.js b/lab1/main_test.js new file mode 100644 index 0000000..74a716b --- /dev/null +++ b/lab1/main_test.js @@ -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"); +}); \ No newline at end of file diff --git a/lab1/validate.sh b/lab1/validate.sh new file mode 100755 index 0000000..8f2fcd4 --- /dev/null +++ b/lab1/validate.sh @@ -0,0 +1,38 @@ +#!/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 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%, should be 100%." + exit 1 + else + echo "[V] Coverage is 100%, great job!" + 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 From b630cfa70f0c7abf11fb3907652a007a6292f48b Mon Sep 17 00:00:00 2001 From: AxelHowe Date: Wed, 12 Mar 2025 20:50:08 +0800 Subject: [PATCH 2/5] feat: lab2 --- lab2/README.md | 22 +++++++++++++ lab2/main.js | 81 +++++++++++++++++++++++++++++++++++++++++++++++ lab2/main_test.js | 6 ++++ lab2/validate.sh | 38 ++++++++++++++++++++++ 4 files changed, 147 insertions(+) create mode 100644 lab2/README.md create mode 100644 lab2/main.js create mode 100644 lab2/main_test.js create mode 100755 lab2/validate.sh diff --git a/lab2/README.md b/lab2/README.md new file mode 100644 index 0000000..60a9c80 --- /dev/null +++ b/lab2/README.md @@ -0,0 +1,22 @@ +# 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 new file mode 100644 index 0000000..2e159e7 --- /dev/null +++ b/lab2/main.js @@ -0,0 +1,81 @@ +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 new file mode 100644 index 0000000..5034468 --- /dev/null +++ b/lab2/main_test.js @@ -0,0 +1,6 @@ +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 new file mode 100755 index 0000000..13b53ed --- /dev/null +++ b/lab2/validate.sh @@ -0,0 +1,38 @@ +#!/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 From d4fa0f32bcd4c6cbd5007afbb0728fa1a5421cf7 Mon Sep 17 00:00:00 2001 From: CTHua Date: Thu, 20 Mar 2025 12:57:56 +0800 Subject: [PATCH 3/5] feat: lab3 --- lab3/README.md | 29 +++++++++++++++++++++++++++++ lab3/main.js | 34 ++++++++++++++++++++++++++++++++++ lab3/main_test.js | 5 +++++ lab3/validate.sh | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 106 insertions(+) create mode 100644 lab3/README.md create mode 100644 lab3/main.js create mode 100644 lab3/main_test.js create mode 100755 lab3/validate.sh diff --git a/lab3/README.md b/lab3/README.md new file mode 100644 index 0000000..b591ab8 --- /dev/null +++ b/lab3/README.md @@ -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. 312XXXXXX, 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/lab3/main.js b/lab3/main.js new file mode 100644 index 0000000..cee5de7 --- /dev/null +++ b/lab3/main.js @@ -0,0 +1,34 @@ +class Calculator { + exp(x) { + if (!Number.isFinite(x)) { + throw Error('unsupported operand type'); + } + const result = Math.exp(x); + if (result === Infinity) { + throw Error('overflow'); + } + return result; + } + + log(x) { + if (!Number.isFinite(x)) { + throw Error('unsupported operand type'); + } + const result = Math.log(x); + if (result === -Infinity) { + throw Error('math domain error (1)'); + } + if (Number.isNaN(result)) { + throw Error('math domain error (2)'); + } + return result; + } +} + +// const calculator = new Calculator(); +// console.log(calculator.exp(87)); +// console.log(calculator.log(48763)); + +module.exports = { + Calculator +}; \ No newline at end of file diff --git a/lab3/main_test.js b/lab3/main_test.js new file mode 100644 index 0000000..e6d6414 --- /dev/null +++ b/lab3/main_test.js @@ -0,0 +1,5 @@ +const {describe, it} = require('node:test'); +const assert = require('assert'); +const { Calculator } = require('./main'); + +// TODO: write your tests here diff --git a/lab3/validate.sh b/lab3/validate.sh new file mode 100755 index 0000000..7a758fb --- /dev/null +++ b/lab3/validate.sh @@ -0,0 +1,38 @@ +#!/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 lab3-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 From 865c66fd8eb0bf4e853bf1fb312a866f288a2ba0 Mon Sep 17 00:00:00 2001 From: nanlioniya Date: Thu, 20 Mar 2025 15:32:04 +0800 Subject: [PATCH 4/5] feat: update lab2 test file --- lab2/main_test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lab2/main_test.js b/lab2/main_test.js index d7ee576..1c31633 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -15,6 +15,7 @@ const fs = require('fs'); - notifySelected */ +// MailSystem test test('MailSystem write test', (t) => { const mail = new MailSystem(); const result = mail.write('Test'); @@ -104,12 +105,12 @@ test('Application notifySelected test', async (t) => { let writeCount = 0; let sendCount = 0; app.mailSystem.write = (name) => { - writeCount++; - return `Congrats, ${name}!`; + writeCount++; + return `Congrats, ${name}!`; }; app.mailSystem.send = (name, context) => { - sendCount++; - return true; + sendCount++; + return true; }; app.notifySelected(); From 9a70133a84992da36c4c67b88eaf49153aa30f3b Mon Sep 17 00:00:00 2001 From: nanlioniya Date: Thu, 20 Mar 2025 15:56:57 +0800 Subject: [PATCH 5/5] lab3 with 100% coverage --- lab3/main_test.js | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/lab3/main_test.js b/lab3/main_test.js index e6d6414..1de3219 100644 --- a/lab3/main_test.js +++ b/lab3/main_test.js @@ -3,3 +3,63 @@ const assert = require('assert'); const { Calculator } = require('./main'); // TODO: write your tests here +describe('Calculator', () => { + const calculator = new Calculator(); + + describe('exp(x)', () => { + // 測試正常情況 + const normalCases = [ + { input: 0, expected: 1 }, + { input: 1, expected: Math.E }, + { input: -1, expected: 1/Math.E } + ]; + + normalCases.forEach(({ input, expected }) => { + it(`should correctly calculate exp(${input})`, () => { + assert.strictEqual(calculator.exp(input), expected); + }); + }); + + // 測試錯誤情況 + const errorCases = [ + { input: Infinity, error: 'unsupported operand type' }, + { input: NaN, error: 'unsupported operand type' }, + { input: 1000, error: 'overflow' } // 大數會造成溢位 + ]; + + errorCases.forEach(({ input, error }) => { + it(`should throw error for exp(${input})`, () => { + assert.throws(() => calculator.exp(input), { message: error }); + }); + }); + }); + + describe('log(x)', () => { + // 測試正常情況 + const normalCases = [ + { input: 1, expected: 0 }, + { input: Math.E, expected: 1 }, + { input: 10, expected: Math.log(10) } + ]; + + normalCases.forEach(({ input, expected }) => { + it(`should correctly calculate log(${input})`, () => { + assert.strictEqual(calculator.log(input), expected); + }); + }); + + // 測試錯誤情況 + const errorCases = [ + { input: Infinity, error: 'unsupported operand type' }, + { input: NaN, error: 'unsupported operand type' }, + { input: 0, error: 'math domain error (1)' }, + { input: -1, error: 'math domain error (2)' } + ]; + + errorCases.forEach(({ input, error }) => { + it(`should throw error for log(${input})`, () => { + assert.throws(() => calculator.log(input), { message: error }); + }); + }); + }); +});