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

[LAB3] 313553005 #204

Open
wants to merge 7 commits into
base: 313553005
Choose a base branch
from
Open
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
122 changes: 120 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,123 @@ 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
/*
function coverage testing:
- MailSystem
- write
- send
- Application
- getNames
- getRandomPerson
- selectNextPerson
- notifySelected
*/

// MailSystem test
test('MailSystem write test', (t) => {
const mail = new MailSystem();
const result = mail.write('Test');
assert.equal(result, 'Congrats, Test!');
});

// Test Stub
test('MailSystem send test', (t) => {
const mail = new MailSystem();
const originalRandom = Math.random;

// Test success case
Math.random = () => 1;
assert.equal(mail.send('Test', 'content'), true);

// Test failure case
Math.random = () => 0;
assert.equal(mail.send('Test', 'content'), false);

Math.random = originalRandom;
});

// Application tests
test('Application initialization test', async (t) => {
// 準備測試檔案
fs.writeFileSync('name_list.txt', 'Test1\nTest2\nTest3');

try {
const app = new Application();
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成

assert(Array.isArray(app.people));
assert(Array.isArray(app.selected));
assert.equal(app.people.length, 3);
assert.equal(app.selected.length, 0);
} finally {
// 清理測試檔案
fs.unlinkSync('name_list.txt');
}
});


test('Application selectNextPerson test', async (t) => {
// 準備測試檔案
fs.writeFileSync('name_list.txt', 'Test1\nTest2\nTest3');

try {
const app = new Application();
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成

const person1 = app.selectNextPerson();
assert(app.people.includes(person1));
assert.equal(app.selected.length, 1);

const person2 = app.selectNextPerson();
assert(app.people.includes(person2));
assert.equal(app.selected.length, 2);
assert.notEqual(person1, person2);

const person3 = app.selectNextPerson();
assert(app.people.includes(person3));
assert.equal(app.selected.length, 3);

// 當所有人都被選中後,應該返回 null
const person4 = app.selectNextPerson();
assert.equal(person4, null);
} finally {
// 清理測試檔案
fs.unlinkSync('name_list.txt');
}
});


// Mock Object
test('Application notifySelected test', async (t) => {
// 準備測試檔案
fs.writeFileSync('name_list.txt', 'Test1\nTest2');

try {
const app = new Application();
await new Promise(resolve => setTimeout(resolve, 100)); // 等待初始化完成

app.selectNextPerson(); // 選擇第一個人
app.selectNextPerson(); // 選擇第二個人

// Mock MailSystem methods
let writeCount = 0;
let sendCount = 0;
app.mailSystem.write = (name) => {
writeCount++;
return `Congrats, ${name}!`;
};
app.mailSystem.send = (name, context) => {
sendCount++;
return true;
};

app.notifySelected();
assert.equal(writeCount, 2);
assert.equal(sendCount, 2);
} finally {
// 清理測試檔案
fs.unlinkSync('name_list.txt');
}
});


60 changes: 60 additions & 0 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,63 @@ const assert = require('assert');
const { Calculator } = require('./main');

// TODO: write your tests here
describe('Calculator', () => {
const calculator = new Calculator();

describe('exp(x)', () => {
// 測試正常情況
const normalCases = [
{ input: 0, expected: 1 },
{ input: 1, expected: Math.E },
{ input: -1, expected: 1/Math.E }
];

normalCases.forEach(({ input, expected }) => {
it(`should correctly calculate exp(${input})`, () => {
assert.strictEqual(calculator.exp(input), expected);
});
});

// 測試錯誤情況
const errorCases = [
{ input: Infinity, error: 'unsupported operand type' },
{ input: NaN, error: 'unsupported operand type' },
{ input: 1000, error: 'overflow' } // 大數會造成溢位
];

errorCases.forEach(({ input, error }) => {
it(`should throw error for exp(${input})`, () => {
assert.throws(() => calculator.exp(input), { message: error });
});
});
});

describe('log(x)', () => {
// 測試正常情況
const normalCases = [
{ input: 1, expected: 0 },
{ input: Math.E, expected: 1 },
{ input: 10, expected: Math.log(10) }
];

normalCases.forEach(({ input, expected }) => {
it(`should correctly calculate log(${input})`, () => {
assert.strictEqual(calculator.log(input), expected);
});
});

// 測試錯誤情況
const errorCases = [
{ input: Infinity, error: 'unsupported operand type' },
{ input: NaN, error: 'unsupported operand type' },
{ input: 0, error: 'math domain error (1)' },
{ input: -1, error: 'math domain error (2)' }
];

errorCases.forEach(({ input, error }) => {
it(`should throw error for log(${input})`, () => {
assert.throws(() => calculator.log(input), { message: error });
});
});
});
});
Loading