Skip to content

Commit

Permalink
Merge branch 'a121100' into lab7
Browse files Browse the repository at this point in the history
  • Loading branch information
Sharkkcode authored May 8, 2024
2 parents 92b4150 + 2d4b773 commit a1b3803
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 37 deletions.
97 changes: 85 additions & 12 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,96 @@
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 testClass = new MyClass();

// invalid student
const inv_stu = [-1, 0, 100, "QAQ", 3.14];
for(let i = 0; i < inv_stu.length; i++) {
assert.strictEqual(testClass.addStudent(inv_stu[i]), -1);
}

// valid student
const names = ["John", "Jane", "Doe", "Smith"];
stuList = [];
for(let i = 0; i < names.length; i++) {
stuTmp = new Student();
stuTmp.setName(names[i]);
stuList.push(stuTmp);
}
for(let i = 0; i < stuList.length; i++) {
assert.strictEqual(testClass.addStudent(stuList[i]), i);
}

});

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

const testClass = new MyClass();
const names = ["John", "Jane", "Doe", "Smith"];
stuList = [];
for(let i = 0; i < names.length; i++) {
stuTmp = new Student();
stuTmp.setName(names[i]);
stuList.push(stuTmp);
}
for(let i = 0; i < stuList.length; i++) {
testClass.addStudent(stuList[i]);
}

// invalid id
const inv_id = [-1, stuList.length, stuList.length + 1];
for(let i = 0; i < inv_id.length; i++) {
assert.strictEqual(testClass.getStudentById(inv_id[i]), null);
}

// valid id
for(let i = 0; i < stuList.length; i++) {
console.log(testClass.getStudentById(i));
assert.strictEqual(testClass.getStudentById(i), stuList[i]);
}


});

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

// invalid username
const inv_names = [-1, 0, 100, 1234]
for(let i = 0; i < inv_names.length; i++) {
stuTmp = new Student();
assert.strictEqual(stuTmp.setName(inv_names[i]), undefined);
assert.strictEqual(stuTmp.getName(), "");
}

// valid username
const v_names = ["John", "Jane", "Doe", "Smith"];
for(let i = 0; i < v_names.length; i++) {
stuTmp = new Student();
assert.strictEqual(stuTmp.setName(v_names[i]), undefined);
assert.strictEqual(stuTmp.getName(), v_names[i]);
}

});

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

// invalid username
const inv_names = [-1, 0, 100, 1234]
for(let i = 0; i < inv_names.length; i++) {
stuTmp = new Student();
assert.strictEqual(stuTmp.setName(inv_names[i]), undefined);
assert.strictEqual(stuTmp.getName(), "");
}

// valid username
const v_names = ["John", "Jane", "Doe", "Smith"];
for(let i = 0; i < v_names.length; i++) {
stuTmp = new Student();
assert.strictEqual(stuTmp.setName(v_names[i]), undefined);
assert.strictEqual(stuTmp.getName(), v_names[i]);
}

});
94 changes: 89 additions & 5 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,90 @@
const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');
const test = require("node:test");
const assert = require("assert");

const fs = require("fs")

const name = ["John", "Jane", "Doe", "Smith"];
let name_list_txt = "";
for(let i = 0; i < name.length; i++) {
name_list_txt += name[i];
if(i != name.length - 1) {
name_list_txt += "\n"
}
}

test.mock.method(fs, "readFile", (path, options, callback) => callback(null, name_list_txt));


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

test("Test MailSystem's write", () => {
const ms = new MailSystem();
for(let i = 0; i < name.length; i++) {
assert.strictEqual(ms.write(name[i]),"Congrats, " + name[i] + "!");
}
});

test("Test MailSystem's send", () => {
const ms = new MailSystem();

// success
test.mock.method(Math, "random", () => 0.6);
for(let i = 0; i < name.length; i++) {
assert.strictEqual(ms.send(name[i]), true);
}

// not success
test.mock.method(Math, "random", () => 0.4);
for(let i = 0; i < name.length; i++) {
assert.strictEqual(ms.send(name[i]), false);
}
});

test("Test Application's getNames", async () => {
const app = new Application();
const [people, selected] = await app.getNames();
assert.deepStrictEqual(people, name);
assert.deepStrictEqual(selected, []);
});

test("Test Application's getRandomPerson", () => {
const app = new Application();
app.people = name;
test.mock.method(Math, "random", () => 0.2);
assert.strictEqual(app.getRandomPerson(), name[0]);
test.mock.method(Math, "random", () => 0.6);
assert.strictEqual(app.getRandomPerson(), name[2]);
});

