Skip to content

Commit

Permalink
Merge pull request #87 from Nyeeeeeee/312555018
Browse files Browse the repository at this point in the history
[LAB2] 312555018
AlaRduTP authored Mar 20, 2024
2 parents ccac577 + 31c1ffb commit 1378665
Showing 2 changed files with 120 additions and 10 deletions.
26 changes: 18 additions & 8 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
@@ -2,22 +2,32 @@ const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');

const cs = new MyClass();
const john = new Student();
john.setName("John");
var id;

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
assert(cs.addStudent("John") == -1, "Test not implemented");
assert(cs.addStudent(john) != -1, "Test not implemented");
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
id = cs.addStudent(john);
assert(cs.getStudentById(-1) == null, "Test not implemented");
assert(cs.getStudentById(id+1) == null, "Test not implemented");
assert(cs.getStudentById(id) == john, "Test not implemented");
});

const st = new Student();
const st2 = new Student();

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
st.setName(123);
st2.setName("John");
});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
assert(st.getName() == '', "Test not implemented");
assert(st2.getName() == 'John', "Test not implemented");
});
104 changes: 102 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -2,5 +2,105 @@ 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 mailsystem = new MailSystem();
n = 'John'
function write(){
return mailsystem.write(n)
}function send(){
context = mailsystem.write(n);
return mailsystem.send(n, context);
}

fn = t.mock.fn(write);
assert.strictEqual(fn(), 'Congrats, John!');
fn.mock.mockImplementation(send);
Math.random = () => 0.6;
assert.strictEqual(fn(), true);
Math.random = () => 0.4;
assert.strictEqual(fn(), false);
});

test('test getNames', async() =>{
const name_list = 'John\nMay\nAlex\nJay\nPeggy';
await writeFile('name_list.txt', name_list, 'utf-8');
const app = new Application();
const [people, selected] = await app.getNames();

assert.deepStrictEqual(people, ['John','May','Alex','Jay','Peggy']);
assert.deepStrictEqual(selected, []);
});

test('test getRandomPerson', async() =>{
const name_list = 'John\nMay\nAlex\nJay\nPeggy';
await writeFile('name_list.txt', name_list, 'utf-8');
const app = new Application();
const [people, selected] = await app.getNames();

for(var i=0; i<app.people.length; i++){
person = app.getRandomPerson();
assert(people.includes(person));
}
});


test('test selectNextPerson', async(t) =>{
const name_list = 'John\nMay';
await writeFile('name_list.txt', name_list, 'utf-8');
const app = new Application();
const [people, selected] = await app.getNames();
flag = 0;
app.getRandomPerson = () => {
flag++;
if (flag % 2) return 'John';
else return 'May';
}

person = app.selectNextPerson();
assert.strictEqual(person, 'John');
assert.deepStrictEqual(app.selected[0], 'John');

flag = 0;
person = app.selectNextPerson();
assert.strictEqual(person, 'May');
assert.deepStrictEqual(app.selected[1], 'May');

person = app.selectNextPerson();
assert.strictEqual(person, null);
});

test('test notifySelected', async() =>{
const name_list = 'John';
await writeFile('name_list.txt', name_list, 'utf-8');
const app = new Application();
const mailSystemMock = new MailSystemMock();
app.mailSystem = mailSystemMock;
const [people, selected] = await app.getNames();

Math.random = () => 0.1;
app.selectNextPerson();
app.notifySelected();
assert.strictEqual(mailSystemMock.writeCalls.length, 1);
assert.strictEqual(mailSystemMock.sendCalls.length, 1);

fs.unlinkSync('name_list.txt');
});

class MailSystemMock extends MailSystem {
constructor() {
super();
this.writeCalls = [];
this.sendCalls = [];
}write(name){
this.writeCalls.push(name);
return super.write(name);
}send(name, context){
this.sendCalls.push({name, context});
return super.send(name, context);
}
};

0 comments on commit 1378665

Please sign in to comment.