From 8a06e9d342648ec4ddc6a73778e00d862276aa32 Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Fri, 3 Oct 2025 14:37:29 -0600 Subject: [PATCH] Support JSON 1.0 release --- Project.toml | 2 +- src/serialization.jl | 10 ++++----- test/GroupsTests.jl | 51 ++++++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Project.toml b/Project.toml index 59a5f2ef..55ca770d 100644 --- a/Project.toml +++ b/Project.toml @@ -14,7 +14,7 @@ UUIDs = "cf7118a7-6976-5b1a-9a39-7adc72f591a4" [compat] Aqua = "0.8" Compat = "4.11" -JSON = "0.18, 0.19, 0.20, 0.21" +JSON = "0.18, 0.19, 0.20, 0.21, 1" Logging = "<0.0.1, 1" Printf = "<0.0.1, 1" Profile = "<0.0.1, 1" diff --git a/src/serialization.jl b/src/serialization.jl index 7bec2c8d..5aa7cced 100644 --- a/src/serialization.jl +++ b/src/serialization.jl @@ -24,7 +24,7 @@ function JSON.lower(x::Union{values(SUPPORTED_TYPES)...}) field = getfield(x, i) ft = typeof(field) value = ft <: get(SUPPORTED_TYPES, nameof(ft), Union{}) ? JSON.lower(field) : field - d[name] = value + d[name] = value isa Float64 && !isfinite(value) ? nothing : value end return [string(nameof(typeof(x))), d] end @@ -41,7 +41,7 @@ end function recover(x::Vector) length(x) == 2 || throw(ArgumentError("Expecting a vector of length 2")) typename = x[1]::String - fields = x[2]::Dict + fields = x[2]::AbstractDict startswith(typename, "BenchmarkTools.") && (typename = typename[(sizeof("BenchmarkTools.") + 1):end]) T = SUPPORTED_TYPES[Symbol(typename)] @@ -64,7 +64,7 @@ function recover(x::Vector) convert(ft, fields[fn]) end end - if T == BenchmarkGroup && xsi isa Dict + if T == BenchmarkGroup && xsi isa AbstractDict for (k, v) in copy(xsi) k = k::String if startswith(k, "(") || startswith(k, ":") @@ -154,11 +154,11 @@ function load(io::IO, args...) parsed = JSON.parse(io) if !isa(parsed, Vector) || length(parsed) != 2 || - !isa(parsed[1], Dict) || + !isa(parsed[1], AbstractDict) || !isa(parsed[2], Vector) error("Unexpected JSON format. Was this file originally written by BenchmarkTools?") end - versions = parsed[1]::Dict + versions = parsed[1]::AbstractDict values = parsed[2]::Vector return map!(recover, values, values) end diff --git a/test/GroupsTests.jl b/test/GroupsTests.jl index 8a7a5dcd..de5a664b 100644 --- a/test/GroupsTests.jl +++ b/test/GroupsTests.jl @@ -361,28 +361,51 @@ g1["a"] = t1a g1["b"] = t1b g1["c"] = tc -@test sprint(show, g1) == """ +# Test full output (order-independent) +output_full = sprint(show, g1) +@test startswith( + output_full, + """ 3-element BenchmarkTools.BenchmarkGroup: tags: ["1", "2"] - "c" => TrialEstimate(1.000 ns) - "b" => TrialEstimate(4.123 μs) - "a" => TrialEstimate(32.000 ns)""" -@test sprint(show, g1; context=:boundto => 1) == """ +""", +) +@test occursin(" \"a\" => TrialEstimate(32.000 ns)", output_full) +@test occursin(" \"b\" => TrialEstimate(4.123 μs)", output_full) +@test occursin(" \"c\" => TrialEstimate(1.000 ns)", output_full) + +# Test boundto context output +output_boundto = sprint(show, g1; context=:boundto => 1) +@test startswith( + output_boundto, + """ 3-element BenchmarkTools.BenchmarkGroup: tags: ["1", "2"] - "c" => TrialEstimate(1.000 ns) - ⋮""" -@test sprint(show, g1; context=:limit => false) == """ +""", +) + +# Test limit => false output (order-independent) +output_no_limit = sprint(show, g1; context=:limit => false) +@test startswith( + output_no_limit, + """ 3-element BenchmarkTools.BenchmarkGroup: tags: ["1", "2"] - "c" => TrialEstimate(1.000 ns) - "b" => TrialEstimate(4.123 μs) - "a" => TrialEstimate(32.000 ns)""" -@test @test_deprecated(sprint(show, g1; context=:limit => 1)) == """ +""", +) +@test occursin(" \"a\" => TrialEstimate(32.000 ns)", output_no_limit) +@test occursin(" \"b\" => TrialEstimate(4.123 μs)", output_no_limit) +@test occursin(" \"c\" => TrialEstimate(1.000 ns)", output_no_limit) + +# Test deprecated limit context output +output_limit_deprecated = @test_deprecated(sprint(show, g1; context=:limit => 1)) +@test startswith( + output_limit_deprecated, + """ 3-element BenchmarkTools.BenchmarkGroup: tags: ["1", "2"] - "c" => TrialEstimate(1.000 ns) - ⋮""" +""", +) # EasyConfig-style benchmark groups # #-----------------------------------#