Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to testthat 3e #949

Merged
merged 21 commits into from
May 31, 2022
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
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ repos:
tests/testthat/exception_handling/empty_file\.R|
tests/testthat/parse_comments/eol_eof_spaces-.*|
tests/testthat/reference-objects/.*|
tests/testthat/_snaps/.*|
)$
- repo: https://github.com/lorenzwalthert/gitignore-tidy
rev: 9dd648a2cbce0c0ce09255695b63ea4823699bec
Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ Suggests:
rmarkdown,
roxygen2,
rstudioapi (>= 0.7),
testthat (>= 2.1.0)
testthat (>= 3.0.0)
VignetteBuilder:
knitr
Encoding: UTF-8
Roxygen: list(markdown = TRUE, roclets = c("rd", "namespace", "collate",
"pkgapi::api_roclet"))
RoxygenNote: 7.1.2
Config/testthat/edition: 3
Collate:
'addins.R'
'communicate.R'
Expand Down
6 changes: 4 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ editor_options:

**Other**

- All (R)md files in this project's source code are now formatted with default pandoc markdown formatter.
This conversion is required when using the visual mode in RStudio (#941).
- Upgrade testing infra to testthat 3e (\@IndrajeetPatil, #949).
- All (R)md files in this project's source code are now formatted with
default pandoc markdown formatter. This conversion is required when using
the visual mode in RStudio (#941).
- Update {pkgdown} action to always build, but only deploy on default branch
(#946).

Expand Down
30 changes: 30 additions & 0 deletions tests/testthat/_snaps/cache-with-r-cache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# cached expressions are displayed propperly

Code
cache_info[, c("n", "size", "last_modified", "activated")]
Output
# A tibble: 1 x 4
n size last_modified activated
<int> <dbl> <dttm> <lgl>
1 0 0 -Inf -Inf FALSE

---

Code
cache_info[, c("n", "size", "activated")]
Output
# A tibble: 1 x 3
n size activated
<int> <dbl> <lgl>
1 1 0 TRUE

---

Code
cache_info[, c("n", "size", "activated")]
Output
# A tibble: 1 x 3
n size activated
<int> <dbl> <lgl>
1 2 0 TRUE

15 changes: 15 additions & 0 deletions tests/testthat/_snaps/helpers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# can construct and print vertical

Code
construct_vertical(c("1 + 1", "nw"))
Output
1 + 1
nw

# can lookup tokens

Code
lookup_new_special()
Output
[1] "SPECIAL-PIPE" "SPECIAL-IN" "SPECIAL-OTHER"

54 changes: 54 additions & 0 deletions tests/testthat/_snaps/public_api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# messages (via cat()) of style_file are correct

Code
cat(catch_style_file_output(file.path("public-api", "xyzdir-dirty",
"dirty-sample-with-scope-tokens.R")), sep = "\n")
Output
Styling 1 files:
dirty-sample-with-scope-tokens.R i
----------------------------------------
Status Count Legend
v 0 File unchanged.
i 1 File changed.
x 0 Styling threw an error.
----------------------------------------
Please review the changes carefully!

---

Code
cat(catch_style_file_output(file.path("public-api", "xyzdir-dirty",
"clean-sample-with-scope-tokens.R")), sep = "\n")
Output
Styling 1 files:
clean-sample-with-scope-tokens.R v
----------------------------------------
Status Count Legend
v 1 File unchanged.
i 0 File changed.
x 0 Styling threw an error.
----------------------------------------

---

Code
cat(catch_style_file_output(file.path("public-api", "xyzdir-dirty",
"dirty-sample-with-scope-spaces.R")), sep = "\n")
Output
Styling 1 files:
dirty-sample-with-scope-spaces.R i
----------------------------------------
Status Count Legend
v 0 File unchanged.
i 1 File changed.
x 0 Styling threw an error.
----------------------------------------
Please review the changes carefully!

# No sensitive to decimal option

Code
style_text("1")
Output
1

9 changes: 9 additions & 0 deletions tests/testthat/_snaps/roundtrip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# correct styling does not give an error

Code
verify_roundtrip("1+1", "1 + 1")

# corrupt styling does give an error

The expression evaluated before the styling is not the same as the expression after styling. This should not happen. Please file a bug report on GitHub (https://github.com/r-lib/styler/issues) using a reprex.

24 changes: 24 additions & 0 deletions tests/testthat/_snaps/utils.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# files with and without blank EOF line are read correctly

Code
read_utf8(test_path("reference-objects/missing-blank-at-EOF.R"))
Output
$text
[1] "x"

$missing_EOF_line_break
[1] TRUE


---

Code
read_utf8(test_path("reference-objects/non-missing-blank-at-EOF.R"))
Output
$text
[1] "x"

$missing_EOF_line_break
[1] FALSE


Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("testing styler on package")


test_that("hi there", {
I(am(a(package(x))))
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("testing styler on package")


test_that("hi there", {
I(am(a(package(x))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("testing styler on package")


test_that("hi there", {
I(am(a(package(x))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("testing styler on package")


test_that("hi there", {
I(am(a(package(x))))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("testing styler on package")


test_that("hi there", {
I(am(a(package(x))))
Expand Down
26 changes: 11 additions & 15 deletions tests/testthat/test-cache-with-r-cache.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,26 @@ test_that("top-level test: Caches top-level expressions efficiently on style_tex


test_that("cached expressions are displayed propperly", {
skip_if(getRversion() < "4.2")

cache_info <- cache_info("testthat", format = "tabular")
expect_known_value(
cache_info[, c("n", "size", "last_modified", "activated")],
file = test_path("reference-objects/cache-info-1"),
update = getOption("styler.test_dir_writable", TRUE)
)
expect_snapshot({
cache_info[, c("n", "size", "last_modified", "activated")]
})

local_test_setup(cache = TRUE)
style_text("1+1")
cache_info <- cache_info(format = "tabular")
cache_info$size <- round(cache_info$size, -2)
expect_known_value(
cache_info[, c("n", "size", "activated")],
file = test_path("reference-objects/cache-info-2"),
update = getOption("styler.test_dir_writable", TRUE)
)
expect_snapshot({
cache_info[, c("n", "size", "activated")]
})
style_text("a <-function() NULL")
cache_info <- cache_info(format = "tabular")
cache_info$size <- round(cache_info$size, -2)
expect_known_value(
cache_info[, c("n", "size", "activated")],
file = test_path("reference-objects/cache-info-3"),
update = getOption("styler.test_dir_writable", TRUE)
)
expect_snapshot({
cache_info[, c("n", "size", "activated")]
})
})


Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-create_token.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("token insertion")


test_that("can create a token that has relevant columns", {
pd_names <- c(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-create_tree.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("test tree creation")


test_that("create_trees outputs identical structure if trees have same structure", {
skip_if_not_installed("DiagrammeR")
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-curly-curly.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("curly-curly")


test_that("curly-culry", {
expect_warning(test_collection("curly-curly",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-encoding.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("non-ASCII characters are handled properly")


test_that("non-ASCII characters are handled properly for text styling", {
expect_equal(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-exception_handling.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("Exception handling")


test_that("style_text returns custom error", {
expect_error(style_text("a <- 3 4"), "unexpected numeric constant")
Expand Down
13 changes: 7 additions & 6 deletions tests/testthat/test-helpers.R
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
context("various helpers")


test_that("can construct and print vertical", {
expect_error(construct_vertical(c("1 + 1", "nw")), NA)
capture.output(
expect_error(construct_vertical(c("1 + 1", "nw")) %>% print(), NA)
)
expect_snapshot({
construct_vertical(c("1 + 1", "nw"))
})
})


Expand All @@ -13,7 +12,9 @@ test_that("file types can be asserted", {
})

test_that("can lookup tokens", {
expect_error(lookup_new_special(), NA)
expect_snapshot({
lookup_new_special()
})
})

test_that("can extend non-comment", {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-identify-roxygen-examples.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("test-roxygen-examples-identify.R")


#' Things to consider:
#' * one function declaration or many
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-indention_curly.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("indent curly brackets")


test_that("indention on one-liner curley only is not changed", {
expect_warning(test_collection("indention_curly_brackets",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-indention_fun_calls.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("test-indention_fun_calls.R")


test_that("edge cases work", {
expect_warning(test_collection("indention_fun_calls",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-indention_multiple.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("test indent multiple")


test_that("multiple round brackets don't cause extraindention", {
expect_warning(test_collection("indention_multiple",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-indention_operators.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("indention operators")


test_that("pipe is indended correctly", {
expect_warning(test_collection("indention_operators",
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-indention_round_brackets.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("Function calls with round brackets")


test_that("one-line function call yields correct indention", {
expect_warning(test_collection("indention_round_brackets",
Expand All @@ -19,7 +19,7 @@ test_that(paste("multi-line function call yields correct indention"), {
## ............................................................................


context("grouping arithmetic expressions with round brackets. ")


# Does NOT cover indention by operators such as +"

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-insertion_comment_interaction.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("test comment token insertion interaction")


## ............................................................................
## strict = TRUE ####
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-line_breaks_and_other.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("linebreaking added / removed correctly")


test_that("line breaks involing curly brackets", {
expect_warning(test_collection("line_breaks_and_other", "curly",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-line_breaks_fun_call.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("line breaks for function calls")

test_that("line breaks work in general", {
expect_warning(test_collection("line_breaks_fun_call",
"token_dependent_mixed",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-math_token_spacing.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("math token spacing")


test_that("invalid tokens return error", {
expect_error(test_collection(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-multiple_expressions.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library("testthat")
context("multiple expressions")


test_that("simple multiple expressions are styled correctly", {
expect_warning(test_collection("multiple_expressions",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-parse_comments.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
library("testthat")
context("correctly treats comments")


test_that("spacing within comments is done correctly", {
expect_warning(test_collection("parse_comments",
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-parsing.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
context("circumvent parsing bugs")


test_that("repreated parsing solves wrong parent assignment", {
expect_warning(
Expand Down
Loading