Skip to content

[LAB1] 512558006 #7

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 3 commits into from
Mar 17, 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
10 changes: 5 additions & 5 deletions lab1/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ 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);
// 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 };
63 changes: 59 additions & 4 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,77 @@ 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");
//throw new Error("Test not implemented");
const myClass = new MyClass();
const student = new Student();


//!(student instanceof Student)
const notAStudent = { name: "Rex" }
const notAStudentInstance = myClass.addStudent(notAStudent);
assert.strictEqual(notAStudentInstance, -1, "If it's not a Student instance, it should return -1.");

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

// return id
assert.strictEqual(addStudentResult, 0, "Return wrong id.")
});

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


// id < 0
assert.strictEqual(myClass.getStudentById(-1), null, "get(-1) should be null");

// id >= this.students.length
assert.strictEqual(myClass.getStudentById(2), null, "Out of range should be null");


// return id
assert.strictEqual(myClass.getStudentById(0), student1, "Abnormal return of ID value");
assert.strictEqual(myClass.getStudentById(1), student2, "Abnormal return of ID value");

});

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

// return userName
student.setName("Alice");
assert.strictEqual(student.getName(), "Alice", "setName Fail");

// typeof userName !== 'string'
student.setName(123);
assert.strictEqual(student.getName(), "Alice", "userName should not be changed to anything other than a String");
});

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

// this.name === undefined
const student = new Student();
assert.strictEqual(student.getName(), "", "When the name is not set, it should return an empty string");

// return name
student.setName("Bob");
assert.strictEqual(student.getName(), "Bob", "Get the incorrect name");

});