Skip to content

Commit

Permalink
feat: finish test of main.js
Browse files Browse the repository at this point in the history
  • Loading branch information
YJack0000 committed Mar 4, 2024
1 parent 033f080 commit a101512
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions lab1/main_test.js
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,23 +1,48 @@
const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');
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");
let myClass = new MyClass();
// test if the type of the student is not Student
assert.strictEqual(myClass.addStudent("not a student"), -1);

// test if there is no student in MyClass
assert.strictEqual(myClass.students.length, 0);

// test if there is one student in MyClass
let student = new Student();
student.setName("John");
myClass.addStudent(student);
assert.strictEqual(myClass.students.length, 1);
});

test("Test MyClass's getStudentById", () => {
// TODO
throw new Error("Test not implemented");
let myClass = new MyClass();
// test if the search id is not a number
assert.strictEqual(myClass.getStudentById("not a number"), undefined);

// test if student id is out of range
assert.strictEqual(myClass.getStudentById(-1), null);
assert.strictEqual(myClass.getStudentById(0), null);

// test if the student is found
let student = new Student();
student.setName("John");
myClass.addStudent(student);
assert.strictEqual(myClass.getStudentById(0), student);
});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
let student = new Student();
assert.strictEqual(student.setName(123), undefined);
student.setName("John");
assert.strictEqual(student.name, "John");
});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});
let student = new Student();
assert.strictEqual(student.getName(), "");
student.setName("John");
assert.strictEqual(student.getName(), "John");
});

0 comments on commit a101512

Please sign in to comment.