Skip to content

Commit

Permalink
Fix some lints in the tests folder (#6051)
Browse files Browse the repository at this point in the history
* fix more lints

* revert any(duplicated()) changes
  • Loading branch information
etiennebacher committed Aug 20, 2024
1 parent d8eb945 commit 1a6e495
Show file tree
Hide file tree
Showing 26 changed files with 151 additions and 152 deletions.
2 changes: 1 addition & 1 deletion tests/testthat/test-aes-calculated.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ test_that("names surrounded by .. is calculated", {
expect_equal(is_calculated_aes(aes(..x.., ..x, x..)), c(TRUE, FALSE, FALSE))

# even when nested
expect_equal(is_calculated_aes(aes(f(..x..))), TRUE)
expect_true(is_calculated_aes(aes(f(..x..))))
})

test_that("call to stat() is calculated", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-aes-grouping.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ test_that("no error for aes(groupS)", {
g <- add_group(df2)

expect_equal(nrow(g), nrow(df2))
expect_equal(names(g), c("x", "y", "groupS", "group"))
expect_named(g, c("x", "y", "groupS", "group"))
})

test_that("label is not used as a grouping var", {
Expand Down
3 changes: 1 addition & 2 deletions tests/testthat/test-coord-.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ test_that("guide names are not removed by `train_panel_guides()`", {
layout$setup_panel_guides(guides_list(NULL), plot$layers)

# Line showing change in outcome
expect_equal(names(layout$panel_params[[1]]$guides$aesthetics),
c("x", "y", "x.sec", "y.sec"))
expect_named(layout$panel_params[[1]]$guides$aesthetics, c("x", "y", "x.sec", "y.sec"))
})

test_that("check coord limits errors only on bad inputs", {
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-coord-polar.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_that("polar distance is calculated correctly", {
dat <- data_frame(
theta = c(0, 2*pi, 2, 6, 6, 1, 1, 0),
r = c(0, 0, 0.5, 0.5, 1, 1, 0.75, .5))
r = c(0, 0, 0.5, 0.5, 1, 1, 0.75, 0.5))

scales <- list(
x = scale_x_continuous(limits = c(0, 2*pi)),
Expand Down Expand Up @@ -176,7 +176,7 @@ test_that("polar coordinates draw correctly", {

dat <- data_frame(
theta = c(0, 2*pi, 2, 6, 6, 1, 1, 0),
r = c(0, 0, 0.5, 0.5, 1, 1, 0.75, .5),
r = c(0, 0, 0.5, 0.5, 1, 1, 0.75, 0.5),
g = 1:8
)
expect_doppelganger("Rays, circular arcs, and spiral arcs",
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-coord-train.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("NA's don't appear in breaks", {
ns <- names(trained)[grepl("(\\.major)|(\\.minor)$", names(trained))]

for (n in ns) {
if (!is.null(trained[n]) && any(is.na(trained[n])))
if (!is.null(trained[n]) && anyNA(trained[n]))
return(TRUE)
}

Expand All @@ -19,8 +19,8 @@ test_that("NA's don't appear in breaks", {
# This is a test to make sure the later tests will be useful!
# It's possible that changes to the way that breaks are calculated will
# make it so that scale_break_positions will no longer give NA for range 1, 12
expect_true(any(is.na(scale_x$break_positions())))
expect_true(any(is.na(scale_y$break_positions())))
expect_true(anyNA(scale_x$break_positions()))
expect_true(anyNA(scale_y$break_positions()))

# Check the various types of coords to make sure they don't have NA breaks
expect_false(any_NA_major_minor(coord_polar()$setup_panel_params(scale_x, scale_y)))
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-coord_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -309,8 +309,8 @@ test_that("sf_transform_xy() works", {
# transform back
out2 <- sf_transform_xy(out, 4326, 3347)
expect_identical(data$city, out2$city)
expect_true(all(abs(out2$x - data$x) < .01))
expect_true(all(abs(out2$y - data$y) < .01))
expect_true(all(abs(out2$x - data$x) < 0.01))
expect_true(all(abs(out2$y - data$y) < 0.01))

})

Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-empty-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ test_that("empty layers still generate one grob per panel", {
geom_point() +
facet_wrap(~y)

expect_equal(length(get_layer_grob(d)), 3)
expect_length(get_layer_grob(d), 3)
})

test_that("missing layers generate one grob per panel", {
df <- data_frame(x = 1:4, y = rep(1:2, 2), g = rep(1:2, 2))
base <- ggplot(df, aes(x, y)) + geom_point(shape = NA, na.rm = TRUE)

expect_equal(length(get_layer_grob(base)), 1)
expect_equal(length(get_layer_grob(base + facet_wrap(~ g))), 2)
expect_length(get_layer_grob(base), 1)
expect_length(get_layer_grob(base + facet_wrap(~ g)), 2)
})
18 changes: 9 additions & 9 deletions tests/testthat/test-facet-strips.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,24 @@ test_that("padding is only added if axis is present", {
strip.switch.pad.grid = unit(10, "mm")
)
pg <- ggplotGrob(p)
expect_equal(length(pg$heights), 19)
expect_equal(length(pg$widths), 18)
expect_length(pg$heights, 19)
expect_length(pg$widths, 18)

pg <- ggplotGrob(
p + scale_x_continuous(position = "top") +
scale_y_continuous(position = "right")
)
expect_equal(length(pg$heights), 20)
expect_length(pg$heights, 20)
expect_equal(as.character(pg$heights[9]), "1cm")
expect_equal(length(pg$widths), 19)
expect_length(pg$widths, 19)
expect_equal(as.character(pg$widths[13]), "1cm")

# Also add padding with negative ticks and no text (#5251)
pg <- ggplotGrob(
p + scale_x_continuous(labels = NULL, position = "top") +
theme(axis.ticks.length.x.top = unit(-2, "mm"))
)
expect_equal(length(pg$heights), 20)
expect_length(pg$heights, 20)
expect_equal(as.character(pg$heights[9]), "1cm")

# Inverse should be true when strips are switched
Expand All @@ -172,17 +172,17 @@ test_that("padding is only added if axis is present", {
)

pg <- ggplotGrob(p)
expect_equal(length(pg$heights), 20)
expect_length(pg$heights, 20)
expect_equal(as.character(pg$heights[13]), "1cm")
expect_equal(length(pg$widths), 19)
expect_length(pg$widths, 19)
expect_equal(as.character(pg$widths[7]), "1cm")

pg <- ggplotGrob(
p + scale_x_continuous(position = "top") +
scale_y_continuous(position = "right")
)
expect_equal(length(pg$heights), 19)
expect_equal(length(pg$widths), 18)
expect_length(pg$heights, 19)
expect_length(pg$widths, 18)
})

test_that("y strip labels are rotated when strips are switched", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-fortify.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test_that("spatial polygons have correct ordering", {
y - dely,y - dely,y + dely,y + dely,y - dely), ncol = 2))
}

make_hole <- function(x = 0, y = 0, height = .5, width = .5){
make_hole <- function(x = 0, y = 0, height = 0.5, width = 0.5){
p <- make_square(x = x, y = y, height = height, width = width)
p@hole <- TRUE
p
Expand Down
86 changes: 43 additions & 43 deletions tests/testthat/test-geom-dotplot.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test_that("dodging works", {
ndodge <- 3

# The amount of space allocated within each dodge group
dwidth <- .9 / ndodge
dwidth <- 0.9 / ndodge

# This should be the x position for each before dodging
xbase <- ceiling(df$group / ndodge)
Expand All @@ -36,20 +36,20 @@ test_that("dodging works", {

test_that("binning works", {
bp <- ggplot(dat, aes(y)) +
geom_dotplot(binwidth = .4, method = "histodot")
geom_dotplot(binwidth = 0.4, method = "histodot")
x <- get_layer_data(bp)$x

# Need ugly hack to make sure mod function doesn't give values like -3.99999
# due to floating point error
expect_true(all(abs((x - min(x) + 1e-7) %% .4) < 1e-6))
expect_true(all(abs((x - min(x) + 1e-7) %% 0.4) < 1e-6))

bp <- ggplot(dat, aes(x = y)) +
geom_dotplot(binwidth = .4, method = "dotdensity")
geom_dotplot(binwidth = 0.4, method = "dotdensity")
x <- get_layer_data(bp)$x

# This one doesn't ensure that dotdensity works, but it does check that it's not
# doing fixed bin sizes
expect_false(all(abs((x - min(x) + 1e-7) %% .4) < 1e-6))
expect_false(all(abs((x - min(x) + 1e-7) %% 0.4) < 1e-6))
})

test_that("NA's result in warning from stat_bindot", {
Expand All @@ -58,7 +58,7 @@ test_that("NA's result in warning from stat_bindot", {
dat$x[c(2,10)] <- NA

# Need to assign it to a var here so that it doesn't automatically print
expect_snapshot_warning(ggplot_build(ggplot(dat, aes(x)) + geom_dotplot(binwidth = .2)))
expect_snapshot_warning(ggplot_build(ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.2)))
})

test_that("when binning on y-axis, limits depend on the panel", {
Expand Down Expand Up @@ -92,99 +92,99 @@ test_that("geom_dotplot draws correctly", {

# Basic dotplot with binning along x axis
expect_doppelganger("basic dotplot with dot-density binning, binwidth = .4",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4)
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4)
)
expect_doppelganger("histodot binning (equal bin spacing)",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, method = "histodot")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, method = "histodot")
)
expect_doppelganger("dots stacked closer: stackratio=.5, fill=white",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackratio = .5, fill = "white")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackratio = 0.5, fill = "white")
)
expect_doppelganger("larger dots: dotsize=1.5, fill=white",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, dotsize = 1.4, fill = "white")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, dotsize = 1.4, fill = "white")
)

# Stacking methods
expect_doppelganger("stack up",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "up")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "up")
)
expect_doppelganger("stack down",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "down")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "down")
)
expect_doppelganger("stack center",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "center")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "center")
)
expect_doppelganger("stack centerwhole",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "centerwhole")
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "centerwhole")
)

# Stacking methods with coord_flip
expect_doppelganger("stack up with coord_flip",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "up") + coord_flip()
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "up") + coord_flip()
)
expect_doppelganger("stack down with coord_flip",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "down") + coord_flip()
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "down") + coord_flip()
)
expect_doppelganger("stack center with coord_flip",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "center") + coord_flip()
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "center") + coord_flip()
)
expect_doppelganger("stack centerwhole with coord_flip",
ggplot(dat, aes(x)) + geom_dotplot(binwidth = .4, stackdir = "centerwhole") + coord_flip()
ggplot(dat, aes(x)) + geom_dotplot(binwidth = 0.4, stackdir = "centerwhole") + coord_flip()
)

# Binning along x, with groups
expect_doppelganger("multiple groups, bins not aligned",
ggplot(dat, aes(x, fill = g)) + geom_dotplot(binwidth = .4, alpha = .4)
ggplot(dat, aes(x, fill = g)) + geom_dotplot(binwidth = 0.4, alpha = 0.4)
)
expect_doppelganger("multiple groups, bins aligned",
ggplot(dat, aes(x, fill = g)) + geom_dotplot(binwidth = .4, alpha = .4, binpositions = "all")
ggplot(dat, aes(x, fill = g)) + geom_dotplot(binwidth = 0.4, alpha = 0.4, binpositions = "all")
)

# Binning along y axis
expect_doppelganger("bin along y, stack center",
ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "center")
ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = 0.4, binaxis = "y", stackdir = "center")
)
expect_doppelganger("bin along y, stack centerwhole",
ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "centerwhole")
ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = 0.4, binaxis = "y", stackdir = "centerwhole")
)
expect_doppelganger("bin along y, stack centerwhole, histodot",
ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "centerwhole", method = "histodot")
ggplot(dat, aes(0, x)) + geom_dotplot(binwidth = 0.4, binaxis = "y", stackdir = "centerwhole", method = "histodot")
)

