1local_edition(2) 2 3# expect_known_output ----------------------------------------------------- 4 5test_that("uses specified width", { 6 old <- options(width = 20) 7 on.exit(options(old), add = TRUE) 8 9 x <- 1:100 10 expect_known_output(print(x), "width-80.txt") 11}) 12 13test_that("creates file on first run", { 14 file <- tempfile() 15 expect_success( 16 expect_warning( 17 expect_known_output(cat("ok!\n"), file), 18 "Creating reference" 19 ) 20 ) 21 22 expect_true(file.exists(file)) 23}) 24 25test_that("igores incomplete last line", { 26 file <- tempfile() 27 write_lines("Hi!", file) 28 expect_success(expect_known_output(cat("Hi!"), file)) 29 expect_success(expect_known_output(cat("Hi!\n"), file)) 30 expect_failure(expect_known_output(cat("Hi!\n\n"), file)) 31 expect_failure(expect_known_output(cat("oops"), file)) 32}) 33 34test_that("updates by default", { 35 file <- tempfile() 36 write_lines("Hi!", file) 37 expect_failure(expect_known_output(cat("oops"), file, update = FALSE)) 38 39 expect_equal(read_lines(file), "Hi!") 40 expect_failure(expect_known_output(cat("oops"), file, update = TRUE)) 41 expect_success(expect_known_output(cat("oops"), file)) 42}) 43 44test_that("works in non-UTF-8 locale", { 45 skip_on_cran() 46 text <- c("\u00fc", "\u2a5d", "\u6211", "\u0438") 47 file <- tempfile() 48 write_lines(text, file) 49 50 expect_success(expect_known_output(cat(text, sep = "\n"), file, update = FALSE)) 51 withr::with_locale( 52 c(LC_CTYPE = "C"), 53 { 54 expect_false(l10n_info()$`UTF-8`) 55 expect_success(expect_known_output(cat(text, sep = "\n"), file, update = FALSE)) 56 } 57 ) 58}) 59 60test_that("Warning for non-UTF-8 reference files", { 61 x <- "\xe9\xe1\xed\xf6\xfc" 62 Encoding(x) <- "latin1" 63 64 tmp <- tempfile() 65 on.exit(unlink(tmp), add = TRUE) 66 writeBin(x, tmp) 67 68 suppressWarnings( 69 expect_failure( 70 expect_known_output("foobar", tmp, update = FALSE) 71 ) 72 ) 73}) 74 75 76# expect_known_value ------------------------------------------------------ 77 78 79test_that("correctly matches to a file", { 80 x <- 1 81 expect_success(expect_known_value(x, "one.rds")) 82 83 x <- 2 84 expect_failure(expect_known_value(x, "one.rds", update = FALSE)) 85}) 86 87test_that("first run is successful", { 88 expect_success( 89 expect_warning( 90 expect_known_value(2, "two.rds"), 91 "Creating reference" 92 ) 93 ) 94 unlink("two.rds") 95}) 96 97test_that("equal_to_ref does not overwrite existing", { 98 tmp_rds <- tempfile(fileext=".rds") 99 on.exit(unlink(tmp_rds)) 100 ref_obj1 <- 1:3 101 ref_obj2 <- 2:4 102 saveRDS(ref_obj1, tmp_rds) 103 104 expect_success(expect_equal_to_reference(ref_obj1, tmp_rds)) 105 106 # Failure does not update object 107 expect_failure(expect_equal_to_reference(ref_obj2, tmp_rds)) 108 expect_equal(readRDS(tmp_rds), ref_obj1) 109 110 # Now failure does update object 111 expect_failure(expect_equal_to_reference(ref_obj2, tmp_rds, update=TRUE)) 112 expect_success(expect_equal_to_reference(ref_obj2, tmp_rds)) 113}) 114 115test_that("serializes to version 2 by default", { 116 skip_if(getRversion() < 3.5) 117 tmp_rds <- tempfile(fileext = ".rds") 118 on.exit(unlink(tmp_rds)) 119 120 expect_warning( 121 expect_known_value("a", tmp_rds), 122 "Creating reference" 123 ) 124 125 expect_identical(tools:::get_serialization_version(tmp_rds)[[1]], 2L) 126}) 127 128test_that("version 3 is possible", { 129 skip_if(getRversion() < 3.5) 130 tmp_rds <- tempfile(fileext = ".rds") 131 on.exit(unlink(tmp_rds)) 132 133 expect_warning( 134 expect_known_value("a", tmp_rds, version = 3), 135 "Creating reference" 136 ) 137 138 expect_identical(tools:::get_serialization_version(tmp_rds)[[1]], 3L) 139}) 140 141# expect_known_hash ------------------------------------------------------- 142 143test_that("empty hash succeeds with warning", { 144 expect_success( 145 expect_warning( 146 expect_known_hash(1:10), 147 "No recorded hash" 148 ) 149 ) 150}) 151 152test_that("only succeeds if hash is correct", { 153 expect_success(expect_known_hash(1:10, "c08951d2c2")) 154 expect_failure(expect_known_hash(1:10, "c08951d2c3")) 155}) 156