Skip to content

Commit 8c2461f

Browse files
committed
feat: lab2
1 parent 65be176 commit 8c2461f

File tree

5 files changed

+156
-60
lines changed

5 files changed

+156
-60
lines changed

lab1/main_test.js

Lines changed: 9 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -2,73 +2,22 @@ const test = require('node:test');
22
const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

5-
// 測試 MyClass 的 addStudent 方法
65
test("Test MyClass's addStudent", () => {
7-
const myClass = new MyClass();
8-
const student = new Student();
9-
10-
// 新增正確的 Student 物件
11-
let index = myClass.addStudent(student);
12-
assert.strictEqual(index, 0);
13-
assert.strictEqual(myClass.students.length, 1);
14-
15-
// 嘗試新增非 Student 物件
16-
index = myClass.addStudent({});
17-
assert.strictEqual(index, -1);
18-
assert.strictEqual(myClass.students.length, 1); // 確保長度未變
6+
// TODO
7+
throw new Error("Test not implemented");
198
});
209

21-
// 測試 MyClass 的 getStudentById 方法
2210
test("Test MyClass's getStudentById", () => {
23-
const myClass = new MyClass();
24-
const student1 = new Student();
25-
student1.setName("Alice");
26-
27-
const student2 = new Student();
28-
student2.setName("Bob");
29-
30-
myClass.addStudent(student1);
31-
myClass.addStudent(student2);
32-
33-
// 測試有效 ID
34-
let result = myClass.getStudentById(0);
35-
assert.strictEqual(result.getName(), "Alice");
36-
37-
result = myClass.getStudentById(1);
38-
assert.strictEqual(result.getName(), "Bob");
39-
40-
// 測試無效 ID
41-
result = myClass.getStudentById(-1);
42-
assert.strictEqual(result, null);
43-
44-
result = myClass.getStudentById(10);
45-
assert.strictEqual(result, null);
11+
// TODO
12+
throw new Error("Test not implemented");
4613
});
4714

48-
// 測試 Student 的 setName 方法
4915
test("Test Student's setName", () => {
50-
const student = new Student();
51-
52-
// 設定正確的名字
53-
student.setName("Charlie");
54-
assert.strictEqual(student.getName(), "Charlie");
55-
56-
// 設定無效的名字(非字串)
57-
student.setName(12345);
58-
assert.strictEqual(student.getName(), "Charlie"); // 名字應該不變
59-
60-
student.setName(null);
61-
assert.strictEqual(student.getName(), "Charlie"); // 名字應該不變
16+
// TODO
17+
throw new Error("Test not implemented");
6218
});
6319

64-
// 測試 Student 的 getName 方法
6520
test("Test Student's getName", () => {
66-
const student = new Student();
67-
68-
// 初始 name 應該是空字串
69-
assert.strictEqual(student.getName(), "");
70-
71-
// 設定名稱後應該正確返回
72-
student.setName("David");
73-
assert.strictEqual(student.getName(), "David");
74-
});
21+
// TODO
22+
throw new Error("Test not implemented");
23+
});

lab2/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Lab2
2+
3+
## Introduction
4+
5+
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)
6+
7+
## Requirement
8+
9+
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%)
10+
11+
You can run `validate.sh` in your local to test if you satisfy the requirements.
12+
13+
Please note that you must not alter files other than `main_test.js`. You will get 0 points if
14+
15+
1. you modify other files to achieve requirements.
16+
2. you can't pass all CI on your PR.
17+
18+
## Submission
19+
20+
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.
21+
22+
Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.

lab2/main.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
const fs = require('fs');
2+
const util = require('util');
3+
const readFile = util.promisify(fs.readFile);
4+
5+
class MailSystem {
6+
write(name) {
7+
console.log('--write mail for ' + name + '--');
8+
const context = 'Congrats, ' + name + '!';
9+
return context;
10+
}
11+
12+
send(name, context) {
13+
console.log('--send mail to ' + name + '--');
14+
// Interact with mail system and send mail
15+
// random success or failure
16+
const success = Math.random() > 0.5;
17+
if (success) {
18+
console.log('mail sent');
19+
} else {
20+
console.log('mail failed');
21+
}
22+
return success;
23+
}
24+
}
25+
26+
class Application {
27+
constructor() {
28+
this.people = [];
29+
this.selected = [];
30+
this.mailSystem = new MailSystem();
31+
this.getNames().then(([people, selected]) => {
32+
this.people = people;
33+
this.selected = selected;
34+
});
35+
}
36+
37+
async getNames() {
38+
const data = await readFile('name_list.txt', 'utf8');
39+
const people = data.split('\n');
40+
const selected = [];
41+
return [people, selected];
42+
}
43+
44+
getRandomPerson() {
45+
const i = Math.floor(Math.random() * this.people.length);
46+
return this.people[i];
47+
}
48+
49+
selectNextPerson() {
50+
console.log('--select next person--');
51+
if (this.people.length === this.selected.length) {
52+
console.log('all selected');
53+
return null;
54+
}
55+
let person = this.getRandomPerson();
56+
while (this.selected.includes(person)) {
57+
person = this.getRandomPerson();
58+
}
59+
this.selected.push(person);
60+
return person;
61+
}
62+
63+
notifySelected() {
64+
console.log('--notify selected--');
65+
for (const x of this.selected) {
66+
const context = this.mailSystem.write(x);
67+
this.mailSystem.send(x, context);
68+
}
69+
}
70+
}
71+
72+
// const app = new Application();
73+
// app.selectNextPerson();
74+
// app.selectNextPerson();
75+
// app.selectNextPerson();
76+
// app.notifySelected();
77+
78+
module.exports = {
79+
Application,
80+
MailSystem,
81+
};

lab2/main_test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const test = require('node:test');
2+
const assert = require('assert');
3+
const { Application, MailSystem } = require('./main');
4+
5+
// TODO: write your tests here
6+
// Remember to use Stub, Mock, and Spy when necessary

lab2/validate.sh

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#!/bin/bash
2+
3+
# Check for unwanted files
4+
for file in *; do
5+
if [[ $file != "main.js" && $file != "main_test.js" && $file != "README.md" && $file != "validate.sh" ]]; then
6+
echo "[!] Unwanted file detected: $file."
7+
exit 1
8+
fi
9+
done
10+
11+
node=$(which node)
12+
test_path="${BASH_SOURCE[0]}"
13+
solution_path="$(realpath .)"
14+
tmp_dir=$(mktemp -d -t lab2-XXXXXXXXXX)
15+
16+
cd $tmp_dir
17+
18+
rm -rf *
19+
cp $solution_path/*.js .
20+
result=$($"node" --test --experimental-test-coverage) ; ret=$?
21+
if [ $ret -ne 0 ] ; then
22+
echo "[!] testing fails"
23+
exit 1
24+
else
25+
coverage=$(echo "$result" | grep 'all files' | awk -F '|' '{print $2}' | sed 's/ //g')
26+
if (( $(echo "$coverage < 100" | bc -l) )); then
27+
echo "[!] Coverage is only $coverage%"
28+
exit 1
29+
else
30+
echo "[V] Coverage is 100%"
31+
fi
32+
fi
33+
34+
rm -rf $tmp_dir
35+
36+
exit 0
37+
38+
# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2:

0 commit comments

Comments
 (0)