Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LAB2] 312555001 #620

Closed
wants to merge 24 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Finish lab2
  • Loading branch information
YingMuo committed Jun 20, 2024
commit 2feb877826b969d6b2d65b5659eb9f6a82b33d2e
96 changes: 94 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,98 @@
const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');
const fs = require('fs');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
const createSpy = () => {
const calls = [];
const spy = (...args) => {
calls.push(args);
};
spy.calls = calls;
return spy;
};

test("Test MailSystem's write", (t) => {
console.log = createSpy();
const system = new MailSystem();
assert.strictEqual(system.write("Alice"), "Congrats, Alice!");
assert.strictEqual(console.log.calls[0][0], "--write mail for Alice--");
});

test("Test MailSystem's send", (t) => {
console.log = createSpy();
const system = new MailSystem();
const fn = t.mock.fn(system.send);

// case success
t.mock.method(Math, 'random', () =>0.7);
assert.strictEqual(fn("Alice", "hi"), true);
assert.strictEqual(console.log.calls[0][0], "--send mail to Alice--");
// case fail
t.mock.method(Math, 'random', () =>0.4);
assert.strictEqual(fn("Alice", "hi"), false);
assert.strictEqual(console.log.calls[0][0], "--send mail to Alice--");

t.mock.reset();
});

test("Test Application's getNames", async (t) => {
fs.writeFile('name_list.txt', 'name1\nname2\nname3', () =>{});

const app = new Application();
const fn = t.mock.fn(app.getNames);
let [people, selected] = await fn();
assert.deepStrictEqual(people, ['name1', 'name2', 'name3']);

fs.unlink('name_list.txt', () =>{});
});

test("Test Applicaiton getRandomPerson", (t) => {
fs.writeFile('name_list.txt', 'name1\nname2\nname3', () =>{});

const app = new Application();
app.people = ['name1', 'name2', 'name3'];

t.mock.method(Math, 'random', () =>0);
assert.strictEqual(app.getRandomPerson(), 'name1');

t.mock.reset();
});

test("Test Applicaiton selectNextPerson", (t) => {

const app = new Application();
app.people = ['name1', 'name2', 'name3'];
t.mock.method(app, 'getRandomPerson', () =>'name1');
assert.strictEqual(app.selectNextPerson(), 'name1');

t.mock.method(app, 'getRandomPerson', () =>'name2');
assert.strictEqual(app.selectNextPerson(), 'name2');

t.mock.method(app, 'getRandomPerson', () =>'name3');
assert.strictEqual(app.selectNextPerson(), 'name3');

assert.strictEqual(app.selectNextPerson(), null);

let cnt = "";
t.mock.method(app, 'getRandomPerson', () =>{
cnt += 1;
return "name" + cnt;
});
app.selected = ["name1"];
assert.strictEqual(app.selectNextPerson(), 'name11');

t.mock.reset();
});

test("Test Applicaiton notifySelected", async () => {
console.log = createSpy();
const app = new Application();
app.selected = ["name1"];
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(console.log.calls[0][0], "--notify selected--");
assert.strictEqual(console.log.calls[1][0], "--write mail for name1--");
assert.strictEqual(console.log.calls[2][0], "--send mail to name1--");

fs.unlink('name_list.txt', () =>{});
});