Skip to content

Commit 7c5a6f0

Browse files
authored
Update tests grade-school (#2747)
* Update tests grade-school * Run format.mjs
1 parent db38db3 commit 7c5a6f0

File tree

4 files changed

+227
-69
lines changed

4 files changed

+227
-69
lines changed

exercises/practice/grade-school/.meta/config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"ankorGH",
77
"draalger",
88
"ee7",
9+
"jagdish-15",
910
"kytrinyx",
1011
"matthewmorgan",
1112
"mgmatola",

exercises/practice/grade-school/.meta/proof.ci.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,32 @@ export class GradeSchool {
44
}
55

66
add(student, level) {
7-
this.students.set(student, level);
7+
for (const names of this.students.values()) {
8+
if (names.has(student)) {
9+
return false;
10+
}
11+
}
12+
13+
if (!this.students.has(level)) {
14+
this.students.set(level, new Set());
15+
}
16+
17+
this.students.get(level).add(student);
18+
return true;
819
}
920

1021
grade(level) {
11-
return Array.from(this.students.entries())
12-
.filter(([, studentGrade]) => studentGrade === level)
13-
.map(([student]) => student)
14-
.sort();
22+
if (!this.students.has(level)) {
23+
return [];
24+
}
25+
return [...this.students.get(level)].sort();
1526
}
1627

1728
roster() {
18-
const result = {};
19-
20-
Array.from(this.students.entries()).forEach(([, studentGrade]) => {
21-
if (!result[studentGrade]) {
22-
result[studentGrade] = this.grade(studentGrade);
23-
}
24-
});
25-
29+
const result = [];
30+
for (const level of [...this.students.keys()].sort((a, b) => a - b)) {
31+
result.push(...[...this.students.get(level)].sort());
32+
}
2633
return result;
2734
}
2835
}
Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,86 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[a3f0fb58-f240-4723-8ddc-e644666b85cc]
13+
description = "Roster is empty when no student is added"
14+
15+
[9337267f-7793-4b90-9b4a-8e3978408824]
16+
description = "Add a student"
417

518
[6d0a30e4-1b4e-472e-8e20-c41702125667]
6-
description = "Adding a student adds them to the sorted roster"
19+
description = "Student is added to the roster"
20+
21+
[73c3ca75-0c16-40d7-82f5-ed8fe17a8e4a]
22+
description = "Adding multiple students in the same grade in the roster"
23+
24+
[233be705-dd58-4968-889d-fb3c7954c9cc]
25+
description = "Multiple students in the same grade are added to the roster"
26+
27+
[87c871c1-6bde-4413-9c44-73d59a259d83]
28+
description = "Cannot add student to same grade in the roster more than once"
729

830
[c125dab7-2a53-492f-a99a-56ad511940d8]
931
description = "A student can't be in two different grades"
32+
include = false
1033

11-
[233be705-dd58-4968-889d-fb3c7954c9cc]
12-
description = "Adding more students adds them to the sorted roster"
34+
[a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1]
35+
description = "A student can only be added to the same grade in the roster once"
36+
include = false
37+
reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8"
38+
39+
[d7982c4f-1602-49f6-a651-620f2614243a]
40+
description = "Student not added to same grade in the roster more than once"
41+
reimplements = "a0c7b9b8-0e89-47f8-8b4a-c50f885e79d1"
42+
43+
[e70d5d8f-43a9-41fd-94a4-1ea0fa338056]
44+
description = "Adding students in multiple grades"
1345

1446
[75a51579-d1d7-407c-a2f8-2166e984e8ab]
15-
description = "Adding students to different grades adds them to the same sorted roster"
47+
description = "Students in multiple grades are added to the roster"
1648

17-
[a3f0fb58-f240-4723-8ddc-e644666b85cc]
18-
description = "Roster returns an empty list if there are no students enrolled"
49+
[7df542f1-57ce-433c-b249-ff77028ec479]
50+
description = "Cannot add same student to multiple grades in the roster"
1951

20-
[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]
21-
description = "Student names with grades are displayed in the same sorted roster"
52+
[6a03b61e-1211-4783-a3cc-fc7f773fba3f]
53+
description = "A student cannot be added to more than one grade in the sorted roster"
54+
include = false
55+
reimplements = "c125dab7-2a53-492f-a99a-56ad511940d8"
2256

23-
[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]
24-
description = "Grade returns the students in that grade in alphabetical order"
57+
[c7ec1c5e-9ab7-4d3b-be5c-29f2f7a237c5]
58+
description = "Student not added to multiple grades in the roster"
59+
reimplements = "6a03b61e-1211-4783-a3cc-fc7f773fba3f"
60+
61+
[d9af4f19-1ba1-48e7-94d0-dabda4e5aba6]
62+
description = "Students are sorted by grades in the roster"
63+
64+
[d9fb5bea-f5aa-4524-9d61-c158d8906807]
65+
description = "Students are sorted by name in the roster"
66+
67+
[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]
68+
description = "Students are sorted by grades and then by name in the roster"
2569

