Skip to content

Commit

Permalink
fix TOML printer for empty tables (#38746)
Browse files Browse the repository at this point in the history
Co-authored-by: Roger Luo <[email protected]>
  • Loading branch information
KristofferC and Roger-luo committed Dec 8, 2020
1 parent 30d309a commit be073d2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 1 addition & 1 deletion stdlib/TOML/src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ function _print(f::MbyFunc, io::IO, a::AbstractDict,
value = a[key]
if is_table(value)
push!(ks, String(key))
header = !all(is_tabular(v) for v in values(value))::Bool
header = isempty(value) || !all(is_tabular(v) for v in values(value))::Bool
if header
# print table
first_block || println(io)
Expand Down
10 changes: 10 additions & 0 deletions stdlib/TOML/test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,13 @@ end
"""

@test toml_str(Dict("b" => SubString("foo"))) == "b = \"foo\"\n"

@testset "empty dict print" begin
s = """
user = "me"
[julia]
[option]
"""
d = TOML.parse(s)
@test toml_str(d) == "user = \"me\"\n\n[julia]\n\n[option]\n"
end
8 changes: 4 additions & 4 deletions stdlib/TOML/test/readme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ str = """
[ g . h . i ] # same as [g.h.i]
[ j . "ʞ" . 'l' ] # same as [j."ʞ".'l']
"""
@test_broken roundtrip(str) # Printer removes empty tables right now
@test roundtrip(str)
d = parse(str)
@test d == Dict(
"a" => Dict("b" => Dict("c" => Dict())),
Expand All @@ -689,7 +689,7 @@ str = """
[x] # defining a super-table afterward is ok
"""
@test_broken roundtrip(str) # Printer removes empty tables right now
@test roundtrip(str)
d = parse(str)
@test d == Dict("x" => Dict("y" => Dict("z" => Dict("w" => Dict()))))

Expand Down Expand Up @@ -747,7 +747,7 @@ str = """
[animal]
[fruit.orange]
"""
@test_broken roundtrip(str) # Printer removes empty tables right now
@test roundtrip(str)
d = parse(str)
@test d == Dict(
"fruit" => Dict("apple" => Dict(), "orange" => Dict()),
Expand All @@ -760,7 +760,7 @@ str = """
[fruit.orange]
[animal]
"""
@test_broken roundtrip(str) # Printer removes empty tables right now
@test roundtrip(str)
@test d == Dict(
"fruit" => Dict("apple" => Dict(), "orange" => Dict()),
"animal" => Dict()
Expand Down

0 comments on commit be073d2

Please sign in to comment.