File: helper-expectations.R

package info (click to toggle)
r-cran-skimr 2.1.5%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,188 kB
  • sloc: sh: 13; makefile: 2
file content (109 lines) | stat: -rw-r--r-- 3,746 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Simpler expectations for data frames.

expect_n_columns <- function(object, n) {
  stopifnot(is.numeric(n), length(n) == 1)
  act <- testthat::quasi_label(rlang::enquo(object))
  act$n <- length(act$val)
  testthat::expect(act$n == n, sprintf(
    "%s has %i columns, not %i columns.",
    act$lab, act$n, n
  ))
  invisible(act$val)
}

expect_n_rows <- function(object, n) {
  stopifnot(is.numeric(n), length(n) == 1)
  act <- testthat::quasi_label(rlang::enquo(object))
  act$n <- nrow(act$val)
  testthat::expect(
    act$n == n,
    sprintf("%s has %i rows, not %i rows", act$lab, act$n, n)
  )
  invisible(act$val)
}

expect_NA <- function(object) {
  act <- testthat::quasi_label(rlang::enquo(object))
  testthat::expect(is.na(act$val), sprintf("%s is not NA", act$lab))
  invisible(act$val)
}

expect_print_matches_file <- function(object,
                                      filename,
                                      skip_on_windows = getOption(
                                        "skimr_skip_on_windows",
                                        TRUE
                                      ),
                                      skip_on_cran = getOption(
                                        "skimr_skip_on_cran",
                                        TRUE
                                      ),
                                      width = 100,
                                      update = getOption(
                                        "skimr_update_print",
                                        FALSE
                                      ),
                                      skimr_table_header_width = NULL,
                                      ...) {
  if (skip_on_windows) testthat::skip_on_os("windows")
  if (skip_on_windows) testthat::skip_if_not(l10n_info()$`UTF-8`)
  if (skip_on_cran) testthat::skip_on_cran()
  withr::with_options(list(
    crayon.enabled = FALSE,
    width = width,
    skimr_table_header_width = skimr_table_header_width
  ), {
    testthat::expect_known_output(
      print(object, ...),
      filename,
      update = update,
      width = width
    )
  })
}

expect_matches_file <- function(object,
                                file,
                                update = getOption(
                                  "skimr_update_print",
                                  FALSE
                                ),
                                skip_on_windows = getOption(
                                  "skimr_skip_on_windows",
                                  TRUE
                                ),
                                skip_on_cran = getOption(
                                  "skimr_skip_on_cran",
                                  TRUE
                                ),
                                width = 100,
                                ...) {
  if (skip_on_windows) testthat::skip_on_os("windows")
  if (skip_on_windows) testthat::skip_if_not(l10n_info()$`UTF-8`)
  if (skip_on_cran) testthat::skip_on_cran()
  withr::local_options(list(crayon.enabled = FALSE, width = width))
  act <- testthat::quasi_label(rlang::enquo(object), NULL)

  if (!file.exists(file)) {
    warning("Creating reference value", call. = FALSE)
    writeLines(object, file)
    testthat::succeed()
  } else {
    ref_val <- paste0(readLines(file), collapse = "\n")
    comp <- testthat::compare(as.character(act$val), ref_val, ...)
    if (update && !comp$equal) {
      writeLines(act$val, file)
    }

    testthat::expect(
      comp$equal,
      sprintf(
        "%s has changed from known value recorded in %s.\n%s",
        act$lab, encodeString(file, quote = "'"), comp$message
      ),
      info = NULL
    )
  }

  invisible(act$value)
}