# Binning along y, with multiple grouping factors
dat2 <- data_frame(x = rep(factor(LETTERS[1:3]), 30), y = rnorm(90), g = rep(factor(LETTERS[1:2]), 45))

expect_doppelganger("bin x, three y groups, stack centerwhole",
ggplot(dat2, aes(y, x)) + geom_dotplot(binwidth = .25, binaxis = "x", stackdir = "centerwhole")
ggplot(dat2, aes(y, x)) + geom_dotplot(binwidth = 0.25, binaxis = "x", stackdir = "centerwhole")
)
expect_doppelganger("bin y, three x groups, stack centerwhole",
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "centerwhole")
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = 0.25, binaxis = "y", stackdir = "centerwhole")
)
expect_doppelganger("bin y, three x groups, bins aligned across groups",
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "center", binpositions = "all")
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = 0.25, binaxis = "y", stackdir = "center", binpositions = "all")
)
expect_doppelganger("bin y, three x groups, bins aligned, coord_flip",
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = .25, binaxis = "y", stackdir = "center", binpositions = "all") +
ggplot(dat2, aes(x, y)) + geom_dotplot(binwidth = 0.25, binaxis = "y", stackdir = "center", binpositions = "all") +
coord_flip()
)
expect_doppelganger("bin y, dodged",
ggplot(dat2, aes("foo", y, fill = x)) + scale_y_continuous(breaks = seq(-4, 4, .4)) +
geom_dotplot(binwidth = .25, position = "dodge", binaxis = "y", stackdir = "center")
ggplot(dat2, aes("foo", y, fill = x)) + scale_y_continuous(breaks = seq(-4, 4, 0.4)) +
geom_dotplot(binwidth = 0.25, position = "dodge", binaxis = "y", stackdir = "center")
)
expect_doppelganger("bin y, dodged, coord_flip",
ggplot(dat2, aes("foo", y, fill = x)) + scale_y_continuous(breaks = seq(-4, 4, .4)) +
geom_dotplot(binwidth = .25, position = "dodge", binaxis = "y", stackdir = "center") +
ggplot(dat2, aes("foo", y, fill = x)) + scale_y_continuous(breaks = seq(-4, 4, 0.4)) +
geom_dotplot(binwidth = 0.25, position = "dodge", binaxis = "y", stackdir = "center") +
coord_flip()
)
expect_doppelganger("bin y, three x groups, fill and dodge",
ggplot(dat2, aes(x, y, fill = g)) + scale_y_continuous(breaks = seq(-4 ,4, .4)) +
geom_dotplot(binwidth = .2, position = "dodge", binaxis = "y", stackdir = "center")
ggplot(dat2, aes(x, y, fill = g)) + scale_y_continuous(breaks = seq(-4 ,4, 0.4)) +
geom_dotplot(binwidth = 0.2, position = "dodge", binaxis = "y", stackdir = "center")
)
expect_doppelganger("bin y, continous x-axis, grouping by x",
ggplot(dat2, aes(as.numeric(x), y, group = x)) + geom_dotplot(binwidth = .2, binaxis = "y", stackdir = "center")
ggplot(dat2, aes(as.numeric(x), y, group = x)) + geom_dotplot(binwidth = 0.2, binaxis = "y", stackdir = "center")
)
expect_doppelganger("bin y, continous x-axis, single x group",
ggplot(dat2, aes(as.numeric(x), y)) + geom_dotplot(binwidth = .2, binaxis = "y", stackdir = "center")
ggplot(dat2, aes(as.numeric(x), y)) + geom_dotplot(binwidth = 0.2, binaxis = "y", stackdir = "center")
)

