Skip to content

Commit fe3c580

Browse files
authored
Merge pull request #6 from CTHua/312551008
[LAB1] 312551008
2 parents 2d41d26 + e09e3fb commit fe3c580

File tree

1 file changed

+40
-8
lines changed

1 file changed

+40
-8
lines changed

lab1/main_test.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,53 @@ const assert = require('assert');
33
const { MyClass, Student } = require('./main');
44

55
test("Test MyClass's addStudent", () => {
6-
// TODO
7-
throw new Error("Test not implemented");
6+
const myClass = new MyClass();
7+
// if student is not an instance of Student, return -1
8+
assert.strictEqual(myClass.addStudent({}), -1);
9+
10+
// normal case
11+
const names = ['John', 'Jane', 'Doe', 'Smith'];
12+
names.forEach(name => {
13+
const student = new Student();
14+
student.setName(name);
15+
const newStudentId = myClass.addStudent(student);
16+
const newStudentName = myClass.getStudentById(newStudentId).getName();
17+
assert.strictEqual(newStudentName, name);
18+
});
819
});
920

1021
test("Test MyClass's getStudentById", () => {
11-
// TODO
12-
throw new Error("Test not implemented");
22+
const myClass = new MyClass();
23+
// if id is less than 0, return null
24+
assert.strictEqual(myClass.getStudentById(-1), null);
25+
26+
// normal case
27+
const names = ['John', 'Jane', 'Doe', 'Smith'];
28+
names.forEach(name => {
29+
const student = new Student();
30+
student.setName(name);
31+
const newStudentId = myClass.addStudent(student);
32+
const newStudent = myClass.getStudentById(newStudentId);
33+
assert.strictEqual(student, newStudent);
34+
});
35+
// if id is greater than or equal to the length of students, return null
36+
assert.strictEqual(myClass.getStudentById(names.length), null);
1337
});
1438

1539
test("Test Student's setName", () => {
16-
// TODO
17-
throw new Error("Test not implemented");
40+
const student = new Student();
41+
student.setName('John');
42+
assert.strictEqual(student.getName(), 'John');
43+
// Test if setName accepts only string
44+
student.setName(123);
45+
assert.strictEqual(student.getName(), 'John');
1846
});
1947

2048
test("Test Student's getName", () => {
21-
// TODO
22-
throw new Error("Test not implemented");
49+
const student = new Student();
50+
// Test if getName returns empty string if name is undefined
51+
assert.strictEqual(student.getName(), '');
52+
// Test if getName returns the name set by setName
53+
student.setName('John');
54+
assert.strictEqual(student.getName(), 'John');
2355
});

0 commit comments

Comments
 (0)