Add tests for contour levels and the start of Plotly tests

- Test that the `levels` keyword argument is correctly stored.
- For the Plotly (and PlotlyJS) backend, test that `levels`
  is correctly converted to backend-specific settings.
This commit is contained in:
Andy Nowacki 2021-08-20 15:49:22 +01:00
parent 79ce34a712
commit f1c0c7e4ec
3 changed files with 90 additions and 0 deletions

View File

@ -31,6 +31,7 @@ end # testset
include("test_defaults.jl") include("test_defaults.jl")
include("test_pipeline.jl") include("test_pipeline.jl")
include("test_axes.jl") include("test_axes.jl")
include("test_contours.jl")
include("test_axis_letter.jl") include("test_axis_letter.jl")
include("test_components.jl") include("test_components.jl")
include("test_shorthands.jl") include("test_shorthands.jl")
@ -38,6 +39,7 @@ include("integration_dates.jl")
include("test_recipes.jl") include("test_recipes.jl")
include("test_hdf5plots.jl") include("test_hdf5plots.jl")
include("test_pgfplotsx.jl") include("test_pgfplotsx.jl")
include("test_plotly.jl")
reference_dir(args...) = reference_dir(args...) =
joinpath(homedir(), ".julia", "dev", "PlotReferenceImages", args...) joinpath(homedir(), ".julia", "dev", "PlotReferenceImages", args...)

31
test/test_contours.jl Normal file
View File

@ -0,0 +1,31 @@
using Plots, Test
@testset "Contours" begin
x = (-2π):0.1:(2π)
y = (-π):0.1:π
z = cos.(y) .* sin.(x')
@testset "Default number" begin
@test contour(x, y, z)[1][1].plotattributes[:levels] ==
Plots._series_defaults[:levels]
end
@testset "Number" begin
@testset "$n contours" for n in (2, 5, 100)
p = contour(x, y, z, levels = n)
attr = p[1][1].plotattributes
@test attr[:seriestype] == :contour
@test attr[:levels] == n
end
end
@testset "Range" begin
levels = -1:0.5:1
@test contour(x, y, z, levels = levels)[1][1].plotattributes[:levels] == levels
end
@testset "Set of levels" begin
levels = [-1, 0.25, 0, 0.25, 1]
@test contour(x, y, z, levels = levels)[1][1].plotattributes[:levels] == levels
end
end

57
test/test_plotly.jl Normal file
View File

@ -0,0 +1,57 @@
using Plots, Test
@testset "Plotly" begin
@testset "Basic" begin
@test plotly() == Plots.PlotlyBackend()
@test backend() == Plots.PlotlyBackend()
p = plot(rand(10))
@test isa(p, Plots.Plot) == true
end
@testset "Contours" begin
x = (-2π):0.1:(2π)
y = (-π):0.1:π
z = cos.(y) .* sin.(x')
@testset "Contour numbers" begin
@testset "Default" begin
@test Plots.plotly_series(contour(x, y, z))[1][:ncontours] ==
Plots._series_defaults[:levels] + 2
end
@testset "Specified number" begin
@test Plots.plotly_series(contour(x, y, z, levels = 10))[1][:ncontours] ==
12
end
end
@testset "Contour values" begin
@testset "Range" begin
levels = -1:0.5:1
p = contour(x, y, z, levels = levels)
@test p[1][1].plotattributes[:levels] == levels
@test Plots.plotly_series(p)[1][:contours][:start] == first(levels)
@test Plots.plotly_series(p)[1][:contours][:end] == last(levels)
@test Plots.plotly_series(p)[1][:contours][:size] == step(levels)
end
@testset "Set of contours" begin
levels = [-1, -0.25, 0, 0.25, 1]
levels_range =
range(first(levels), stop = last(levels), length = length(levels))
p = contour(x, y, z, levels = levels)
@test p[1][1].plotattributes[:levels] == levels
series_dict = @test_logs (
:warn,
"setting arbitrary contour levels with Plotly backend " *
"is not supported; use a range to set equally-spaced contours or an " *
"integer to set the approximate number of contours with the keyword " *
"`levels`. Using levels -1.0:0.5:1.0",
) Plots.plotly_series(p)
@test series_dict[1][:contours][:start] == first(levels_range)
@test series_dict[1][:contours][:end] == last(levels_range)
@test series_dict[1][:contours][:size] == step(levels_range)
end
end
end
end