# border width and size
Expand All @@ -198,31 +198,31 @@ test_that("geom_dotplot draws correctly", {
stroke = rep(c(1, 2), length.out = nrow(dat))
)
) +
geom_dotplot(binwidth = .4, fill = "red", col = "blue") +
geom_dotplot(binwidth = 0.4, fill = "red", col = "blue") +
continuous_scale("stroke", palette = function(x) scales::rescale(x, to = c(1, 6))) +
guides(linetype = guide_legend(order = 1))
)

# Stacking groups
expect_doppelganger("3 stackgroups, dot-density with aligned bins",
ggplot(dat2, aes(y, fill = x)) + geom_dotplot(binwidth = .25, stackgroups = TRUE, binpositions = "all", alpha = 0.5)
ggplot(dat2, aes(y, fill = x)) + geom_dotplot(binwidth = 0.25, stackgroups = TRUE, binpositions = "all", alpha = 0.5)
)
expect_doppelganger("3 stackgroups, histodot",
ggplot(dat2, aes(y, fill = x)) + geom_dotplot(binwidth = .25, stackgroups = TRUE, method = "histodot", alpha = 0.5)
ggplot(dat2, aes(y, fill = x)) + geom_dotplot(binwidth = 0.25, stackgroups = TRUE, method = "histodot", alpha = 0.5)
)
expect_doppelganger("3 stackgroups, bin y, histodot",
ggplot(dat2, aes(1, y, fill = x)) + geom_dotplot(binaxis = "y", binwidth = .25, stackgroups = TRUE, method = "histodot", alpha = 0.5)
ggplot(dat2, aes(1, y, fill = x)) + geom_dotplot(binaxis = "y", binwidth = 0.25, stackgroups = TRUE, method = "histodot", alpha = 0.5)
)

