Skip to content

Commit

Permalink
Fix reference string decoding test
Browse files Browse the repository at this point in the history
Closes #104
  • Loading branch information
michalmuskala committed May 4, 2020
1 parent 188e66b commit bdbd96d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions test/decode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -87,20 +87,22 @@ defmodule Jason.DecodeTest do

test "copying strings on decode" do
assert parse!("{}", strings: :copy) == %{}
as = String.duplicate("a", 101)
bs = String.duplicate("b", 102)

# Copy decode, copies the key
assert [{key, value}] = Map.to_list(parse!(~s({"foo": "bar"}), strings: :copy))
assert key == "foo"
assert value == "bar"
assert :binary.referenced_byte_size(key) == 3
assert :binary.referenced_byte_size(value) == 3
assert [{key, value}] = Map.to_list(parse!(~s({"#{as}": "#{bs}"}), strings: :copy))
assert key == as
assert value == bs
assert :binary.referenced_byte_size(key) == byte_size(as)
assert :binary.referenced_byte_size(value) == byte_size(bs)

# Regular decode references the original string
assert [{key, value}] = Map.to_list(parse!(~s({"foo": "bar"})))
assert key == "foo"
assert value == "bar"
assert :binary.referenced_byte_size(key) == 14
assert :binary.referenced_byte_size(value) == 14
assert [{key, value}] = Map.to_list(parse!(~s({"#{as}": "#{bs}"})))
assert key == as
assert value == bs
assert :binary.referenced_byte_size(key) > byte_size(as) + byte_size(bs)
assert :binary.referenced_byte_size(value) > byte_size(bs) + byte_size(bs)
end

test "custom object key mapping function" do
Expand Down

0 comments on commit bdbd96d

Please sign in to comment.