I'm writing tests for a function that has an option to use alternate end-of-line characters, and had a bit of fun writing a test for this.
ff <- function(a, eol="\n") { l <- paste(collapse=" ", rep(a, 5)); cat(file="", l, eol, l, eol) }
ff(3)
Testing against a \n end-of-line with expect_output works fine.
test_that("demo", { expect_output(ff(3), "3 3 \n 3 3") })
When I change end-of-line to \r and test against that with expect_output, I get odd output as if there's some buffer contents left over. It looks like the output of the previous test, followed by the match pattern of the current test with the pairs of 3s separated by carriage-return.
test_that("demo", { expect_output(ff(3, eol="\r"), "3 3 \r 3 3") })
Error: Test failed: 'demo'
3 3'. Actual value: "3 3 3 3 3 \n 3 3 3 3 3 ".'3 3
I can get the test I want if the function is wrapped with capture.output and tested with expect_match.
test_that("demo", { expect_match(capture.output(ff(3, eol="\r")), "3 3 \r 3 3") })
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] readGenalex_0.9.9000 testthat_0.9.1
loaded via a namespace (and not attached):
[1] devtools_1.7.0 Rcpp_0.11.4 roxygen2_4.1.0 stringr_0.6.2 tools_3.1.2
I'm writing tests for a function that has an option to use alternate end-of-line characters, and had a bit of fun writing a test for this.
Testing against a
\nend-of-line withexpect_outputworks fine.When I change end-of-line to
\rand test against that withexpect_output, I get odd output as if there's some buffer contents left over. It looks like the output of the previous test, followed by the match pattern of the current test with the pairs of 3s separated by carriage-return.I can get the test I want if the function is wrapped with
capture.outputand tested withexpect_match.