From f0f0c2927886dd7a8e21e64c0412c6c234b244c6 Mon Sep 17 00:00:00 2001 From: LYX Date: Sat, 30 Mar 2024 10:55:43 +0800 Subject: [PATCH] lab4 v1.0 --- lab2/main_test.js | 143 +++++++++++++++++++++++++++++++++++++++++++++- lab3/main_test.js | 63 ++++++++++++++++++++ lab4/main_test.js | 15 +++++ 3 files changed, 219 insertions(+), 2 deletions(-) diff --git a/lab2/main_test.js b/lab2/main_test.js index 5034468e..1b0de875 100644 --- a/lab2/main_test.js +++ b/lab2/main_test.js @@ -1,6 +1,145 @@ const test = require('node:test'); const assert = require('assert'); +const fs = require('fs'); +const os = require('os'); +const path = require('path'); + const { Application, MailSystem } = require('./main'); -// TODO: write your tests here -// Remember to use Stub, Mock, and Spy when necessary \ No newline at end of file +test('should return a message context', () => { + const mailSystem = new MailSystem(); + const name = 'John'; + + const context = mailSystem.write(name); + assert.strictEqual(context, 'Congrats, John!'); + + }); + +test('should return true if mail is sent successfully', (t) => { + const mailSystem = new MailSystem(); + const name = 'John'; + + // test send mail success return true + t.mock.method(Math,'random', () => 1); + is_send = mailSystem.send('Joy', "test mail"); + assert.strictEqual(is_send, true); + + + // test send mail fail return false + t.mock.method(Math,'random', () => 0.4); + is_send = mailSystem.send('Joy', "test mail"); + assert.strictEqual(is_send, false); + + }); + + + + + +test('should return name_list ', async()=>{ + + // Write mock name list to a temporary file + const nameListContent = 'Alice\nBob\nCharlie'; + const tempFilePath = path.join('name_list.txt'); + fs.writeFileSync(tempFilePath, nameListContent); + + // Attach cleanup handler to the process exit event + process.on('exit', () => { + if (tempFilePath) { + // Clean up: Delete the temporary directory + fs.unlinkSync(tempFilePath); + } + }); + + // Instantiate Application class and call getNames with the temporary file path + const app = new Application(); + const [people, selected] = await app.getNames(tempFilePath); + + // Assert the results + assert.deepStrictEqual(people, ['Alice', 'Bob', 'Charlie']); + assert.deepStrictEqual(selected, []); + +}); + + +test('should return null if all people are selected', async (t) => { + const app = new Application(); + app.people = ['Alice', 'Bob', 'Charlie']; + app.selected = ['Alice', 'Bob', 'Charlie']; + + const result = app.selectNextPerson(); + assert.strictEqual(result, null); + }); + + //Test case for getRandomPerson() method +test('should return a random person', () => { + // Stub Math.random() to return a fixed value + Math.random = () => 0.5; // Set Math.random() to always return 0.5 + const randomNumber = Math.random(); + + // Create an instance of the Application class + const app = new Application(); + app.people = ['Alice', 'Bob', 'Charlie']; + // Call the getRandomPerson() method + const randomPerson = app.getRandomPerson(); + + // Ensure that the random person is one of the people in the list + assert(app.people.includes(randomPerson)); + +}); + +test('should select and return a person who has not been selected yet', () => { + const app = new Application(); + app.people = ['Alice', 'Bob', 'Charlie']; + + let getRandomPersonCallCount = 0; + app.getRandomPerson = () => { + switch (getRandomPersonCallCount++) { + case 0: + return 'Alice'; + case 1: + return 'Bob'; + case 2: + return 'Charlie'; + } + }; + + app.selected = ['Alice', 'Bob']; + + const result = app.selectNextPerson(); + + assert.strictEqual(result, 'Charlie'); + assert.strictEqual(getRandomPersonCallCount, 3); +}); + +class MockMailSystem { + constructor() { + this.writeCallCount = 0; + this.sendCallCount = 0; + } + + write() { + this.writeCallCount++; + return 'Message context'; + } + + send() { + this.sendCallCount++; + return true; + } +} + +test('should call write and send methods of MailSystem for each selected person', () => { + const mailSystem = new MockMailSystem(); + + const app = new Application(); + app.mailSystem = mailSystem; + app.selected = ['Alice', 'Bob', 'Charlie']; + + app.notifySelected(); + + assert.strictEqual(mailSystem.writeCallCount, 3); + assert.strictEqual(mailSystem.sendCallCount, 3); + + +}); \ No newline at end of file diff --git a/lab3/main_test.js b/lab3/main_test.js index 096fd421..2cfca59d 100644 --- a/lab3/main_test.js +++ b/lab3/main_test.js @@ -3,3 +3,66 @@ const assert = require('assert'); const { Calculator } = require('./main'); // TODO: write your tests here +describe('Calculate', (t) =>{ + const calculator = new Calculator(); + + it('test exp() functionality', () => { + const testcases = [ + { param: Infinity, expected: Error, msg: 'unsupported operand type'}, + { param: -Infinity, expected: Error, msg: 'unsupported operand type'}, + { param: NaN, expected: Error, msg: 'unsupported operand type'}, + { param: 'abc', expected: Error, msg: 'unsupported operand type'}, + { param: true, expected: Error, msg: 'unsupported operand type'}, + { param: null, expected: Error, msg: 'unsupported operand type'}, + { param: undefined, expected: Error, msg: 'unsupported operand type'}, + { param: {}, expected: Error, msg: 'unsupported operand type'}, + + { param: Number.MAX_VALUE, expected: Error, msg: 'overflow' }, + + { param: 42, expected: Math.exp(42)}, + { param: 3.14, expected: Math.exp(3.14)}, + { param: 0, expected: Math.exp(0)} + ]; + for (const tc of testcases) { + if (tc.expected === Error) { + assert.throws(() => { + calculator.exp(tc.param); + }, + { + name: 'Error', + message: tc.msg + }); + } else { + assert.strictEqual(calculator.exp(tc.param), tc.expected); + } + } + }); + it('test log() functionality', () => { + const testcases = [ + { param: Infinity, expected: Error, msg: 'unsupported operand type'}, + { param: -Infinity, expected: Error, msg: 'unsupported operand type'}, + { param: true, expected: Error, msg: 'unsupported operand type'}, + { param: 'abc', expected: Error, msg: 'unsupported operand type'}, + + { param: 0, expected: Error, msg: "math domain error (1)" }, + { param: -1, expected: Error, msg: 'math domain error (2)'}, + + { param: 42, expected: Math.log(42)}, + { param: 3.14, expected: Math.log(3.14)}, + ]; + + for (const tc of testcases) { + if(tc.expected === Error){ + assert.throws(() =>{ + calculator.log(tc.param); + },{ + name: 'Error', + message: tc.msg + }); + }else{ + assert.strictEqual(calculator.log(tc.param), tc.expected); + } + } + }); + +}); \ No newline at end of file diff --git a/lab4/main_test.js b/lab4/main_test.js index e37d21a5..2e9f41b6 100644 --- a/lab4/main_test.js +++ b/lab4/main_test.js @@ -10,12 +10,27 @@ const puppeteer = require('puppeteer'); // Hints: // Click search button + await page.click('button.DocSearch.DocSearch-Button'); + // Type into search box + await page.waitForSelector('#docsearch-input'); + // Wait for search result + await page.type('#docsearch-input', 'chipi chipi chapa chapa', { delay: 150 }); + // Get the `Docs` result section + await page.waitForSelector('#docsearch-item-5'); + // Click on first result in `Docs` section + await page.click('#docsearch-item-5'); + // Locate the title + let textSelector = await page.waitForSelector('h1'); + let title = await textSelector.evaluate(element => element.textContent); + // Print the title + console.log(title); + // Close the browser await browser.close();