Skip to content

[LAB2] 512558015 #61

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

Merged
merged 12 commits into from
Mar 20, 2024
Merged
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
73 changes: 60 additions & 13 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,69 @@ 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");
// Test MyClass addStudent functionality
test('Test MyClass addStudent', () => {
const myClass = new MyClass();
const student = new Student();

//check if student is instance of Student
const notAStudent = 123
const notAStudentInstance = myClass.addStudent(notAStudent);
assert.strictEqual(notAStudentInstance, -1, "If it's not a Student instance, it should return -1.");

// push student to Student list
student.setName("Joy");
const addStudentResult = myClass.addStudent(student);
assert.strictEqual(myClass.students[0], student, "The student has not been added to the student list. Add success");

// return id
assert.strictEqual(addStudentResult, 0, "Id should be 0 or positive number.")
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
// Test MyClass getStudentById functionality
test('Test MyClass getStudentById', () => {
const myClass = new MyClass();
const student = new Student();
student.setName('Tiffany');
myClass.addStudent(student);

// Id is negative number
assert.strictEqual(myClass.getStudentById(-1), null, "Id should not be negative number, otherwise it will return null");

// Id is not exist
assert.strictEqual(myClass.getStudentById(myClass.students.length+1), null, "Id is not exist");

// student exist
assert.strictEqual(myClass.getStudentById(0), student, "return student object");

});

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

// pass a value to setName that is a string type
student1.setName('Alice');
assert.strictEqual(student1.getName(), 'Alice', 'Student name set correctly');

// pass a value to setName that is not a string type
student2.setName(123);
assert.strictEqual(student2.getName(), '', "userName should be in type of a string value");


});

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

// Test getting the name of a student without setting a name
assert.strictEqual(student.getName(), '', 'Student name should be an empty string if not set');

// Test after set a name to student can return name
student.setName('Bob');
assert.strictEqual(student.getName(), 'Bob', 'Student name retrieved correctly');


});
144 changes: 142 additions & 2 deletions lab2/main_test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,146 @@
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
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);


});