Skip to content

Commit d134711

Browse files
committed
Refactor into some edition-specific snapshot tests
1 parent 3cde0af commit d134711

File tree

5 files changed

+71
-5
lines changed

5 files changed

+71
-5
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# warning lines are correct after skipping
2+
3+
Code
4+
out3 <- read_csv(I("v1,v2\n\n1,2\n\n3,4"), col_types = "i")
5+
Warning <simpleWarning>
6+
Unnamed `col_types` should have the same length as `col_names`. Using smaller of the two.
7+
2 parsing failures.
8+
row col expected actual file
9+
1 -- 1 columns 2 columns literal data
10+
2 -- 1 columns 2 columns literal data
11+
12+
---
13+
14+
Code
15+
out4 <- read_csv(I("v1,v2\n#foo\n1,2\n#bar\n3,4"), col_types = "i", comment = "#")
16+
Warning <simpleWarning>
17+
Unnamed `col_types` should have the same length as `col_names`. Using smaller of the two.
18+
2 parsing failures.
19+
row col expected actual file
20+
1 -- 1 columns 2 columns literal data
21+
2 -- 1 columns 2 columns literal data
22+
23+
# too few or extra col_types generates warnings
24+
25+
Code
26+
out1 <- read_csv(I("v1,v2\n1,2"), col_types = "i", lazy = FALSE)
27+
Warning <simpleWarning>
28+
Unnamed `col_types` should have the same length as `col_names`. Using smaller of the two.
29+
1 parsing failure.
30+
row col expected actual file
31+
1 -- 1 columns 2 columns literal data
32+
33+
# comments are ignored regardless of where they appear
34+
35+
Code
36+
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc")
37+
Warning <simpleWarning>
38+
1 parsing failure.
39+
row col expected actual file
40+
1 -- 2 columns 1 columns literal data
41+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# comments are ignored regardless of where they appear
2+
3+
Code
4+
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc")
5+

tests/testthat/helper.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ skip_if_edition_second <- function() {
4242
skip_if_edition_first <- function() {
4343
skip_if(edition_first())
4444
}
45+
46+
edition_variant <- function() {
47+
paste0("edition-", edition_get())
48+
}

tests/testthat/test-read-csv.R

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,14 @@ test_that("warning lines are correct after skipping", {
135135

136136
expect_equal(problems(out2)$row, 1)
137137

138-
expect_warning(out3 <- read_csv(I("v1,v2\n\n1,2\n\n3,4"), col_types = "i"))
139-
expect_warning(out4 <- read_csv(I("v1,v2\n#foo\n1,2\n#bar\n3,4"), col_types = "i", comment = "#"))
138+
expect_snapshot(
139+
out3 <- read_csv(I("v1,v2\n\n1,2\n\n3,4"), col_types = "i"),
140+
variant = edition_variant()
141+
)
142+
expect_snapshot(
143+
out4 <- read_csv(I("v1,v2\n#foo\n1,2\n#bar\n3,4"), col_types = "i", comment = "#"),
144+
variant = edition_variant()
145+
)
140146

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

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

159165
test_that("too few or extra col_types generates warnings", {
160166
skip_if_edition_second()
161-
expect_warning(out1 <- read_csv(I("v1,v2\n1,2"), col_types = "i", lazy = FALSE))
167+
expect_snapshot(
168+
out1 <- read_csv(I("v1,v2\n1,2"), col_types = "i", lazy = FALSE),
169+
variant = edition_variant()
170+
)
162171
expect_equal(problems(out1)$expected, "1 columns")
163172
expect_equal(problems(out1)$actual, "2 columns")
164173

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

222-
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc")
231+
expect_snapshot(
232+
out4 <- read_csv(I("x,y\n1,#comment"), comment = "#", col_types = "cc"),
233+
variant = edition_variant()
234+
)
223235
expect_equal(out4$y, NA_character_)
224236

225237
expect_warning(out5 <- read_csv(I("x1,x2,x3\nA2,B2,C2\nA3#,B2,C2\nA4,A5,A6"), comment = "#", lazy = FALSE))

tests/testthat/test-write.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ test_that("roundtrip preserves dates and datetimes", {
5757
})
5858

5959
test_that("fails to create file in non-existent directory", {
60-
expect_error(write_csv(mtcars, file.path(tempdir(), "/x/y")), "open")
60+
expect_error(
61+
expect_warning(
62+
write_csv(mtcars, file.path(tempdir(), "/x/y")), "open"
63+
)
64+
)
6165
})
6266

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

0 commit comments

Comments
 (0)