Skip to content

Commit

Permalink
Lab1 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
Carreynycu committed Mar 6, 2024
1 parent b12b371 commit 9f3af93
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 50 deletions.
18 changes: 9 additions & 9 deletions lab1/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
58 changes: 17 additions & 41 deletions lab1/main_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,32 @@ const test = require('node:test');
const assert = require('assert');
const { MyClass, Student } = require('./main');

// Test MyClass's addStudent with various inputs
test("MyClass's addStudent with various inputs", async (t) => {
test("Test MyClass's addStudent", () => {
const myClass = new MyClass();
const student = new Student();
student.setName("Test Student");
const index = myClass.addStudent(student);
assert.strictEqual(index, 0, "Should return index 0 for the first student");

const nonStudent = {}; // Not an instance of Student
const result = myClass.addStudent(nonStudent);
assert.strictEqual(result, -1, "Should return -1 when adding non-Student");

// Testing with null, undefined and basic types
assert.strictEqual(myClass.addStudent(null), -1, "Should return -1 for null");
assert.strictEqual(myClass.addStudent(undefined), -1, "Should return -1 for undefined");
assert.strictEqual(myClass.addStudent("string"), -1, "Should return -1 for string");
assert.strictEqual(myClass.addStudent(student), 0);
assert.strictEqual(myClass.addStudent(123), -1);
});

// Test MyClass's getStudentById with boundary values
test("MyClass's getStudentById with boundary values", async (t) => {
test("Test MyClass's getStudentById", () => {
const myClass = new MyClass();
const student = new Student();
student.setName("Boundary Test Student");
const index = myClass.addStudent(student);

const outOfBoundIndex = myClass.getStudentById(myClass.students.length);
assert.strictEqual(outOfBoundIndex, null, "Should return null for out of bounds index");

const negativeIndex = myClass.getStudentById(-1);
assert.strictEqual(negativeIndex, null, "Should return null for negative index");
myClass.addStudent(student);
assert.strictEqual(myClass.getStudentById(0), student);
assert.strictEqual(myClass.getStudentById(-1), null);
});

// Test Student's setName with different types of input
test("Student's setName with different types of input", async (t) => {
test("Test Student's setName", () => {
const student = new Student();
student.setName("Test Name");
assert.strictEqual(student.getName(), "Test Name", "Should return the correct name after setting it");

student.setName(123); // Attempt to set name with non-string
assert.strictEqual(student.getName(), "Test Name", "Name should remain unchanged when setting with non-string");

student.setName(""); // Setting name to an empty string
assert.strictEqual(student.getName(), "", "Name should be empty when set to an empty string");

student.setName(" "); // Setting name to a space
assert.strictEqual(student.getName(), " ", "Name should be a space when set to a space");
student.setName("John");
assert.strictEqual(student.getName(), "John");
student.setName(123);
assert.strictEqual(student.getName(), "John");
});

// Test Student's getName without setting name
test("Student's getName without setting name", async (t) => {
test("Test Student's getName", () => {
const student = new Student();
assert.strictEqual(student.getName(), '', "Should return an empty string if name has not been set");
});
assert.strictEqual(student.getName(), "");
student.setName("John");
assert.strictEqual(student.getName(), "John");
});

0 comments on commit 9f3af93

Please sign in to comment.