Skip to content

Commit

Permalink
✅ Add test case for creating coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
NONONOexe committed Sep 3, 2024
1 parent 8639401 commit 6a2f924
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
9 changes: 6 additions & 3 deletions R/create-coordinates.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ create_coordinates <- function(...) {

#' @export
print.coordinates <- function(x, ...) {
cat("{")
cat(apply(x, 1, function(c) paste0("(", paste(c, collapse = ", "), ")")))
cat("}")
coordinates_str <- apply(
x,
1,
function(c) paste0("(", paste0(c, collapse = ", "), ")")
)
cat("{", paste0(coordinates_str, collapse = ", "), "}", sep = "")
}
67 changes: 67 additions & 0 deletions tests/testthat/test-create-coordinates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
test_that(
paste("`create_coordinates()` returns a `coordinates` object with the",
"correct dimensions when given a sequence of x and y coordinates"),
{
expect_equal(
create_coordinates(1, 2, 3, 4),
structure(
matrix(c(1, 2, 3, 4), ncol = 2, byrow = TRUE),
class = "coordinates")
)
}
)

test_that(
paste("`create_coordinates()` returns a `coordinates` object with the ",
"correct dimensions when given a vector of x and y coordinates"),
{
expect_equal(
create_coordinates(c(1, 2, 3, 4)),
structure(
matrix(c(1, 2, 3, 4), ncol = 2, byrow = TRUE),
class = "coordinates"
)
)
}
)

test_that(
paste("`create_coordinates()` returns an empty `coordinates` object when",
"no arguments are provided"),
{
expect_equal(
create_coordinates(),
structure(
matrix(double(), ncol = 2, byrow = TRUE),
class = "coordinates"
)
)
}
)

test_that(
paste("`create_coordinates()` throws an error when an odd number of",
"arguments are provided"),
{
expect_error(
create_coordinates(1, 2, 3),
"arguments must be provided in pairs \\(x, y\\) coordinates"
)
}
)

test_that(
paste("print.coordinates() prints a coordinates object in the correct",
"format"),
{
coordinates <- create_coordinates(1, 2, 3, 4)
expect_output(print(coordinates), "\\{\\(1, 2\\), \\(3, 4\\)\\}")
}
)

test_that(
paste("print.coordinates() prints an empty coordinates object correctly"), {
coordinates <- create_coordinates()
expect_output(print(coordinates), "\\{\\}")
}
)

0 comments on commit 6a2f924

Please sign in to comment.