diff --git a/lab1/main.js b/lab1/main.js index c9aed9f0..ce672501 100644 --- a/lab1/main.js +++ b/lab1/main.js @@ -42,14 +42,14 @@ class Student { } } -// 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); + //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); // }); -module.exports = { MyClass, Student }; \ No newline at end of file +module.exports = { MyClass, Student }; diff --git a/lab1/main_test.js b/lab1/main_test.js index 74a716b4..6963c7ba 100644 --- a/lab1/main_test.js +++ b/lab1/main_test.js @@ -3,21 +3,39 @@ 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(); + const student = new Student(); + student.setName("John"); + const index = myClass.addStudent(student); + assert.strictEqual(index, 0, "addStudent should return index 0 for the first student"); + const notAStudent = {}; + const indexForNotAStudent = myClass.addStudent(notAStudent); + assert.strictEqual(indexForNotAStudent, -1, "addStudent should return -1 when adding a non-Student instance"); }); test("Test MyClass's getStudentById", () => { - // TODO - throw new Error("Test not implemented"); + const myClass = new MyClass(); + const student = new Student(); + student.setName("Jane"); + const index = myClass.addStudent(student); + const fetchedStudent = myClass.getStudentById(index); + assert.strictEqual(fetchedStudent.getName(), "Jane", "getStudentById should retrieve the student with the correct name"); + const invalidFetchedStudent = myClass.getStudentById(-1); + assert.strictEqual(invalidFetchedStudent, null, "getStudentById should return null for an invalid id"); }); test("Test Student's setName", () => { - // TODO - throw new Error("Test not implemented"); + const student = new Student(); + student.setName("Doe"); + assert.strictEqual(student.name, "Doe", "setName should correctly set the student's name"); + student.setName(123); + assert.strictEqual(student.name, "Doe", "setName should not set name when the input is not a string"); }); test("Test Student's getName", () => { - // TODO - throw new Error("Test not implemented"); -}); \ No newline at end of file + const student = new Student(); + student.setName("Smith"); + assert.strictEqual(student.getName(), "Smith", "getName should return the correct name"); + const newStudent = new Student(); + assert.strictEqual(newStudent.getName(), '', "getName should return an empty string for a student without a name"); +});