Skip to content

Commit

Permalink
feat(given): ✨ take object in input from path (#684)
Browse files Browse the repository at this point in the history
* feat(given): ✨ take object in input from path

* test(zencode): 🧪 add failing test for given statement that takes input from a path
  • Loading branch information
matteo-cristino committed Jun 26, 2023
1 parent 88acb42 commit 2fc4cac
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/lua/zencode_given.lua
Original file line number Diff line number Diff line change
Expand Up @@ -471,3 +471,22 @@ Given("a '' part of '' before string suffix ''", function(enc, src, sfx)
ack(src)
gc()
end)

Given("a '' in path ''", function(enc, path)
local path_array = strtok(uscore(path), '([^.]+)')
local root = path_array[1]
table.remove(path_array, 1)
local dest = path_array[#path_array]
local res = KIN[root] or IN[root]
for k,v in pairs(path_array) do
ZEN.assert(luatype(res) == 'table', "Object is not a table: "..root)
ZEN.assert(res[v] ~= nil, "Key "..v.." not found in "..root)
res = res[v]
root = v
end
TMP = guess_conversion(res, enc)
TMP.name = dest
ack(dest)
gc()
end)

53 changes: 53 additions & 0 deletions test/zencode/given.bats
Original file line number Diff line number Diff line change
Expand Up @@ -404,3 +404,56 @@ EOF
assert_output '{"sfx_onebyte":"1bb0515e4fe007600355be41f4d7d93508b3b11b6741b9af51ec295a1b544c40"}'

}

@test "Given I have a '' in path ''" {
cat << EOF | save_asset given_in_path.data
{
"my_dict": {
"result": {
"my_string_array": [
"hello",
"world"
],
"my_number_array": [
1,
2,
3
],
"my_hex": "0123",
"my_base64": "W8ZFMccV+jErS2wLP3nn6jH46WgNp8vzzfzuFMxmWtA=",
"my_base58": "6nLf3J6QhF94jE6A6BNVcHEyjBXdS1H1YqGBfaWgTULv"
}
}
}
EOF

cat << EOF | zexe given_in_path.zen given_in_path.data
Given I have a 'string array' in path 'my_dict.result.my_string_array'
and I have a 'number array' in path 'my_dict.result.my_number_array'
and I have a 'hex' in path 'my_dict.result.my_hex'
and I have a 'base64' in path 'my_dict.result.my_base64'
and I have a 'base58' in path 'my_dict.result.my_base58'
Then print the data
EOF
save_output 'given_in_path.json'
assert_output '{"my_base58":"6nLf3J6QhF94jE6A6BNVcHEyjBXdS1H1YqGBfaWgTULv","my_base64":"W8ZFMccV+jErS2wLP3nn6jH46WgNp8vzzfzuFMxmWtA=","my_hex":"0123","my_number_array":[1,2,3],"my_string_array":["hello","world"]}'
}

@test "Given I have a '' in path '' (fail)" {
cat << EOF | save_asset given_in_path_fail_not_found.zen
Given I have a 'string array' in path 'my_dict.result.not_my_string_array'
Then print the data
EOF
run $ZENROOM_EXECUTABLE -z -a given_in_path.data given_in_path_fail_not_found.zen
assert_line '[W] [!] Key not_my_string_array not found in result'

cat << EOF | save_asset given_in_path_fail_not_a_table.zen
Given I have a 'string array' in path 'my_dict.result.my_hex.not_existing_element'
Then print the data
EOF
run $ZENROOM_EXECUTABLE -z -a given_in_path.data given_in_path_fail_not_a_table.zen
assert_line '[W] [!] Object is not a table: my_hex'
}

0 comments on commit 2fc4cac

Please sign in to comment.