From 2873532bfe38c7daf9df9a4a7d748eab5b122660 Mon Sep 17 00:00:00 2001 From: Kyle-12 Date: Thu, 29 Feb 2024 23:02:39 +0800 Subject: [PATCH] Add main_test MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加4種 Unit Test (validate的bc跟path有因 git bash修改,不應上傳) --- lab1/main_test.js | 47 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..358963cf 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -4,20 +4,59 @@ 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("John"); + const index = myClass.addStudent(student); + assert.strictEqual(index, 0); //check if the student index is added to the class + + const notAStudent = {}; + const invalidIndex = myClass.addStudent(notAStudent); + assert.strictEqual(invalidIndex, -1); //check if the student's name is not a string + + //throw new Error("Test not implemented"); }); test("Test MyClass's getStudentById", () => { // TODO - throw new Error("Test not implemented"); + const myClass = new MyClass(); + const student = new Student(); + student.setName("Jane"); + const id = myClass.addStudent(student); + + const retrievedStudent = myClass.getStudentById(id); + assert.strictEqual(retrievedStudent, student); // use the id to get the student + + const invalidStudent = myClass.getStudentById(999); + assert.strictEqual(invalidStudent, null); // check if the id is invalid + + const invalidStudent1 = myClass.getStudentById(-1); + assert.strictEqual(invalidStudent1, null); // check if the id is invalid + + //throw new Error("Test not implemented"); }); test("Test Student's setName", () => { // TODO - throw new Error("Test not implemented"); + const student = new Student(); + student.setName("Doe"); + assert.strictEqual(student.name, "Doe"); //check if the name is set to Doe + + student.setName(123); + assert.strictEqual(student.name, "Doe"); //check if the name is not a string + + //throw new Error("Test not implemented"); }); test("Test Student's getName", () => { // TODO - throw new Error("Test not implemented"); + const student = new Student(); + student.setName("Smith"); + assert.strictEqual(student.getName(), "Smith"); //check if the name is Smith + + const newStudent = new Student(); + assert.strictEqual(newStudent.getName(), ''); //check if the name is empty + + //throw new Error("Test not implemented"); }); \ No newline at end of file