Skip to content
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

[LAB2] 312551183 #113

Closed
wants to merge 4 commits into from
Closed
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
50 changes: 46 additions & 4 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,62 @@ const { MyClass, Student } = require('./main');

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

const myclass = new MyClass();
// input not a student instance, expect -1
assert.strictEqual( -1 , myclass.addStudent());

assert.strictEqual( 0 , myclass.addStudent(new Student()));
assert.strictEqual( 1 , myclass.addStudent(new Student()));


});

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

student0.setName('Stu0');
student1.setName('Stu1');

myclass.addStudent(student0);
myclass.addStudent(student1);

assert.strictEqual( 'Stu0' , myclass.getStudentById(0).getName());
assert.strictEqual( 'Stu1' , myclass.getStudentById(1).getName());

assert.strictEqual( null , myclass.getStudentById(20));
});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
// throw new Error("Test not implemented");
const student = new Student();
// inoput a number => not string => undefined
student.setName(312550000);
assert.strictEqual( undefined , student.name );
// input a string => set name
student.setName('312550000');
assert.strictEqual( '312550000' , student.name );


});

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

// inoput a number => not string => undefined
student.setName(312550000);
assert.strictEqual( '' , student.getName() );

// input a string => set name
student.setName('312550000');
assert.strictEqual( '312550000' , student.getName() );

});
99 changes: 98 additions & 1 deletion lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,103 @@
const test = require('node:test');
const assert = require('assert');
const { Application, MailSystem } = require('./main');
const fs = require('node:fs');
const util = require('util');
const writeFile = util.promisify(fs.writeFile);


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


test("Test Mailsystem" , () => {
const mailsystem = new MailSystem();
const mock_fn = test.mock.fn( mailsystem.write );
assert.strictEqual( mock_fn("Writer") , "Congrats, Writer!" );


mock_fn.mock.mockImplementation( mailsystem.send );
Math.random = () => 0.8;
assert.strictEqual( mock_fn("Sender" , "send_context") , true );

Math.random = () => 0.3;
assert.strictEqual( mock_fn("Sender" , "send_context") , false );

});


test('Test Application', async () => {

const my_name_list = "p1\np2\np3"
await writeFile( "name_list.txt" , my_name_list , 'utf-8');
const application = new Application();

//test getname
const [ application_people , application_selected ] = await application.getNames();
assert.deepStrictEqual( application_people , ["p1" , "p2" , "p3"] );
assert.deepStrictEqual( application_selected , [] );



//test getrandom person
const random_person = application.getRandomPerson();
assert.ok( application_people.includes( random_person ) );



function mock_selNext(){
return application.selectNextPerson();
}
const mock_sel = test.mock.fn(mock_selNext);

//test selectNextPerson
application.getRandomPerson = () => 'p1';
assert.equal( mock_sel() , 'p1' );

application.getRandomPerson = () => 'p2';
assert.equal( mock_sel() , 'p2' );

application.getRandomPerson = () => ['p3']
assert.equal( mock_sel() , 'p3' );

assert.equal( mock_sel() , null );


let cnt = 1;
application.getRandomPerson = () => {
if( cnt % 2 ){
cnt--;
return 'p1';
}else{
return 'p2';
}

}

application.selected = ['p1'];
assert.strictEqual( application.selectNextPerson() , 'p2' );
assert.deepStrictEqual( application.selected , ['p1' , 'p2'] );

//test notifyselected
application.selected = ['p1','p2','p3'];

const mock_write = test.mock.fn(MailSystem.prototype.write);
const mock_send = test.mock.fn(MailSystem.prototype.send );

application.mailSystem.write = mock_write;
application.mailSystem.send = mock_send;

application.notifySelected();
assert.strictEqual( application.mailSystem.write.mock.callCount() , 3 );
assert.strictEqual( application.mailSystem.send.mock.callCount() , 3 );

application.mailSystem = new MailSystem();
const res = application.notifySelected();
assert.strictEqual(res, undefined);



fs.unlinkSync('name_list.txt');


});
Loading