Skip to content

Commit 4136665

Browse files
sync series tests (#568)
1 parent 9b95989 commit 4136665

File tree

4 files changed

+111
-15
lines changed

4 files changed

+111
-15
lines changed

exercises/practice/series/.meta/example.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
return function(s, length)
2+
assert(string.len(s) > 0, "series cannot be empty")
3+
assert(length >= 0, "slice length cannot be negative")
4+
assert(length > 0, "slice length cannot be zero")
5+
assert(length <= string.len(s), "slice length cannot be greater than series length")
6+
27
return coroutine.wrap(function()
38
for i = 1, #s - length + 1 do
49
coroutine.yield(s:sub(i, i + length - 1))
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
local map = function(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 render_substrings(substrings)
10+
return table.concat(map(substrings, function(substring)
11+
return string.format('"%s"', substring)
12+
end), ', ')
13+
end
14+
15+
return {
16+
module_name = 'series',
17+
18+
generate_test = function(case)
19+
if case.expected.error then
20+
local template = [[
21+
assert.has_error(function()
22+
series("%s", %s)
23+
end, "%s")]]
24+
return template:format(case.input.series, case.input.sliceLength, case.expected.error)
25+
else
26+
local template = [[
27+
local result = {}
28+
for s in series("%s", %s) do
29+
table.insert(result, s)
30+
end
31+
assert.same({ %s }, result)]]
32+
return template:format(case.input.series, case.input.sliceLength, render_substrings(case.expected))
33+
end
34+
end
35+
}

exercises/practice/series/.meta/tests.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
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.
411

512
[7ae7a46a-d992-4c2a-9c15-a112d125ebad]
613
description = "slices of one from one"
@@ -23,6 +30,9 @@ description = "slices of a long series"
2330
[6d235d85-46cf-4fae-9955-14b6efef27cd]
2431
description = "slice length is too large"
2532

33+
[d7957455-346d-4e47-8e4b-87ed1564c6d7]
34+
description = "slice length is way too large"
35+
2636
[d34004ad-8765-4c09-8ba1-ada8ce776806]
2737
description = "slice length cannot be zero"
2838

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,81 @@
11
local series = require('series')
22

33
describe('series', function()
4-
it('should generate 1 character series', function()
4+
it('slices of one from one', function()
55
local result = {}
6-
for s in series('abcde', 1) do
6+
for s in series("1", 1) do
77
table.insert(result, s)
88
end
9-
assert.same({ 'a', 'b', 'c', 'd', 'e' }, result)
9+
assert.same({ "1" }, result)
1010
end)
1111

12-
it('should generate multi-character series', function()
12+
it('slices of one from two', function()
1313
local result = {}
14-
for s in series('hello', 3) do
14+
for s in series("12", 1) do
1515
table.insert(result, s)
1616
end
17-
assert.same({ 'hel', 'ell', 'llo' }, result)
17+
assert.same({ "1", "2" }, result)
1818
end)
1919

20-
it('should generate one series when the series length equals the string length', function()
20+
it('slices of two', function()
2121
local result = {}
22-
for s in series('exercism', 8) do
22+
for s in series("35", 2) do
2323
table.insert(result, s)
2424
end
25-
assert.same({ 'exercism' }, result)
25+
assert.same({ "35" }, result)
2626
end)
2727

28-
it('should return no series when the series length requested is longer than the string', function()
28+
it('slices of two overlap', function()
2929
local result = {}
30-
for s in series('1234', 5) do
30+
for s in series("9142", 2) do
3131
table.insert(result, s)
3232
end
33-
assert.same({}, result)
33+
assert.same({ "91", "14", "42" }, result)
34+
end)
35+
36+
it('slices can include duplicates', function()
37+
local result = {}
38+
for s in series("777777", 3) do
39+
table.insert(result, s)
40+
end
41+
assert.same({ "777", "777", "777", "777" }, result)
42+
end)
43+
44+
it('slices of a long series', function()
45+
local result = {}
46+
for s in series("918493904243", 5) do
47+
table.insert(result, s)
48+
end
49+
assert.same({ "91849", "18493", "84939", "49390", "93904", "39042", "90424", "04243" }, result)
50+
end)
51+
52+
it('slice length is too large', function()
53+
assert.has_error(function()
54+
series("12345", 6)
55+
end, "slice length cannot be greater than series length")
56+
end)
57+
58+
it('slice length is way too large', function()
59+
assert.has_error(function()
60+
series("12345", 42)
61+
end, "slice length cannot be greater than series length")
62+
end)
63+
64+
it('slice length cannot be zero', function()
65+
assert.has_error(function()
66+
series("12345", 0)
67+
end, "slice length cannot be zero")
68+
end)
69+
70+
it('slice length cannot be negative', function()
71+
assert.has_error(function()
72+
series("123", -1)
73+
end, "slice length cannot be negative")
74+
end)
75+
76+
it('empty series is invalid', function()
77+
assert.has_error(function()
78+
series("", 1)
79+
end, "series cannot be empty")
3480
end)
3581
end)

0 commit comments

Comments
 (0)