Skip to content

Commit

Permalink
finish lab1/main_test.js
Browse files Browse the repository at this point in the history
  • Loading branch information
DENGRENHAO committed Feb 29, 2024
1 parent 01554ce commit 457a405
Showing 1 changed file with 48 additions and 8 deletions.
56 changes: 48 additions & 8 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,62 @@ const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');

// 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);
// });

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
const myClass = new MyClass();
const names = ['John', 'Jane', 'Doe', 'Smith'];
for (let i = 0; i < names.length; i++) {
const student = new Student();
const validStudentId = myClass.addStudent(student);
assert.strictEqual(validStudentId, i);
}

const invalidStudentId = myClass.addStudent({});
assert.strictEqual(invalidStudentId, -1);
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
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();
assert.strictEqual(newStudentName, name);
});

const nonExistingStudent = myClass.getStudentById(4);
assert.strictEqual(nonExistingStudent, null);

const nonExistingStudent_2 = myClass.getStudentById(-1);
assert.strictEqual(nonExistingStudent_2, null);
});

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

student.setName('John');
assert.strictEqual(student.getName(), 'John');

student.setName(123);
assert.strictEqual(student.getName(), 'John');
});

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

assert.strictEqual(student.getName(), '');

student.setName('John');
assert.strictEqual(student.getName(), 'John');
});

0 comments on commit 457a405

Please sign in to comment.