From 92b903c7f33e7910c5ea5b874c093e28f0b49a57 Mon Sep 17 00:00:00 2001 From: chenchen Date: Thu, 29 Feb 2024 22:25:33 +0800 Subject: [PATCH] feat: add test in main_test.js --- lab1/main_test.js | 58 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 4 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..a4cfbe97 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -4,20 +4,70 @@ const { MyClass, Student } = require('./main'); test("Test MyClass's addStudent", () => { // TODO - throw new Error("Test not implemented"); + //throw new Error("Test not implemented"); + const myClass = new MyClass(); + const names = ['John', 'Jane', 'Doe', 'Smith']; + let i = 0; + + // test vaild case + names.forEach(name => { + const student = new Student(); + student.setName(name); + const newStudentId = myClass.addStudent(student); + assert.strictEqual(newStudentId, i); + i = i + 1; + }); + + // test invalid case + assert.strictEqual(myClass.addStudent(13), -1); + }); test("Test MyClass's getStudentById", () => { // TODO - throw new Error("Test not implemented"); + //throw new Error("Test not implemented"); + const myClass = new MyClass(); + const names = ['John', 'Jane', 'Doe', 'Smith']; + let i = 0; + + // test vaild case + names.forEach(name => { + const student = new Student(); + student.setName(name); + myClass.addStudent(student); + assert.strictEqual(myClass.getStudentById(i), student); + i = i + 1; + }); + + // test invalid case + assert.strictEqual(myClass.getStudentById(-100), null); + assert.strictEqual(myClass.getStudentById(100), null); }); test("Test Student's setName", () => { // TODO - throw new Error("Test not implemented"); + //throw new Error("Test not implemented"); + const myClass = new MyClass(); + const names = ['John', 'Jane', 'Doe', 'Smith']; + names.forEach(name => { + const student = new Student(); + student.setName(name); + assert.strictEqual(student.getName(), name); + }); + }); test("Test Student's getName", () => { // TODO - throw new Error("Test not implemented"); + //throw new Error("Test not implemented"); + const myClass = new MyClass(); + const names = ['John', 'Jane', 'Doe', 'Smith']; + names.forEach(name => { + const student = new Student(); + student.setName(name); + assert.strictEqual(student.getName(), name); + }); + + const student = new Student(); + assert.strictEqual(student.getName(), ''); }); \ No newline at end of file