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] 310552064 #143

Merged
merged 14 commits into from
Mar 20, 2024
Prev Previous commit
Next Next commit
Update main_test.js
veture7275 authored Mar 13, 2024
commit dab451018c1344ca11f8c0b794b36bf404e22943
39 changes: 35 additions & 4 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
const originalMathRandom = Math.random;
test("Test MailSystem's write", () => {
const mailsystem = new MailSystem();
assert.strictEqual(mailsystem.write("John"), 'Congrats, John!');
@@ -75,6 +76,36 @@ test("Test Application's selectNextPerson", async() => {
//fs.unlink('name_list.txt', () => {});
});

test("Test Application's selectNextPerson2", async() => {
let app = new Application();
[app.people,app.selected] = await app.getNames();
//const originalMathRandom = Math.random;
//Math.random = () => 0;
Math.random = originalMathRandom;
let oneFlag = false;
Math.random = () => {
if (app.selected.includes('John') && oneFlag) {
return 0.5;
}
if (app.selected.includes('John')) {
oneFlag = true;
return 0;
}
else {
return 0;
}
}
assert.deepStrictEqual(app.selectNextPerson(), 'John');
//Math.random = originalMathRandom;
assert.deepStrictEqual(app.selectNextPerson(), 'Jane');
//Math.random = () => 0.9;
Math.random = originalMathRandom;
assert.deepStrictEqual(app.selectNextPerson(), 'Doe');
assert.deepStrictEqual(app.selectNextPerson(), null);
assert.deepStrictEqual(app.selectNextPerson(), null);
assert.deepStrictEqual(app.selectNextPerson(), null);
//fs.unlink('name_list.txt', () => {});
});
test("Test Application's notifySelected", async(t) => {
let app = new Application();
[app.people,app.selected] = await app.getNames();