diff --git a/src/ticks.jl b/src/ticks.jl index 19e899a..1236dbf 100644 --- a/src/ticks.jl +++ b/src/ticks.jl @@ -36,25 +36,27 @@ function postdecimal_digits(x::T) where {T} return 0 end -fallback_ticks(x_min::T, x_max::T, k_min, k_max) where {T} = ( +function fallback_ticks(x_min::T, x_max::T, k_min, k_max, strict_span) where {T} + if !strict_span && x_min ≈ x_max + x_min, x_max = prevfloat(x_min), nextfloat(x_max) + end if k_min != 2 && isfinite(x_min) && isfinite(x_max) collect(T, range(x_min, x_max; length = k_min)), x_min, x_max else T[x_min, x_max], x_min, x_max end -) +end # Empty catchall optimize_ticks() = Any[] """ - optimize_ticks(xmin, xmax; extend_ticks::Bool=false, - Q=[(1.0,1.0), (5.0, 0.9), (2.0, 0.7), (2.5, 0.5), (3.0, 0.2)], - k_min::Int=2, k_max::Int=10, k_ideal::Int=5, - granularity_weight::Float64=1/4, simplicity_weight::Float64=1/6, - coverage_weight::Float64=1/3, niceness_weight::Float64=1/4, - strict_span=true, span_buffer = nothing - ) +optimize_ticks(xmin, xmax; extend_ticks::Bool = false, + Q = [(1.0,1.0), (5.0, 0.9), (2.0, 0.7), (2.5, 0.5), (3.0, 0.2)], + k_min = 2, k_max = 10, k_ideal = 5, + granularity_weight = 1/4, simplicity_weight = 1/6, + coverage_weight = 1/3, niceness_weight = 1/4, + strict_span = true, span_buffer = nothing) Find some reasonable values for tick marks. @@ -152,11 +154,9 @@ function optimize_ticks( span_buffer = nothing, scale = nothing, ) where {T} - F = float(T) - if x_max - x_min < eps(F) - return fallback_ticks(x_min, x_max, k_min, k_max) - end + x_min ≈ x_max && return fallback_ticks(x_min, x_max, k_min, k_max, strict_span) + F = float(T) Qv = F[q[1] for q in Q] Qs = F[q[2] for q in Q] @@ -190,7 +190,7 @@ function optimize_ticks( if sspan @warn "No strict ticks found" else - return fallback_ticks(x_min, x_max, k_min, k_max) + return fallback_ticks(x_min, x_max, k_min, k_max, strict_span) end else return best, min_best, max_best diff --git a/test/downstream.jl b/test/downstream.jl index d62a747..b23d022 100644 --- a/test/downstream.jl +++ b/test/downstream.jl @@ -1,47 +1,90 @@ -using Pkg, PlotUtils +using Pkg, PlotUtils, Test LibGit2 = Pkg.GitTools.LibGit2 TOML = Pkg.TOML -Plots_jl = joinpath(mkpath(tempname()), "Plots.jl") -Plots_toml = joinpath(Plots_jl, "Project.toml") - -# clone and checkout the latest stable version of Plots -depot = joinpath(first(DEPOT_PATH), "registries", "General", "P", "Plots", "Versions.toml") -stable = maximum(VersionNumber.(keys(TOML.parse(read(depot, String))))) -for i in 1:6 - try - global repo = Pkg.GitTools.ensure_clone( - stdout, - Plots_jl, - "https://github.com/JuliaPlots/Plots.jl", - ) - break - catch err - @warn err - sleep(20i) +failsafe_clone_checkout(versions, path, url) = begin + local repo + for i in 1:6 + try + repo = Pkg.GitTools.ensure_clone( + stdout, + path, + url, + ) + break + catch err + @warn err + sleep(20i) + end end + + @assert isfile(joinpath(path, "Project.toml")) "spurious network error: clone failed, bailing out" + stable = maximum(VersionNumber.(keys(TOML.parse(read(versions, String))))) + tag = LibGit2.GitObject(repo, "v$stable") + hash = string(LibGit2.target(tag)) + LibGit2.checkout!(repo, hash) + nothing +end + +fake_supported_version!(path) = begin + toml = joinpath(path, "Project.toml") + # fake the supported PlotUtils version for testing (for `Pkg.develop`) + PlotUtils_version = Pkg.Types.read_package(normpath(@__DIR__, "..", "Project.toml")).version + parsed_toml = TOML.parse(read(toml, String)) + parsed_toml["compat"]["PlotUtils"] = string(PlotUtils_version) + open(toml, "w") do io + TOML.print(io, parsed_toml) + end + nothing end -@assert isfile(Plots_toml) "spurious network error: clone failed, bailing out" -tag = LibGit2.GitObject(repo, "v$stable") -hash = string(LibGit2.target(tag)) -LibGit2.checkout!(repo, hash) - -# fake the supported PlotUtils version for testing (for `Pkg.develop`) -plotutils_version = Pkg.Types.read_package(normpath(@__DIR__, "..", "Project.toml")).version -toml = TOML.parse(read(Plots_toml, String)) -toml["compat"]["PlotUtils"] = string(plotutils_version) -open(Plots_toml, "w") do io - TOML.print(io, toml) + +deploy_Plots() = begin + tmpd = mktempdir() + Plots_jl = joinpath(tmpd, "Plots.jl") + Plots_toml = joinpath(Plots_jl, "Project.toml") + versions = joinpath(first(DEPOT_PATH), "registries", "General", "P", "Plots", "Versions.toml") + + failsafe_clone_checkout(versions, Plots_jl, "https://github.com/JuliaPlots/Plots.jl") + fake_supported_version!(Plots_jl) + + Pkg.develop(path = Plots_jl) + Pkg.status(["PlotUtils", "Plots"]) + nothing end -Pkg.develop(path = Plots_jl) -Pkg.status(["PlotUtils", "Plots"]) -# test basic plots creation & display -using Plots, Test +deploy_Makie(extended=false) = begin + tmpd = mktempdir() + Makie_jl = joinpath(tmpd, "Makie.jl") + versions = joinpath(first(DEPOT_PATH), "registries", "General", "M", "Makie", "Versions.toml") + + failsafe_clone_checkout(versions, Makie_jl, "https://github.com/MakieOrg/Makie.jl") + fake_supported_version!(Makie_jl) + + Pkg.develop(path = joinpath(tmpd, "Makie.jl", "MakieCore")) + Pkg.develop(path = joinpath(tmpd, "Makie.jl")) + if extended # too costly ? + Pkg.develop(path = joinpath(tmpd, "Makie.jl", "ReferenceTests")) + Pkg.develop(path = joinpath(tmpd, "Makie.jl", "CairoMakie")) + # Pkg.develop(path = joinpath(tmpd, "Makie.jl", "GLMakie")) + end + Pkg.status(["PlotUtils", "MakieCore", "Makie"]) + nothing +end + +deploy_Plots() +using Plots + +@testset "downstream Plots" begin + # test basic plots creation & display (Plots tests are too long to run) + true && @time for i in 1:length(Plots._examples) + i ∈ Plots._backend_skips[:gr] && continue # skip unsupported examples + Plots._examples[i].imports ≡ nothing || continue # skip examples requiring optional test deps + show(devnull, Plots.test_examples(:gr, i; disp = false)) # trigger display logic + end +end -@time for i in 1:length(Plots._examples) - i ∈ Plots._backend_skips[:gr] && continue # skip unsupported examples - Plots._examples[i].imports ≡ nothing || continue # skip examples requiring optional test deps - show(devnull, Plots.test_examples(:gr, i; disp = false)) # trigger display logic +deploy_Makie() +@testset "downstream Makie" begin + Pkg.test("Makie") end diff --git a/test/grid.png b/test/grid.png new file mode 100644 index 0000000..9825538 Binary files /dev/null and b/test/grid.png differ diff --git a/test/runtests.jl b/test/runtests.jl index ccc9a3f..63e98fd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -108,7 +108,7 @@ end function test_ticks(x, y, ticks) @test issorted(ticks) - @test all(x .<= ticks .<= y) + @test all(x .≤ ticks .≤ y) if x < y @test length(ticks) >= 2 @test is_uniformly_spaced(ticks) @@ -132,7 +132,7 @@ end x, y = minmax(x, y) ticks, = optimize_ticks(x, y) @test issorted(ticks) - @test all(x .<= ticks .<= y) + @test all(x .≤ ticks .≤ y) # Fails: # @test allunique(ticks) end @@ -185,7 +185,7 @@ end end @testset "PlotUtils.jl/issues/114" begin - let x = -0.1eps(), y = 0.1eps() + let x = -1.2eps(), y = 1.2eps() test_ticks(x, y, optimize_ticks(x, y)[1]) end end