2670
[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]
27-
description = "Grade returns an empty list if there are no students in that grade"
71+
description = "Grade is empty if no students in the roster"
72+
73+
[1e0cf06b-26e0-4526-af2d-a2e2df6a51d6]
74+
description = "Grade is empty if no students in that grade"
75+
76+
[2bfc697c-adf2-4b65-8d0f-c46e085f796e]
77+
description = "Student not added to same grade more than once"
78+
79+
[66c8e141-68ab-4a04-a15a-c28bc07fe6b9]
80+
description = "Student not added to multiple grades"
81+
82+
[c9c1fc2f-42e0-4d2c-b361-99271f03eda7]
83+
description = "Student not added to other grade for multiple grades"
84+
85+
[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]
86+
description = "Students are sorted by name in a grade"
Lines changed: 133 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,178 @@
11
import { beforeEach, describe, expect, test, xtest } from '@jest/globals';
22
import { GradeSchool } from './grade-school';
33

4-
describe('School', () => {
4+
describe('Grade School', () => {
55
let school;
66

77
beforeEach(() => {
88
school = new GradeSchool();
99
});
1010

11-
test('a new school has an empty roster', () => {
12-
expect(school.roster()).toEqual({});
11+
test('Roster is empty when no student is added', () => {
12+
expect(school.roster()).toEqual([]);
1313
});
1414

15-
xtest('adding a student adds them to the roster for the given grade', () => {
15+
xtest('Add a student', () => {
16+
expect(school.add('Aimee', 2)).toEqual(true);
17+
});
18+
19+
xtest('Student is added to the roster', () => {
1620
school.add('Aimee', 2);
1721

18-
const expectedDb = { 2: ['Aimee'] };
22+
const expectedDb = ['Aimee'];
1923
expect(school.roster()).toEqual(expectedDb);
2024
});
2125

22-
xtest('adding more students to the same grade adds them to the roster', () => {
26+
xtest('Adding multiple students in the same grade in the roster', () => {
27+
expect(school.add('Blair', 2)).toEqual(true);
28+
expect(school.add('James', 2)).toEqual(true);
29+
expect(school.add('Paul', 2)).toEqual(true);
30+
});
31+
32+
xtest('Multiple students in the same grade are added to the roster', () => {
2333
school.add('Blair', 2);
2434
school.add('James', 2);
2535
school.add('Paul', 2);
2636

27-
const expectedDb = { 2: ['Blair', 'James', 'Paul'] };
37+
const expectedDb = ['Blair', 'James', 'Paul'];
2838
expect(school.roster()).toEqual(expectedDb);
2939
});
3040

31-
xtest('adding students to different grades adds them to the roster', () => {
41+
xtest('Cannot add student to same grade in the roster more than once', () => {
42+
expect(school.add('Blair', 2)).toEqual(true);
43+
expect(school.add('James', 2)).toEqual(true);
44+
expect(school.add('James', 2)).toEqual(false);
45+
expect(school.add('Paul', 2)).toEqual(true);
46+
});
47+
48+
xtest('Student not added to same grade in the roster more than once', () => {
49+
school.add('Blair', 2);
50+
school.add('James', 2);
51+
school.add('James', 2);
52+
school.add('Paul', 2);
53+
54+
const expectedDb = ['Blair', 'James', 'Paul'];
55+
expect(school.roster()).toEqual(expectedDb);
56+
});
57+
58+
xtest('Adding students in multiple grades', () => {
59+
expect(school.add('Chelsea', 3)).toEqual(true);
60+
expect(school.add('Logan', 7)).toEqual(true);
61+
});
62+
63+
xtest('Students in multiple grades are added to the roster', () => {
3264
school.add('Chelsea', 3);
3365
school.add('Logan', 7);
3466

35-
const expectedDb = { 3: ['Chelsea'], 7: ['Logan'] };
67+
const expectedDb = ['Chelsea', 'Logan'];
3668
expect(school.roster()).toEqual(expectedDb);
3769
});
3870

39-
xtest('grade returns the students in that grade in alphabetical order', () => {
40-
school.add('Franklin', 5);
41-
school.add('Bradley', 5);
42-
school.add('Jeff', 1);
43-
44-
const expectedStudents = ['Bradley', 'Franklin'];
45-
expect(school.grade(5)).toEqual(expectedStudents);
71+
xtest('Cannot add same student to multiple grades in the roster', () => {
72+
expect(school.add('Blair', 2)).toEqual(true);
73+
expect(school.add('James', 2)).toEqual(true);
74+
expect(school.add('James', 3)).toEqual(false);
75+
expect(school.add('Paul', 3)).toEqual(true);
4676
});
4777

48-
xtest('grade returns an empty array if there are no students in that grade', () => {
49-
expect(school.grade(1)).toEqual([]);
78+
xtest('Student not added to multiple grades in the roster', () => {
79+
school.add('Blair', 2);
80+
school.add('James', 2);
81+
school.add('James', 3);
82+
school.add('Paul', 3);
83+
84+
const expectedDb = ['Blair', 'James', 'Paul'];
85+
expect(school.roster()).toEqual(expectedDb);
5086
});
5187

52-
xtest('the students names in each grade in the roster are sorted', () => {
53-
school.add('Jennifer', 4);
54-
school.add('Kareem', 6);
55-
school.add('Christopher', 4);
56-
school.add('Kyle', 3);
88+
xtest('Students are sorted by grades in the roster', () => {
89+
school.add('Jim', 3);
90+
school.add('Peter', 2);
91+
school.add('Anna', 1);
5792

58-
const expectedSortedStudents = {
59-
3: ['Kyle'],
60-
4: ['Christopher', 'Jennifer'],
61-
6: ['Kareem'],
62-
};
63-
expect(school.roster()).toEqual(expectedSortedStudents);
93+
const expectedDb = ['Anna', 'Peter', 'Jim'];
94+
expect(school.roster()).toEqual(expectedDb);
6495
});
6596

66-
xtest('roster cannot be modified outside of module', () => {
67-
school.add('Aimee', 2);
68-
const roster = school.roster();
69-
roster[2].push('Oops.');
70-
const expectedDb = { 2: ['Aimee'] };
97+
xtest('Students are sorted by name in the roster', () => {
98+
school.add('Peter', 2);
99+
school.add('Zoe', 2);
100+
school.add('Alex', 2);
101+
102+
const expectedDb = ['Alex', 'Peter', 'Zoe'];
71103
expect(school.roster()).toEqual(expectedDb);
72104
});
73105

74-
xtest('roster cannot be modified outside of module using grade()', () => {
75-
school.add('Aimee', 2);
76-
school.grade(2).push('Oops.');
77-
const expectedDb = { 2: ['Aimee'] };
106+
xtest('Students are sorted by grades and then by name in the roster', () => {
107+
school.add('Peter', 2);
108+
school.add('Anna', 1);
109+
school.add('Barb', 1);
110+
school.add('Zoe', 2);
111+
school.add('Alex', 2);
112+
school.add('Jim', 3);
113+
school.add('Charlie', 1);
114+
115+
const expectedDb = [
116+
'Anna',
117+
'Barb',
118+
'Charlie',
119+
'Alex',
120+
'Peter',
121+
'Zoe',
122+
'Jim',
123+
];
78124
expect(school.roster()).toEqual(expectedDb);
79125
});
80126

81-
xtest("a student can't be in two different grades", () => {
82-
school.add('Aimee', 2);
83-
school.add('Aimee', 1);
127+
xtest('Grade is empty if no students in the roster', () => {
128+
expect(school.grade(1)).toEqual([]);
129+
});
130+
131+
xtest('Grade is empty if no students in that grade', () => {
132+
school.add('Peter', 2);
133+
school.add('Zoe', 2);
134+
school.add('Alex', 2);
135+
school.add('Jim', 3);
136+
137+
expect(school.grade(1)).toEqual([]);
138+
});
139+
140+
xtest('Student not added to same grade more than once', () => {
141+
school.add('Blair', 2);
142+
school.add('James', 2);
143+
school.add('James', 2);
144+
school.add('Paul', 2);
145+
146+
const expectedDb = ['Blair', 'James', 'Paul'];
147+
expect(school.grade(2)).toEqual(expectedDb);
148+
});
149+
150+
xtest('Student not added to multiple grades', () => {
151+
school.add('Blair', 2);
152+
school.add('James', 2);
153+
school.add('James', 3);
154+
school.add('Paul', 3);
155+
156+
const expectedDb = ['Blair', 'James'];
157+
expect(school.grade(2)).toEqual(expectedDb);
158+
});
159+
160+
xtest('Student not added to other grade for multiple grades', () => {
161+
school.add('Blair', 2);
162+
school.add('James', 2);
163+
school.add('James', 3);
164+
school.add('Paul', 3);
165+
166+
const expectedDb = ['Paul'];
167+
expect(school.grade(3)).toEqual(expectedDb);
168+
});
169+
170+
xtest('Students are sorted by name in a grade', () => {
171+
school.add('Franklin', 5);
172+
school.add('Bradley', 5);
173+
school.add('Jeff', 1);
84174

85-
expect(school.grade(2)).toEqual([]);
175+
const expectedDb = ['Bradley', 'Franklin'];
176+
expect(school.grade(5)).toEqual(expectedDb);
86177
});
87178
});

0 commit comments

Comments
 (0)