Skip to content

Commit 65df397

Browse files
committed
Update grade-school tests
1 parent cc28783 commit 65df397

File tree

4 files changed

+267
-53
lines changed

4 files changed

+267
-53
lines changed

exercises/practice/grade-school/.meta/example.lua

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,36 @@ function School:new()
66
end
77

88
function School:roster()
9-
return self.db
9+
local roster = {}
10+
for _, grade in pairs(self.db) do
11+
for _, student in ipairs(grade) do
12+
table.insert(roster, student)
13+
end
14+
end
15+
return roster
1016
end
1117

1218
function School:add(name, grade)
19+
for _, grade in pairs(self.db) do
20+
for _, student in ipairs(grade) do
21+
if student == name then
22+
return false
23+
end
24+
end
25+
end
26+
1327
if self.db[grade] then
1428
table.insert(self.db[grade], name)
1529
table.sort(self.db[grade])
1630
else
1731
self.db[grade] = { name }
1832
end
33+
34+
return true
1935
end
2036

2137
function School:grade(grade)
22-
if self.db[grade] then
23-
return self.db[grade]
24-
end
25-
return {}
38+
return self.db[grade] or {}
2639
end
2740

2841
return School
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
local function map(t, f)
2+
local mapped = {}
3+
for i, v in ipairs(t) do
4+
mapped[i] = f(v)
5+
end
6+
return mapped
7+
end
8+
9+
local function stringify(x)
10+
return ("'%s'"):format(x)
11+
end
12+
13+
return {
14+
module_name = 'School',
15+
16+
generate_test = function(case)
17+
local lines = { 'local school = School:new()' }
18+
19+
if case.property == 'grade' or case.property == 'roster' then
20+
for _, input in ipairs(case.input.students) do
21+
local student, grade = table.unpack(input)
22+
table.insert(lines, ('school:add(%s, %d)'):format(stringify(student), grade))
23+
end
24+
table.insert(lines,
25+
('assert.are.same({ %s }, school:%s(%s))'):format(table.concat(map(case.expected, stringify), ', '),
26+
case.property, case.input.desiredGrade or ''))
27+
elseif case.property == 'add' then
28+
for i, input in ipairs(case.input.students) do
29+
local student, grade = table.unpack(input)
30+
local expected
31+
do
32+
if type(case.expected) == 'table' then
33+
expected = case.expected[i]
34+
else
35+
expected = case.expected
36+
end
37+
end
38+
table.insert(lines, ('assert.is_%s(school:add(%s, %d))'):format(expected, stringify(student), grade))
39+
end
40+
else
41+
error("Unknown property: " .. case.property)
42+
end
43+
44+
return table.concat(lines, '\n')
45+
end
46+
}
Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +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"
723

824
[233be705-dd58-4968-889d-fb3c7954c9cc]
9-
description = "Adding more student adds them to the sorted roster"
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"
29+
30+
[c125dab7-2a53-492f-a99a-56ad511940d8]
31+
description = "A student can't be in two different grades"
32+
include = false
33+
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"
1045

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

14-
[a3f0fb58-f240-4723-8ddc-e644666b85cc]
15-
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"
1651

17-
[180a8ff9-5b94-43fc-9db1-d46b4a8c93b6]
18-
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"
1956

20-
[1bfbcef1-e4a3-49e8-8d22-f6f9f386187e]
21-
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"
2269

2370
[5e67aa3c-a3c6-4407-a183-d8fe59cd1630]
24-
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: 128 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,158 @@
11
local School = require('grade-school')
22

