Skip to content

Commit

Permalink
Add initial tests for point_offset
Browse files Browse the repository at this point in the history
  • Loading branch information
mhpob committed May 24, 2024
1 parent 9016179 commit 64237d6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
7 changes: 5 additions & 2 deletions R/util-point_offset.r
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ point_offset <- function(lon = NA, lat = NA, offsetDist = NA, offsetDir = NA,

lat2 <- 180/pi * lat2

coords <- matrix(c(lon2, lat2), ncol = 2)
colnames(coords) <- c('lon', 'lat')
coords <- matrix(
c(lon2, lat2),
ncol = 2,
dimnames = list(NULL, c('lon', 'lat'))
)

return(coords)
}
Expand Down
69 changes: 69 additions & 0 deletions tests/testthat/test-point_offset.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
test_that("Returns named matrix", {
expect_type(point_offset(lon = -83.0, lat = 44.0), 'double')
expect_vector(point_offset(lon = -83.0, lat = 44.0))

# No inputs
expect_type(point_offset(), 'double')

expect_identical(
colnames(point_offset(lon = -83.0, lat = 44.0)),
c('lon', 'lat')
)
})




test_that("Converts feet to meters", {
expect_equal(
point_offset(lon = -83.0, lat = 44.0, offsetDist = 100,
offsetDir = 'NE', distUnit = 'ft'),
matrix(c(-82.99973, 44.00019),
ncol = 2,
dimnames = list(NULL, c('lon', 'lat'))),
tolerance = 1e-5
)

expect_false(
identical(
point_offset(lon = -83.0, lat = 44.0, offsetDist = 100,
offsetDir = 'NE', distUnit = 'm'),
point_offset(lon = -83.0, lat = 44.0, offsetDist = 100,
offsetDir = 'NE', distUnit = 'ft')
)
)
})




test_that("Errors with wrong units", {
expect_error(
point_offset(lon = -83.0, lat = 44.0, offsetDist = 100,
offsetDir = 'NE', distUnit = 'km'),
"Input attribute 'dirUnit' must be 'm' \\(meters\\) or 'ft' \\(feet\\)\\."
)
})




test_that("No input returns NA", {
# No distance
expect_true(
all(
is.na(
point_offset(lon = -83.0, lat = 44.0, offsetDir = 'NE')
)
)
)

# No direction
expect_true(
all(
is.na(
point_offset(lon = -83.0, lat = 44.0, offsetDist = 100)
)
)
)
})

0 comments on commit 64237d6

Please sign in to comment.