From f1c0c7e4ec891950e3730f93bd729393e3f7bd32 Mon Sep 17 00:00:00 2001 From: Andy Nowacki Date: Fri, 20 Aug 2021 15:49:22 +0100 Subject: [PATCH] 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. --- test/runtests.jl | 2 ++ test/test_contours.jl | 31 +++++++++++++++++++++++ test/test_plotly.jl | 57 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 test/test_contours.jl create mode 100644 test/test_plotly.jl diff --git a/test/runtests.jl b/test/runtests.jl index 24931870..7e4150ff 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -31,6 +31,7 @@ end # testset include("test_defaults.jl") include("test_pipeline.jl") include("test_axes.jl") +include("test_contours.jl") include("test_axis_letter.jl") include("test_components.jl") include("test_shorthands.jl") @@ -38,6 +39,7 @@ include("integration_dates.jl") include("test_recipes.jl") include("test_hdf5plots.jl") include("test_pgfplotsx.jl") +include("test_plotly.jl") reference_dir(args...) = joinpath(homedir(), ".julia", "dev", "PlotReferenceImages", args...) diff --git a/test/test_contours.jl b/test/test_contours.jl new file mode 100644 index 00000000..134d4625 --- /dev/null +++ b/test/test_contours.jl @@ -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 diff --git a/test/test_plotly.jl b/test/test_plotly.jl new file mode 100644 index 00000000..85cc6f9f --- /dev/null +++ b/test/test_plotly.jl @@ -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