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] 312552057 #108

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
42 changes: 30 additions & 12 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');
const test = require("node:test");
const assert = require("assert");
const { MyClass, Student } = require("./main");

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
assert.strictEqual(myClass.addStudent('Not a student'), -1);
const student = new Student();
for (let i = 0; i < 3; i++) {
const student = new Student();
assert.strictEqual(myClass.addStudent(student), i);
}
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
assert.strictEqual(myClass.getStudentById(-1), null);
for (let i = 0; i < 3; i++) {
const student = new Student();
myClass.addStudent(student);
assert.strictEqual(myClass.getStudentById(i), student);
}
assert.strictEqual(myClass.getStudentById(3), null);
});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
const student = new Student();
student.setName(123);
assert.strictEqual(student.getName(), '');
const name = "Pudding0803";
student.setName(name);
assert.strictEqual(student.getName(), name);
});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});
const student = new Student();
assert.strictEqual(student.getName(), '');
const name = "Pudding0803";
student.setName(name);
assert.strictEqual(student.getName(), name);
});
1 change: 1 addition & 0 deletions lab2/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class Application {
return null;
}
let person = this.getRandomPerson();
console.log(person);
while (this.selected.includes(person)) {
person = this.getRandomPerson();
}
Expand Down
83 changes: 78 additions & 5 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,79 @@
const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');
const { test, mock } = require("node:test");
const assert = require("assert");
const { Application, MailSystem } = require("./main");
const fs = require('fs');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);
const unlinkFile = util.promisify(fs.unlink);

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
const MATH_RANDOM = Math.random;
const FILE_NAME = "name_list.txt";
const CONTENT = "name 0\nname 1\nname 2"

test("Test Mail's write", () => {
const mailSystem = new MailSystem();
assert.strictEqual(
mailSystem.write("Pudding0803"),
"Congrats, Pudding0803!"
);
});

test("Test Mail's send", () => {
const mailSystem = new MailSystem();
Math.random = mock.fn(() => 0.6);
assert.strictEqual(mailSystem.send("Pudding0803", "Hello, world!"), true);
Math.random = mock.fn(() => 0.5);
assert.strictEqual(mailSystem.send("Pudding0803", "Hello, world!"), false);
Math.random = MATH_RANDOM;
});

test("Test Application's getNames", async () => {
fs.writeFile(FILE_NAME, CONTENT, () => {});
const app = new Application();
assert.deepStrictEqual(
await app.getNames(),
[["name 0", "name 1", "name 2"], []]
);
fs.unlink(FILE_NAME, () => {});
});

test("Test Application's getRandomPerson", async () => {
fs.writeFile(FILE_NAME, CONTENT, () => {});
const app = new Application();
await app.getNames();
Math.random = mock.fn(() => 0);
assert.strictEqual(app.getRandomPerson(), "name 0");
Math.random = mock.fn(() => 0.99);
assert.strictEqual(app.getRandomPerson(), "name 2");
Math.random = MATH_RANDOM;
fs.unlink(FILE_NAME, () => {});
});

test("Test Application's selectNextPerson", async () => {
fs.writeFile(FILE_NAME, CONTENT, () => {});
const app = new Application();
await app.getNames();
Math.random = mock.fn(() => 0);
assert.strictEqual(app.selectNextPerson(), "name 0");
Math.random = mock.fn(() => 0.4);
assert.strictEqual(app.selectNextPerson(), "name 1");
Math.random = MATH_RANDOM;
assert.strictEqual(app.selectNextPerson(), "name 2");
assert.strictEqual(app.selectNextPerson(), null);
fs.unlink(FILE_NAME, () => {});
});

test("Test Application's notifySelected", async () => {
fs.writeFile(FILE_NAME, CONTENT, () => {});
const app = new Application();
await app.getNames();
app.mailSystem.write = mock.fn((name) => name);
app.mailSystem.send = mock.fn((name, context) => true);
app.selectNextPerson();
app.selectNextPerson();
app.notifySelected();
assert.strictEqual(app.mailSystem.write.mock.calls.length, 2);
assert.strictEqual(app.mailSystem.send.mock.calls.length, 2);
Math.random = MATH_RANDOM;
fs.unlink(FILE_NAME, () => {});
});
Loading