Skip to content

Commit aa88dba

Browse files
authored
Update word-search tests (#598)
1 parent 76d4ad4 commit aa88dba

File tree

5 files changed

+561
-68
lines changed

5 files changed

+561
-68
lines changed

exercises/practice/word-search/.meta/example.lua

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,23 @@ end
4242

4343
local deltas = { { 1, 0 }, { 0, 1 }, { -1, 0 }, { 0, -1 }, { 1, 1 }, { 1, -1 }, { -1, 1 }, { -1, -1 } }
4444

45-
return function(puzzle)
45+
return function(grid)
46+
grid = Grid(grid)
47+
4648
return {
47-
find = function(word)
48-
for _, delta in ipairs(deltas) do
49-
local first, last = find(Grid(puzzle), word, delta[1], delta[2])
50-
if first then
51-
return first, last
49+
search = function(words)
50+
local result = {}
51+
52+
for _, word in ipairs(words) do
53+
for _, delta in ipairs(deltas) do
54+
local first, last = find(grid, word, delta[1], delta[2])
55+
if first and last then
56+
result[word] = { start = first, ['end'] = last }
57+
end
5258
end
5359
end
60+
61+
return result
5462
end
5563
}
5664
end
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
local function map(t, f)
2+
local result = {}
3+
for i, v in ipairs(t) do
4+
result[i] = f(v)
5+
end
6+
return result
7+
end
8+
9+
local function stringify(x)
10+
return ("'%s'"):format(x)
11+
end
12+
13+
local function render_expected(expected)
14+
local key_value_pairs = {}
15+
for k, v in pairs(expected) do
16+
local template = "%s = { start = { %d, %d }, ['end'] = { %d, %d } }"
17+
local key_value_pair = template:format(k, v.start.column, v.start.row, v['end'].column, v['end'].row)
18+
table.insert(key_value_pairs, key_value_pair)
19+
end
20+
table.sort(key_value_pairs)
21+
22+
if #key_value_pairs == 0 then
23+
return '{}'
24+
else
25+
return '{ --\n' .. table.concat(key_value_pairs, ', --\n') .. '\n}'
26+
end
27+
end
28+
29+
return {
30+
module_name = 'WordSearch',
31+
32+
generate_test = function(case)
33+
local template = [[
34+
local grid = { --
35+
%s
36+
}
37+
local words = { --
38+
%s
39+
}
40+
local expected = %s
41+
assert.are.same(expected, WordSearch(grid).search(words))]]
42+
43+
local grid = table.concat(map(case.input.grid, stringify), ', --\n')
44+
local words = table.concat(map(case.input.wordsToSearchFor, stringify), ', --\n')
45+
local expected = render_expected(case.expected)
46+
47+
return template:format(grid, words, expected)
48+
end
49+
}

exercises/practice/word-search/.meta/tests.toml

Lines changed: 22 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
[b4057815-0d01-41f0-9119-6a91f54b2a0a]
613
description = "Should accept an initial game grid and a target search word"
@@ -61,3 +68,15 @@ description = "Should locate words written top right to bottom left"
6168

6269
[695531db-69eb-463f-8bad-8de3bf5ef198]
6370
description = "Should fail to locate a word that is not in the puzzle"
71+
72+
[fda5b937-6774-4a52-8f89-f64ed833b175]
73+
description = "Should fail to locate words that are not on horizontal, vertical, or diagonal lines"
74+
75+
[5b6198eb-2847-4e2f-8efe-65045df16bd3]
76+
description = "Should not concatenate different lines to find a horizontal word"
77+
78+
[eba44139-a34f-4a92-98e1-bd5f259e5769]
79+
description = "Should not wrap around horizontally to find a word"
80+
81+
[cd1f0fa8-76af-4167-b105-935f78364dac]
82+
description = "Should not wrap around vertically to find a word"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
return function(puzzle)
1+
return function(grid)
22
end

0 commit comments

Comments
 (0)