Skip to content

[LAB1] 510558017 #16

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

Closed
wants to merge 5 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
2 changes: 1 addition & 1 deletion lab1/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Introduction

In this lab, you will write unit tests for functions implemented in `main.js`. You can learn how to use classes and functions in it by uncommenting the code in `main.js.` (But remember don't commit them on GitHub)
In this lab, you will write unit tests for functions implemented in `main.js`. You can learn how to use classes and functions in it by uncommenting the code in it. (But remember don't commit them on GitHub)

## Requirement

Expand Down
22 changes: 11 additions & 11 deletions lab1/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ class Student {
}
}

// const myClass = new MyClass();
// const names = ['John', 'Jane', 'Doe', 'Smith'];
// names.forEach(name => {
// const student = new Student();
// student.setName(name);
// const newStudentId = myClass.addStudent(student);
// const newStudentName = myClass.getStudentById(newStudentId).getName();
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
// });

module.exports = { MyClass, Student };
const myClass = new MyClass();
const names = ['John', 'Jane', 'Doe', 'Smith'];
names.forEach(name => {
const student = new Student();
student.setName(name);
const newStudentId = myClass.addStudent(student);
const newStudentName = myClass.getStudentById(newStudentId).getName();
console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
});

module.exports = { MyClass, Student };
57 changes: 48 additions & 9 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,60 @@ const assert = require('assert');
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();

// Test adding a student
myClass.addStudent(student);
assert.strictEqual(myClass.students.length, 1);
assert.strictEqual(myClass.students[0], student);

// Test adding multiple students
const student2 = new Student();
const student3 = new Student();
myClass.addStudent(student2);
myClass.addStudent(student3);
assert.strictEqual(myClass.students.length, 3);
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
const student1 = new Student();
const student2 = new Student();
const student3 = new Student();
myClass.addStudent(student1);
myClass.addStudent(student2);
myClass.addStudent(student3);

// Test getting a student by ID
const foundStudent = myClass.getStudentById(1);
assert.strictEqual(foundStudent, student2);

// Test getting a non-existing student by ID
const nonExistingStudent = myClass.getStudentById(5);
assert.strictEqual(nonExistingStudent, null);
});

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

// Test setting a valid name
student.setName("John");
assert.strictEqual(student.getName(), "John");

// Test setting an invalid name
student.setName(123); // Passing a number instead of a string
assert.strictEqual(student.getName(), ""); // Name should remain unchanged
});

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

// Test getting name when name is set
student.setName("John");
assert.strictEqual(student.getName(), "John");

// Test getting name when name is not set
assert.strictEqual(student.getName(), ""); // Should return an empty string
});