33
describe('grade-school', function()
4-
it('a new school has an empty roster', function()
4+
it('roster is empty when no student is added', function()
55
local school = School:new()
6-
local expected = {}
7-
local result = school:roster()
8-
assert.are.same(expected, result)
6+
assert.are.same({}, school:roster())
97
end)
108

11-
it('adding a student adds them to the roster for the given grade', function()
9+
it('add a student', function()
10+
local school = School:new()
11+
assert.is_true(school:add('Aimee', 2))
12+
end)
13+
14+
it('student is added to the roster', function()
1215
local school = School:new()
1316
school:add('Aimee', 2)
14-
local expected = { [2] = { 'Aimee' } }
15-
local result = school:roster()
16-
assert.are.same(expected, result)
17+
assert.are.same({ 'Aimee' }, school:roster())
18+
end)
19+
20+
it('adding multiple students in the same grade in the roster', function()
21+
local school = School:new()
22+
assert.is_true(school:add('Blair', 2))
23+
assert.is_true(school:add('James', 2))
24+
assert.is_true(school:add('Paul', 2))
25+
end)
26+
27+
it('multiple students in the same grade are added to the roster', function()
28+
local school = School:new()
29+
school:add('Blair', 2)
30+
school:add('James', 2)
31+
school:add('Paul', 2)
32+
assert.are.same({ 'Blair', 'James', 'Paul' }, school:roster())
33+
end)
34+
35+
it('cannot add student to same grade in the roster more than once', function()
36+
local school = School:new()
37+
assert.is_true(school:add('Blair', 2))
38+
assert.is_true(school:add('James', 2))
39+
assert.is_false(school:add('James', 2))
40+
assert.is_true(school:add('Paul', 2))
1741
end)
1842

19-
it('adding more students to the same grade adds them to the roster', function()
43+
it('student not added to same grade in the roster more than once', function()
2044
local school = School:new()
2145
school:add('Blair', 2)
2246
school:add('James', 2)
47+
school:add('James', 2)
2348
school:add('Paul', 2)
24-
local expected = { [2] = { 'Blair', 'James', 'Paul' } }
25-
local result = school:roster()
26-
assert.are.same(expected, result)
49+
assert.are.same({ 'Blair', 'James', 'Paul' }, school:roster())
50+
end)
51+
52+
it('adding students in multiple grades', function()
53+
local school = School:new()
54+
assert.is_true(school:add('Chelsea', 3))
55+
assert.is_true(school:add('Logan', 7))
2756
end)
2857

29-
it('adding students to different grades adds them to the roster', function()
58+
it('students in multiple grades are added to the roster', function()
3059
local school = School:new()
3160
school:add('Chelsea', 3)
3261
school:add('Logan', 7)
33-
local expected = { [3] = { 'Chelsea' }, [7] = { 'Logan' } }
34-
local result = school:roster()
35-
assert.are.same(expected, result)
62+
assert.are.same({ 'Chelsea', 'Logan' }, school:roster())
3663
end)
3764

38-
it('grade returns the students in that grade in alphabetical order', function()
65+
it('cannot add same student to multiple grades in the roster', function()
3966
local school = School:new()
40-
school:add('Franklin', 5)
41-
school:add('Bradley', 5)
42-
school:add('Jeff', 1)
43-
local expected = { 'Bradley', 'Franklin' }
44-
local result = school:grade(5)
45-
assert.are.same(expected, result)
67+
assert.is_true(school:add('Blair', 2))
68+
assert.is_true(school:add('James', 2))
69+
assert.is_false(school:add('James', 3))
70+
assert.is_true(school:add('Paul', 3))
71+
end)
72+
73+
it('student not added to multiple grades in the roster', function()
74+
local school = School:new()
75+
school:add('Blair', 2)
76+
school:add('James', 2)
77+
school:add('James', 3)
78+
school:add('Paul', 3)
79+
assert.are.same({ 'Blair', 'James', 'Paul' }, school:roster())
80+
end)
81+
82+
it('students are sorted by grades in the roster', function()
83+
local school = School:new()
84+
school:add('Jim', 3)
85+
school:add('Peter', 2)
86+
school:add('Anna', 1)
87+
assert.are.same({ 'Anna', 'Peter', 'Jim' }, school:roster())
4688
end)
4789

48-
it('grade returns an empty array if there are no students in that grade', function()
90+
it('students are sorted by name in the roster', function()
4991
local school = School:new()
50-
local result = school:grade(1)
51-
local expected = {}
52-
assert.are.same(expected, result)
92+
school:add('Peter', 2)
93+
school:add('Zoe', 2)
94+
school:add('Alex', 2)
95+
assert.are.same({ 'Alex', 'Peter', 'Zoe' }, school:roster())
5396
end)
5497

55-
it('the students names in each grade in the roster are sorted', function()
98+
it('students are sorted by grades and then by name in the roster', function()
5699
local school = School:new()
57-
school:add('Jennifer', 4)
58-
school:add('Kareem', 6)
59-
school:add('Christopher', 4)
60-
school:add('Kyle', 3)
61-
local expected = { [3] = { 'Kyle' }, [4] = { 'Christopher', 'Jennifer' }, [6] = { 'Kareem' } }
62-
local result = school:roster()
63-
assert.are.same(expected, result)
100+
school:add('Peter', 2)
101+
school:add('Anna', 1)
102+
school:add('Barb', 1)
103+
school:add('Zoe', 2)
104+
school:add('Alex', 2)
105+
school:add('Jim', 3)
106+
school:add('Charlie', 1)
107+
assert.are.same({ 'Anna', 'Barb', 'Charlie', 'Alex', 'Peter', 'Zoe', 'Jim' }, school:roster())
108+
end)
109+
110+
it('grade is empty if no students in the roster', function()
111+
local school = School:new()
112+
assert.are.same({}, school:grade(1))
113+
end)
114+
115+
it('grade is empty if no students in that grade', function()
116+
local school = School:new()
117+
school:add('Peter', 2)
118+
school:add('Zoe', 2)
119+
school:add('Alex', 2)
120+
school:add('Jim', 3)
121+
assert.are.same({}, school:grade(1))
122+
end)
123+
124+
it('student not added to same grade more than once', function()
125+
local school = School:new()
126+
school:add('Blair', 2)
127+
school:add('James', 2)
128+
school:add('James', 2)
129+
school:add('Paul', 2)
130+
assert.are.same({ 'Blair', 'James', 'Paul' }, school:grade(2))
131+
end)
132+
133+
it('student not added to multiple grades', function()
134+
local school = School:new()
135+
school:add('Blair', 2)
136+
school:add('James', 2)
137+
school:add('James', 3)
138+
school:add('Paul', 3)
139+
assert.are.same({ 'Blair', 'James' }, school:grade(2))
140+
end)
141+
142+
it('student not added to other grade for multiple grades', function()
143+
local school = School:new()
144+
school:add('Blair', 2)
145+
school:add('James', 2)
146+
school:add('James', 3)
147+
school:add('Paul', 3)
148+
assert.are.same({ 'Paul' }, school:grade(3))
149+
end)
150+
151+
it('students are sorted by name in a grade', function()
152+
local school = School:new()
153+
school:add('Franklin', 5)
154+
school:add('Bradley', 5)
155+
school:add('Jeff', 1)
156+
assert.are.same({ 'Bradley', 'Franklin' }, school:grade(5))
64157
end)
65158
end)

0 commit comments

Comments
 (0)