Skip to content

Commit

Permalink
Merge pull request #103 from kflin88/312552038
Browse files Browse the repository at this point in the history
[LAB2] 312552038
  • Loading branch information
AlaRduTP authored Mar 20, 2024
2 parents e060ab9 + 98941df commit 0752df2
Showing 1 changed file with 75 additions and 2 deletions.
77 changes: 75 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,78 @@ const test = 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 fs = require('fs');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);


test ("Test MailSystem", (t)=>{
const MyMailSystem = new MailSystem();
function write() {
return MyMailSystem.write('Jane');
}
function send() {
return MyMailSystem.send('Jane', 'Congrats, Jane!');
}

const fn = t.mock.fn(write);
assert.strictEqual(fn(), 'Congrats, Jane!', 'write() failed');

Math.random = () => 0.9;
fn.mock.mockImplementation(send);
assert.strictEqual(fn(), true, 'send() failed');

Math.random = () => 0.1;
fn.mock.mockImplementation(send);
assert.strictEqual(fn(), false, 'send() failed');
});

test ("Test Application", async()=>{
const arr = ['Jane','Emma'];
const name_list = "Jane\nEmma";
await writeFile('name_list.txt', name_list, 'utf-8');
const app = new Application();
const [people, selected] = await app.getNames();

// test fot getNames
assert.deepStrictEqual(people, arr, 'getNames() failed');
assert.deepStrictEqual(selected, [], 'getNames() failed');


// test for getRandomPerson()
const ret1 = app.getRandomPerson();
assert(people.includes(ret1));


// test for selectNextPerson()
app.getRandomPerson = () => 'Jane';
const ret2 = app.selectNextPerson();
assert.strictEqual(ret2, 'Jane', 'selectNextPerson() failed');

app.getRandomPerson = () => 'Emma';
const ret3 = app.selectNextPerson();
assert.strictEqual(ret3, 'Emma', 'selectNextPerson() failed');

app.selected = ['Jane'];
var cnt=0;
app.getRandomPerson = () => {
if (cnt%2==0) {
cnt++;
return 'Jane';
}
else {
cnt--;
return 'Emma';
}
}
const ret4 = app.selectNextPerson();
assert.strictEqual(ret4, 'Emma', 'selectNextPerson() failed');

const ret5 = app.selectNextPerson();
assert.strictEqual(ret5, null);

// test for notifySelected()
const ret6 = app.notifySelected();
assert.strictEqual(ret6, undefined);
fs.unlinkSync('name_list.txt');
});

0 comments on commit 0752df2

Please sign in to comment.