Skip to content

[LAB4] 313553034 #327

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

Open
wants to merge 13 commits into
base: main
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
44 changes: 44 additions & 0 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,50 @@ const assert = require('assert');
const { MyClass, Student } = require('./main');

test("Test MyClass's addStudent", () => {
const Class = new MyClass();
const student = new Student();
const id = Class.addStudent(student);
assert.strictEqual(id, 0);
assert.strictEqual(Class.addStudent({}), -1);
});

test("Test MyClass's getStudentById", () => {
const Class = new MyClass();
const student = new Student();
student.setName("John");

const id = Class.addStudent(student);
const StudentID = Class.getStudentById(id);

assert.strictEqual(StudentID.getName(), "John");
assert.strictEqual(Class.getStudentById(-1), null);
assert.strictEqual(Class.getStudentById(999), null);

});

test("Test Student's setName", () => {
const student = new Student();
student.setName(123);
assert.strictEqual(student.getName(), "");

student.setName("John");
const StudentName = student.getName();


assert.strictEqual(StudentName, "John");

});

test("Test Student's getName", () => {
const student = new Student();

// "" in default
assert.strictEqual(student.getName(), "");

student.setName("John");
const StudentName = student.getName();
assert.strictEqual(StudentName, "John");
=======
// TODO
throw new Error("Test not implemented");
});
Expand Down
95 changes: 94 additions & 1 deletion lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,99 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');

// 模擬 fs.readFile
function mockReadFile(path, encoding, callback) {
callback(null, 'Alice\nBob\nCharlie'); // Stub
}
const mockFsRead = test.mock.method(fs, 'readFile', mockReadFile);

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

test('MailSystem: write()', () => {
const mailSystem = new MailSystem;
assert.strictEqual(mailSystem.write('Alice'), 'Congrats, Alice!');
});

test('MailSystem: send() success case', () => {
function returnHigh() { return 0.9; } // Stub
const mailSystem = new MailSystem();
const mockRandom = test.mock.method(Math, 'random', returnHigh);
assert.strictEqual(mailSystem.send('Alice', 'Congrats, Alice!'), true);
mockRandom.mock.restore();
});

test('MailSystem: send() failure case', () => {
function returnLow() { return 0.1; } // Stub
const mailSystem = new MailSystem();
const mockRandom = test.mock.method(Math, 'random', returnLow);
assert.strictEqual(mailSystem.send('Alice', 'Congrats, Alice!'), false);
mockRandom.mock.restore();
});

test('Application: constructor initializes names', async () => {
const app = new Application();
await new Promise((resolve) => setTimeout(resolve, 10)); // Wait for async init
assert.deepStrictEqual(app.people, ['Alice', 'Bob', 'Charlie']);
assert.deepStrictEqual(app.selected, []);
});

test('Application: getRandomPerson()', () => {
function returnFirst() { return 0; }
const app = new Application();
const mockRandom = test.mock.method(Math, 'random', returnFirst);
assert.strictEqual(app.getRandomPerson(), 'Alice');
mockRandom.mock.restore();
});

test('Application: selectNextPerson()', async () => {
const application = new Application();
await new Promise(resolve => setTimeout(resolve, 50));

function returnApple() {
return 'Apple';
}
const mockrandompersonA = test.mock.method(application, 'getRandomPerson', returnApple);
assert.strictEqual(application.selectNextPerson(), 'Apple');
assert.deepStrictEqual(application.selected, ['Apple']);
mockrandompersonA.mock.restore();

let cnt = 0;
function returnAppleThenBanana() {
return cnt++ === 0 ? 'Apple' : 'Banana';
}
const mockrandompersonAB = test.mock.method(application, 'getRandomPerson', returnAppleThenBanana);
assert.strictEqual(application.selectNextPerson(), 'Banana');
assert.deepStrictEqual(application.selected, ['Apple', 'Banana']);
mockrandompersonAB.mock.restore();

function returnOrange() {
return 'Orange';
}
const mockrandompersonO = test.mock.method(application, 'getRandomPerson', returnOrange);
assert.strictEqual(application.selectNextPerson(), 'Orange');
assert.deepStrictEqual(application.selected, ['Apple', 'Banana', 'Orange']);
mockrandompersonO.mock.restore();

assert.strictEqual(application.selectNextPerson(), null);
});

test('Application: notifySelected()', () => {
const app = new Application();
app.selected = ['Alice', 'Bob'];

const mockWrite = test.mock.method(app.mailSystem, 'write');
const mockSend = test.mock.method(app.mailSystem, 'send');

app.notifySelected();

assert.strictEqual(mockWrite.mock.callCount(), 2);
assert.strictEqual(mockSend.mock.callCount(), 2);
mockWrite.mock.restore();
mockSend.mock.restore();
});
=======
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary
// Remember to use Stub, Mock, and Spy when necessary
54 changes: 54 additions & 0 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
<<<<<<< HEAD
// const {describe, it} = require('node:test');
const assert = require('assert');
const { Calculator } = require('./main');
const test = require('node:test');
// TODO: write your tests here

const calc = new Calculator();
test('exp', () => {
const cases = [
{ input: 1, expected: Math.exp(1) },
{ input: 0, expected: 1 },
{ input: -2, expected: Math.exp(-2) }
];

for (const tc of cases) {
assert.ok(Math.abs(calc.exp(tc.input) - tc.expected) < 1e-10);
}
});


test('log', () => {
const cases = [
{ input: 1, expected: 0 },
{ input: Math.E, expected: 1 },
{ input: 10, expected: Math.log(10) }
];

for (const tc of cases) {
assert.ok(Math.abs(calc.log(tc.input) - tc.expected) < 1e-10);
}
});

// error
test('exp type error or overflow', () => {
const badInputs = [Infinity, -Infinity, NaN];
for (const val of badInputs) {
assert.throws(() => calc.exp(val), /unsupported operand type/);
}

assert.throws(() => calc.exp(10000), /overflow/);
});

test('log errors', () => {
const badInputs = [Infinity, -Infinity, NaN];
for (const val of badInputs) {
assert.throws(() => calc.log(val), /unsupported operand type/);
}

assert.throws(() => calc.log(0), /math domain error \(1\)/);
assert.throws(() => calc.log(-1), /math domain error \(2\)/);
});
=======
const {describe, it} = require('node:test');
const assert = require('assert');
const { Calculator } = require('./main');

// TODO: write your tests here
>>>>>>> 0fddd2447c7e3144c1f3e22b6e6b7b6c5e1d49fb
27 changes: 21 additions & 6 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const puppeteer = require('puppeteer');

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

(async () => {
// Launch the browser and open a new blank page
const browser = await puppeteer.launch();
Expand All @@ -10,13 +14,24 @@ const puppeteer = require('puppeteer');

// Hints:
// Click search button
// Type into search box
// Wait for search result
// Get the `Docs` result section
// Click on first result in `Docs` section
// Locate the title
// Print the title
await page.waitForSelector('button.DocSearch-Button', { visible: true });
await page.click('button.DocSearch-Button');

// Type into search box
await page.waitForSelector('#docsearch-input');
await page.type('#docsearch-input', 'andy popoo');
await sleep(300);
// Wait for search results
await page.waitForSelector('#docsearch-hits1-item-4 > a > div', { visible: true });

await page.click('#docsearch-hits1-item-4 > a > div');
// Find the first result in the Docs section and click it
await page.waitForSelector('h1');
const title = await page.$eval('h1', el => el.textContent.trim());
// Wait for navigation to finish and the title to appear


console.log(title)
// Close the browser
await browser.close();
})();
Loading
Loading