Skip to content

Commit b5974d3

Browse files
committed
Merge branch '313551801' of https://github.com/SQLab/113-spring-software-testing into 313551801
2 parents 3d40321 + 601c6b2 commit b5974d3

7 files changed

Lines changed: 126 additions & 97 deletions

File tree

lab1/main_test.js

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

5-
test("Test MyClass\'s addStudent", () => {
6-
const myClass = new MyClass();
7-
const student = new Student();
8-
9-
const id = myClass.addStudent(student);
10-
assert.strictEqual(id, 0);
11-
12-
const invalidId = myClass.addStudent("invalid student");
13-
assert.strictEqual(invalidId, -1);
5+
test("Test MyClass's addStudent", () => {
6+
// TODO
7+
throw new Error("Test not implemented");
148
});
159

16-
test("Test MyClass\'s getStudentById", () => {
17-
const myClass = new MyClass();
18-
const student = new Student();
19-
student.setName("Ann");
20-
21-
const id = myClass.addStudent(student);
22-
const retrievedStudent = myClass.getStudentById(id);
23-
24-
assert.ok(retrievedStudent instanceof Student);
25-
assert.strictEqual(retrievedStudent.getName(), "Ann");
26-
27-
assert.strictEqual(myClass.getStudentById(-1), null);
28-
assert.strictEqual(myClass.getStudentById(100), null);
10+
test("Test MyClass's getStudentById", () => {
11+
// TODO
12+
throw new Error("Test not implemented");
2913
});
3014

3115
test("Test Student's setName", () => {
32-
const student = new Student();
33-
34-
student.setName("Anton");
35-
assert.strictEqual(student.getName(), "Anton");
36-
37-
student.setName(1);
38-
assert.strictEqual(student.getName(), "Anton");
39-
40-
student.setName(null);
41-
assert.strictEqual(student.getName(), "Anton");
42-
43-
student.setName(undefined);
44-
assert.strictEqual(student.getName(), "Anton");
16+
// TODO
17+
throw new Error("Test not implemented");
4518
});
4619

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

lab2/main.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ class Application {
2828
this.people = [];
2929
this.selected = [];
3030
this.mailSystem = new MailSystem();
31+
32+
this.getNames().then(([people, selected]) => {
33+
this.people = people;
34+
this.selected = selected;
35+
});
3136
}
3237

3338
async getNames() {
3439
const data = await readFile('name_list.txt', 'utf8');
35-
this.people = data.split('\n');
36-
this.selected = [];
40+
const people = data.split('\n');
41+
const selected = [];
42+
return [people, selected];
3743
}
3844

39-
4045
getRandomPerson() {
4146
const i = Math.floor(Math.random() * this.people.length);
4247
return this.people[i];

lab2/main_test.js

Lines changed: 2 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,6 @@
11
const test = require('node:test');
22
const assert = require('assert');
33
const { Application, MailSystem } = require('./main');
4-
const sinon = require('sinon');
54

6-
test('test getRandomPerson in main.js', async () => {
7-
const app = new Application();
8-
9-
app.getNames = async () => {
10-
app.people = ["Annika", "Billy", "Cecilia"];
11-
app.selected = [];
12-
};
13-
14-
await app.getNames();
15-
16-
const person = app.getRandomPerson();
17-
assert.ok(app.people.includes(person), 'the person is not in list');
18-
});
19-
20-
21-
test('test mains selectNextPerson to not select same person twice', async () => {
22-
const app = new Application();
23-
24-
app.getNames = async () => {
25-
app.people = ["Annika", "Billy", "Cecilia"];
26-
app.selected = [];
27-
};
28-
29-
await app.getNames();
30-
31-
const person1 = app.selectNextPerson();
32-
const person2 = app.selectNextPerson();
33-
34-
assert.notStrictEqual(person1, person2, 'Same person was selected twice');
35-
});
36-
37-
test('test mains notifySelected that it calls send()', async () => {
38-
const mailSystem = new MailSystem();
39-
const sendSpy = sinon.spy(mailSystem, 'send');
40-
41-
const app = new Application();
42-
app.mailSystem = mailSystem;
43-
44-
app.getNames = async () => {
45-
app.people = ["Annika", "Billy", "Cecilia"];
46-
app.selected = [];
47-
};
48-
49-
await app.getNames();
50-
51-
const person = app.selectNextPerson();
52-
app.selected = [person];
53-
54-
await app.notifySelected();
55-
56-
assert.ok(sendSpy.calledWith(person, `Congrats, ${person}!`), `send() didn't work with args. Captured calls: ${JSON.stringify(sendSpy.args)}`);
57-
});
5+
// TODO: write your tests here
6+
// Remember to use Stub, Mock, and Spy when necessary

lab3/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Lab3
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+
## Preparation (Important!!!)
8+
9+
1. Sync fork on GitHub
10+
2. `git checkout -b lab3` (**NOT** your student ID !!!)
11+
12+
## Requirement
13+
14+
1. (40%) Write test cases in `main_test.js` and achieve 100% code coverage.
15+
2. (30%) For each function, parameterize their testcases to test the error-results.
16+
3. (30%) For each function, use at least 3 parameterized testcases to test the non-error-results.
17+
18+
You can run `validate.sh` in your local to test if you satisfy the requirements.
19+
20+
Please note that you must not alter files other than `main_test.js`. You will get 0 points if
21+
22+
1. you modify other files to achieve requirements.
23+
2. you can't pass all CI on your PR.
24+
25+
## Submission
26+
27+
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.
28+
29+
Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.

lab3/main.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Calculator {
2+
exp(x) {
3+
if (!Number.isFinite(x)) {
4+
throw Error('unsupported operand type');
5+
}
6+
const result = Math.exp(x);
7+
if (result === Infinity) {
8+
throw Error('overflow');
9+
}
10+
return result;
11+
}
12+
13+
log(x) {
14+
if (!Number.isFinite(x)) {
15+
throw Error('unsupported operand type');
16+
}
17+
const result = Math.log(x);
18+
if (result === -Infinity) {
19+
throw Error('math domain error (1)');
20+
}
21+
if (Number.isNaN(result)) {
22+
throw Error('math domain error (2)');
23+
}
24+
return result;
25+
}
26+
}
27+
28+
// const calculator = new Calculator();
29+
// console.log(calculator.exp(87));
30+
// console.log(calculator.log(48763));
31+
32+
module.exports = {
33+
Calculator
34+
};

lab3/main_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const {describe, it} = require('node:test');
2+
const assert = require('assert');
3+
const { Calculator } = require('./main');
4+
5+
// TODO: write your tests here

lab3/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 lab3-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)