Skip to content

Commit b3dc236

Browse files
committed
feat: lab2
1 parent 54ceac4 commit b3dc236

File tree

5 files changed

+152
-37
lines changed

5 files changed

+152
-37
lines changed

lab1/main_test.js

+5-37
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,23 @@
11
const test = require('node:test');
22
const assert = require('assert');
3-
const { MyClass, Student} = require('./main');
3+
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
66
// TODO
7-
const myClass = new MyClass()
8-
const student = new Student()
9-
10-
const newStudentId = myClass.addStudent(student);
11-
12-
assert.strictEqual(newStudentId, 0);
13-
assert.strictEqual(myClass.addStudent({}), -1);
14-
15-
// throw new Error("Test not implemented");
7+
throw new Error("Test not implemented");
168
});
179

1810
test("Test MyClass's getStudentById", () => {
1911
// TODO
20-
const myClass = new MyClass();
21-
const student = new Student();
22-
student.setName("John");
23-
24-
const studentId = myClass.addStudent(student);
25-
26-
const foundStudent = myClass.getStudentById(studentId);
27-
assert.strictEqual(foundStudent, student);
28-
29-
assert.strictEqual(myClass.getStudentById(-5), null)
30-
assert.strictEqual(myClass.getStudentById(123), null)
31-
// throw new Error("Test not implemented");
12+
throw new Error("Test not implemented");
3213
});
3314

3415
test("Test Student's setName", () => {
3516
// TODO
36-
const student = new Student();
37-
38-
student.setName("Mike");
39-
assert.strictEqual(student.getName(), "Mike");
40-
41-
student.setName(222);
42-
assert.strictEqual(student.getName(), "Mike");
43-
// throw new Error("Test not implemented");
17+
throw new Error("Test not implemented");
4418
});
4519

4620
test("Test Student's getName", () => {
4721
// TODO
48-
const student = new Student();
49-
50-
assert.strictEqual(student.getName(), "");
51-
52-
student.setName("John");
53-
assert.strictEqual(student.getName(), "John");
54-
// throw new Error("Test not implemented");
22+
throw new Error("Test not implemented");
5523
});

lab2/README.md

+22
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

+81
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

+6
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

+38
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)