diff --git a/README.md b/README.md index 4880af2..0a63fcb 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/build.sh b/build.sh index 917d4a0..7accf33 100755 --- a/build.sh +++ b/build.sh @@ -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 diff --git a/spec/ingame_spec.lua b/spec/ingame_spec.lua index 9824952..2b215d8 100644 --- a/spec/ingame_spec.lua +++ b/spec/ingame_spec.lua @@ -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) diff --git a/src/ingame/functions.lua b/src/ingame/functions.lua index 676c7e2..25c4901 100644 --- a/src/ingame/functions.lua +++ b/src/ingame/functions.lua @@ -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}) @@ -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)