Skip to content

Commit c1005df

Browse files
authored
Merge pull request #7 from Rexturnull/512558006
[LAB1] 512558006
2 parents f468cee + a8bd0a0 commit c1005df

File tree

2 files changed

+64
-9
lines changed

2 files changed

+64
-9
lines changed

lab1/main.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ class Student {
4545
// const myClass = new MyClass();
4646
// const names = ['John', 'Jane', 'Doe', 'Smith'];
4747
// names.forEach(name => {
48-
// const student = new Student();
49-
// student.setName(name);
50-
// const newStudentId = myClass.addStudent(student);
51-
// const newStudentName = myClass.getStudentById(newStudentId).getName();
52-
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
48+
// const student = new Student();
49+
// student.setName(name);
50+
// const newStudentId = myClass.addStudent(student);
51+
// const newStudentName = myClass.getStudentById(newStudentId).getName();
52+
// console.log('[+] Added student with id: %d, name: %s', newStudentId, newStudentName);
5353
// });
5454

5555
module.exports = { MyClass, Student };

lab1/main_test.js

+59-4
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,77 @@ const test = require('node:test');
22
const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

5+
56
test("Test MyClass's addStudent", () => {
67
// TODO
7-
throw new Error("Test not implemented");
8+
//throw new Error("Test not implemented");
9+
const myClass = new MyClass();
10+
const student = new Student();
11+
12+
13+
//!(student instanceof Student)
14+
const notAStudent = { name: "Rex" }
15+
const notAStudentInstance = myClass.addStudent(notAStudent);
16+
assert.strictEqual(notAStudentInstance, -1, "If it's not a Student instance, it should return -1.");
17+
18+
// push result
19+
student.setName("Rex");
20+
const addStudentResult = myClass.addStudent(student);
21+
assert.strictEqual(myClass.students[0], student, "The student has not been added to the student list.");
22+
23+
// return id
24+
assert.strictEqual(addStudentResult, 0, "Return wrong id.")
825
});
926

1027
test("Test MyClass's getStudentById", () => {
1128
// TODO
12-
throw new Error("Test not implemented");
29+
//throw new Error("Test not implemented");
30+
const myClass = new MyClass();
31+
const student1 = new Student();
32+
const student2 = new Student();
33+
student1.setName("Alice");
34+
student2.setName("Bob");
35+
myClass.addStudent(student1);
36+
myClass.addStudent(student2);
37+
38+
39+
// id < 0
40+
assert.strictEqual(myClass.getStudentById(-1), null, "get(-1) should be null");
41+
42+
// id >= this.students.length
43+
assert.strictEqual(myClass.getStudentById(2), null, "Out of range should be null");
44+
45+
46+
// return id
47+
assert.strictEqual(myClass.getStudentById(0), student1, "Abnormal return of ID value");
48+
assert.strictEqual(myClass.getStudentById(1), student2, "Abnormal return of ID value");
49+
1350
});
1451

1552
test("Test Student's setName", () => {
1653
// TODO
17-
throw new Error("Test not implemented");
54+
//throw new Error("Test not implemented");
55+
const student = new Student();
56+
57+
// return userName
58+
student.setName("Alice");
59+
assert.strictEqual(student.getName(), "Alice", "setName Fail");
60+
61+
// typeof userName !== 'string'
62+
student.setName(123);
63+
assert.strictEqual(student.getName(), "Alice", "userName should not be changed to anything other than a String");
1864
});
1965

2066
test("Test Student's getName", () => {
2167
// TODO
22-
throw new Error("Test not implemented");
68+
//throw new Error("Test not implemented");
69+
70+
// this.name === undefined
71+
const student = new Student();
72+
assert.strictEqual(student.getName(), "", "When the name is not set, it should return an empty string");
73+
74+
// return name
75+
student.setName("Bob");
76+
assert.strictEqual(student.getName(), "Bob", "Get the incorrect name");
77+
2378
});

0 commit comments

Comments
 (0)