Skip to content

Commit

Permalink
unit test lab2
Browse files Browse the repository at this point in the history
  • Loading branch information
poro777 committed Mar 9, 2024
1 parent 5cdb89e commit 8872e0a
Showing 1 changed file with 139 additions and 2 deletions.
141 changes: 139 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,143 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');
const util = require('util');

const readFile = util.promisify(fs.readFile);

const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
function fileExists(filePath) {
try {
fs.accessSync(filePath, fs.constants.F_OK);
return true;
} catch (err) {
return false;
}
}

function createFile(fileName, disable) {
if (disable) return;
fs.writeFileSync(fileName, "Alice\nBob\nCharlie");
}

function deleteFile(fileName, disable) {
if (disable) return;
fs.unlinkSync(fileName);

}

const createApplication = async () => {
const existed = fileExists("name_list.txt");
createFile("name_list.txt", existed);

const app = new Application();

const data = await readFile('name_list.txt', 'utf8');
const people = data.split('\n');
const selected = [];
deleteFile("name_list.txt", existed);

app.people = people;
app.selected = selected;

return app;
};

test('Application - getRandomPerson', async (t) => {

await createApplication().then(app => {
assert.strictEqual(app.people.length, 3);

const randomPerson = app.getRandomPerson();
assert.ok(app.people.includes(randomPerson));
});

});

test('Application - getRandomPerson', async (t) => {

await createApplication().then(app => {
assert.strictEqual(app.people.length, 3);

const randomPerson = app.getRandomPerson();
assert.ok(app.people.includes(randomPerson));
});

});

test('Application - selectNextPerson',async (t) => {

await createApplication().then(app => {

assert.strictEqual(app.people.length, 3);

randomPerson = app.selectNextPerson();
assert.ok(app.people.includes(randomPerson));
assert.ok(app.selected.includes(randomPerson));
assert.strictEqual(app.selected.length, 1);

randomPerson = app.selectNextPerson();
assert.ok(app.people.includes(randomPerson));
assert.ok(app.selected.includes(randomPerson));
assert.strictEqual(app.selected.length, 2);

t.mock.method(app, 'getRandomPerson', () => randomPerson, { times: 1 });

randomPerson = app.selectNextPerson();
assert.ok(app.people.includes(randomPerson));
assert.ok(app.selected.includes(randomPerson));
assert.strictEqual(app.selected.length, 3);

nullPerson = app.selectNextPerson();
assert.strictEqual(nullPerson, null);
assert.strictEqual(app.selected.length, 3);

});


});

test('Application - notifySelected', async (t) => {
await createApplication().then(app => {

assert.strictEqual(app.people.length, 3);
t.mock.method(app, 'getRandomPerson', () => app.people[0]);

app.selectNextPerson();
t.mock.method(app.mailSystem, 'write', (name) => 'Congrats, ' + name + '!');
t.mock.method(app.mailSystem, 'send', (name, context) => {
assert.strictEqual(context, 'Congrats, ' + name + '!');
return true
});

app.notifySelected();
});


});

test('MailSystem - write', () => {
const mailSystem = new MailSystem();
const context = mailSystem.write('Alice');

assert.strictEqual(context, 'Congrats, Alice!');
});

test('MailSystem - send', (t) => {
const mailSystem = new MailSystem();

t.mock.method(Math, 'random', () => 1.0);

const result_true = mailSystem.send('Alice', 'Congratulations!');

assert.strictEqual(result_true, true);

t.mock.method(Math, 'random', () => 0.0);

const result_false = mailSystem.send('Alice', 'Congratulations!');

assert.strictEqual(result_false, false);

});

0 comments on commit 8872e0a

Please sign in to comment.