From 8d359db9a21f7903a602d493730b12e696e375e2 Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 11:38:21 +0800 Subject: [PATCH 1/8] update main_test.js 113/03/10 --- lab1/main_test.js | 54 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 45 insertions(+), 9 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..91713ac3 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -3,21 +3,57 @@ const assert = require('assert'); const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { - // TODO - throw new Error("Test not implemented"); + const myClass = new MyClass(); + const student = new Student(); + student.setName("tingegg"); + //利用Student物件呼叫myClass的addStudent方法,並將回傳值寫在變數newStudentId中 + const newStudentId = myClass.addStudent(student); + + //寫assertions確保addStudent回傳值,如果正確,myClass會包含新增的student + assert.strictEqual(newStudentId, 0); + assert.strictEqual(myClass.students.length, 1); + assert.strictEqual(myClass.getStudentById(newStudentId).getName(), "tingegg"); + + //利用faultStudent宣告非Student型別的物件 + const faultStudent = {}; + const result = myClass.addStudent(faultStudent); + //寫assertions確認faultStudent回傳值為-1 + assert.strictEqual(result, -1); + assert.strictEqual(myClass.students.length, 0,"student"); + // throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); + const myClass = new MyClass(); + const student1 = new Student(); + const student2 = new Student(); + student1.setName("YaYa"); + student2.setName("GaGa"); + myClass.addStudent(student1); + myClass.addStudent(student2); + const validStudent = myClass.getStudentById(1); + const invalidStudent = myClass.getStudentById(2); + assert.strictEqual(validStudent.getName(), "GaGa"); + assert.strictEqual(invalidStudent, null); + + const + // throw new Error("Test not implemented"); }); test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); + const student = new Student(); + student.setName("SaSa"); + assert.strictEqual(student.getName(), "SaSa"); + // throw new Error("Test not implemented"); }); test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file + const student = new Student(); + const nameBefore = student.getName(); + student.setName("DaDa"); + const nameAfter = student.getName(); + assert.strictEqual(nameBefore, ""); + assert.strictEqual(nameAfter,"HaHa"); + // throw new Error("Test not implemented"); +}); + From 410bc301f4c20d32320c10eb60f4bd600ef1640d Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 13:36:08 +0800 Subject: [PATCH 2/8] update main_test.js 113/03/10-1 --- lab1/main_test.js | 30 ++++++++++-------------------- 1 file changed, 10 insertions(+), 20 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 91713ac3..b8ac925e 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -3,24 +3,16 @@ const assert = require('assert'); const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { - const myClass = new MyClass(); - const student = new Student(); - student.setName("tingegg"); - //利用Student物件呼叫myClass的addStudent方法,並將回傳值寫在變數newStudentId中 - const newStudentId = myClass.addStudent(student); - - //寫assertions確保addStudent回傳值,如果正確,myClass會包含新增的student - assert.strictEqual(newStudentId, 0); - assert.strictEqual(myClass.students.length, 1); - assert.strictEqual(myClass.getStudentById(newStudentId).getName(), "tingegg"); - - //利用faultStudent宣告非Student型別的物件 - const faultStudent = {}; - const result = myClass.addStudent(faultStudent); - //寫assertions確認faultStudent回傳值為-1 - assert.strictEqual(result, -1); - assert.strictEqual(myClass.students.length, 0,"student"); - // throw new Error("Test not implemented"); + const myClass = new MyClass(); + const student = new Student(); + student.setName("Tingegg"); + + const addedStudentId = myClass.addStudent(student); + assert.strictEqual(addedStudentId, 0, "應該將學生加入並返回正確的索引"); + + const nonStudent = 123; // 模擬非 Student 物件 + const nonStudentId = myClass.addStudent(nonStudent); + assert.strictEqual(nonStudentId, -1, "非 Student 物件不應該被添加"); }); test("Test MyClass's getStudentById", () => { @@ -36,8 +28,6 @@ test("Test MyClass's getStudentById", () => { assert.strictEqual(validStudent.getName(), "GaGa"); assert.strictEqual(invalidStudent, null); - const - // throw new Error("Test not implemented"); }); test("Test Student's setName", () => { From 939e5948b320d7361af8ee26f28b2cf0878926a6 Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 15:02:34 +0800 Subject: [PATCH 3/8] 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 b8ac925e..bd2dd03a 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,7 +1,7 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); - +///test123 test("Test MyClass's addStudent", () => { const myClass = new MyClass(); const student = new Student(); From 5fc6afc04e22708ed6fa82a9d19085ae9ba1e34d Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 15:17:54 +0800 Subject: [PATCH 4/8] main_test.js_addStudent --- lab1/main_test.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index bd2dd03a..d2b852bb 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,18 +1,15 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -///test123 + test("Test MyClass's addStudent", () => { const myClass = new MyClass(); const student = new Student(); student.setName("Tingegg"); + const index = myClass.addStudent(student); + assert.strictEqual(myClass.students.includes(student), true); + assert.strictEqual(index, 0); - const addedStudentId = myClass.addStudent(student); - assert.strictEqual(addedStudentId, 0, "應該將學生加入並返回正確的索引"); - - const nonStudent = 123; // 模擬非 Student 物件 - const nonStudentId = myClass.addStudent(nonStudent); - assert.strictEqual(nonStudentId, -1, "非 Student 物件不應該被添加"); }); test("Test MyClass's getStudentById", () => { From dd4daf2ee2abb61dde96391c0d19eff1dd221a76 Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 15:22:31 +0800 Subject: [PATCH 5/8] main_test.js_getStudentById --- lab1/main_test.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index d2b852bb..65a5910e 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -13,17 +13,12 @@ test("Test MyClass's addStudent", () => { }); test("Test MyClass's getStudentById", () => { - const myClass = new MyClass(); - const student1 = new Student(); - const student2 = new Student(); - student1.setName("YaYa"); - student2.setName("GaGa"); - myClass.addStudent(student1); - myClass.addStudent(student2); - const validStudent = myClass.getStudentById(1); - const invalidStudent = myClass.getStudentById(2); - assert.strictEqual(validStudent.getName(), "GaGa"); - assert.strictEqual(invalidStudent, null); + const myClass = new MyClass(); + const student = new Student(); + student.setName("Bob"); + const index = myClass.addStudent(student); + const foundStudent = myClass.getStudentById(index); + assert.strictEqual(foundStudent, student); }); From 7353f916a50aaf65a7aacd54d4b62e96a242a860 Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 15:35:16 +0800 Subject: [PATCH 6/8] main_test.js_test --- lab1/main_test.js | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 65a5910e..298b834c 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -10,6 +10,9 @@ test("Test MyClass's addStudent", () => { assert.strictEqual(myClass.students.includes(student), true); assert.strictEqual(index, 0); + const faultresult = myClass.addStudent("NotAStudent"); + assert.strictEqual(faultresult, -1); + }); test("Test MyClass's getStudentById", () => { @@ -20,22 +23,22 @@ test("Test MyClass's getStudentById", () => { const foundStudent = myClass.getStudentById(index); assert.strictEqual(foundStudent, student); + const faultfoundStudent = myClass.getStudentById(999); + assert.strictEqual(faultfoundStudent, null); + }); test("Test Student's setName", () => { const student = new Student(); - student.setName("SaSa"); - assert.strictEqual(student.getName(), "SaSa"); - // throw new Error("Test not implemented"); + student.setName("Charlie"); + assert.strictEqual(student.getName(), "Charlie"); + student.setName(null); + assert.strictEqual(student.getName(), ''); }); test("Test Student's getName", () => { - const student = new Student(); - const nameBefore = student.getName(); - student.setName("DaDa"); - const nameAfter = student.getName(); - assert.strictEqual(nameBefore, ""); - assert.strictEqual(nameAfter,"HaHa"); - // throw new Error("Test not implemented"); + const student = new Student(); + student.setName("David"); + assert.strictEqual(student.getName(), "David"); }); From 0532aba308d19889d4d830cff4dd846be78d46ca Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 15:43:36 +0800 Subject: [PATCH 7/8] main_test.js_test1 --- lab1/main_test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 298b834c..33d1e9fd 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -2,7 +2,7 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -test("Test MyClass's addStudent", () => { +test("Test MyClass's addStudent" , async () =>{ const myClass = new MyClass(); const student = new Student(); student.setName("Tingegg"); @@ -15,7 +15,7 @@ test("Test MyClass's addStudent", () => { }); -test("Test MyClass's getStudentById", () => { +test("Test MyClass's getStudentById", async () => { const myClass = new MyClass(); const student = new Student(); student.setName("Bob"); @@ -28,7 +28,7 @@ test("Test MyClass's getStudentById", () => { }); -test("Test Student's setName", () => { +test("Test Student's setName", async () => { const student = new Student(); student.setName("Charlie"); assert.strictEqual(student.getName(), "Charlie"); @@ -36,9 +36,13 @@ test("Test Student's setName", () => { assert.strictEqual(student.getName(), ''); }); -test("Test Student's getName", () => { +test("Test Student's getName", async () => { + const myClass = new MyClass(); const student = new Student(); student.setName("David"); assert.strictEqual(student.getName(), "David"); + student.setName("Eve"); + await myClass.addStudent(student); + assert.strictEqual(myClass.students.length, 1); }); From f64699e91ee2666b8bf03adc4eef2486a7a5dcd6 Mon Sep 17 00:00:00 2001 From: tingegg Date: Sun, 10 Mar 2024 15:56:40 +0800 Subject: [PATCH 8/8] main_test.js_test2 --- lab1/main_test.js | 100 ++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 39 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 33d1e9fd..c09ab091 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -2,47 +2,69 @@ const test = require('node:test'); const assert = require('assert'); const { MyClass, Student } = require('./main'); -test("Test MyClass's addStudent" , async () =>{ +// Test MyClass addStudent functionality +test('Test MyClass addStudent', () => { const myClass = new MyClass(); const student = new Student(); - student.setName("Tingegg"); - const index = myClass.addStudent(student); - assert.strictEqual(myClass.students.includes(student), true); - assert.strictEqual(index, 0); - - const faultresult = myClass.addStudent("NotAStudent"); - assert.strictEqual(faultresult, -1); - -}); - -test("Test MyClass's getStudentById", async () => { + + //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. Add success"); + + // return id + assert.strictEqual(addStudentResult, 0, "Id should be 0 or positive number.") + }); + + // Test MyClass getStudentById functionality + test('Test MyClass getStudentById', () => { const myClass = new MyClass(); const student = new Student(); - student.setName("Bob"); - const index = myClass.addStudent(student); - const foundStudent = myClass.getStudentById(index); - assert.strictEqual(foundStudent, student); - - const faultfoundStudent = myClass.getStudentById(999); - assert.strictEqual(faultfoundStudent, null); - -}); - -test("Test Student's setName", async () => { - const student = new Student(); - student.setName("Charlie"); - assert.strictEqual(student.getName(), "Charlie"); - student.setName(null); - assert.strictEqual(student.getName(), ''); -}); - -test("Test Student's getName", async () => { - const myClass = new MyClass(); + student.setName('Tiffany'); + myClass.addStudent(student); + + // Id is negative number + assert.strictEqual(myClass.getStudentById(-1), null, "Id should not be negative number, otherwise it will return null"); + + // 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, "return student object"); + + }); + + // Test Student setName functionality + test('Test Student setName', () => { + const student1 = new Student(); + const student2 = new Student(); + + // pass a value to setName that is a string type + student1.setName('Alice'); + assert.strictEqual(student1.getName(), 'Alice', 'Student name set correctly'); + + // pass a value to setName that is not a string type + student2.setName(123); + assert.strictEqual(student2.getName(), '', "userName should be in type of a string value"); + + + }); + + // Test Student getName functionality + test('Test Student getName', () => { const student = new Student(); - student.setName("David"); - assert.strictEqual(student.getName(), "David"); - student.setName("Eve"); - await myClass.addStudent(student); - assert.strictEqual(myClass.students.length, 1); -}); - + + // Test getting the name of a student without setting a name + assert.strictEqual(student.getName(), '', 'Student name should be an empty string if not set'); + + // Test after set a name to student can return name + student.setName('Bob'); + assert.strictEqual(student.getName(), 'Bob', 'Student name retrieved correctly'); + + + }); \ No newline at end of file