Skip to content

Commit eaafdad

Browse files
authored
Merge branch '510558017' into lab2
2 parents 4b619b5 + f930783 commit eaafdad

31 files changed

+1826
-18
lines changed

lab1/main.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ class Student {
4242
}
4343
}
4444

45-
// const myClass = new MyClass();
46-
// const names = ['John', 'Jane', 'Doe', 'Smith'];
47-
// names.forEach(name => {
48-
// const student = new Student();
49-
// student.setName(name);
50-
// const newStudentId = myClass.addStudent(student);
51-
// const newStudentName = myClass.getStudentById(newStudentId).getName();
52-
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
45+
//const myClass = new MyClass();
46+
//const names = ['John', 'Jane', 'Doe', 'Smith'];
47+
//names.forEach(name => {
48+
// const student = new Student();
49+
//student.setName(name);
50+
// const newStudentId = myClass.addStudent(student);
51+
//const newStudentName = myClass.getStudentById(newStudentId).getName();
52+
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
5353
// });
5454

55-
module.exports = { MyClass, Student };
55+
module.exports = { MyClass, Student };

lab1/main_test.js

+27-9
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,39 @@ const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
6+
const myClass = new MyClass();
7+
const student = new Student();
8+
student.setName("John");
9+
const index = myClass.addStudent(student);
10+
assert.strictEqual(index, 0, "addStudent should return index 0 for the first student");
11+
const notAStudent = {};
12+
const indexForNotAStudent = myClass.addStudent(notAStudent);
13+
assert.strictEqual(indexForNotAStudent, -1, "addStudent should return -1 when adding a non-Student instance");
814
});
915

1016
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
17+
const myClass = new MyClass();
18+
const student = new Student();
19+
student.setName("Jane");
20+
const index = myClass.addStudent(student);
21+
const fetchedStudent = myClass.getStudentById(index);
22+
assert.strictEqual(fetchedStudent.getName(), "Jane", "getStudentById should retrieve the student with the correct name");
23+
const invalidFetchedStudent = myClass.getStudentById(-1);
24+
assert.strictEqual(invalidFetchedStudent, null, "getStudentById should return null for an invalid id");
1325
});
1426

1527
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
28+
const student = new Student();
29+
student.setName("Doe");
30+
assert.strictEqual(student.name, "Doe", "setName should correctly set the student's name");
31+
student.setName(123);
32+
assert.strictEqual(student.name, "Doe", "setName should not set name when the input is not a string");
1833
});
1934

2035
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
23-
});
36+
const student = new Student();
37+
student.setName("Smith");
38+
assert.strictEqual(student.getName(), "Smith", "getName should return the correct name");
39+
const newStudent = new Student();
40+
assert.strictEqual(newStudent.getName(), '', "getName should return an empty string for a student without a name");
41+
});

lab2/main_test.js

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const test = require('node:test');
22
const assert = require('assert');
3+
34
const fs = require('fs');
45
const util = require('util');
56
const writeFile = util.promisify(fs.writeFile);
@@ -156,3 +157,9 @@ test("Test Application's notifySelected", async () => {
156157
assert.deepStrictEqual(call.error, undefined);
157158
fs.unlinkSync(fn_);
158159
});
160+
161+
const { Application, MailSystem } = require('./main');
162+
163+
// TODO: write your tests here
164+
// Remember to use Stub, Mock, and Spy when necessary
165+

lab3/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 your branch (e.g., `SQLab:311XXXXXX`)
10+
2. `git checkout 311XXXXXX`
11+
3. `git pull`
12+
4. `git checkout -b lab3` (**NOT** your student ID !!!)
13+
14+
## Requirement
15+
16+
1. (40%) Write test cases in `main_test.js` and achieve 100% code coverage.
17+
2. (30%) For each function, parameterize its testcases to test the error-results.
18+
3. (30%) For each function, use at least 3 parameterized testcases to test the non-error-results.
19+
20+
You can run `validate.sh` in your local to test if you satisfy the requirements.
21+
22+
Please note that you must not alter files other than `main_test.js`. You will get 0 points if
23+
24+
1. you modify other files to achieve requirements.
25+
2. you can't pass all CI on your PR.
26+
27+
## Submission
28+
29+
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.
30+
31+
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

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

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

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

lab4/README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Lab4
2+
3+
## Introduction
4+
5+
In this lab, you will write tests in `main_test.js`. You can learn how to use [Puppeteer](https://pptr.dev/) to tests a web UI.
6+
7+
## Preparation (Important!!!)
8+
9+
1. Sync fork your branch (e.g., `SQLab:311XXXXXX`)
10+
2. `git checkout -b lab4` (**NOT** your student ID !!!)
11+
12+
## Requirement
13+
14+
1. (100%) Goto https://pptr.dev/, type `chipi chipi chapa chapa` into the search box, click on **1st** result in the **Docs** section, and print the title.
15+
16+
For the detailed steps and hints, please check the slide of this lab.
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. 311XXXXXX, 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.

lab4/main_test.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const puppeteer = require('puppeteer');
2+
3+
(async () => {
4+
// Launch the browser and open a new blank page
5+
const browser = await puppeteer.launch();
6+
const page = await browser.newPage();
7+
8+
// Navigate the page to a URL
9+
await page.goto('https://pptr.dev/');
10+
11+
// Hints:
12+
// Click search button
13+
// Type into search box
14+
// Wait for search result
15+
// Get the `Docs` result section
16+
// Click on first result in `Docs` section
17+
// Locate the title
18+
// Print the title
19+
20+
// Close the browser
21+
await browser.close();
22+
})();

0 commit comments

Comments
 (0)