Skip to content

Commit e466836

Browse files
committed
Merge branch 'lab1-313551801' into 313551801
Merge new branch into old original branch
2 parents 8e38df7 + 7eede56 commit e466836

4 files changed

Lines changed: 169 additions & 0 deletions

File tree

lab1/README.md

Lines changed: 22 additions & 0 deletions
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

Lines changed: 55 additions & 0 deletions
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

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
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);
14+
});
15+
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);
29+
});
30+
31+
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");
45+
});
46+
47+
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");
54+
});

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