Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: when printing integers as string do not change the global encoding function #968

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/lua/zencode_data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,12 +355,13 @@ end
return data
elseif dt == 'zenroom.big' then
zencode_assert(fun ~= to_number_f and fun ~= O.to_mnemonic, "Encoding not valid for integers")
if fun == O.to_string then fun = BIG.to_decimal end
if fun ~= BIG.to_decimal then
local correct_fun = fun
if correct_fun == O.to_string then correct_fun = BIG.to_decimal end
if correct_fun ~= BIG.to_decimal then
zencode_assert(BIG.zenpositive(data), "Negative integers can not be encoded")
data = data:octet()
end
return fun(data)
return correct_fun(data)
elseif dt == 'zenroom.float' or dt == 'zenroom.time' then
return to_number_f(data)
elseif iszen(dt) then
Expand Down
4 changes: 1 addition & 3 deletions src/lua/zencode_then.lua
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ local function then_outcast(val, key, enc)
error("Then outcast called on empty variable", 2)
end
local fun
local codec
if enc then
fun = get_encoding_function(enc)
if ZEN.schemas[enc] then
Expand All @@ -88,7 +87,7 @@ local function then_outcast(val, key, enc)
else
fun = default_export_f
end
return deepmap(fun,val)
return deepmap(fun, val)
end

local function then_insert(dest, val, key)
Expand Down Expand Up @@ -338,4 +337,3 @@ Then("print object named by ''", function(name)
OUT[real_name] = then_outcast( val, real_name )
end
end)

28 changes: 26 additions & 2 deletions test/zencode/array.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and I rename the 'array' to 'bonnetjes'
Then print the 'bonnetjes'
EOF
save_output arr.json

}

@test "When I create the size of" {
Expand Down Expand Up @@ -331,7 +331,7 @@ Given I have a 'string array' named 'haystack'
and I have a 'number' named 'quorum'
and I have a 'string' named 'needle'
When I verify the 'needle' is found in 'haystack' at least 'quorum' times
Then Print the string 'Success'
Then Print the string 'Success'
EOF
save_output "needle_in_haystack.json"
assert_output '{"output":["Success"]}'
Expand Down Expand Up @@ -1020,3 +1020,27 @@ EOF
save_output 'copy_from_schema_array.json'
assert_output '{"copy":{"address":"0x2B8070975AF995Ef7eb949AE28ee7706B9039504","signature":"0xed8f36c71989f8660e8f5d4adbfd8f1c0288cca90d3a5330b7bf735d71ab52fe7ba0a7827dc4ba707431f1c10babd389f658f8e208b89390a9be3c097579a2ff1b"}}'
}

@test "print array with integers in it" {
cat << EOF | save_asset print_array_with_int.data.json
{
"integer": "5",
"string": "a",
"another_string": "b"
}
EOF
cat << EOF | zexe print_array_with_int.zen print_array_with_int.data.json
Given I have a 'integer'
Given I have a 'string'
Given I have a 'string' named 'another_string'

When I create the 'string array' named 'res'
When I move 'string' in 'res'
When I move 'integer' in 'res'
When I move 'another_string' in 'res'

Then print the 'res'
EOF
save_output print_array_with_int.out.json
assert_output '{"res":["a","5","b"]}'
}
Loading