Skip to content

Commit

Permalink
closes #57 added assert element exists (#59)
Browse files Browse the repository at this point in the history
* closes #57 added assert element exists
  • Loading branch information
JonasJurczok committed Oct 31, 2018
1 parent d5275ce commit 5b82918
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,12 @@ will replace the `.text` attribute with the provided `text`.

##### faketorio.find_element_by_id(id, player)

Returns a [gui element](http://lua-api.factorio.com/latest/LuaGuiElement.html) with the given `id` for the given `player`.
Returns a [gui element](http://lua-api.factorio.com/latest/LuaGuiElement.html) with the given `id` for the given `player` or `nil if element does not exist.
Both parameters are mandatory.

##### faketorio.ensure_element_exists(id, player)

Like `find_element_by_id` but will fail if element is not found.

#### logging

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
rm -f luacov.*.out

luacheck src spec
busted -c
busted -c -v

echo "Checking where we should deploy or not. Tag is ${TRAVIS_TAG}, branch is ${TRAVIS_BRANCH} and pull request is ${TRAVIS_PULL_REQUEST}".
if [ -n "${TRAVIS_TAG}" ] ; then
Expand Down
19 changes: 19 additions & 0 deletions spec/ingame_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,25 @@ describe("Test feature/scenario registration #ingame", function()
assert.are.equals("wololo", element.text)
end)

it("should find an existing element.", function ()
local byId = faketorio.find_element_by_id("testId", game.players[1])
assert.are.equals("testId", byId.name, "find by id failed.")

local assert_exists = faketorio.assert_element_exists("testId", game.players[1])
assert.are.equals("testId", assert_exists.name, "assert exists failed.")
end)

it("should work for non existing element.", function ()
local element = faketorio.find_element_by_id("testId222", game.players[1])
assert.are.equals(nil, element)
end)

it("should fail for asserting non existing elements.", function ()
local result, error = pcall(faketorio.assert_element_exists,"testId222", game.players[1])
assert.False(result)
assert.is_Truthy(error)
end)

it("should fail to enter text on invalid element type.", function()
assert.has_errors(function() faketorio.enter_text("someOtherId", "shouldFail") end)
end)
Expand Down
9 changes: 8 additions & 1 deletion src/ingame/functions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ function faketorio.enter_text(id, text)
element.text = text
end

function faketorio.assert_element_exists(id, player)
local element = faketorio.find_element_by_id(id, player)
local error = string.format("Could not find element with id [%s] for player [%s].", id, player.name)
assert(element ~= nil, debug.traceback(error))
return element
end

function faketorio.find_element_by_id(id, player)

faketorio.log.trace("Starting find by id for id [%s] and player [%s].", {id, player.name})
Expand All @@ -164,7 +171,7 @@ function faketorio.find_element_by_id(id, player)
end
end

error(debug.traceback(string.format("Could not find element with id [%s] for player [%s].", id, player.name)))
return nil
end

function faketorio.do_find_element_by_id(id, element)
Expand Down

0 comments on commit 5b82918

Please sign in to comment.