Skip to content

Commit c817c98

Browse files
committed
feat: lab1
1 parent 6652b41 commit c817c98

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

lab1/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Lab1
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. (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.

lab1/main.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// NOTICE: DO NOT MODIFY THE CODE IN THIS FILE
2+
// But you can uncomment code below and run this file to understand how to use the classes
3+
4+
class MyClass {
5+
constructor() {
6+
this.students = [];
7+
}
8+
9+
addStudent(student) {
10+
if (!(student instanceof Student)) {
11+
return -1;
12+
}
13+
this.students.push(student);
14+
return this.students.length - 1;
15+
}
16+
17+
getStudentById(id) {
18+
if (id < 0 || id >= this.students.length) {
19+
return null;
20+
}
21+
return this.students[id];
22+
}
23+
}
24+
25+
class Student {
26+
constructor() {
27+
this.name = undefined;
28+
}
29+
30+
setName(userName) {
31+
if (typeof userName !== 'string') {
32+
return;
33+
}
34+
this.name = userName;
35+
}
36+
37+
getName() {
38+
if (this.name === undefined) {
39+
return '';
40+
}
41+
return this.name;
42+
}
43+
}
44+
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);
53+
// });
54+
55+
module.exports = { MyClass, Student };

lab1/main_test.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const test = require('node:test');
2+
const assert = require('assert');
3+
const { MyClass, Student } = require('./main');
4+
5+
test("Test MyClass's addStudent", () => {
6+
// TODO
7+
throw new Error("Test not implemented");
8+
});
9+
10+
test("Test MyClass's getStudentById", () => {
11+
// TODO
12+
throw new Error("Test not implemented");
13+
});
14+
15+
test("Test Student's setName", () => {
16+
// TODO
17+
throw new Error("Test not implemented");
18+
});
19+
20+
test("Test Student's getName", () => {
21+
// TODO
22+
throw new Error("Test not implemented");
23+
});

lab1/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 lab1-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%, should be 100%."
28+
exit 1
29+
else
30+
echo "[V] Coverage is 100%, great job!"
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)