-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from pepijn-devries/work-in-progress
Improved test coverage
- Loading branch information
Showing
8 changed files
with
621 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: ggfields | ||
Title: Add Vector Field Layers to Ggplots | ||
Version: 0.0.6.0001 | ||
Version: 0.0.6.0002 | ||
Authors@R: c(person("Pepijn", "de Vries", role = c("aut", "cre"), | ||
email = "[email protected]", | ||
comment = c(ORCID = "0000-0002-7961-6646"))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
params_mockup <- | ||
c( | ||
ggplot2::ggplot() + geom_fields(), | ||
list( | ||
x_range = c(1, 2), | ||
y_range = c(50, 51), | ||
crs = sf::st_crs(4326), | ||
default_crs = 4326 | ||
) | ||
) | ||
|
||
coord <- ggplot2::coord_sf() | ||
|
||
test_that( | ||
"Angle correction won't work on geometries other then point", { | ||
expect_error({ | ||
data <- | ||
data.frame( | ||
geometry = sf::st_sfc(sf::st_polygon()) | ||
) |> | ||
sf::st_as_sf(crs = 4326) | ||
|
||
angle_correction(data, params_mockup, coord) | ||
}) | ||
}) |> suppressMessages() | ||
|
||
test_that( | ||
"Missing CRS is signalled", { | ||
expect_message({ | ||
data <- | ||
data.frame( | ||
angle = 0, | ||
geometry = sf::st_sfc(sf::st_polygon()) | ||
) |> | ||
sf::st_as_sf() | ||
|
||
angle_correction(data, params_mockup, coord) | ||
}) | ||
}) |> suppressMessages() | ||
|
||
test_that( | ||
"Expect warning for proximity to North Pole", { | ||
expect_warning({ | ||
data <- | ||
data.frame( | ||
x = seq(1, 2, 0.1), | ||
y = seq(98.999, 99.999, 0.1), | ||
angle = 0 | ||
) |> | ||
sf::st_as_sf(coords = c("x", "y"), crs = 4326, remove = FALSE) | ||
|
||
angle_correction(data, params_mockup, coord) | ||
}) | ||
}) |> suppressMessages() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
{ | ||
library(dplyr, quietly = TRUE) | ||
library(sf, quietly = TRUE) | ||
library(stars, quietly = TRUE) | ||
library(ggplot2, quietly = TRUE) | ||
} |> | ||
suppressWarnings() |> | ||
suppressMessages() | ||
|
||
self <- GeomFields | ||
data <- data.frame( | ||
colour = "black", | ||
geometry = st_point(c(1,50)) |> st_sfc(crs = 4326), | ||
angle = 0, | ||
PANEL = 1, | ||
group = 1, | ||
xmin = 1, | ||
xmax = 1, | ||
ymin = 50, | ||
ymax = 50, | ||
linewidth = 1, | ||
linetype = 1, | ||
alpha = 1, | ||
radius = 1 | ||
) |> | ||
st_as_sf() | ||
|
||
params_mockup <- | ||
c( | ||
ggplot() + geom_fields(), | ||
list( | ||
x_range = c(1, 2), | ||
y_range = c(50, 51), | ||
crs = st_crs(4326), | ||
default_crs = 4326 | ||
) | ||
) | ||
|
||
coord <- coord_sf() | ||
|
||
test_that( | ||
"Prep fields coerces stars to sf", { | ||
expect_s3_class({ | ||
system.file("tif/L7_ETMs.tif", package = "stars") |> read_stars() -> x | ||
ggfields:::.data_prep_fields(x) | ||
}, "sf") | ||
} | ||
) | ||
|
||
test_that( | ||
"Setup params add linejoin and lineend when missing", { | ||
expect_true({ | ||
params <- .setup_params_fields(params = list()) | ||
typeof(params) == "list" && | ||
all(c("linejoin", "lineend") %in% names(params)) | ||
}) | ||
} | ||
) | ||
|
||
test_that( | ||
"Error when `x` aesthetic is not combined with `y`", { | ||
expect_error({ | ||
data <- data |> st_drop_geometry() |> | ||
mutate(x = 0) | ||
ggfields:::.compute_panel_stat_fields(data = data) | ||
}) | ||
} | ||
) | ||
|
||
test_that( | ||
"radius and angle are coercible to numerics", { | ||
expect_true({ | ||
test <- ggfields:::.compute_panel_stat_fields(data = data) | ||
is.numeric(test$angle) && is.numeric(test$radius) | ||
}) | ||
}) | ||
|
||
test_that( | ||
"Geometry is added to mapping of sf objects when missing", { | ||
expect_true({ | ||
test <- ggfields:::.mapping_prep_fields(data, aes()) | ||
("geometry" %in% names(test)) && inherits(test$geometry, "quosure") | ||
}) | ||
}) | ||
|
||
test_that( | ||
"Panel draw function returns a gTree object", { | ||
testthat::expect_s3_class({ | ||
ggfields:::.draw_panel_fields( | ||
self, data, params_mockup, coord, | ||
FALSE, grid::unit(0.7, "cm"), | ||
grid::arrow(), angle_correction, "mitre", "butt" | ||
) |> suppressMessages() | ||
}, c("gTree", "grob", "gDesc")) | ||
} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
test_that( | ||
"Pythagoras is correct", { | ||
calc <- pythagoras(1:10, 10:1) | ||
expect_equal( | ||
sum(calc), | ||
87.37186249587302) | ||
}) |