Skip to content

Commit

Permalink
Refactor should_show_types(); add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jennybc committed Nov 30, 2021
1 parent e592c7d commit cec9413
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 23 deletions.
18 changes: 14 additions & 4 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,22 @@ show_progress <- function() {

#' Determine whether column types should be shown
#'
#' Column types are shown unless
#' - They are disabled by setting `options(readr.show_col_types = FALSE)`
#' - The column types are supplied with the `col_types` argument.
#' Wrapper around `getOption("readr.show_col_types")` that implements some fall
#' back logic if the option is unset. This returns:
#' * `TRUE` if the option is set to `TRUE`
#' * `FALSE` if the option is set to `FALSE`
#' * `FALSE` if the option is unset and we appear to be running tests
#' * `NULL` otherwise, in which case the caller determines whether to show
#' column types based on context, e.g. whether `show_col_types` or actual
#' `col_types` were explicitly specified
#' @export
should_show_types <- function() {
if (identical(getOption("readr.show_col_types", TRUE), FALSE)) {
opt <- getOption("readr.show_col_types", NA)
if (isTRUE(opt)) {
TRUE
} else if (identical(opt, FALSE)) {
FALSE
} else if (is.na(opt) && is_testing()) {
FALSE
} else {
NULL
Expand Down
11 changes: 8 additions & 3 deletions man/should_show_types.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions tests/testthat/_snaps/edition-1/col-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# options(readr.show_col_spec) controls column specifications

Code
out <- read_csv(readr_example("mtcars.csv"))
Message <readr_spec_message>
-- Column specification --------------------------------------------------------
cols(
mpg = col_double(),
cyl = col_double(),
disp = col_double(),
hp = col_double(),
drat = col_double(),
wt = col_double(),
qsec = col_double(),
vs = col_double(),
am = col_double(),
gear = col_double(),
carb = col_double()
)

# `show_col_types` controls column specification

Code
out <- read_csv(readr_example("mtcars.csv"), show_col_types = TRUE)
Message <readr_spec_message>
-- Column specification --------------------------------------------------------
cols(
mpg = col_double(),
cyl = col_double(),
disp = col_double(),
hp = col_double(),
drat = col_double(),
wt = col_double(),
qsec = col_double(),
vs = col_double(),
am = col_double(),
gear = col_double(),
carb = col_double()
)

28 changes: 28 additions & 0 deletions tests/testthat/_snaps/edition-2/col-spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# options(readr.show_col_spec) controls column specifications

Code
out <- read_csv(readr_example("mtcars.csv"))
Message <vroom_dim_message>
Rows: 32 Columns: 11
Message <vroom_spec_message>
-- Column specification --------------------------------------------------------
Delimiter: ","
dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.

# `show_col_types` controls column specification

Code
out <- read_csv(readr_example("mtcars.csv"), show_col_types = TRUE)
Message <vroom_dim_message>
Rows: 32 Columns: 11
Message <vroom_spec_message>
-- Column specification --------------------------------------------------------
Delimiter: ","
dbl (11): mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb
i Use `spec()` to retrieve the full column specification for this data.
i Specify the column types or set `show_col_types = FALSE` to quiet this message.

1 change: 0 additions & 1 deletion tests/testthat/setup.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pre_test_options <- options(
readr.show_col_types = FALSE,
readr.show_progress = FALSE
)
29 changes: 14 additions & 15 deletions tests/testthat/test-col-spec.R
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ test_that("spec object attached to read data", {
)
})


test_that("print(col_spec) works with dates", {
out <- col_spec_standardise("a,b,c\n",
col_types = cols(
Expand Down Expand Up @@ -317,21 +316,21 @@ test_that("as.character() works on col_spec objects", {
expect_equal(as.character(spec), "ddddf")
})

test_that("options(readr.show_col_spec) can turn off showing column specifications", {
skip_if_edition_first()

old <- options("readr.show_col_types")
on.exit(options(old))

options(readr.show_col_types = NULL)
expect_message(
expect_message(
expect_message(
read_csv(readr_example("mtcars.csv"))
)
)
test_that("options(readr.show_col_spec) controls column specifications", {
withr::local_options(list(readr.show_col_types = TRUE))
expect_snapshot(
out <- read_csv(readr_example("mtcars.csv")),
variant = edition_variant()
)

options(readr.show_col_types = FALSE)
withr::local_options(list(readr.show_col_types = FALSE))
expect_silent(read_csv(readr_example("mtcars.csv")))
})

test_that("`show_col_types` controls column specification", {
expect_snapshot(
out <- read_csv(readr_example("mtcars.csv"), show_col_types = TRUE),
variant = edition_variant()
)
expect_silent(read_csv(readr_example("mtcars.csv"), show_col_types = FALSE))
})

0 comments on commit cec9413

Please sign in to comment.