From a7c8630d08fd0f72cca5e28e92bebaafd19244df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kim=20L=C3=B3pez=20G=C3=BCell?= <99121045+KimLopezGuell@users.noreply.github.com> Date: Thu, 15 Aug 2024 21:58:29 +0100 Subject: [PATCH] Convert expect_error() to expect_snapshot() --- tests/testthat/_snaps/conv.md | 8 ++++++++ tests/testthat/_snaps/equal.md | 8 ++++++++ tests/testthat/_snaps/flatten.md | 8 ++++++++ tests/testthat/_snaps/match.md | 13 +++++++++++++ tests/testthat/test-conv.R | 4 +++- tests/testthat/test-equal.R | 4 +++- tests/testthat/test-flatten.R | 4 +++- tests/testthat/test-match.R | 6 ++++-- 8 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tests/testthat/_snaps/conv.md create mode 100644 tests/testthat/_snaps/equal.md create mode 100644 tests/testthat/_snaps/flatten.md diff --git a/tests/testthat/_snaps/conv.md b/tests/testthat/_snaps/conv.md new file mode 100644 index 00000000..759b2235 --- /dev/null +++ b/tests/testthat/_snaps/conv.md @@ -0,0 +1,8 @@ +# check encoding argument + + Code + str_conv("A", c("ISO-8859-1", "ISO-8859-2")) + Condition + Error in `str_conv()`: + ! `encoding` must be a single string, not a character vector. + diff --git a/tests/testthat/_snaps/equal.md b/tests/testthat/_snaps/equal.md new file mode 100644 index 00000000..260a4fca --- /dev/null +++ b/tests/testthat/_snaps/equal.md @@ -0,0 +1,8 @@ +# vectorised using TRR + + Code + str_equal(letters[1:3], c("a", "b")) + Condition + Error in `str_equal()`: + ! Can't recycle `x` (size 3) to match `y` (size 2). + diff --git a/tests/testthat/_snaps/flatten.md b/tests/testthat/_snaps/flatten.md new file mode 100644 index 00000000..8713525d --- /dev/null +++ b/tests/testthat/_snaps/flatten.md @@ -0,0 +1,8 @@ +# collapse must be single string + + Code + str_flatten("A", c("a", "b")) + Condition + Error in `str_flatten()`: + ! `collapse` must be a single string, not a character vector. + diff --git a/tests/testthat/_snaps/match.md b/tests/testthat/_snaps/match.md index bec5c334..7dd542e5 100644 --- a/tests/testthat/_snaps/match.md +++ b/tests/testthat/_snaps/match.md @@ -1,3 +1,16 @@ +# match and match_all fail when pattern is not a regex + + Code + str_match(phones, fixed("3")) + Condition + Error in `str_match()`: + ! `pattern` must be a regular expression. + Code + str_match_all(phones, coll("9")) + Condition + Error in `str_match_all()`: + ! `pattern` must be a regular expression. + # match can't use other modifiers Code diff --git a/tests/testthat/test-conv.R b/tests/testthat/test-conv.R index 44b35937..bb28574a 100644 --- a/tests/testthat/test-conv.R +++ b/tests/testthat/test-conv.R @@ -6,5 +6,7 @@ test_that("encoding conversion works", { }) test_that("check encoding argument", { - expect_error(str_conv("A", c("ISO-8859-1", "ISO-8859-2")), "single string") + expect_snapshot(error = TRUE, + str_conv("A", c("ISO-8859-1", "ISO-8859-2")) + ) }) diff --git a/tests/testthat/test-equal.R b/tests/testthat/test-equal.R index 21e4f09d..1a48e70a 100644 --- a/tests/testthat/test-equal.R +++ b/tests/testthat/test-equal.R @@ -2,7 +2,9 @@ test_that("vectorised using TRR", { expect_equal(str_equal("a", character()), logical()) expect_equal(str_equal("a", "b"), FALSE) expect_equal(str_equal("a", c("a", "b")), c(TRUE, FALSE)) - expect_error(str_equal(letters[1:3], c("a", "b")), "recycle") + expect_snapshot(error = TRUE, + str_equal(letters[1:3], c("a", "b")) + ) }) test_that("can ignore case", { diff --git a/tests/testthat/test-flatten.R b/tests/testthat/test-flatten.R index 6fcbdf60..e19a4a55 100644 --- a/tests/testthat/test-flatten.R +++ b/tests/testthat/test-flatten.R @@ -3,7 +3,9 @@ test_that("equivalent to paste with collapse", { }) test_that("collapse must be single string", { - expect_error(str_flatten("A", c("a", "b")), "single string") + expect_snapshot(error = TRUE, + str_flatten("A", c("a", "b")) + ) }) test_that("last optionally used instead of final separator", { diff --git a/tests/testthat/test-match.R b/tests/testthat/test-match.R index 0398b724..f0960edd 100644 --- a/tests/testthat/test-match.R +++ b/tests/testthat/test-match.R @@ -65,8 +65,10 @@ test_that("multiple match works", { }) test_that("match and match_all fail when pattern is not a regex", { - expect_error(str_match(phones, fixed("3"))) - expect_error(str_match_all(phones, coll("9"))) + expect_snapshot(error = TRUE, { + str_match(phones, fixed("3")) + str_match_all(phones, coll("9")) + }) }) test_that("uses tidyverse recycling rules", {