Skip to content

Commit e92e0db

Browse files
authored
Merge pull request #209 from TidierOrg/test-items
Changed testing to use testitems, which allow tests to be co-located with code and run individually
2 parents 39b0ce6 + 8475454 commit e92e0db

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2875
-2923
lines changed

Project.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4"
1010
Colors = "5ae59095-9a9b-59fe-a467-6f913c188581"
1111
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
1212
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
13+
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
1314
FixedPointNumbers = "53c48c17-4a7d-5ca2-90c5-79b7896eea93"
1415
Format = "1fa38f19-a742-5d3f-a2b9-30dd87b9d5f8"
1516
GLM = "38e38edf-8417-5370-95a0-9cbb8c7f171a"
@@ -24,6 +25,7 @@ Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
2425
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
2526
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
2627
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
28+
TestItems = "1c621080-faea-4a02-84b6-bbd5e436b8fe"
2729
TidierData = "fe2206b3-d496-4ee9-a338-6a095c4ece80"
2830

2931
[compat]
@@ -33,6 +35,7 @@ ColorSchemes = "3.24"
3335
Colors = "0.13"
3436
DataFrames = "1.7"
3537
Dates = "1.10"
38+
Documenter = "1.16.1"
3639
FixedPointNumbers = "0.8"
3740
Format = "1.3"
3841
GLM = "1.9"
@@ -47,6 +50,7 @@ Random = "1.10"
4750
Reexport = "1.2"
4851
Statistics = "1.10"
4952
Test = "1.10"
53+
TestItems = "1.0.0"
5054
TidierData = "0.17"
5155
julia = "1.10"
5256

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ using TidierPlots
44
makedocs(
55
modules=[TidierPlots],
66
clean=true,
7-
doctest=false,
7+
doctest=true,
8+
warnonly=true,
89
sitename="TidierPlots.jl",
910
authors="Randall Boyes et al.",
1011
checkdocs=:all,

src/TidierPlots.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ using KernelDensity
3535
# for size scales
3636
using Statistics
3737

38+
# for tests
39+
using TestItems
40+
3841
include("structs.jl")
3942

4043
include("addplots.jl")
@@ -165,4 +168,8 @@ const plot_show = Ref{Bool}(true)
165168
const plot_pluto = Ref{Bool}(true)
166169
const verbose = Ref{Bool}(false)
167170

171+
@testitem "TidierPlots options" begin
172+
@test_throws ArgumentError TidierPlots_set("na", true)
173+
end
174+
168175
end

src/aes.jl

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,208 @@ end
7575
# COV_EXCL_STOP
7676

7777
@eval const $(Symbol("@es")) = $(Symbol("@aes"))
78+
79+
@testitem "Aes operations" setup=[TidierPlotsSetup] begin
80+
t = ggplot(penguins) +
81+
geom_point(aes(x = :bill_length_mm => (x -> x .+ 10),
82+
y = :bill_depth_mm => (y -> y .+ 20)))
83+
m = Makie.plot(
84+
Makie.SpecApi.GridLayout(
85+
Makie.SpecApi.Axis(
86+
plots = [
87+
Makie.PlotSpec(
88+
:Scatter,
89+
penguins.bill_length_mm .+ 10,
90+
penguins.bill_depth_mm .+ 20)
91+
]
92+
)
93+
)
94+
)
95+
@test plot_images_equal(t, m)
96+
t = ggplot(penguins) +
97+
geom_point(aes(x = [:bill_length_mm, :bill_depth_mm] => ((x, y) ->x .+ y), y = :bill_depth_mm))
98+
m = Makie.plot(
99+
Makie.SpecApi.GridLayout(
100+
Makie.SpecApi.Axis(
101+
plots = [
102+
Makie.PlotSpec(
103+
:Scatter,
104+
penguins.bill_length_mm .+ penguins.bill_depth_mm,
105+
penguins.bill_depth_mm)
106+
]
107+
)
108+
)
109+
)
110+
@test plot_images_equal(t, m)
111+
t = ggplot(penguins) +
112+
geom_point(aes(x = :bill_length_mm => (x -> x .- 10),
113+
y = :bill_depth_mm => (y -> y .- 20)))
114+
m = Makie.plot(
115+
Makie.SpecApi.GridLayout(
116+
Makie.SpecApi.Axis(
117+
plots = [
118+
Makie.PlotSpec(
119+
:Scatter,
120+
penguins.bill_length_mm .- 10,
121+
penguins.bill_depth_mm .- 20)
122+
]
123+
)
124+
)
125+
)
126+
@test plot_images_equal(t, m)
127+
t = ggplot(penguins) +
128+
geom_point(aes(x = [:bill_length_mm, :bill_depth_mm] => ((x, y) ->x .- y), y = :bill_depth_mm))
129+
tm = ggplot(penguins) +
130+
geom_point(@aes(x = bill_length_mm - bill_depth_mm,
131+
y = bill_depth_mm))
132+
m = Makie.plot(
133+
Makie.SpecApi.GridLayout(
134+
Makie.SpecApi.Axis(
135+
plots = [
136+
Makie.PlotSpec(
137+
:Scatter,
138+
penguins.bill_length_mm .- penguins.bill_depth_mm,
139+
penguins.bill_depth_mm)
140+
]
141+
)
142+
)
143+
)
144+
@test plot_images_equal(t, m)
145+
@test plot_images_equal(tm, m)
146+
t = ggplot(penguins) +
147+
geom_point(aes(x = :bill_length_mm => (x -> x .* 10),
148+
y = :bill_depth_mm => (y -> y .* 20)))
149+
m = Makie.plot(
150+
Makie.SpecApi.GridLayout(
151+
Makie.SpecApi.Axis(
152+
plots = [
153+
Makie.PlotSpec(
154+
:Scatter,
155+
penguins.bill_length_mm .* 10,
156+
penguins.bill_depth_mm .* 20)
157+
]
158+
)
159+
)
160+
)
161+
@test plot_images_equal(t, m)
162+
t = ggplot(penguins) +
163+
geom_point(aes(x = [:bill_length_mm, :bill_depth_mm] => ((x, y) ->x .* y), y = :bill_depth_mm))
164+
m = Makie.plot(
165+
Makie.SpecApi.GridLayout(
166+
Makie.SpecApi.Axis(
167+
plots = [
168+
Makie.PlotSpec(
169+
:Scatter,
170+
penguins.bill_length_mm .* penguins.bill_depth_mm,
171+
penguins.bill_depth_mm)
172+
]
173+
)
174+
)
175+
)
176+
@test plot_images_equal(t, m)
177+
t = ggplot(penguins) +
178+
geom_point(aes(x = :bill_length_mm => (x -> x ./ 10),
179+
y = :bill_depth_mm => (y -> y ./ 20)))
180+
m = Makie.plot(
181+
Makie.SpecApi.GridLayout(
182+
Makie.SpecApi.Axis(
183+
plots = [
184+
Makie.PlotSpec(
185+
:Scatter,
186+
penguins.bill_length_mm ./ 10,
187+
penguins.bill_depth_mm ./ 20)
188+
]
189+
)
190+
)
191+
)
192+
@test plot_images_equal(t, m)
193+
t = ggplot(penguins) +
194+
geom_point(aes(x = [:bill_length_mm, :bill_depth_mm] => ((x, y) ->x ./ y), y = :bill_depth_mm))
195+
m = Makie.plot(
196+
Makie.SpecApi.GridLayout(
197+
Makie.SpecApi.Axis(
198+
plots = [
199+
Makie.PlotSpec(
200+
:Scatter,
201+
penguins.bill_length_mm ./ penguins.bill_depth_mm,
202+
penguins.bill_depth_mm)
203+
]
204+
)
205+
)
206+
)
207+
@test plot_images_equal(t, m)
208+
my_func(x) = x ./ 10
209+
t = ggplot(penguins) +
210+
geom_point(aes(x = :bill_length_mm => my_func, y = :bill_depth_mm))
211+
m = Makie.plot(
212+
Makie.SpecApi.GridLayout(
213+
Makie.SpecApi.Axis(
214+
plots = [
215+
Makie.PlotSpec(
216+
:Scatter,
217+
penguins.bill_length_mm ./ 10,
218+
penguins.bill_depth_mm)
219+
]
220+
)
221+
)
222+
)
223+
@test plot_images_equal(t, m)
224+
my_func(x, y) = x ./ y
225+
t = ggplot(penguins) +
226+
geom_point(aes(x = [:bill_length_mm, :bill_depth_mm] => my_func, y= :bill_depth_mm))
227+
m = Makie.plot(
228+
Makie.SpecApi.GridLayout(
229+
Makie.SpecApi.Axis(
230+
plots = [
231+
Makie.PlotSpec(
232+
:Scatter,
233+
penguins.bill_length_mm ./ penguins.bill_depth_mm,
234+
penguins.bill_depth_mm)
235+
]
236+
)
237+
)
238+
)
239+
@test plot_images_equal(t, m)
240+
end
241+
242+
@testitem "Aes specifications" setup=[TidierPlotsSetup] begin
243+
function Base.:(==)(aes1::TidierPlots.Aesthetics, aes2::TidierPlots.Aesthetics)
244+
if length(aes1.positional) != 0 || length(aes2.positional) != 0
245+
if length(aes2.positional) != length(aes1.positional)
246+
return false
247+
end
248+
if any(aes1.positional .!= aes2.positional)
249+
return false
250+
end
251+
end
252+
253+
if length(aes1.named) != 0 || length(aes2.named) != 0
254+
if length(aes2.named) != length(aes1.named)
255+
return false
256+
end
257+
if length(intersect(keys(aes1.named), keys(aes2.named))) != length(aes1. named)
258+
return false
259+
end
260+
for (k, v) in aes1.named
261+
if aes2.named[k] != v
262+
return false
263+
end
264+
end
265+
end
266+
267+
return true
268+
end
269+
270+
@test aes(x = "x", y = "y") == @aes(x = x, y = y)
271+
@test aes(x = "x", y = "y") == @es(x = x, y = y)
272+
@test aes(x = "x", y = "y") == aes(x = :x, y = :y)
273+
@test aes("x", "y") == @aes(x, y)
274+
@test aes("x", "y") == @es(x, y)
275+
@test aes("x", "y") == aes(:x, :y)
276+
@test aes("x", y = "y") == aes(:x, y = :y)
277+
@test aes("x", y = "y") == @es(x, y = y)
278+
@test aes("x", y = "y") == @aes(x, y = y)
279+
@test_throws ErrorException @macroexpand @aes(x/10)
280+
to_cm(x) = x ./ 10
281+
@test_throws ErrorException aes(:x => to_cm)
282+
end

src/broadcasting.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,49 @@ import Base.length
66
Base.iterate(p::TidierPlots.GGPlot) = (p, 1)
77
Base.iterate(p::TidierPlots.GGPlot, s::Any) = nothing
88
Base.length(p::TidierPlots.GGPlot) = 1
9+
10+
@testitem "Broadcasting" setup=[TidierPlotsSetup] begin
11+
plots = ggplot(penguins, aes(x=:bill_length_mm, y=:bill_depth_mm)) .+
12+
[geom_point(), geom_line(), geom_path()]
13+
14+
t = +(plots...)
15+
16+
mpoint = Makie.SpecApi.GridLayout(
17+
Makie.SpecApi.Axis(
18+
plots=[
19+
Makie.PlotSpec(
20+
:Scatter,
21+
penguins.bill_length_mm,
22+
penguins.bill_depth_mm, color = :black)
23+
]; xlabel="bill_length_mm", ylabel="bill_depth_mm"
24+
)
25+
)
26+
27+
mpath = Makie.SpecApi.GridLayout(
28+
Makie.SpecApi.Axis(
29+
plots=[
30+
Makie.PlotSpec(
31+
:Lines,
32+
penguins.bill_length_mm,
33+
penguins.bill_depth_mm, color = :black)
34+
]; xlabel="bill_length_mm", ylabel="bill_depth_mm"
35+
)
36+
)
37+
38+
perm = sortperm(penguins.bill_length_mm)
39+
40+
mline = Makie.SpecApi.GridLayout(
41+
Makie.SpecApi.Axis(
42+
plots=[
43+
Makie.PlotSpec(
44+
:Lines,
45+
penguins.bill_length_mm[perm],
46+
penguins.bill_depth_mm[perm], color = :black)
47+
]; xlabel="bill_length_mm", ylabel="bill_depth_mm"
48+
)
49+
)
50+
51+
m = Makie.plot(Makie.SpecApi.GridLayout([mpoint mline mpath]))
52+
53+
@test plot_images_equal(t, m)
54+
end

src/convert.jl

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,45 @@ function convert_aes_type(aes_col::CategoricalArray, ::Type{Integer}, col::Symbo
9797
verbose[] && println("Converting $col to Integer")
9898
return levelcode.(aes_col)
9999
end
100+
101+
@testitem "Conversions" setup=[TidierPlotsSetup] begin
102+
using CategoricalArrays
103+
int_array = [1, 2, 3]
104+
num_array = [1.0, 2.0, 3.0]
105+
cat_array = CategoricalArray(int_array)
106+
color_array = TidierPlots._default_discrete_palette.(int_array)
107+
108+
@test TidierPlots.try_convert(Symbol, "test", "dodge", "geom_bar") == :test
109+
@test_throws ArgumentError TidierPlots.try_convert(Integer, "test", "dodge", "geom_bar")
110+
111+
@test all(
112+
TidierPlots.convert_aes_type(num_array, Integer, :dodge) .== int_array
113+
)
114+
@test all(
115+
TidierPlots.convert_aes_type(int_array, Integer, :dodge) .== int_array
116+
)
117+
@test all(
118+
TidierPlots.convert_aes_type(cat_array, Integer, :dodge) .== int_array
119+
)
120+
@test all(
121+
TidierPlots.convert_aes_type(num_array, Colors.RGBA, :color) .==
122+
[RGB(0.190631, 0.407061, 0.556089),
123+
RGB(0.20803, 0.718701, 0.472873),
124+
RGB(0.993248, 0.906157, 0.143936)]
125+
)
126+
@test all(
127+
TidierPlots.convert_aes_type(
128+
["a", "b", "c"], Colors.RGBA, :color) .==
129+
[RGB(0.0, 0.4470588235294118, 0.6980392156862745),
130+
RGB(0.9019607843137255, 0.6235294117647059, 0.0),
131+
RGB(0.0, 0.6196078431372549, 0.45098039215686275)]
132+
)
133+
@test all(
134+
TidierPlots.convert_aes_type(
135+
CategoricalArray(["a", "b", "c"]), Colors.RGBA, :color) .==
136+
[RGB(0.0, 0.4470588235294118, 0.6980392156862745),
137+
RGB(0.9019607843137255, 0.6235294117647059, 0.0),
138+
RGB(0.0, 0.6196078431372549, 0.45098039215686275)]
139+
)
140+
end
141+

src/extract_aes.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ function make_aes_extractor(required_aes)
3333
end
3434

3535
extract_aes = make_aes_extractor([])
36+
37+
@testitem "extract aes" begin
38+
@test_throws ArgumentError TidierPlots.extract_aes([aes(:x)], Dict())
39+
40+
x_extractor = TidierPlots.make_aes_extractor(["x"])
41+
42+
@test_throws ArgumentError x_extractor([aes(:col1, x = :col2)], Dict())
43+
44+
pos = x_extractor([aes(:col1)], Dict())
45+
named = x_extractor([aes(x = :col1)], Dict())
46+
47+
@test pos[1][:x] == named[1][:x]
48+
end

0 commit comments

Comments
 (0)