Skip to content
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

512558011 #142

Closed
wants to merge 1 commit into from
Closed
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
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();
//create 2 students John & Jane
const student1 = new Student();
student1.setName('John');
const id1 = myClass.addStudent(student1);
assert.strictEqual(id1, 0);

const student2 = new Student();
student2.setName('Jane');
const id2 = myClass.addStudent(student2);
assert.strictEqual(id2, 1);

//新增一個TESTING student
const invalidStudent = 'TESTING';
const invalidId = myClass.addStudent(invalidStudent);
assert.strictEqual(invalidId, -1);

});

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

const retrievedStudent = myClass.getStudentById(0);
assert.strictEqual(retrievedStudent.getName(), 'John');

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

const outOfBoundsStudent = myClass.getStudentById(1);
assert.strictEqual(outOfBoundsStudent, null);

});

test("Test Student's setName", () => {
// TODO
throw new Error("Test not implemented");
const student = new Student();
//測試變數是否有效
student.setName('Alice');
assert.strictEqual(student.getName(), 'Alice');

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

});

test("Test Student's getName", () => {
// TODO
throw new Error("Test not implemented");
});
const student = new Student();
// 初始化未設置
assert.strictEqual(student.getName(), '');

// 設置變數後返回
student.setName('Bob');
assert.strictEqual(student.getName(), 'Bob');

});
Loading