From 083394065e7ae73f73d05c117a6e80e347fcccbe 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 15:14:17 +0000 Subject: [PATCH] finish lab2 --- lab2/main.js | 1 + lab2/main_test.js | 69 ++++++++++++++++++++++++++++++++++++++++++---- lab2/name_list.txt | 3 ++ 3 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 lab2/name_list.txt 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..5e7e25c6 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,65 @@ -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"); -// 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; + +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 () => { + const app = new Application(); + assert.deepStrictEqual( + await app.getNames(), + [["name 0", "name 1", "name 2"], []] + ); +}); + +test("Test Application's getRandomPerson", async () => { + 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; +}); + +test("Test Application's selectNextPerson", async () => { + 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); +}); + +test("Test Application's notifySelected", async () => { + 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; +}); diff --git a/lab2/name_list.txt b/lab2/name_list.txt new file mode 100644 index 00000000..7b113bf8 --- /dev/null +++ b/lab2/name_list.txt @@ -0,0 +1,3 @@ +name 0 +name 1 +name 2 \ No newline at end of file