From e9160e13c34dac10c3b6cde6386e085019e9f4db Mon Sep 17 00:00:00 2001 From: pudding0803 Date: Thu, 29 Feb 2024 07:42:46 +0000 Subject: [PATCH 1/2] finish lab1 --- lab1/main_test.js | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..4b5e9b54 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -1,23 +1,41 @@ -const test = require('node:test'); -const assert = require('assert'); -const { MyClass, Student } = require('./main'); +const test = require("node:test"); +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(); + assert.strictEqual(myClass.addStudent('Not a student'), -1); + const student = new Student(); + for (let i = 0; i < 3; i++) { + const student = new Student(); + assert.strictEqual(myClass.addStudent(student), i); + } }); test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); + const myClass = new MyClass(); + assert.strictEqual(myClass.getStudentById(-1), null); + for (let i = 0; i < 3; i++) { + const student = new Student(); + myClass.addStudent(student); + assert.strictEqual(myClass.getStudentById(i), student); + } + assert.strictEqual(myClass.getStudentById(3), null); }); test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); + const student = new Student(); + student.setName(123); + assert.strictEqual(student.getName(), ''); + const name = "Pudding0803"; + student.setName(name); + assert.strictEqual(student.getName(), name); }); test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file + const student = new Student(); + assert.strictEqual(student.getName(), ''); + const name = "Pudding0803"; + student.setName(name); + assert.strictEqual(student.getName(), name); +}); From b215e4b9626c82fae1e473e973edfbf7eb093fc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A6=E7=B3=96=E5=B8=83=E4=B8=81?= <45879162+pudding0803@users.noreply.github.com> Date: Sat, 9 Mar 2024 16:10:55 +0000 Subject: [PATCH 2/2] finish lab2 --- lab2/main.js | 1 + lab2/main_test.js | 83 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 79 insertions(+), 5 deletions(-) diff --git a/lab2/main.js b/lab2/main.js index 2e159e75..17efcce7 100644 --- a/lab2/main.js +++ b/lab2/main.js @@ -53,6 +53,7 @@ class Application { return null; } let person = this.getRandomPerson(); + console.log(person); while (this.selected.includes(person)) { person = this.getRandomPerson(); } diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..43255616 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,79 @@ -const test = require('node:test'); -const assert = require('assert'); -const { Application, MailSystem } = require('./main'); +const { test, mock } = require("node:test"); +const assert = require("assert"); +const { Application, MailSystem } = require("./main"); +const fs = require('fs'); +const util = require('util'); +const writeFile = util.promisify(fs.writeFile); +const unlinkFile = util.promisify(fs.unlink); -// TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary \ No newline at end of file +const MATH_RANDOM = Math.random; +const FILE_NAME = "name_list.txt"; +const CONTENT = "name 0\nname 1\nname 2" + +test("Test Mail's write", () => { + const mailSystem = new MailSystem(); + assert.strictEqual( + mailSystem.write("Pudding0803"), + "Congrats, Pudding0803!" + ); +}); + +test("Test Mail's send", () => { + const mailSystem = new MailSystem(); + Math.random = mock.fn(() => 0.6); + assert.strictEqual(mailSystem.send("Pudding0803", "Hello, world!"), true); + Math.random = mock.fn(() => 0.5); + assert.strictEqual(mailSystem.send("Pudding0803", "Hello, world!"), false); + Math.random = MATH_RANDOM; +}); + +test("Test Application's getNames", async () => { + fs.writeFile(FILE_NAME, CONTENT, () => {}); + const app = new Application(); + assert.deepStrictEqual( + await app.getNames(), + [["name 0", "name 1", "name 2"], []] + ); + fs.unlink(FILE_NAME, () => {}); +}); + +test("Test Application's getRandomPerson", async () => { + fs.writeFile(FILE_NAME, CONTENT, () => {}); + const app = new Application(); + await app.getNames(); + Math.random = mock.fn(() => 0); + assert.strictEqual(app.getRandomPerson(), "name 0"); + Math.random = mock.fn(() => 0.99); + assert.strictEqual(app.getRandomPerson(), "name 2"); + Math.random = MATH_RANDOM; + fs.unlink(FILE_NAME, () => {}); +}); + +test("Test Application's selectNextPerson", async () => { + fs.writeFile(FILE_NAME, CONTENT, () => {}); + const app = new Application(); + await app.getNames(); + Math.random = mock.fn(() => 0); + assert.strictEqual(app.selectNextPerson(), "name 0"); + Math.random = mock.fn(() => 0.4); + assert.strictEqual(app.selectNextPerson(), "name 1"); + Math.random = MATH_RANDOM; + assert.strictEqual(app.selectNextPerson(), "name 2"); + assert.strictEqual(app.selectNextPerson(), null); + fs.unlink(FILE_NAME, () => {}); +}); + +test("Test Application's notifySelected", async () => { + fs.writeFile(FILE_NAME, CONTENT, () => {}); + const app = new Application(); + await app.getNames(); + app.mailSystem.write = mock.fn((name) => name); + app.mailSystem.send = mock.fn((name, context) => true); + app.selectNextPerson(); + app.selectNextPerson(); + app.notifySelected(); + assert.strictEqual(app.mailSystem.write.mock.calls.length, 2); + assert.strictEqual(app.mailSystem.send.mock.calls.length, 2); + Math.random = MATH_RANDOM; + fs.unlink(FILE_NAME, () => {}); +});