From 250429449a3432ae05d73183ea80e45d21e91487 Mon Sep 17 00:00:00 2001 From: luancs11 <125711852+luancs11@users.noreply.github.com> Date: Wed, 6 Mar 2024 23:07:15 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BD=9C=E6=A5=ADV1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 完成作業V1 --- lab1/HelloWorld.js | 19 +++++++++++++++++ lab1/main_test.js | 53 ++++++++++++++++++++++++++++++++++------------ 2 files changed, 59 insertions(+), 13 deletions(-) create mode 100644 lab1/HelloWorld.js diff --git a/lab1/HelloWorld.js b/lab1/HelloWorld.js new file mode 100644 index 00000000..a490d20b --- /dev/null +++ b/lab1/HelloWorld.js @@ -0,0 +1,19 @@ +//引用'http'模組 +const http = require('http'); + +//設定server網址,因為在本機端測試,所以輸入127.0.0.1 +const hostname = '127.0.0.1'; +//設定port號,這邊多少都可以,不要和現有的port重複就好 +const port = 3000; + +//新增一個server並指定他的頁面資訊,內容為'Hello World!' +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.end('Hello World!\n'); +}); + +//監聽剛剛新建的server,開啟後就執行裡面的console.log +server.listen(port, hostname, () => { + console.log(`Server running at http://${hostname}:${port}/`); +}); \ No newline at end of file diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..23eac216 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -2,22 +2,49 @@ 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"); +// 測試 MyClass 的 addStudent 方法 +test("Test MyClass's addStudent", async (t) => { + const myClass = new MyClass(); + const student = new Student(); + student.setName("John Doe"); + + const addedStudentId = myClass.addStudent(student); + assert.strictEqual(addedStudentId, 0, "應該將學生加入並返回正確的索引"); + + const nonStudent = {}; // 模擬非 Student 實例 + const nonStudentId = myClass.addStudent(nonStudent); + assert.strictEqual(nonStudentId, -1, "非 Student 實例不應該被添加"); }); -test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); +// 測試 MyClass 的 getStudentById 方法 +test("Test MyClass's getStudentById", async (t) => { + const myClass = new MyClass(); + const student = new Student(); + student.setName("Jane Doe"); + myClass.addStudent(student); + + const retrievedStudent = myClass.getStudentById(0); + assert.strictEqual(retrievedStudent.getName(), "Jane Doe", "應該返回正確的學生名稱"); + + const invalidStudent = myClass.getStudentById(-1); + assert.strictEqual(invalidStudent, null, "無效索引應該返回 null"); }); -test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); +// 測試 Student 的 setName 方法 +test("Test Student's setName", async (t) => { + const student = new Student(); + student.setName("John Smith"); + assert.strictEqual(student.name, "John Smith", "setName 應該正確設置學生名稱"); + + student.setName(123); // 嘗試用非字符串設置名稱 + assert.notStrictEqual(student.name, 123, "setName 應該忽略非字符串值"); }); -test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file +// 測試 Student 的 getName 方法 +test("Test Student's getName", async (t) => { + const student = new Student(); + assert.strictEqual(student.getName(), '', "未設置名稱時應返回空字符串"); + + student.setName("Doe Jane"); + assert.strictEqual(student.getName(), "Doe Jane", "getName 應返回設置的學生名稱"); +}); From 3c7959156b0bb03c3502ecbc5dacec261795603c Mon Sep 17 00:00:00 2001 From: FortanAlaric <160106148+FortanAlaric@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:28:51 +0800 Subject: [PATCH 2/2] V3 V3 --- lab1/HelloWorld.js | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 lab1/HelloWorld.js diff --git a/lab1/HelloWorld.js b/lab1/HelloWorld.js deleted file mode 100644 index a490d20b..00000000 --- a/lab1/HelloWorld.js +++ /dev/null @@ -1,19 +0,0 @@ -//引用'http'模組 -const http = require('http'); - -//設定server網址,因為在本機端測試,所以輸入127.0.0.1 -const hostname = '127.0.0.1'; -//設定port號,這邊多少都可以,不要和現有的port重複就好 -const port = 3000; - -//新增一個server並指定他的頁面資訊,內容為'Hello World!' -const server = http.createServer((req, res) => { - res.statusCode = 200; - res.setHeader('Content-Type', 'text/plain'); - res.end('Hello World!\n'); -}); - -//監聽剛剛新建的server,開啟後就執行裡面的console.log -server.listen(port, hostname, () => { - console.log(`Server running at http://${hostname}:${port}/`); -}); \ No newline at end of file