From dde6f0e92cc43d15265ee981e0ed23096046c6c2 Mon Sep 17 00:00:00 2001 From: CTHua Date: Thu, 29 Feb 2024 15:16:23 +0800 Subject: [PATCH 1/4] test: MyClass addStudent --- lab1/main_test.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..ad885a08 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -3,8 +3,19 @@ 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(); + // if student is not an instance of Student, return -1 + assert.strictEqual(myClass.addStudent({}), -1); + + // normal case + const names = ['John', 'Jane', 'Doe', 'Smith']; + names.forEach(name => { + const student = new Student(); + student.setName(name); + const newStudentId = myClass.addStudent(student); + const newStudentName = myClass.getStudentById(newStudentId).getName(); + assert.strictEqual(newStudentName, name); + }); }); test("Test MyClass's getStudentById", () => { From 94b602815e90214f364e740b098283e115726158 Mon Sep 17 00:00:00 2001 From: CTHua Date: Thu, 29 Feb 2024 15:16:37 +0800 Subject: [PATCH 2/4] test: MyClass getStudentById --- lab1/main_test.js | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index ad885a08..cd7a5838 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -19,8 +19,21 @@ test("Test MyClass's addStudent", () => { }); test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); + const myClass = new MyClass(); + // if id is less than 0, return null + assert.strictEqual(myClass.getStudentById(-1), null); + + // normal case + const names = ['John', 'Jane', 'Doe', 'Smith']; + names.forEach(name => { + const student = new Student(); + student.setName(name); + const newStudentId = myClass.addStudent(student); + const newStudent = myClass.getStudentById(newStudentId); + assert.strictEqual(student, newStudent); + }); + // if id is greater than or equal to the length of students, return null + assert.strictEqual(myClass.getStudentById(names.length), null); }); test("Test Student's setName", () => { From f837dfaefaef369cde294842d9aa370684e46a8b Mon Sep 17 00:00:00 2001 From: CTHua Date: Thu, 29 Feb 2024 15:16:51 +0800 Subject: [PATCH 3/4] test: Student setName --- lab1/main_test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index cd7a5838..ff467975 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -37,8 +37,12 @@ test("Test MyClass's getStudentById", () => { }); test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); + const student = new Student(); + student.setName('John'); + assert.strictEqual(student.getName(), 'John'); + // Test if setName accepts only string + student.setName(123); + assert.strictEqual(student.getName(), 'John'); }); test("Test Student's getName", () => { From e09e3fb64130b0bd0fb23940d9ad12646de193d3 Mon Sep 17 00:00:00 2001 From: CTHua Date: Thu, 29 Feb 2024 15:16:58 +0800 Subject: [PATCH 4/4] test: Student getName --- lab1/main_test.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index ff467975..92823652 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -46,6 +46,10 @@ test("Test Student's setName", () => { }); test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); + const student = new Student(); + // Test if getName returns empty string if name is undefined + assert.strictEqual(student.getName(), ''); + // Test if getName returns the name set by setName + student.setName('John'); + assert.strictEqual(student.getName(), 'John'); }); \ No newline at end of file