From 775703781c5fa5261bbdecea1afce02c885b2a1e Mon Sep 17 00:00:00 2001 From: ShangHungWan Date: Thu, 7 Mar 2024 00:12:26 +0800 Subject: [PATCH 01/11] doc: fix typo in lab1 README --- lab1/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From 417a92ca1ef80712a5e6818a057d2e7dac52a92a Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 03:23:09 +0800 Subject: [PATCH 02/11] Update main_test.js --- lab1/main_test.js | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..5903a8cf 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,23 +1,37 @@ 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 + 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"); +}); From d5a4914e9cf73c149a7bbfa46c14cb16debb66e4 Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 04:02:01 +0800 Subject: [PATCH 03/11] Update main_test.js --- lab1/main_test.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 5903a8cf..7596a1f3 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,11 +1,11 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -const student = new Student(); -var id=0; +const student = new Student(); +const myClass = new MyClass(); test("Test MyClass's addStudent", ()=>{ // TODO - this.students = []; + this.students = ['John', 'Jane', 'Doe', 'Smith']; if (!(student instanceof Student)) { return -1; } @@ -15,20 +15,26 @@ test("Test MyClass's addStudent", ()=>{ }); test("Test MyClass's getStudentById", () => { // TODO - return this.students[id]; + const newStudentId = myClass.addStudent(student); + const newStudentName = myClass.getStudentById(newStudentId).getName(); + return this.students[0]; + return this.students[1]; + return this.students[2]; + return this.students[3]; throw new Error("Test not implemented"); }); test("Test Student's setName", () => { // TODO + student.setName('John'); if (typeof userName !== 'string') { return; } - this.name = userName; - + this.name = userName; throw new Error("Test not implemented"); }); test("Test Student's getName", () => { // TODO + student.getName(); if (this.name === undefined) { return ''; } From 8919c2b634c9bd84d680e946cf713794e4da8b60 Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 04:07:14 +0800 Subject: [PATCH 04/11] Update main_test.js --- lab1/main_test.js | 102 +++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 38 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 7596a1f3..63bbf3cd 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,43 +1,69 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -const student = new Student(); -const myClass = new MyClass(); -test("Test MyClass's addStudent", ()=>{ - // TODO - this.students = ['John', 'Jane', 'Doe', 'Smith']; - 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 - const newStudentId = myClass.addStudent(student); - const newStudentName = myClass.getStudentById(newStudentId).getName(); - return this.students[0]; - return this.students[1]; - return this.students[2]; - return this.students[3]; - throw new Error("Test not implemented"); + +// Test MyClass addStudent functionality +test('Test MyClass addStudent', () => { + const myClass = new MyClass(); + const student = new Student(); + + //check if student is instance of Student + const notAStudent = 123 + const notAStudentInstance = myClass.addStudent(notAStudent); + assert.strictEqual(notAStudentInstance, -1, "If it's not a Student instance, it should return -1."); + + // push student to Student list + student.setName("Joy"); + const addStudentResult = myClass.addStudent(student); + assert.strictEqual(myClass.students[0], student, "The student has not been added to the student list."); + + // return id + assert.strictEqual(addStudentResult, 0, "Id is not positive number.") }); -test("Test Student's setName", () => { - // TODO - student.setName('John'); - if (typeof userName !== 'string') { - return; - } - this.name = userName; - throw new Error("Test not implemented"); + +// Test MyClass getStudentById functionality +test('Test MyClass getStudentById', () => { + const myClass = new MyClass(); + const student = new Student(); + student.setName('Tiffany'); + myClass.addStudent(student); + + // Id is negative number + assert.strictEqual(myClass.getStudentById(-1), null, "Id should not be negative number"); + + //Id is not exist + assert.strictEqual(myClass.getStudentById(myClass.students.length+1), null, "Id is not exist"); + + // student exist + assert.strictEqual(myClass.getStudentById(0), student); + +}); + +// Test Student setName functionality +test('Test Student setName', () => { + const student = new Student(); + student.setName('Alice'); + assert.strictEqual(student.getName(), 'Alice', 'Student name should be set correctly'); + + // Test not string value + student.setName(123); + assert.strictEqual(student.getName(), 'Alice', "userName should not be changed to anything other than a String"); + + +}); + +// Test Student getName functionality +test('Test Student getName', () => { + const student = new Student(); + student.setName('Bob'); + + +// Test getting the name of a student without a name set + const studentWithoutName = new Student(); + assert.strictEqual(studentWithoutName.getName(), '', 'Student name should be an empty string if not set'); + + //return exist name + assert.strictEqual(student.getName(), 'Bob', 'Student name should be retrieved correctly'); + + }); -test("Test Student's getName", () => { - // TODO - student.getName(); - if (this.name === undefined) { - return ''; - } - return this.name; - throw new Error("Test not implemented"); -}); From 3013c821581be92fb2849820fc5950e0df2218c2 Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 04:14:25 +0800 Subject: [PATCH 05/11] Update main_test.js --- lab1/main_test.js | 106 ++++++++++++++++++---------------------------- 1 file changed, 42 insertions(+), 64 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 63bbf3cd..422cef23 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,69 +1,47 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); - -// Test MyClass addStudent functionality -test('Test MyClass addStudent', () => { - const myClass = new MyClass(); - const student = new Student(); - - //check if student is instance of Student - const notAStudent = 123 - const notAStudentInstance = myClass.addStudent(notAStudent); - assert.strictEqual(notAStudentInstance, -1, "If it's not a Student instance, it should return -1."); - - // push student to Student list - student.setName("Joy"); - const addStudentResult = myClass.addStudent(student); - assert.strictEqual(myClass.students[0], student, "The student has not been added to the student list."); - - // return id - assert.strictEqual(addStudentResult, 0, "Id is not positive number.") +const student = new Student(); +const myClass = new MyClass(); +test("Test MyClass's addStudent", ()=>{ + // TODO + student.setName("John"); + if (!(student instanceof Student)) { + return -1; + } + this.students.push(student); + return this.students.length - 1; + student.setName(111); + if (!(student instanceof Student)) { + return -1; + } + throw new Error("Test not implemented"); +}); +test("Test MyClass's getStudentById", () => { + // TODO + const newStudentId = myClass.addStudent(student); + const newStudentName = myClass.getStudentById(newStudentId).getName(); + return this.students[0]; + return this.students[1]; + return this.students[2]; + return this.students[3]; + throw new Error("Test not implemented"); }); - -// Test MyClass getStudentById functionality -test('Test MyClass getStudentById', () => { - const myClass = new MyClass(); - const student = new Student(); - student.setName('Tiffany'); - myClass.addStudent(student); - - // Id is negative number - assert.strictEqual(myClass.getStudentById(-1), null, "Id should not be negative number"); - - //Id is not exist - assert.strictEqual(myClass.getStudentById(myClass.students.length+1), null, "Id is not exist"); - - // student exist - assert.strictEqual(myClass.getStudentById(0), student); - -}); - -// Test Student setName functionality -test('Test Student setName', () => { - const student = new Student(); - student.setName('Alice'); - assert.strictEqual(student.getName(), 'Alice', 'Student name should be set correctly'); - - // Test not string value - student.setName(123); - assert.strictEqual(student.getName(), 'Alice', "userName should not be changed to anything other than a String"); - - -}); - -// Test Student getName functionality -test('Test Student getName', () => { - const student = new Student(); - student.setName('Bob'); - - -// Test getting the name of a student without a name set - const studentWithoutName = new Student(); - assert.strictEqual(studentWithoutName.getName(), '', 'Student name should be an empty string if not set'); - - //return exist name - assert.strictEqual(student.getName(), 'Bob', 'Student name should be retrieved correctly'); - - +test("Test Student's setName", () => { + // TODO + student.setName('John'); + if (typeof userName !== 'string') { + return; + } + this.name = userName; + throw new Error("Test not implemented"); }); +test("Test Student's getName", () => { + // TODO + student.getName(); + if (this.name === undefined) { + return ''; + } + return this.name; + throw new Error("Test not implemented"); +}); From 639f2794ce103b9fbc45dc61f4016fde7ff093d3 Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 04:16:35 +0800 Subject: [PATCH 06/11] Update main_test.js --- lab1/main_test.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 422cef23..46024ada 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -12,10 +12,7 @@ test("Test MyClass's addStudent", ()=>{ this.students.push(student); return this.students.length - 1; student.setName(111); - if (!(student instanceof Student)) { - return -1; - } - throw new Error("Test not implemented"); + throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { // TODO From 599516108b8ff6f280dba8ceda86648a497a4112 Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 04:56:27 +0800 Subject: [PATCH 07/11] Update main_test.js --- lab1/main_test.js | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 46024ada..7f323634 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -6,39 +6,35 @@ const myClass = new MyClass(); test("Test MyClass's addStudent", ()=>{ // TODO student.setName("John"); - if (!(student instanceof Student)) { - return -1; - } - this.students.push(student); - return this.students.length - 1; - student.setName(111); + const newStudentId = myClass.addStudent(student); + assert.strictEqual(newStudentId, 0,"正確"); + const notstudentID=111; + const newStudentId2 = myClass.addStudent(notstudentID); + assert.strictEqual(newStudentId2,-1,"錯誤"); throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { // TODO - const newStudentId = myClass.addStudent(student); - const newStudentName = myClass.getStudentById(newStudentId).getName(); - return this.students[0]; - return this.students[1]; - return this.students[2]; - return this.students[3]; + student.setName("kevin"); + myClass.addStudent(student); + const newStudentName = myClass.getStudentById(0); + assert.strictEqual(newStudentName.getName(), "kevin","正確"); + const newStudentName = myClass.getStudentById(-1); + assert.strictEqual(newStudentName, null,"錯誤"); throw new Error("Test not implemented"); }); test("Test Student's setName", () => { // TODO student.setName('John'); - if (typeof userName !== 'string') { - return; - } - this.name = userName; + assert.strictEqual(student.name, "John","正確"); + student.setName(111); + assert.strictEqual(student.name,111,"錯誤"); throw new Error("Test not implemented"); }); test("Test Student's getName", () => { // TODO - student.getName(); - if (this.name === undefined) { - return ''; - } - return this.name; + student.setName('John'); + assert.strictEqual(student.getName(), "John","正確"); + assert.strictEqual(student.getName(),null,"錯誤"); throw new Error("Test not implemented"); }); From 4111746d3a9a910f015c74f99ec54c27455c10b9 Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 04:58:02 +0800 Subject: [PATCH 08/11] Update main_test.js --- lab1/main_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 7f323634..66fac544 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -35,6 +35,6 @@ test("Test Student's getName", () => { // TODO student.setName('John'); assert.strictEqual(student.getName(), "John","正確"); - assert.strictEqual(student.getName(),null,"錯誤"); + assert.strictEqual(student.getName(),'',"錯誤"); throw new Error("Test not implemented"); }); From d615a16bd70493a57892e5cd513cd419c47c87bf Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 05:01:05 +0800 Subject: [PATCH 09/11] Update main_test.js --- lab1/main_test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 66fac544..ee171804 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -19,7 +19,7 @@ test("Test MyClass's getStudentById", () => { myClass.addStudent(student); const newStudentName = myClass.getStudentById(0); assert.strictEqual(newStudentName.getName(), "kevin","正確"); - const newStudentName = myClass.getStudentById(-1); + newStudentName = myClass.getStudentById(-1); assert.strictEqual(newStudentName, null,"錯誤"); throw new Error("Test not implemented"); }); From 98912e62d21fb3b4eeb8be64283207500d9cebdb Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 05:16:39 +0800 Subject: [PATCH 10/11] Update main_test.js --- lab1/main_test.js | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index ee171804..1789be7d 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,40 +1,46 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -const student = new Student(); -const myClass = new MyClass(); + test("Test MyClass's addStudent", ()=>{ - // TODO + // TODO + const student = new Student(); + const myClass = new MyClass(); student.setName("John"); const newStudentId = myClass.addStudent(student); assert.strictEqual(newStudentId, 0,"正確"); const notstudentID=111; const newStudentId2 = myClass.addStudent(notstudentID); assert.strictEqual(newStudentId2,-1,"錯誤"); - throw new Error("Test not implemented"); + }); test("Test MyClass's getStudentById", () => { // TODO + const student = new Student(); + const myClass = new MyClass(); student.setName("kevin"); myClass.addStudent(student); const newStudentName = myClass.getStudentById(0); assert.strictEqual(newStudentName.getName(), "kevin","正確"); newStudentName = myClass.getStudentById(-1); assert.strictEqual(newStudentName, null,"錯誤"); - throw new Error("Test not implemented"); + }); test("Test Student's setName", () => { // TODO + const student = new Student(); + const myClass = new MyClass(); student.setName('John'); assert.strictEqual(student.name, "John","正確"); student.setName(111); assert.strictEqual(student.name,111,"錯誤"); - throw new Error("Test not implemented"); + }); test("Test Student's getName", () => { // TODO + const student = new Student(); + const myClass = new MyClass(); student.setName('John'); assert.strictEqual(student.getName(), "John","正確"); assert.strictEqual(student.getName(),'',"錯誤"); - throw new Error("Test not implemented"); }); From e930379c17e4d88744ae69ed106851812b6bc52c Mon Sep 17 00:00:00 2001 From: agtfsfhgsdf <131828636+agtfsfhgsdf@users.noreply.github.com> Date: Fri, 8 Mar 2024 05:32:14 +0800 Subject: [PATCH 11/11] Update main_test.js --- lab1/main_test.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 1789be7d..1b0c85d9 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -22,25 +22,24 @@ test("Test MyClass's getStudentById", () => { myClass.addStudent(student); const newStudentName = myClass.getStudentById(0); assert.strictEqual(newStudentName.getName(), "kevin","正確"); - newStudentName = myClass.getStudentById(-1); - assert.strictEqual(newStudentName, null,"錯誤"); + const newStudentName2 = myClass.getStudentById(-1); + assert.strictEqual(newStudentName2, null,"錯誤"); }); test("Test Student's setName", () => { // TODO const student = new Student(); - const myClass = new MyClass(); - student.setName('John'); - assert.strictEqual(student.name, "John","正確"); + student.setName("John"); + assert.strictEqual(student.getName(), "John","正確"); student.setName(111); - assert.strictEqual(student.name,111,"錯誤"); - + assert.strictEqual(student.getName(),"John","錯誤"); + }); test("Test Student's getName", () => { // TODO const student = new Student(); - const myClass = new MyClass(); - student.setName('John'); + assert.strictEqual(student.getName(),'',"錯誤"); + student.setName("John"); assert.strictEqual(student.getName(), "John","正確"); - assert.strictEqual(student.getName(),'',"錯誤"); + });