# This one is currently broken but it would be a really rare case, and it
# probably requires a really ugly hack to fix
expect_doppelganger("bin y, dodging, 3 stackgroups, histodot",
ggplot(dat2, aes(x, y, fill = g)) +
geom_dotplot(binaxis = "y", binwidth = .25, stackgroups = TRUE, method = "histodot",
geom_dotplot(binaxis = "y", binwidth = 0.25, stackgroups = TRUE, method = "histodot",
alpha = 0.5, stackdir = "centerwhole")
)
expect_doppelganger("facets, 3 groups, histodot, stackgroups",
ggplot(dat2, aes(y, fill = g)) + geom_dotplot(binwidth = .25, stackgroups = TRUE, method = "histodot", alpha = 0.5) +
ggplot(dat2, aes(y, fill = g)) + geom_dotplot(binwidth = 0.25, stackgroups = TRUE, method = "histodot", alpha = 0.5) +
facet_grid(x ~ .)
)

Expand All @@ -231,10 +231,10 @@ test_that("geom_dotplot draws correctly", {
dat2$x[c(1, 10)] <- NA

expect_warning(expect_doppelganger("2 NA values, dot-density binning, binwidth = .4",
ggplot(dat2, aes(x)) + geom_dotplot(binwidth = .4)
ggplot(dat2, aes(x)) + geom_dotplot(binwidth = 0.4)
))
expect_warning(expect_doppelganger("2 NA values, bin along y, stack center",
ggplot(dat2, aes(0, x)) + geom_dotplot(binwidth = .4, binaxis = "y", stackdir = "center")
ggplot(dat2, aes(0, x)) + geom_dotplot(binwidth = 0.4, binaxis = "y", stackdir = "center")
))
})

Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-geom-path.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test_that("keep_mid_true drops leading/trailing FALSE", {
expect_equal(keep_mid_true(c(F, F)), c(F, F))
expect_equal(keep_mid_true(c(F, T, F, T, F)), c(F, T, T, T, F))
expect_equal(keep_mid_true(c(T, T, F, T, F)), c(T, T, T, T, F))
expect_equal(keep_mid_true(c(F, T, F, T, T)), c(F, T, T, T, T))
expect_equal(keep_mid_true(c(FALSE, FALSE)), c(FALSE, FALSE))
expect_equal(keep_mid_true(c(FALSE, TRUE, FALSE, TRUE, FALSE)), c(FALSE, TRUE, TRUE, TRUE, FALSE))
expect_equal(keep_mid_true(c(TRUE, TRUE, FALSE, TRUE, FALSE)), c(TRUE, TRUE, TRUE, TRUE, FALSE))
expect_equal(keep_mid_true(c(FALSE, TRUE, FALSE, TRUE, TRUE)), c(FALSE, TRUE, TRUE, TRUE, TRUE))
})

test_that("geom_path() throws meaningful error on bad combination of varying aesthetics", {
Expand Down
Loading

0 comments on commit 1a6e495

Please sign in to comment.