From f4f586c8412a59531d71130e62b229c07b4fc550 Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 29 Feb 2024 15:12:28 +0800 Subject: [PATCH 1/7] test --- lab1/main_test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..5248e99f 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -3,7 +3,8 @@ const assert = require('assert'); const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { - // TODO + //TODO + testetest; throw new Error("Test not implemented"); }); From 412f4a2fc5ffc3e95c112421f2a0bc3f5f753677 Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 29 Feb 2024 15:46:48 +0800 Subject: [PATCH 2/7] add testing function --- lab1/main_test.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 5248e99f..7ce5aab7 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -4,21 +4,48 @@ const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { //TODO - testetest; + const myclass = new MyClass(); + assert.strictEqual(myclass.addStudent(), 0); + assert.strictEqual(myclass.addStudent(), 1); + assert.strictEqual("Return -1", -1); throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { // TODO + const myclass = new MyClass(); + assert.strictEqual(myclass.getStudentById(-1), null); + assert.strictEqual(myclass.getStudentById(myclass.students.length), null); + + const student1 = new Student(); + student1.setName("std1"); + myclass.addStudent(student1); + + const student2 = new Student(); + student2.setName("std2"); + myclass.addStudent(student2); + + for (i = 0; i < myclass.students.length(); i++) { + assert.strictEqual(myclass.getStudentById(i), myclass.students[i]); + } + throw new Error("Test not implemented"); }); test("Test Student's setName", () => { // TODO + const student = new Student(); + assert.strictEqual(student.name("std1"), undefined); + student.setName("std1"); + assert.strictEqual(student.name, "std1"); throw new Error("Test not implemented"); }); test("Test Student's getName", () => { // TODO + const student = new Student(); + assert.strictEqual(student.getName(), ''); + student.setName("std1"); + assert.strictEqual(student.getName(), "std1"); throw new Error("Test not implemented"); }); \ No newline at end of file From 2754e71461525d86d3c7994fc21cb13e5814ec24 Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 29 Feb 2024 16:09:08 +0800 Subject: [PATCH 3/7] fix setName testing function --- lab1/main_test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 7ce5aab7..5d50ba45 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -4,7 +4,7 @@ const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { //TODO - const myclass = new MyClass(); + const a = new MyClass(); assert.strictEqual(myclass.addStudent(), 0); assert.strictEqual(myclass.addStudent(), 1); assert.strictEqual("Return -1", -1); @@ -35,7 +35,7 @@ test("Test MyClass's getStudentById", () => { test("Test Student's setName", () => { // TODO const student = new Student(); - assert.strictEqual(student.name("std1"), undefined); + assert.strictEqual(student.name, undefined); student.setName("std1"); assert.strictEqual(student.name, "std1"); throw new Error("Test not implemented"); From 02fa7d14731ad8e769767544b999a18ea5d68ad5 Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 7 Mar 2024 20:15:01 +0800 Subject: [PATCH 4/7] fix cl error --- lab1/main_test.js | 5 --- lab2/README.md | 22 -------------- lab2/main.js | 77 ----------------------------------------------- lab2/main_test.js | 6 ---- lab2/validate.sh | 38 ----------------------- 5 files changed, 148 deletions(-) delete mode 100644 lab2/README.md delete mode 100644 lab2/main.js delete mode 100644 lab2/main_test.js delete mode 100755 lab2/validate.sh diff --git a/lab1/main_test.js b/lab1/main_test.js index 5d50ba45..bd81e094 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -8,7 +8,6 @@ test("Test MyClass's addStudent", () => { assert.strictEqual(myclass.addStudent(), 0); assert.strictEqual(myclass.addStudent(), 1); assert.strictEqual("Return -1", -1); - throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { @@ -28,8 +27,6 @@ test("Test MyClass's getStudentById", () => { for (i = 0; i < myclass.students.length(); i++) { assert.strictEqual(myclass.getStudentById(i), myclass.students[i]); } - - throw new Error("Test not implemented"); }); test("Test Student's setName", () => { @@ -38,7 +35,6 @@ test("Test Student's setName", () => { assert.strictEqual(student.name, undefined); student.setName("std1"); assert.strictEqual(student.name, "std1"); - throw new Error("Test not implemented"); }); test("Test Student's getName", () => { @@ -47,5 +43,4 @@ test("Test Student's getName", () => { assert.strictEqual(student.getName(), ''); student.setName("std1"); assert.strictEqual(student.getName(), "std1"); - throw new Error("Test not implemented"); }); \ No newline at end of file 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 142163d9..00000000 --- a/lab2/main.js +++ /dev/null @@ -1,77 +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 - sleep(1000); - // 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(); \ 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 From db21add780c4657927bd0d2e4376a2e96c76af41 Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 7 Mar 2024 20:34:29 +0800 Subject: [PATCH 5/7] Revert "fix cl error" This reverts commit 02fa7d14731ad8e769767544b999a18ea5d68ad5. --- lab1/main_test.js | 5 +++ lab2/README.md | 22 ++++++++++++++ lab2/main.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++ lab2/main_test.js | 6 ++++ lab2/validate.sh | 38 +++++++++++++++++++++++ 5 files changed, 148 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/lab1/main_test.js b/lab1/main_test.js index bd81e094..5d50ba45 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -8,6 +8,7 @@ test("Test MyClass's addStudent", () => { assert.strictEqual(myclass.addStudent(), 0); assert.strictEqual(myclass.addStudent(), 1); assert.strictEqual("Return -1", -1); + throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { @@ -27,6 +28,8 @@ test("Test MyClass's getStudentById", () => { for (i = 0; i < myclass.students.length(); i++) { assert.strictEqual(myclass.getStudentById(i), myclass.students[i]); } + + throw new Error("Test not implemented"); }); test("Test Student's setName", () => { @@ -35,6 +38,7 @@ test("Test Student's setName", () => { assert.strictEqual(student.name, undefined); student.setName("std1"); assert.strictEqual(student.name, "std1"); + throw new Error("Test not implemented"); }); test("Test Student's getName", () => { @@ -43,4 +47,5 @@ test("Test Student's getName", () => { assert.strictEqual(student.getName(), ''); student.setName("std1"); assert.strictEqual(student.getName(), "std1"); + throw new Error("Test not implemented"); }); \ No newline at end of file diff --git a/lab2/README.md b/lab2/README.md new file mode 100644 index 00000000..60a9c805 --- /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 00000000..142163d9 --- /dev/null +++ b/lab2/main.js @@ -0,0 +1,77 @@ +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 + sleep(1000); + // 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(); \ No newline at end of file diff --git a/lab2/main_test.js b/lab2/main_test.js new file mode 100644 index 00000000..5034468e --- /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 00000000..13b53ed8 --- /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 896753d970bf8a8470d11ab79b05f29ff46931af Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 7 Mar 2024 20:45:17 +0800 Subject: [PATCH 6/7] fix unit test error --- lab1/main_test.js | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 5d50ba45..58f5693e 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -4,32 +4,21 @@ const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { //TODO - const a = new MyClass(); - assert.strictEqual(myclass.addStudent(), 0); - assert.strictEqual(myclass.addStudent(), 1); - assert.strictEqual("Return -1", -1); - throw new Error("Test not implemented"); + const myclass = new MyClass(); + const student = new Student(); + assert.strictEqual(myclass.addStudent(""), -1); + assert.strictEqual(myclass.addStudent(student), 0); }); test("Test MyClass's getStudentById", () => { // TODO const myclass = new MyClass(); - assert.strictEqual(myclass.getStudentById(-1), null); - assert.strictEqual(myclass.getStudentById(myclass.students.length), null); - const student1 = new Student(); - student1.setName("std1"); myclass.addStudent(student1); - - const student2 = new Student(); - student2.setName("std2"); - myclass.addStudent(student2); - - for (i = 0; i < myclass.students.length(); i++) { - assert.strictEqual(myclass.getStudentById(i), myclass.students[i]); - } + assert.strictEqual(myclass.getStudentById(-1), null); + assert.strictEqual(myclass.getStudentById(100), null); + assert.strictEqual(myclass.getStudentById(0), student1); - throw new Error("Test not implemented"); }); test("Test Student's setName", () => { @@ -38,7 +27,6 @@ test("Test Student's setName", () => { assert.strictEqual(student.name, undefined); student.setName("std1"); assert.strictEqual(student.name, "std1"); - throw new Error("Test not implemented"); }); test("Test Student's getName", () => { @@ -47,5 +35,4 @@ test("Test Student's getName", () => { assert.strictEqual(student.getName(), ''); student.setName("std1"); assert.strictEqual(student.getName(), "std1"); - throw new Error("Test not implemented"); }); \ No newline at end of file From 24f8d9fc35c499f57f1e2893068b9470c6517771 Mon Sep 17 00:00:00 2001 From: MingHsien Wu Date: Thu, 7 Mar 2024 20:50:48 +0800 Subject: [PATCH 7/7] fix coverge error --- lab1/main_test.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 58f5693e..1316f486 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -8,6 +8,7 @@ test("Test MyClass's addStudent", () => { const student = new Student(); assert.strictEqual(myclass.addStudent(""), -1); assert.strictEqual(myclass.addStudent(student), 0); + }); test("Test MyClass's getStudentById", () => { @@ -24,15 +25,19 @@ test("Test MyClass's getStudentById", () => { test("Test Student's setName", () => { // TODO const student = new Student(); + const name = "std1"; + student.setName(0); assert.strictEqual(student.name, undefined); - student.setName("std1"); - assert.strictEqual(student.name, "std1"); + student.setName(name); + assert.strictEqual(student.name, name); }); test("Test Student's getName", () => { // TODO const student = new Student(); - assert.strictEqual(student.getName(), ''); - student.setName("std1"); - assert.strictEqual(student.getName(), "std1"); + const name = "std1"; + student.setName(100); + assert.strictEqual(student.getName(), ""); + student.setName(name); + assert.strictEqual(student.getName(), name); }); \ No newline at end of file