diff --git a/src/lua/zencode_given.lua b/src/lua/zencode_given.lua index 9ad2da092..e009cc4b3 100644 --- a/src/lua/zencode_given.lua +++ b/src/lua/zencode_given.lua @@ -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) + diff --git a/test/zencode/given.bats b/test/zencode/given.bats index 1a4fc0796..348d323ac 100644 --- a/test/zencode/given.bats +++ b/test/zencode/given.bats @@ -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' +}