Skip to content

Commit

Permalink
cleanup - fix Plots tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Apr 6, 2024
1 parent 71a0a4f commit 02c3a2c
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 107 deletions.
17 changes: 15 additions & 2 deletions PlotsBase/src/Commons/Commons.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,21 @@ function _override_seriestype_check(plotattributes::AKW, st::Symbol)
st
end

"These should only be needed in frontend modules"
PlotsBase.@ScopeModule(
macro ScopeModule(mod::Symbol, parent::Symbol, symbols...)
import_ex = Expr(
:import,
Expr(
:(:),
Expr(:., :., :., parent),
(Expr(:., s isa Expr ? s.args[1] : s) for s in symbols)...,
),
)
export_ex = Expr(:export, (s isa Expr ? s.args[1] : s for s in symbols)...)
Expr(:module, true, mod, Expr(:block, import_ex, export_ex)) |> esc
end

"these should only be needed in frontend modules"
@ScopeModule(
Frontend,
Commons,
_subplot_defaults,
Expand Down
28 changes: 7 additions & 21 deletions PlotsBase/src/PlotsBase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -120,25 +120,16 @@ export
#! format: on
import NaNMath

macro ScopeModule(mod::Symbol, parent::Symbol, symbols...)
import_ex = Expr(
:import,
Expr(
:(:),
Expr(:., :., :., parent),
(Expr(:., s isa Expr ? s.args[1] : s) for s in symbols)...,
),
)
export_ex = Expr(:export, (s isa Expr ? s.args[1] : s for s in symbols)...)
Expr(:module, true, mod, Expr(:block, import_ex, export_ex)) |> esc
end
const _plots_project = Pkg.Types.read_package(normpath(@__DIR__, "..", "Project.toml"))
const _current_plots_version = _plots_project.version
const _plots_compats = _plots_project.compat

include("Commons/Commons.jl")
using .Commons
using .Commons.Frontend

Commons.@generic_functions attr attr! rotate rotate!

# ---------------------------------------------------------
include("Fonts.jl")
include("Ticks.jl")
include("DataSeries.jl")
Expand All @@ -147,7 +138,6 @@ include("Axes.jl")
include("Surfaces.jl")
include("Colorbars.jl")
include("Plots.jl")
# ---------------------------------------------------------
include("layouts.jl")
include("utils.jl")
include("axes_utils.jl")
Expand All @@ -165,16 +155,12 @@ include("recipes.jl")
include("animation.jl")
include("examples.jl")
include("plotattr.jl")

include("backends/nobackend.jl")
include("abstract_backend.jl")
const CURRENT_BACKEND = CurrentBackend(:none)

include("alignment.jl")
include("output.jl")
include("shorthands.jl")
include("backends/web.jl")
include("backends/plotly.jl")
include("backends.jl")
include("web.jl")
include("plotly.jl")
include("init.jl")
include("users.jl")

Expand Down
35 changes: 23 additions & 12 deletions PlotsBase/src/abstract_backend.jl → PlotsBase/src/backends.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
const _plots_project = Pkg.Types.read_package(normpath(@__DIR__, "..", "Project.toml"))
const _current_plots_version = _plots_project.version
const _plots_compats = _plots_project.compat
struct NoBackend <: AbstractBackend end

backend_name(::NoBackend) = :none

for sym in (:attr, :seriestype, :marker, :style, :scale)
f1 = Symbol("is_$(sym)_supported")
f2 = Symbol("supported_$(sym)s")
@eval begin
$f1(::NoBackend, $sym::Symbol) = true
$f2(::NoBackend) = $(getproperty(Commons, Symbol("_all_$(sym)s")))
end
end

_display(::Plot{NoBackend}) =
@info "No backend activated yet. Load the backend library and call the activation function to do so.\nE.g. `import GR; gr()` activates the GR backend."

const _backendSymbol = Dict{DataType,Symbol}(NoBackend => :none)
const _backendType = Dict{Symbol,DataType}(:none => NoBackend)
const _backend_packages = (gaston = :Gaston, gr = :GR, unicodeplots = :UnicodePlots, pgfplotsx = :PGFPlotsX, pythonplot = :PythonPlot, plotly = nothing, plotlyjs = :PlotlyJS, hdf5 = :HDF5)
const _initialized_backends = Set{Symbol}()
const _supported_backends = keys(_backend_packages)

const _plots_deps = let toml = Pkg.TOML.parsefile(normpath(@__DIR__, "..", "Project.toml"))
merge(toml["deps"], toml["extras"])
end

function _check_installed(backend::Union{Module,AbstractString,Symbol}; warn = true)
sym = Symbol(lowercase(string(backend)))
if warn && !haskey(_backend_packages, sym)
Expand Down Expand Up @@ -55,6 +63,10 @@ mutable struct CurrentBackend
pkg::AbstractBackend
end

@inline backend_type(sym::Symbol) = get(_backendType, sym, NoBackend)
@inline backend_instance(sym::Symbol) = backend_type(sym)()
@inline backend(type::Type{<:AbstractBackend}) = backend(type())

CurrentBackend(sym::Symbol) = CurrentBackend(sym, backend_instance(sym))

"returns the current plotting package name. Initializes package on first call."
Expand All @@ -63,12 +75,11 @@ CurrentBackend(sym::Symbol) = CurrentBackend(sym, backend_instance(sym))
"returns a list of supported backends."
@inline backends() = _supported_backends

@inline backend_name() = CURRENT_BACKEND.sym
@inline backend_type(sym::Symbol) = get(_backendType, sym, NoBackend)
@inline backend_instance(sym::Symbol) = backend_type(sym)()
@inline backend(type::Type{<:AbstractBackend}) = backend(type())

backend_package_name(sym::Symbol = backend_name()) = get(_backend_packages, sym, nothing)
const CURRENT_BACKEND = CurrentBackend(:none)

@inline backend_name() = CURRENT_BACKEND.sym
@inline backend_package_name(sym::Symbol = backend_name()) = get(_backend_packages, sym, nothing)

# Traits to be implemented by the extensions
backend_name(::AbstractBackend) = @info "`backend_name(::Backend) not implemented."
Expand Down
15 changes: 0 additions & 15 deletions PlotsBase/src/backends/nobackend.jl

This file was deleted.

File renamed without changes.
File renamed without changes.
56 changes: 56 additions & 0 deletions PlotsBase/test/test_backends.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,58 @@
@testset "UnicodePlots" begin
with(:unicodeplots) do
@test backend() == PlotsBase.backend_instance(:unicodeplots)

io = IOContext(IOBuffer(), :color => true)

# lets just make sure it runs without error
pl = plot(rand(10))
@test show(io, pl) isa Nothing

pl = bar(randn(10))
@test show(io, pl) isa Nothing

pl = plot([1, 2], [3, 4])
annotate!(pl, [(1.5, 3.2, PlotsBase.text("Test", :red, :center))])
hline!(pl, [3.1])
@test show(io, pl) isa Nothing

pl = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
hline!(pl, [3.1])
annotate!(
pl,
[(Dates.Date(2019, 1, 15), 3.2, PlotsBase.text("Test", :red, :center))],
)
@test show(io, pl) isa Nothing

pl = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
annotate!(pl, [(Dates.Date(2019, 1, 15), 3.2, :auto)])
hline!(pl, [3.1])
@test show(io, pl) isa Nothing

pl = plot(map(plot, 1:4)..., layout = (2, 2))
@test show(io, pl) isa Nothing

pl = plot(map(plot, 1:3)..., layout = (2, 2))
@test show(io, pl) isa Nothing

pl = plot(map(plot, 1:2)..., layout = @layout([° _; _ °]))
@test show(io, pl) isa Nothing

redirect_stdout(devnull) do
show(plot(1:2))
end
end
end

(is_pkgeval() || is_ci()) || @testset "PlotlyJS" begin
with(:plotlyjs) do
PlotlyJSExt = Base.get_extension(PlotsBase, :PlotlyJSExt)
@test backend() == PlotlyJSExt.PlotlyJSBackend()
pl = plot(rand(10))
@test pl isa Plot
display(pl)
end
end

is_pkgeval() || @testset "Backends" begin
callback(m, pkgname, i) = begin
Expand All @@ -16,3 +71,4 @@ is_pkgeval() || @testset "Backends" begin
closeall()
end
end

56 changes: 0 additions & 56 deletions PlotsBase/test/test_reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,52 +133,6 @@ with(:pgfplotsx) do
end
=#

@testset "UnicodePlots" begin
with(:unicodeplots) do
@test backend() == PlotsBase.backend_instance(:unicodeplots)

io = IOContext(IOBuffer(), :color => true)

# lets just make sure it runs without error
pl = plot(rand(10))
@test show(io, pl) isa Nothing

pl = bar(randn(10))
@test show(io, pl) isa Nothing

pl = plot([1, 2], [3, 4])
annotate!(pl, [(1.5, 3.2, PlotsBase.text("Test", :red, :center))])
hline!(pl, [3.1])
@test show(io, pl) isa Nothing

pl = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
hline!(pl, [3.1])
annotate!(
pl,
[(Dates.Date(2019, 1, 15), 3.2, PlotsBase.text("Test", :red, :center))],
)
@test show(io, pl) isa Nothing

pl = plot([Dates.Date(2019, 1, 1), Dates.Date(2019, 2, 1)], [3, 4])
annotate!(pl, [(Dates.Date(2019, 1, 15), 3.2, :auto)])
hline!(pl, [3.1])
@test show(io, pl) isa Nothing

pl = plot(map(plot, 1:4)..., layout = (2, 2))
@test show(io, pl) isa Nothing

pl = plot(map(plot, 1:3)..., layout = (2, 2))
@test show(io, pl) isa Nothing

pl = plot(map(plot, 1:2)..., layout = @layout([° _; _ °]))
@test show(io, pl) isa Nothing

redirect_stdout(devnull) do
show(plot(1:2))
end
end
end

@testset "GR - reference images" begin
with(:gr) do
# NOTE: use `ENV["VISUAL_REGRESSION_TESTS_AUTO"] = true;` to automatically replace reference images
Expand All @@ -191,13 +145,3 @@ end
)
end
end

(is_pkgeval() || is_ci()) || @testset "PlotlyJS" begin
with(:plotlyjs) do
PlotlyJSExt = Base.get_extension(PlotsBase, :PlotlyJSExt)
@test backend() == PlotlyJSExt.PlotlyJSBackend()
pl = plot(rand(10))
@test pl isa Plot
display(pl)
end
end
6 changes: 5 additions & 1 deletion test/test_preferences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ const DEBUG = false
# this test mimics a restart, which is needed after a preferences change
Plots.set_default_backend!(:unicodeplots)
script = tempname()
plots_dir = escape_string(pkgdir(Plots))
write(
script,
"""
using Pkg, Test; io = (devnull, stdout)[1] # toggle for debugging
Pkg.activate(; temp = true, io)
Pkg.develop(; path = "$(escape_string(pkgdir(Plots)))", io)
Pkg.develop(; path = "$(joinpath(plots_dir, "RecipesBase"))", io)
Pkg.develop(; path = "$(joinpath(plots_dir, "RecipesPipeline"))", io)
Pkg.develop(; path = "$(joinpath(plots_dir, "PlotsBase"))", io)
Pkg.develop(; path = "$plots_dir", io)
Pkg.add("UnicodePlots"; io) # checked by Plots
import UnicodePlots
using Plots
Expand Down

0 comments on commit 02c3a2c

Please sign in to comment.