Skip to content

Commit

Permalink
finish lab2
Browse files Browse the repository at this point in the history
  • Loading branch information
pudding0803 committed Mar 9, 2024
1 parent 7e581ae commit 0833940
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
1 change: 1 addition & 0 deletions lab2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Application {
return null;
}
let person = this.getRandomPerson();
console.log(person);
while (this.selected.includes(person)) {
person = this.getRandomPerson();
}
Expand Down
69 changes: 64 additions & 5 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -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
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;
});
3 changes: 3 additions & 0 deletions lab2/name_list.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name 0
name 1
name 2

0 comments on commit 0833940

Please sign in to comment.