Skip to content

Commit

Permalink
Using snapshot tests for format() and print() methods (#2382)
Browse files Browse the repository at this point in the history
* initial conversion to snapshot tests

* flatten the tests a bit more

* remove redundant tests

* add cat output for format

* don't use snapshot tests for format

* also add a test where `width` is directly passed

---------

Co-authored-by: Michael Chirico <[email protected]>
  • Loading branch information
IndrajeetPatil and MichaelChirico authored Dec 4, 2023
1 parent 9945b10 commit 9146122
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 8 deletions.
108 changes: 108 additions & 0 deletions tests/testthat/_snaps/methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# print.lint, print.lints support optional message wrapping : width = 10

Code
print(lints, width = width)
Output
<text>:1:1:
warning:
[test_linter]
The
quick
brown
fox
jumps
over
the
lazy
dog.
a
^

---

Code
print(lints)
Output
<text>:1:1:
warning:
[test_linter]
The
quick
brown
fox
jumps
over
the
lazy
dog.
a
^

# print.lint, print.lints support optional message wrapping : width = 20

Code
print(lints, width = width)
Output
<text>:1:1:
warning:
[test_linter]
The quick brown
fox jumps over
the lazy dog.
a
^

---

Code
print(lints)
Output
<text>:1:1:
warning:
[test_linter]
The quick brown
fox jumps over
the lazy dog.
a
^

# print.lint, print.lints support optional message wrapping : width = 40

Code
print(lints, width = width)
Output
<text>:1:1: warning: [test_linter] The
quick brown fox jumps over the lazy
dog.
a
^

---

Code
print(lints)
Output
<text>:1:1: warning: [test_linter] The
quick brown fox jumps over the lazy
dog.
a
^

# print.lint, print.lints support optional message wrapping : width = 80

Code
print(lints, width = width)
Output
<text>:1:1: warning: [test_linter] The quick brown fox jumps over the lazy dog.
a
^

---

Code
print(lints)
Output
<text>:1:1: warning: [test_linter] The quick brown fox jumps over the lazy dog.
a
^

7 changes: 6 additions & 1 deletion tests/testthat/test-lint.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ test_that("lint() results do not depend on the position of the .lintr", {
})

test_that("lint uses linter names", {
expect_lint("a = 2", list(linter = "bla"), linters = list(bla = assignment_linter()), parse_settings = FALSE)
expect_lint(
"a = 2",
list(linter = "bla"),
linters = list(bla = assignment_linter()),
parse_settings = FALSE
)
})

test_that("lint() results from file or text should be consistent", {
Expand Down
26 changes: 19 additions & 7 deletions tests/testthat/test-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ local({
lints <- lint(text = "a", linters = test_linter())
lint <- lints[[1L]]

widths <- c(10L, 20L, 40L, 80L)
test_names <- paste0(": width = ", widths)

patrick::with_parameters_test_that(
"print.lint, print.lints support optional message wrapping",
{
expect_snapshot(print(lints, width = width))

withr::with_options(c(lintr.format_width = width), {
expect_snapshot(print(lints))
})
},
.test_name = test_names,
width = widths
)

wrapped_strings <- c(
"[test_linter]\n The\n quick\n brown\n fox\n jumps\n over\n the\n lazy\n dog.",
"[test_linter]\n The quick brown\n fox jumps over\n the lazy dog.",
Expand All @@ -178,22 +194,18 @@ local({
)

patrick::with_parameters_test_that(
"format.lint, format.lints, print.lint, print.lints support optional message wrapping",
"format.lint, format.lints support optional message wrapping",
{
expect_match(format(lint, width = width), wrapped_string, fixed = TRUE)
expect_match(format(lints, width = width), wrapped_string, fixed = TRUE)
expect_output(print(lint, width = width), wrapped_string, fixed = TRUE)
expect_output(print(lints, width = width), wrapped_string, fixed = TRUE)

withr::with_options(c(lintr.format_width = width), {
expect_match(format(lint), wrapped_string, fixed = TRUE)
expect_match(format(lints), wrapped_string, fixed = TRUE)
expect_output(print(lint), wrapped_string, fixed = TRUE)
expect_output(print(lints), wrapped_string, fixed = TRUE)
})
},
.test_name = c(10L, 20L, 40L, 80L),
width = c(10L, 20L, 40L, 80L),
.test_name = test_names,
width = widths,
wrapped_string = wrapped_strings
)
})

0 comments on commit 9146122

Please sign in to comment.