| 1 |
test_that("with_output_sink works as expected", { |
| 2 |
tmp <- tempfile() |
| 3 |
on.exit(unlink(tmp), add = TRUE) |
| 4 |
tmp2 <- tempfile() |
| 5 |
on.exit(unlink(tmp2), add = TRUE) |
| 6 |
tmp3 <- tempfile() |
| 7 |
on.exit(unlink(tmp3), add = TRUE) |
| 8 |
|
| 9 |
expect_identical(sink.number(), 0L) |
| 10 |
|
| 11 |
with_output_sink(tmp, { |
| 12 |
expect_identical(sink.number(), 1L) |
| 13 |
cat("output\n") |
| 14 |
}) |
| 15 |
expect_identical(readLines(tmp), "output") |
| 16 |
|
| 17 |
expect_identical(sink.number(), 0L) |
| 18 |
|
| 19 |
with_output_sink(tmp, append = TRUE, { |
| 20 |
expect_identical(sink.number(), 1L) |
| 21 |
cat("output 2\n") |
| 22 |
}) |
| 23 |
expect_identical(readLines(tmp), c("output", "output 2")) |
| 24 |
|
| 25 |
expect_identical(sink.number(), 0L) |
| 26 |
|
| 27 |
expect_warning( |
| 28 |
with_output_sink(tmp, { |
| 29 |
sink() |
| 30 |
}), |
| 31 |
"already removed" |
| 32 |
) |
| 33 |
|
| 34 |
expect_identical(sink.number(), 0L) |
| 35 |
|
| 36 |
expect_error( |
| 37 |
with_output_sink(NULL, { |
| 38 |
NULL |
| 39 |
}), |
| 40 |
"cannot be NULL" |
| 41 |
) |
| 42 |
|
| 43 |
expect_identical(sink.number(), 0L) |
| 44 |
|
| 45 |
}) |