Skip to content

Update word-search tests #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions exercises/practice/word-search/.meta/example.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,23 @@ end

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

return function(puzzle)
return function(grid)
grid = Grid(grid)

return {
find = function(word)
for _, delta in ipairs(deltas) do
local first, last = find(Grid(puzzle), word, delta[1], delta[2])
if first then
return first, last
search = function(words)
local result = {}

for _, word in ipairs(words) do
for _, delta in ipairs(deltas) do
local first, last = find(grid, word, delta[1], delta[2])
if first and last then
result[word] = { start = first, ['end'] = last }
end
end
end

return result
end
}
end
49 changes: 49 additions & 0 deletions exercises/practice/word-search/.meta/spec_generator.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local function map(t, f)
local result = {}
for i, v in ipairs(t) do
result[i] = f(v)
end
return result
end

local function stringify(x)
return ("'%s'"):format(x)
end

local function render_expected(expected)
local key_value_pairs = {}
for k, v in pairs(expected) do
local template = "%s = { start = { %d, %d }, ['end'] = { %d, %d } }"
local key_value_pair = template:format(k, v.start.column, v.start.row, v['end'].column, v['end'].row)
table.insert(key_value_pairs, key_value_pair)
end
table.sort(key_value_pairs)

if #key_value_pairs == 0 then
return '{}'
else
return '{ --\n' .. table.concat(key_value_pairs, ', --\n') .. '\n}'
end
end

return {
module_name = 'WordSearch',

generate_test = function(case)
local template = [[
local grid = { --
%s
}
local words = { --
%s
}
local expected = %s
assert.are.same(expected, WordSearch(grid).search(words))]]

local grid = table.concat(map(case.input.grid, stringify), ', --\n')
local words = table.concat(map(case.input.wordsToSearchFor, stringify), ', --\n')
local expected = render_expected(case.expected)

return template:format(grid, words, expected)
end
}
25 changes: 22 additions & 3 deletions exercises/practice/word-search/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[b4057815-0d01-41f0-9119-6a91f54b2a0a]
description = "Should accept an initial game grid and a target search word"
Expand Down Expand Up @@ -61,3 +68,15 @@ description = "Should locate words written top right to bottom left"

[695531db-69eb-463f-8bad-8de3bf5ef198]
description = "Should fail to locate a word that is not in the puzzle"

[fda5b937-6774-4a52-8f89-f64ed833b175]
description = "Should fail to locate words that are not on horizontal, vertical, or diagonal lines"

[5b6198eb-2847-4e2f-8efe-65045df16bd3]
description = "Should not concatenate different lines to find a horizontal word"

[eba44139-a34f-4a92-98e1-bd5f259e5769]
description = "Should not wrap around horizontally to find a word"

[cd1f0fa8-76af-4167-b105-935f78364dac]
description = "Should not wrap around vertically to find a word"
2 changes: 1 addition & 1 deletion exercises/practice/word-search/word-search.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
return function(puzzle)
return function(grid)
end
Loading
Loading