Skip to content

Commit

Permalink
feat(when): ✨ statement to exit with error message
Browse files Browse the repository at this point in the history
  • Loading branch information
matteo-cristino authored and jaromil committed Mar 11, 2024
1 parent 96f6fee commit d08d516
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lua/zencode_when.lua
Original file line number Diff line number Diff line change
Expand Up @@ -780,3 +780,16 @@ When("create result of ''", function(expr)
zentype = 'e' })
end
end)

When("exit with error message ''", function(err)
local e, e_codec = mayhave(err)
if e then
zencode_assert(luatype(e) ~= 'table', "Error message can not be a table "..err)
zencode_assert(e_codec.encoding == 'string', "Error message must be a string "..err)
e = O.to_string(e)
else
e = err
end
error(space(e))
ZEN.OK = false
end)
28 changes: 28 additions & 0 deletions test/zencode/when.bats
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,31 @@ EOF
save_output 'found.out'
assert_output '{"output":["found_everything"]}'
}

@test "exit with error message" {
cat <<EOF | save_asset error_message.data.json
{
"error": "object not found"
}
EOF
cat <<EOF | save_asset error_message.zen
Given I have a 'string' named 'error'
If I verify 'object' is not found
When I exit with error message 'error'
EndIf
Then print the string 'object found'
EOF
cat <<EOF | save_asset error_message_inline.zen
Given nothing
If I verify 'object' is not found
When I exit with error message 'Object not found'
EndIf
Then print the string 'object found'
EOF
run $ZENROOM_EXECUTABLE -z -a error_message.data.json error_message.zen
assert_failure
assert_line '[!] object not found'
run $ZENROOM_EXECUTABLE -z error_message_inline.zen
assert_failure
assert_line '[!] Object not found'
}

0 comments on commit d08d516

Please sign in to comment.