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

[LAB1] 312552058 #69

Merged
merged 3 commits into from
Mar 7, 2024
Merged
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
61 changes: 49 additions & 12 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,60 @@
const test = require('node:test');
const test = require('node:test'); //從名為 'node:test' 的模組中引入某些東西,並將其指派給名為 test 的變數
const assert = require('assert');
const { MyClass, Student } = require('./main');
const { MyClass, Student } = require('./main'); //從 require('./main') 返回的物件中,選擇性地取出了兩個成員,分別是 MyClass 和 Student

test("Test MyClass's addStudent", () => {
// TODO
throw new Error("Test not implemented");
test("Test MyClass's addStudent", () => { //第一個參數是測試案例的描述或名稱,第二個參數是一個回調函式,其中包含了實際的測試程式碼。() => { ... } 定義了一個不接受任何參數的匿名箭頭函式,其主體包含在花括號 {} 中。這樣的函式可以在需要函式的地方被調用或傳遞,並且它的主體程式碼會在函式被呼叫時執行。
Comment on lines +1 to +5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are those comments written on your own? If yes, don't leave long comments in your code describing languages' built-in features. It will reduce readability and maintainability.


const myClass = new MyClass();
const student = new Student();
student.setName('Annie');

const result = myClass.addStudent(student);

assert.strictEqual(result, 0);
assert.strictEqual(myClass.students.length, 1);

const invalidstudent = {}; // 一個空物件,不是 Student 的實例
const invalidstudentIndex = myClass.addStudent(invalidstudent);
assert.strictEqual(invalidstudentIndex, -1);

//throw new Error("Test not implemented"); //當程式碼執行到 throw 語句時,它將立即停止執行,並拋出一個錯誤
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the comment here.

});

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

const myClass = new MyClass();
const student = new Student();
student.setName('Annie');
myClass.addStudent(student);

const getstudent1 = myClass.getStudentById(0);

assert.strictEqual(getstudent1.getName(), 'Annie');

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

//throw new Error("Test not implemented");
});

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

const student = new Student();
student.setName('Annie');

assert.strictEqual(student.getName(), 'Annie');
//throw new Error("Test not implemented");

const invalidName = 123; // 數字不是有效的名字
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an acceptable example because it describes the test case we want to execute.
However, it's better to leave comments in English.

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

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

const student = new Student(); // 創建 Student 的一個實例
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the comment here


assert.strictEqual(student.getName(), ''); // 檢查未設置名字的情況下 getName 方法的行為是否正確

//throw new Error("Test not implemented");
});