Skip to content

with_mock() function#160

Merged
hadley merged 29 commits intor-lib:masterfrom
krlmlr:159-mocking
Sep 17, 2014
Merged

with_mock() function#160
hadley merged 29 commits intor-lib:masterfrom
krlmlr:159-mocking

Conversation

@krlmlr
Copy link
Copy Markdown
Member

@krlmlr krlmlr commented Aug 27, 2014

As discussed in #159. Interface as suggested by @hadley, supports nesting, mocking more than one function, not mocking at all, and returning a value.

Early draft, with tests and a documentation stub. Comments are welcome.

Fixes #159.

Comment thread R/mock.r Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we got rid of the env argument and made it base::all.equal = ...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a neat idea, but there's a catch:

> list(a::b = 5)
Error: unexpected '=' in "list(a::b ="
> list(`a::b` = 5)
$`a::b`
[1] 5

So it'd have to be ``base::all.equal = ... . But how about:

with_mock({ ... }, list(compare = ...), base = list(all.equal = ...))

?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, I meant to say that but forget that I needed multiple backticks. Still, I think that's better than a named list for each environment. Most of time you'll only be mocking one function in one.env so that use case needs to be concise.

@krlmlr krlmlr mentioned this pull request Aug 27, 2014
@krlmlr
Copy link
Copy Markdown
Member Author

krlmlr commented Sep 5, 2014

Should we do a second review round?

Comment thread R/mock.r Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a style like this would look better?

with_mock(
  `base::all.equal` = function(x, y, ...) TRUE,
  {
    expect_equal(3, 5),
  }
)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tried this: with_mock <- function(..., .code, .env = topenv()) { ... }

Then it only works when I explicitly use the .code parameter:

with_mock(
  `base::all.equal` = function(x, y, ...) TRUE,
  .code = {
    expect_equal(3, 5),
  }
)

Otherwise, the code is part of ..., and nothing is assigned to .code.

I probably could make it "just work" by using the unnamed parameters of ... as code to be mocked. This would also permit

with_mock(
  `base::all.equal` = function(x, y, ...) TRUE,
  expect_equal(2 * 3, 4),
  expect_equal(3 * 3, 6),
  expect_equal(3 * 3 + 4, 9)
)

However, I haven't captured unevaluated ... before.

What do you think? Is it ok to import pryr?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually rather like that idea. But you don't need to import pryr, just inline the dots function.

@krlmlr
Copy link
Copy Markdown
Member Author

krlmlr commented Sep 17, 2014

@hadley: I have incorporated your comments except for the set pattern. Thanks for your feedback.

Comment thread R/mock.r
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this will work - if nothing is named then mock_qual_names() will be NULL

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my system:

> NULL == ""
logical(0)
> all(NULL == "")
[1] TRUE

True, this is not very explicit -- but this code path is tested in the tests. Do you prefer an explicit check for NULL?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, that's fine.

hadley added a commit that referenced this pull request Sep 17, 2014
@hadley hadley merged commit 397345c into r-lib:master Sep 17, 2014
@hadley
Copy link
Copy Markdown
Member

hadley commented Sep 17, 2014

Thanks!

@krlmlr krlmlr deleted the 159-mocking branch September 17, 2014 22:35
krlmlr pushed a commit to krlmlr/testthat that referenced this pull request Sep 18, 2014
krlmlr pushed a commit to krlmlr/testthat that referenced this pull request Sep 18, 2014
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

Successfully merging this pull request may close these issues.

Mocking

2 participants