Skip to content

Commit

Permalink
Refactor into some edition-specific snapshot tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Nov 30, 2021
1 parent 3cde0af commit d134711
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 5 deletions.
41 changes: 41 additions & 0 deletions tests/testthat/_snaps/edition-1/read-csv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# warning lines are correct after skipping

Code
out3 <- read_csv(I("v1,v2\n\n1,2\n\n3,4"), col_types = "i")
Warning <simpleWarning>
Unnamed `col_types` should have the same length as `col_names`. Using smaller of the two.
2 parsing failures.
row col expected actual file
1 -- 1 columns 2 columns literal data
2 -- 1 columns 2 columns literal data

---

Code
out4 <- read_csv(I("v1,v2\n#foo\n1,2\n#bar\n3,4"), col_types = "i", comment = "#")
Warning <simpleWarning>
Unnamed `col_types` should have the same length as `col_names`. Using smaller of the two.
2 parsing failures.
row col expected actual file
1 -- 1 columns 2 columns literal data
2 -- 1 columns 2 columns literal data

# too few or extra col_types generates warnings

Code
out1 <- read_csv(I("v1,v2\n1,2"), col_types = "i", lazy = FALSE)
Warning <simpleWarning>
Unnamed `col_types` should have the same length as `col_names`. Using smaller of the two.
1 parsing failure.
row col expected actual file
1 -- 1 columns 2 columns literal data

# comments are ignored regardless of where they appear

Code
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc")
Warning <simpleWarning>
1 parsing failure.
row col expected actual file
1 -- 2 columns 1 columns literal data

5 changes: 5 additions & 0 deletions tests/testthat/_snaps/edition-2/read-csv.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# comments are ignored regardless of where they appear

Code
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc")

4 changes: 4 additions & 0 deletions tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ skip_if_edition_second <- function() {
skip_if_edition_first <- function() {
skip_if(edition_first())
}

edition_variant <- function() {
paste0("edition-", edition_get())
}
20 changes: 16 additions & 4 deletions tests/testthat/test-read-csv.R
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,14 @@ test_that("warning lines are correct after skipping", {

expect_equal(problems(out2)$row, 1)

expect_warning(out3 <- read_csv(I("v1,v2\n\n1,2\n\n3,4"), col_types = "i"))
expect_warning(out4 <- read_csv(I("v1,v2\n#foo\n1,2\n#bar\n3,4"), col_types = "i", comment = "#"))
expect_snapshot(
out3 <- read_csv(I("v1,v2\n\n1,2\n\n3,4"), col_types = "i"),
variant = edition_variant()
)
expect_snapshot(
out4 <- read_csv(I("v1,v2\n#foo\n1,2\n#bar\n3,4"), col_types = "i", comment = "#"),
variant = edition_variant()
)

expect_equal(problems(out3)$row, c(1, 2))

Expand All @@ -158,7 +164,10 @@ test_that("extra columns generates warnings", {

test_that("too few or extra col_types generates warnings", {
skip_if_edition_second()
expect_warning(out1 <- read_csv(I("v1,v2\n1,2"), col_types = "i", lazy = FALSE))
expect_snapshot(
out1 <- read_csv(I("v1,v2\n1,2"), col_types = "i", lazy = FALSE),
variant = edition_variant()
)
expect_equal(problems(out1)$expected, "1 columns")
expect_equal(problems(out1)$actual, "2 columns")

Expand Down Expand Up @@ -219,7 +228,10 @@ test_that("comments are ignored regardless of where they appear", {
expect_equal(out2$x, 1)
expect_equal(out3$x, 1)

out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc")
expect_snapshot(
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc"),
variant = edition_variant()
)
expect_equal(out4$y, NA_character_)

expect_warning(out5 <- read_csv(I("x1,x2,x3\nA2,B2,C2\nA3#,B2,C2\nA4,A5,A6"), comment = "#", lazy = FALSE))
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-write.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ test_that("roundtrip preserves dates and datetimes", {
})

test_that("fails to create file in non-existent directory", {
expect_error(write_csv(mtcars, file.path(tempdir(), "/x/y")), "open")
expect_error(
expect_warning(
write_csv(mtcars, file.path(tempdir(), "/x/y")), "open"
)
)
})

test_that("write_excel_csv/csv2 includes a byte order mark", {
Expand Down

0 comments on commit d134711

Please sign in to comment.