|
1 | 1 | local series = require('series')
|
2 | 2 |
|
3 | 3 | describe('series', function()
|
4 |
| - it('should generate 1 character series', function() |
| 4 | + it('slices of one from one', function() |
5 | 5 | local result = {}
|
6 |
| - for s in series('abcde', 1) do |
| 6 | + for s in series("1", 1) do |
7 | 7 | table.insert(result, s)
|
8 | 8 | end
|
9 |
| - assert.same({ 'a', 'b', 'c', 'd', 'e' }, result) |
| 9 | + assert.same({ "1" }, result) |
10 | 10 | end)
|
11 | 11 |
|
12 |
| - it('should generate multi-character series', function() |
| 12 | + it('slices of one from two', function() |
13 | 13 | local result = {}
|
14 |
| - for s in series('hello', 3) do |
| 14 | + for s in series("12", 1) do |
15 | 15 | table.insert(result, s)
|
16 | 16 | end
|
17 |
| - assert.same({ 'hel', 'ell', 'llo' }, result) |
| 17 | + assert.same({ "1", "2" }, result) |
18 | 18 | end)
|
19 | 19 |
|
20 |
| - it('should generate one series when the series length equals the string length', function() |
| 20 | + it('slices of two', function() |
21 | 21 | local result = {}
|
22 |
| - for s in series('exercism', 8) do |
| 22 | + for s in series("35", 2) do |
23 | 23 | table.insert(result, s)
|
24 | 24 | end
|
25 |
| - assert.same({ 'exercism' }, result) |
| 25 | + assert.same({ "35" }, result) |
26 | 26 | end)
|
27 | 27 |
|
28 |
| - it('should return no series when the series length requested is longer than the string', function() |
| 28 | + it('slices of two overlap', function() |
29 | 29 | local result = {}
|
30 |
| - for s in series('1234', 5) do |
| 30 | + for s in series("9142", 2) do |
31 | 31 | table.insert(result, s)
|
32 | 32 | 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") |
34 | 80 | end)
|
35 | 81 | end)
|
0 commit comments