Skip to content

expect_output result unusual when output not actually multiline #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
douglasgscofield opened this issue Feb 23, 2015 · 1 comment
Closed
Milestone

Comments

@douglasgscofield
Copy link

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)
3 3 3 3 3
 3 3 3 3 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
@hadley hadley modified the milestone: 0.11.0 Sep 29, 2015
@hadley hadley closed this as completed in c133257 Sep 29, 2015
@hadley
Copy link
Member

hadley commented Sep 29, 2015

I couldn't reproduce your problem exactly, but I'm pretty sure this fixes the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants