Skip to content

Commit

Permalink
fix: make the test pass on otp 26+
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Timmer committed Jun 6, 2024
1 parent 403092d commit 081460c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lib/json5/decode/backend/combine/helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ defmodule Json5.Decode.Backend.Combine.Helper do
"\u{2029}"
]

@line_terminator_chars '\u{000A}\u{000D}\u{2028}\u{2029}'
@line_terminator_chars ~c"\u{000A}\u{000D}\u{2028}\u{2029}"

defparser lazy(%ParserState{status: :ok} = state, generator) do
generator.().(state)
Expand Down
55 changes: 43 additions & 12 deletions test/json5/encode_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ defmodule Json5.EncodeTest do
[:object, "{'using spaces': 1, }", Macro.escape(%{"using spaces" => 1})],
[
:mixed_object,
"""
{array: [1, 2, 3], nested: {more: 123, }, valid_key: true, 'using spaces': 1, }\
""",
{
"""
{array: [1, 2, 3], nested: {more: 123, }, valid_key: true, 'using spaces': 1, }\
""",
"""
{array: [1, 2, 3], valid_key: true, nested: {more: 123, }, 'using spaces': 1, }\
"""
},
Macro.escape(%{
"using spaces" => 1,
valid_key: true,
Expand All @@ -25,11 +30,18 @@ defmodule Json5.EncodeTest do
]
]

for [prefix, expected, input] <- @valid do
test "encode #{prefix} #{expected}" do
assert {:ok, unquote(expected)} = Json5.encode(unquote(input))
end
end
Enum.map(@valid, fn
[prefix, expected, input] when is_tuple(expected) ->
test "encode #{prefix} #{elem(expected, 0)}" do
{:ok, out} = Json5.encode(unquote(input))
assert out in Tuple.to_list(unquote(expected))
end

[prefix, expected, input] ->
test "encode #{prefix} #{expected}" do
assert {:ok, unquote(expected)} = Json5.encode(unquote(input))
end
end)

test "encode array pretty" do
input = ["one", "two", "three"]
Expand Down Expand Up @@ -70,7 +82,7 @@ defmodule Json5.EncodeTest do
array: [1, 2, 3]
}

expected = """
expected_1 = """
{
array: [
1,
Expand All @@ -85,7 +97,22 @@ defmodule Json5.EncodeTest do
}
"""

assert expected == Json5.encode!(input, pretty: true)
expected_2 = """
{
array: [
1,
2,
3,
],
valid_key: true,
nested: {
more: 123,
},
'using spaces': 1,
}
"""

assert Json5.encode!(input, pretty: true) in [expected_1, expected_2]
end

test "encode object map compact" do
Expand All @@ -96,11 +123,15 @@ defmodule Json5.EncodeTest do
array: [1, 2, 3]
}

expected = """
expected_1 = """
{array:[1,2,3],nested:{more:123},valid_key:true,'using spaces':1}\
"""

assert expected == Json5.encode!(input, compact: true)
expected_2 = """
{array:[1,2,3],valid_key:true,nested:{more:123},'using spaces':1}\
"""

assert Json5.encode!(input, compact: true) in [expected_1, expected_2]
end

test "encode invalid input" do
Expand Down
2 changes: 2 additions & 0 deletions test/json5_test.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
defmodule Json5Test do
use ExUnit.Case

@moduletag :doctest
doctest Json5
end
11 changes: 10 additions & 1 deletion test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
ExUnit.start()
opts = if System.otp_release |> String.to_integer() |> Kernel.>=(26) do
# formatting of maps changed in OTP 26,
# so for now just test the lower versions in doctest.
# the unittest do check both formats
[exclude: [:doctest]]
else
[]
end

ExUnit.start(opts)

0 comments on commit 081460c

Please sign in to comment.