Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion R/expect-comparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ expect_compare_ <- function(

cmp <- op(act$val, exp$val)
if (length(cmp) != 1 || !is.logical(cmp)) {
abort("Result of comparison must be a single logical value")
abort(
"Result of comparison must be a single logical value",
call = trace_env
)
}
if (!isTRUE(cmp)) {
digits <- max(
Expand Down
4 changes: 3 additions & 1 deletion R/skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,12 @@ package_version <- function(x) {
#' should only be run on R versions 4.1.0 and later.
#' @rdname skip
skip_unless_r <- function(spec) {
check_string(spec)

parts <- unlist(strsplit(spec, " ", fixed = TRUE))
if (length(parts) != 2L) {
cli::cli_abort(
"{.arg spec} should be a comparison like '>=' and an R version separated by a space."
"{.arg spec} must be an valid version specification, like {.str >= 4.0.0}, not {.str {spec}}."
)
}
comparator <- match.fun(parts[1L])
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/examples.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# test_examples fails if no examples

Code
test_examples("asdf")
Condition
Error:
! Could not find examples

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/expect-comparison.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,11 @@
`x` is not strictly less than 0.001.
100.000 - 0.001 = 99.999 >= 0

# comparison must yield a single logical

Code
expect_lt(1:10, 5)
Condition
Error in `expect_lt()`:
! Result of comparison must be a single logical value

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/expect-setequal.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# warns if both inputs are named

Code
expect_setequal(c(a = 1), c(b = 1))
Condition
Warning:
expect_setequal() ignores names

# checks inputs

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/reporter-stop.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,11 @@



# stop if needed errors when needed

Code
r$stop_if_needed()
Condition
Error:
! Test failed

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/reporter-zzz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# useful error message if can't find reporter

Code
find_reporter(c("summary", "blah"))
Condition
Error:
! Can not find test reporter blah

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/skip.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@

Reason: On Windows i386

# skip_unless_r works as expected

Code
skip_unless_r("idfjdij")
Condition
Error in `skip_unless_r()`:
! `spec` must be an valid version specification, like ">= 4.0.0", not "idfjdij".

# skip_unless_r gives the expected output

Reason: Current R version (4.5.0) does not satisfy requirement (>= 999.999.999)
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/_snaps/snapshot-file.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# warns on first creation

Code
out <- snapshot_file_equal(tempdir(), "test.txt", NULL, path)
Condition
Warning:
Adding new file snapshot: 'tests/testthat/_snaps/test.txt'

---

Code
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, "doesnt-exist.txt"))
Condition
Error in `snapshot_file_equal()`:
! `doesnt-exist.txt` not found

# snapshot_hint output differs in R CMD check

Code
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/_snaps/test-compiled-code.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# get_routine() fails when no routine exists

Code
get_routine("utils", "no_such_routine")
Condition
Error:
! failed to locate routine 'no_such_routine' in package 'utils'

# validates inputs

Code
Expand Down
40 changes: 40 additions & 0 deletions tests/testthat/_snaps/test-files.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# stops on failure

Code
test_dir(test_path("test_dir"), reporter = "silent")
Condition
Error:
! Test failures

# runs all tests and records output

file context test nb failed skipped error warning passed
Expand All @@ -19,3 +27,35 @@
16 test-helper.R helper test 1 0 FALSE FALSE 0 1
17 test-skip.R Skips skip 1 0 TRUE FALSE 0 0

# complains if no files

Code
test_dir(path)
Condition
Error in `test_dir()`:
! No test files found

# can control if failures generate errors

Code
test_error(stop_on_failure = TRUE)
Condition
Error:
! Test failures

# can control if warnings errors

Code
test_warning(stop_on_warning = TRUE)
Condition
Error:
! Tests generated warnings

# complains if file doesn't exist

Code
test_file("DOESNTEXIST")
Condition
Error:
! `path` does not exist

8 changes: 8 additions & 0 deletions tests/testthat/_snaps/verify-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# can't record plots

Code
verify_output(tempfile(), plot(1:10))
Condition
Error in `FUN()`:
! Plots are not supported

4 changes: 2 additions & 2 deletions tests/testthat/test-edition.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test_that("checks its inputs", {

test_that("deprecation only fired for newer edition", {
local_edition(2)
expect_warning(edition_deprecate(3, "old stuff"), NA)
expect_no_warning(edition_deprecate(3, "old stuff"))

local_edition(3)
expect_snapshot(edition_deprecate(3, "old stuff"))
Expand All @@ -28,7 +28,7 @@ test_that("required only fired for older edition", {

withr::local_options(testthat.edition_ignore = FALSE)
local_edition(3)
expect_error(edition_require(3, "new stuff"), NA)
expect_no_error(edition_require(3, "new stuff"))
})

test_that("edition for testthat is 3", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-examples.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ test_that("test_examples works with installed packages", {

test_that("test_examples fails if no examples", {
withr::local_envvar(TESTTHAT_PKG = "")
expect_error(test_examples("asdf"), "Could not find examples")
expect_snapshot(error = TRUE, test_examples("asdf"))
})
2 changes: 1 addition & 1 deletion tests/testthat/test-expect-comparison.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ test_that("comparisons with more complicated objects work", {
})

test_that("comparison must yield a single logical", {
expect_error(expect_lt(1:10, 5), "single logical")
expect_snapshot(error = TRUE, expect_lt(1:10, 5))
})

test_that("wordy versions are deprecated", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-expect-setequal.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test_that("can compare data frames", {
})

test_that("warns if both inputs are named", {
expect_warning(expect_setequal(c(a = 1), c(b = 1)), "ignores names")
expect_snapshot(expect_setequal(c(a = 1), c(b = 1)))
})

test_that("checks inputs", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-reporter-list.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# regression test: test_file() used to crash with a NULL reporter
test_that("ListReporter with test_file and NULL reporter", {
test_file_path <- 'test-list-reporter/test-exercise-list-reporter.R'
expect_error(test_file(test_path(test_file_path), reporter = NULL), NA)
expect_no_error(test_file(test_path(test_file_path), reporter = NULL))
})

# regression: check that an exception is reported if it is raised in the test file outside
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-reporter-stop.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ test_that("can suppress praise", {

test_that("stop if needed errors when needed", {
r <- StopReporter$new()
expect_error(r$stop_if_needed(), NA)
expect_no_error(r$stop_if_needed())
r$n_fail <- 1
expect_error(r$stop_if_needed(), "Test failed")
expect_snapshot(error = TRUE, r$stop_if_needed())
r$stop_reporter <- FALSE
expect_error(r$stop_if_needed(), NA)
expect_no_error(r$stop_if_needed())
})
7 changes: 3 additions & 4 deletions tests/testthat/test-reporter-zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ test_that("can locate reporter from name", {
})

test_that("useful error message if can't find reporter", {
expect_error(
find_reporter(c("summary", "blah")),
"Can not find test reporter blah"
)
expect_snapshot(error = TRUE, {
find_reporter(c("summary", "blah"))
})
})

test_that("character vector yields multi reporter", {
Expand Down
6 changes: 1 addition & 5 deletions tests/testthat/test-skip.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,7 @@ test_that("skip_unless_r works as expected", {
expect_skip(skip_unless_r("== 0.0.0"))
expect_skip(skip_unless_r("<= 0.0.0"))

expect_error(
skip_unless_r("idfjdij"),
"should be a comparison like '>='",
fixed = TRUE
)
expect_snapshot(error = TRUE, skip_unless_r("idfjdij"))
})

test_that("skip_unless_r gives the expected output", {
Expand Down
18 changes: 6 additions & 12 deletions tests/testthat/test-snapshot-file.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ test_that("expect_snapshot_file works in a different directory", {
brio::write_lines("a", "a.txt", eol = "\r\n")

# expect no warning
expect_warning(
expect_snapshot_file("a.txt"),
regexp = NA
)
expect_no_warning(expect_snapshot_file("a.txt"))
})

test_that("expect_snapshot_file works with variant", {
Expand Down Expand Up @@ -99,21 +96,18 @@ test_that("warns on first creation", {
withr::defer(unlink(file.path(tempdir(), "test.txt")))

# Warns on first run
expect_warning(
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path)),
"new file snapshot"
)
expect_snapshot(out <- snapshot_file_equal(tempdir(), "test.txt", NULL, path))
expect_true(out)

# Errors on non-existing file
expect_error(
expect_snapshot(error = TRUE, {
expect_true(snapshot_file_equal(
tempdir(),
"test.txt",
NULL,
"doesnt-exist.txt"
)),
"`doesnt-exist.txt` not found"
)
))
})

# Unchanged returns TRUE
expect_true(snapshot_file_equal(tempdir(), "test.txt", NULL, path))
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-test-compiled-code.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ test_that("get_routine() finds own 'run_testthat_tests'", {
})

test_that("get_routine() fails when no routine exists", {
expect_error(get_routine("utils", "no_such_routine"))
expect_snapshot(error = TRUE, get_routine("utils", "no_such_routine"))
})

test_that("validates inputs", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-test-env.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test_that("environment has package name", {

setClass("MyClass")
test_that("Cannot create S4 class without special behaviour", {
expect_error(setClass("MyClass2"), NA)
expect_no_error(setClass("MyClass2"))
})

test_that("is_checking respects env var", {
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-test-files.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

test_that("stops on failure", {
withr::local_envvar(TESTTHAT_PARALLEL = "FALSE")
expect_error(
expect_snapshot(error = TRUE, {
test_dir(test_path("test_dir"), reporter = "silent")
)
})
})

test_that("runs all tests and records output", {
Expand All @@ -27,7 +27,7 @@ test_that("complains if no files", {
path <- withr::local_tempfile()
dir.create(path)

expect_error(test_dir(path), "test files")
expect_snapshot(error = TRUE, test_dir(path))
})

test_that("can control if failures generate errors", {
Expand All @@ -36,8 +36,8 @@ test_that("can control if failures generate errors", {
test_dir(test_path("test-error"), reporter = "silent", ...)
}

expect_error(test_error(stop_on_failure = TRUE), "Test failures")
expect_error(test_error(stop_on_failure = FALSE), NA)
expect_snapshot(error = TRUE, test_error(stop_on_failure = TRUE))
expect_no_error(test_error(stop_on_failure = FALSE))
})

test_that("can control if warnings errors", {
Expand All @@ -46,8 +46,8 @@ test_that("can control if warnings errors", {
test_dir(test_path("test-warning"), reporter = "silent", ...)
}

expect_error(test_warning(stop_on_warning = TRUE), "Tests generated warnings")
expect_error(test_warning(stop_on_warning = FALSE), NA)
expect_snapshot(error = TRUE, test_warning(stop_on_warning = TRUE))
expect_no_error(test_warning(stop_on_warning = FALSE))
})

# test_file ---------------------------------------------------------------
Expand All @@ -58,7 +58,7 @@ test_that("can test single file", {
})

test_that("complains if file doesn't exist", {
expect_error(test_file("DOESNTEXIST"), "does not exist")
expect_snapshot(error = TRUE, test_file("DOESNTEXIST"))
})


Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-verify-output.R
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test_that("can record all types of output", {

test_that("can't record plots", {
skip_if(interactive())
expect_error(verify_output(tempfile(), plot(1:10)), "Plots")
expect_snapshot(error = TRUE, verify_output(tempfile(), plot(1:10)))
})

test_that("verify_output() splits condition messages on newlines", {
Expand Down
Loading