Skip to content

Commit

Permalink
Merge branch '312552019' into lab5
Browse files Browse the repository at this point in the history
  • Loading branch information
AxelHowe authored May 9, 2024
2 parents 624319f + ebd7b77 commit 1b9d6c6
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 9 deletions.
49 changes: 41 additions & 8 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,54 @@ 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();
const student = new Student();
const index = myClass.addStudent(student);

assert.strictEqual(index, 0);
assert.strictEqual(myClass.students.length, 1);
assert.strictEqual(myClass.getStudentById(0), student);

const index2 = myClass.addStudent("Not student type");
assert.strictEqual(index2, -1);
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");

const myClass = new MyClass();
const student = new Student();
myClass.addStudent(student);

const retrievedStudent = myClass.getStudentById(0);

assert.strictEqual(retrievedStudent, student);

const retrievedStudent2 = myClass.getStudentById(-100);
assert.strictEqual(retrievedStudent2, null);


});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");

const student = new Student();
student.setName("Charlie");
assert.strictEqual(student.name, "Charlie");

student.setName(123);
assert.strictEqual(student.name, "Charlie")

});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");

const student = new Student();
const name = student.getName();
assert.strictEqual(name, "");

student.setName("David");
const name2 = student.getName();
assert.strictEqual(name2, "David");

});
82 changes: 81 additions & 1 deletion lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,86 @@
const test = require('node:test');
const assert = require('assert');
const fs = require('fs');
test.mock.method(fs, 'readFile', (path, options, callback) => callback(null, 'John\nDoe'));
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

test('getNames should read names from file and return people and selected arrays', async () => {
const app = new Application();
const [people, selected] = await app.getNames();

assert.deepStrictEqual(people, ['John', 'Doe']);
assert.deepStrictEqual(selected, []);
});

test('getRandomPerson should return a random person from the list', () => {
const app = new Application();
app.people = ['John', 'Doe', 'Jane'];

test.mock.method(Math, 'random', () => 0);

const selected = app.getRandomPerson();
assert.strictEqual(selected, 'John');
});


test('selectNextPerson should handle when all people are selected', () => {
const app = new Application();
app.people = ['John'];
app.selected = ['John'];

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

test('selectNextPerson call getRandomPerson twice', () => {
const app = new Application();
app.people = ['John', 'Doe', 'Jane'];
app.selected = ['John'];

//第一次 call random 回傳 0,第二次 call 回傳 0.5
const randomMock = test.mock.method(Math, 'random', () => 0.5);
randomMock.mock.mockImplementationOnce(() => 0);

const selected = app.selectNextPerson();
assert.strictEqual(selected, 'Doe');
});

test('notifySelected should send mail to all selected people', () => {
const app = new Application();
app.selected = ['John', 'Doe'];

test.mock.method(app.mailSystem, 'write', (name) => `Congratulations, ${name}!`);
test.mock.method(app.mailSystem, 'send', (name, context) => true);

app.notifySelected();
assert.equal(app.mailSystem.write.mock.calls.length, 2);
assert.equal(app.mailSystem.send.mock.calls.length, 2);

const call1 = app.mailSystem.send.mock.calls[0];
const call2 = app.mailSystem.send.mock.calls[1];
assert.deepEqual(call1.arguments, ['John', 'Congratulations, John!']);
assert.deepEqual(call2.arguments, ['Doe', 'Congratulations, Doe!']);
});

test('MailSystem write should return mail context for the given name', () => {
const mailSystem = new MailSystem();
const context = mailSystem.write('Alice');
assert.strictEqual(context, 'Congrats, Alice!');
});

test('MailSystem send randomly succeed or fail to send mail', () => {
const mailSystem = new MailSystem();

// fail
test.mock.method(Math, 'random', () => 0);
const result1 = mailSystem.send('Alice', 'Message');
assert(result1 === false);

// success
test.mock.method(Math, 'random', () => 1);
const result2 = mailSystem.send('Alice', 'Message');
assert(result2 === true);
});
52 changes: 52 additions & 0 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,55 @@ const assert = require('assert');
const { Calculator } = require('./main');

// TODO: write your tests here

describe('Calculator', () => {
describe('exp', () => {

const testcase = [
[0, 1],
[1, 2.718281828459045],
[-1, 0.36787944117144233],
[Infinity, Error],
[1000, Error]
];

testcase.forEach(([input, expected]) => {
it(`should return ${expected} for ${input}`, () => {
const calculator = new Calculator();
if (expected === Error) {
assert.throws(() => calculator.exp(input), Error);
} else {
assert.strictEqual(calculator.exp(input), expected);
}
});
}
);

});

describe('log', () => {

const testcase = [
[1, 0],
[1000, 6.907755278982137],
[Math.E, 1],
[0, Error],
[-1, Error],
[Infinity, Error]
];

testcase.forEach(([input, expected]) => {
it(`should return ${expected} for ${input}`, () => {
const calculator = new Calculator();
if (expected === Error) {
assert.throws(() => calculator.log(input), Error);
} else {
assert.strictEqual(calculator.log(input), expected);
}
});
}
);

});
}
);
12 changes: 12 additions & 0 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ const puppeteer = require('puppeteer');
// Locate the title
// Print the title

await page.click('button[class="DocSearch DocSearch-Button"]');

await page.waitForSelector('input[class="DocSearch-Input"]');
await page.type('input[class="DocSearch-Input"]', 'chipi chipi chapa chapa', { delay: 1000 });

await page.waitForSelector('#docsearch-item-5');
await page.click('#docsearch-item-5');

const titleSelector = await page.waitForSelector('h1');
const title = await titleSelector?.evaluate(el => el.textContent);
console.log(title);

// Close the browser
await browser.close();
})();

0 comments on commit 1b9d6c6

Please sign in to comment.