test("Test Application's selectNextPerson", () => {
const app = new Application();
app.people = name;
app.selected = name;
assert.strictEqual(app.selectNextPerson(), null);

// test someone reapeats at least 10 times
const mock_random = test.mock.fn(() => 0.6);
test.mock.method(Math, 'random', () => {
if (mock_random.mock.calls.length < 10) {
return mock_random();
}
return 0.2;
});

app.selected = [];
assert.strictEqual(app.selectNextPerson(), name[2]);
assert.strictEqual(app.selectNextPerson(), name[0]);
});

test("Test Application's notifySelected", () => {
const app = new Application();
app.people = name;
app.selected = name;
app.mailSystem.write = test.mock.fn(app.mailSystem.write);
app.mailSystem.send = test.mock.fn(app.mailSystem.send);
app.notifySelected();
assert.strictEqual(app.mailSystem.write.mock.calls.length, name.length);
assert.strictEqual(app.mailSystem.send.mock.calls.length, name.length);
});


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

test("Test Calculator's exp", () => {
const cal = new Calculator();
const testcases = [
{ param : 87, expected : 6.076030225056873e+37 },
{ param : 0, expected : 1 },
{ param : -1, expected : 0.36787944117144233 }
];
for( const tc of testcases ) {
assert.strictEqual(cal.exp(tc.param), tc.expected);
}
});

test("Test Calculator's exp's Error('unsupported operand type')", () => {
const cal = new Calculator();
const testcases = [
{ param : Infinity, name : "Error", message : "unsupported operand type" },
{ param : -Infinity, name : "Error", message : "unsupported operand type" },
{ param : NaN, name : "Error", message : "unsupported operand type" },
{ param : "aaa", name : "Error", message : "unsupported operand type" },
{ param : undefined, name : "Error", message : "unsupported operand type" }
];
for( const tc of testcases ) {
assert.throws(() => cal.exp(tc.param), {
name : tc.name,
message : tc.message
});
}
});

test("Test Calculator's exp's Error('overflow')", () => {
const cal = new Calculator();
const testcases = [
{ param : 1337, name : "Error", message : "overflow" }
];
for( const tc of testcases ) {
assert.throws(() => cal.exp(tc.param), {
name : tc.name,
message : tc.message
});
}
});

test("Test Calculator's log", () => {
const cal = new Calculator();
const testcases = [
{ param : 48763, expected : 10.794727107543425 },
{ param : 2, expected : 0.6931471805599453 },
{ param : 1, expected : 0 }
];
for( const tc of testcases ) {
assert.strictEqual(cal.log(tc.param), tc.expected);
}
});

test("Test Calculator's log's Error('unsupported operand type')", () => {
const cal = new Calculator();
const testcases = [
{ param : Infinity, name : "Error", message : "unsupported operand type" },
{ param : -Infinity, name : "Error", message : "unsupported operand type" },
{ param : NaN, name : "Error", message : "unsupported operand type" },
{ param : "aaa", name : "Error", message : "unsupported operand type" },
{ param : undefined, name : "Error", message : "unsupported operand type" }
];
for( const tc of testcases ) {
assert.throws(() => cal.log(tc.param), {
name : tc.name,
message : tc.message
});
}
});

test("Test Calculator's log's Error('math domain error (1)')", () => {
const cal = new Calculator();
const testcases = [
{ param : 0, name : "Error", message : "math domain error (1)" }
];
for( const tc of testcases ) {
assert.throws(() => cal.log(tc.param), {
name : tc.name,
message : tc.message
});
}
});

test("Test Calculator's log's Error('math domain error (2)')", () => {
const cal = new Calculator();
const testcases = [
{ param : -1, name : "Error", message : "math domain error (2)" }
];
for( const tc of testcases ) {
assert.throws(() => cal.log(tc.param), {
name : tc.name,
message : tc.message
});
}
});


// TODO: write your tests here
42 changes: 26 additions & 16 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
const puppeteer = require('puppeteer');
const puppeteer = require("puppeteer");

(async () => {
// Launch the browser and open a new blank page
// open website
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto("https://pptr.dev/");

// click search box
const searchArea = ".DocSearch-Button"
await page.waitForSelector(searchArea);
await page.click(searchArea);

// type and srearch
const searchBox = ".DocSearch-Input";
const searchText = "chipi chipi chapa chapa";
await page.waitForSelector(searchBox);
await page.type(searchBox, searchText, { delay: 1000 });

// click search item
const docsSearchItem = "#docsearch-item-5"
await page.waitForSelector(docsSearchItem);
await page.click(docsSearchItem);

// select and print text
const title = await page.waitForSelector('.markdown h1');
const titleText = await page.evaluate(x => x.textContent, title);
console.log(titleText);

// Navigate the page to a URL
await page.goto('https://pptr.dev/');

// 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

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

0 comments on commit a1b3803

Please sign in to comment.