Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…testing into lab7
  • Loading branch information
s123tg committed May 7, 2024
2 parents 2f0151a + 99c8bf4 commit 0d59116
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 54 deletions.
28 changes: 24 additions & 4 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@ 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();
assert.strictEqual(myClass.addStudent(1),-1);
assert.strictEqual(myClass.addStudent(student),0);
//throw new Error("Test not implemented");
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
const student = new Student();
student.setName("stu");
myClass.addStudent(student);
assert.strictEqual(myClass.getStudentById(0),student);
assert.strictEqual(myClass.getStudentById(1),null);

//throw new Error("Test not implemented");
});

test("Test Student's setName", () => {

const student = new Student();
assert.strictEqual(student.setName("stu"),undefined);
assert.strictEqual(student.getName(),"stu");
assert.strictEqual(student.setName(123),undefined);
// TODO
throw new Error("Test not implemented");
//throw new Error("Test not implemented");
});

test("Test Student's getName", () => {
const student = new Student();
assert.strictEqual(student.getName(),'');
student.setName("stu");
assert.strictEqual(student.getName(),"stu");
// TODO
throw new Error("Test not implemented");
//throw new Error("Test not implemented");
});
72 changes: 70 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,74 @@
const test = require('node:test');
const assert = require('assert');
const fs=require('fs');
const { Application, MailSystem } = require('./main');

// TODO: write your tests here
// Remember to use Stub, Mock, and Spy when necessary

test('mailsystem write',(t)=>{
const ms=new MailSystem();
assert.strictEqual(ms.write('sam'), 'Congrats, sam!');
});
test('mailsystem send',(t)=>{
const ms=new MailSystem();
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
assert.strictEqual(typeof(ms.send('sam','mail')), 'boolean');
});
test('mailsystem getName',async (t)=>{
fs.writeFileSync('name_list.txt','sam\ntom\njeck','utf8');
const app=new Application();
const [people, selected] = await app.getNames();
assert.strictEqual(people[0], 'sam');
assert.strictEqual(people[1], 'tom');
assert.strictEqual(people[2], 'jeck');
fs.unlinkSync('name_list.txt');
});
test('mailsystem getran',async (t)=>{
fs.writeFileSync('name_list.txt','sam\ntom\njeck','UTF-8');
const app=new Application();
await app.getNames();
await app.getNames();
await app.getNames();
await app.getNames();
assert.match(app.getRandomPerson(), /^(sam|tom|jeck)$/);
fs.unlinkSync('name_list.txt');
});
test('Application selectNextPerson',async (t)=>{
fs.writeFileSync('name_list.txt','sam\ntom\njeck','UTF-8');
const app=new Application();
await app.getNames();
await app.getNames();
await app.getNames();
await app.getNames();
assert.match(app.selectNextPerson(), /^(sam|tom|jeck)$/);
assert.match(app.selectNextPerson(), /^(sam|tom|jeck)$/);
assert.match(app.selectNextPerson(), /^(sam|tom|jeck)$/);
assert.strictEqual(app.selectNextPerson(), null);
fs.unlinkSync('name_list.txt');
});
test('Application notifySelected',async (t)=>{
fs.writeFileSync('name_list.txt','sam\ntom\njeck','UTF-8');
const app=new Application();
await app.getNames();
await app.getNames();
await app.getNames();
await app.getNames();
app.selectNextPerson();
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(app.notifySelected(), undefined);
app.selectNextPerson();
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(app.notifySelected(), undefined);
assert.strictEqual(app.notifySelected(), undefined);
fs.unlinkSync('name_list.txt');
});
19 changes: 1 addition & 18 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ it('error exp',(t)=>{
calculator.exp(tc.param);
},{message:tc.expected});
}

// assert.throws(()=>{
// calculator.exp('abc');
// },{message:"unsupported operand type"});
// assert.throws(()=>{
// calculator.exp(Infinity);
// },{message:"overflow"});
});

it('happy test log',(t)=>{
Expand All @@ -58,14 +51,4 @@ it('error log',(t)=>{
calculator.log(tc.param);
},{message:tc.expected});
}

// assert.throws(()=>{
// calculator.log('abc');
// },{message:"unsupported operand type"});
// assert.throws(()=>{
// calculator.log(-Infinity);
// },{message:"math domain error (1)"});
// assert.throws(()=>{
// calculator.log(-1);
// },{message:"math domain error (2)"});
});
});
42 changes: 12 additions & 30 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
const puppeteer = require('puppeteer');

(async () => {

// Launch the browser and open a new blank page
const browser = await puppeteer.launch();
const page = await browser.newPage();

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

const serachButton='.DocSearch-Button-Placeholder';
await page.waitForSelector(serachButton);
await page.click(serachButton);
await page.waitForSelector('#docsearch-input');
await page.type('#docsearch-input','chipi chipi chapa chapa');
// const searchBar = await page.waitForSelector('.DocSearch-Dropdown-Container');
// const Docs = await searchBar?.$$('.DocSearch-Hits');
const pageButton = '#docsearch-item-5';
const button = await page.waitForSelector(pageButton);
const b = await button?.waitForSelector('.DocSearch-Hit-Select-Icon');
const a = await b?.evaluate(element => {
if (element) {
// Extract information about the element
return {
tagName: element.tagName,
textContent: element.textContent,
attributes: Array.from(element.attributes).map(attr => ({ name: attr.name, value: attr.value }))
};
} else {
return null; // If element is not found, return null
}
});
console.log(a);
try{await b.click();}catch(error){console.error(error)}
// await page.waitForNavigation();
console.log('Navigation complete. Current URL:', page.url());
const pageSelector = await page.waitForSelector('.theme-doc-markdown.markdown ul');
// const titleSelector = await pageSelector.waitForSelector('.h1');
const title = await pageSelector?.evaluate(element=>element.textContent);
console.log(title);
// 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
await browser.close();
})();

0 comments on commit 0d59116

Please sign in to comment.