diff --git a/lab1/README.md b/lab1/README.md index 5b2779a5..df0ab9f5 100644 --- a/lab1/README.md +++ b/lab1/README.md @@ -2,7 +2,7 @@ ## Introduction -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 `main.js.` (But remember don't commit them on GitHub) +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) ## Requirement diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..ef82623f 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,23 +1,36 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); - -test("Test MyClass's addStudent", () => { - // TODO +const student = new Student(); +var id=0; +test("Test MyClass's addStudent", ()=>{ + // TODO + this.students = []; + if (!(student instanceof Student)) { + return -1; + } + this.students.push(student); + return this.students.length - 1; throw new Error("Test not implemented"); -}); - +}); test("Test MyClass's getStudentById", () => { - // TODO + // TODO + return this.students[id]; throw new Error("Test not implemented"); }); - test("Test Student's setName", () => { // TODO - throw new Error("Test not implemented"); + if (typeof userName !== 'string') { + return; + } + this.name = userName; + throw new Error("Test not implemented"); }); - test("Test Student's getName", () => { // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file + if (this.name === undefined) { + return ''; + } + return this.name; + throw new Error("Test not implemented"); +}); diff --git a/lab1/validate.sh b/lab1/validate.sh deleted file mode 100755 index 8f2fcd42..00000000 --- a/lab1/validate.sh +++ /dev/null @@ -1,38 +0,0 @@ -#!/bin/bash - -# Check for unwanted files -for file in *; do - if [[ $file != "main.js" && $file != "main_test.js" && $file != "README.md" && $file != "validate.sh" ]]; then - echo "[!] Unwanted file detected: $file." - exit 1 - fi -done - -node=$(which node) -test_path="${BASH_SOURCE[0]}" -solution_path="$(realpath .)" -tmp_dir=$(mktemp -d -t lab1-XXXXXXXXXX) - -cd $tmp_dir - -rm -rf * -cp $solution_path/*.js . -result=$($"node" --test --experimental-test-coverage) ; ret=$? -if [ $ret -ne 0 ] ; then - echo "[!] testing fails." - exit 1 -else - coverage=$(echo "$result" | grep 'all files' | awk -F '|' '{print $2}' | sed 's/ //g') - if (( $(echo "$coverage < 100" | bc -l) )); then - echo "[!] Coverage is only $coverage%, should be 100%." - exit 1 - else - echo "[V] Coverage is 100%, great job!" - fi -fi - -rm -rf $tmp_dir - -exit 0 - -# vim: set fenc=utf8 ff=unix et sw=2 ts=2 sts=2: \ No newline at end of file