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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
context("print")
test_that("print without color support", {
mockery::stub(print.function, "should_page", FALSE)
mockery::stub(print.function, "can_pretty_print", FALSE)
f <- function() { }
expect_output(
withr::with_options(list(crayon.enabled = TRUE), print.function(f)),
"function[(][)] { }"
)
})
test_that("print without color support 2", {
called <- FALSE
mockery::stub(print.function, "should_page", FALSE)
mockery::stub(print.function, "can_pretty_print", FALSE)
mockery::stub(
print.function,
"base::print.function",
function(...) called <<- TRUE
)
withr::with_options(list(crayon.enabled = TRUE), print.function(ls))
expect_true(called)
})
test_that("print with color support", {
args <- NULL
mockery::stub(print.function, "should_page", FALSE)
mockery::stub(print.function, "can_pretty_print", TRUE)
mockery::stub(print.function, "cat", function(...) args <<- list(...))
f <- function() { 1 + 2 }
withr::with_options(
list(crayon.enabled = TRUE),
print.function(f)
)
expect_true(crayon::has_style(paste(args[[1]], collapse = "\n")))
})
test_that("print with color support 2", {
args <- NULL
mockery::stub(print.function, "should_page", FALSE)
mockery::stub(print.function, "can_pretty_print", TRUE)
mockery::stub(print.function, "cat", function(...) args <<- list(...))
withr::with_options(
list(crayon.enabled = TRUE),
print.function(ls)
)
expect_true(crayon::has_style(paste(args[[1]], collapse = "\n")))
})
test_that("pager", {
cn <- NULL
mockery::stub(print.function, "should_page", TRUE)
mockery::stub(print.function, "can_pretty_print", TRUE)
mockery::stub(print.function, "file.show", function(f) cn <<- readLines(f))
f <- function() { 1 + 2 }
withr::with_options(
list(crayon.enabled = TRUE),
print.function(f)
)
expect_true(crayon::has_style(paste(cn, collapse = "\n")))
})
test_that("can_pretty_print", {
expect_identical(can_pretty_print(), crayon::has_color())
})
test_that("num_lines", {
mockery::stub(num_lines, "system", "42")
expect_equal(num_lines(), 42)
})
test_that("should_page 1", {
mockery::stub(should_page, "is_interactive", FALSE)
expect_false(should_page())
})
test_that("should_page 2", {
mockery::stub(should_page, "is_interactive", FALSE)
expect_false(should_page(1:100))
})
test_that("should_page 3", {
mockery::stub(should_page, "is_interactive", TRUE)
mockery::stub(should_page, "is_terminal", FALSE)
expect_false(should_page(1:100))
})
test_that("should_page 4", {
mockery::stub(should_page, "is_interactive", TRUE)
mockery::stub(should_page, "is_terminal", TRUE)
mockery::stub(should_page, "num_lines", 50)
expect_false(should_page(1:10))
})
test_that("should_page 5", {
mockery::stub(should_page, "is_interactive", TRUE)
mockery::stub(should_page, "is_terminal", TRUE)
mockery::stub(should_page, "num_lines", 50)
expect_true(should_page(1:100))
})
test_that("fallback", {
called <- FALSE
mockery::stub(print.function, "base::print.function",
function(...) called <<- TRUE)
f <- new("function")
body(f) <- substitute(.External(x), list(x = new("externalptr")))
capture_output(print.function(f))
expect_true(called)
})
|