-
Notifications
You must be signed in to change notification settings - Fork 5
Expect_called fn addition #20
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
Open
danilito19
wants to merge
8
commits into
robertzk:master
Choose a base branch
from
danilito19:exp-called
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
24078df
new expect called fn with tests, also change to gitignore
danilito19 2e237bf
commented out one test
danilito19 2f68271
add duplicate to ttsm
danilito19 dc33a25
ran document
danilito19 9d8cf20
o and gitignore
danilito19 bcd9c02
gitignore for real this time
danilito19 2f43b9a
change to as.date
danilito19 b1cefc1
ran doc
danilito19 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| ..Rcheck | ||
| *.Rcheck | ||
| *.tar.gz | ||
| .DS_Store | ||
| src/*.o | ||
| src/*.so |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #' Duplicate a function. Returns a carbon copy of the original, so that original | ||
| #' can be modified safely. | ||
| #' | ||
| #' @param x function. | ||
| #' @examples | ||
| #' \dontrun{ | ||
| #' fn <- function(x) cat(x, "\n") | ||
| #' fn2 <- duplicate(fn) | ||
| #' fn <- function(x) cat(x, "woof","\n") | ||
| #' fn("hello") # hellowoof | ||
| #' fn2("hello") # hello | ||
| #' } | ||
| #' @export | ||
| #' @useDynLib testthatsomemore duplicate_testthatsomemore_ | ||
| duplicate <- function(x) { | ||
| .Call(duplicate_testthatsomemore_, x) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #' Expect that a function was called while executing an expression. | ||
| #' | ||
| #' @param fn function. The function that was expected to get called. If not | ||
| #' specified with \code{::}, package_name param must be specified. | ||
| #' @param expr expression. The expression to execute. | ||
| #' @param package_name string. The package where fn lives. If not specified, fn must | ||
| #' be package_name::fn. | ||
| #' @param was_called logical. If \code{FALSE}, negate the meaning: \code{fn} | ||
| #' should \emph{not} have been called. By default \code{TRUE}. | ||
| #' @return invisible \code{TRUE}. | ||
| #' @examples \dontrun{ | ||
| #' expect_called(base::print, print("Hello")) # Will pass | ||
| #' expect_called(print, print("Hello"), "base") # Will pass | ||
| #' expect_called(base::print, cat("Hello")) # Will fail | ||
| #' expect_called(base::print, cat("Hello"), was_called = FALSE) # Will pass | ||
| #' } | ||
| expect_called <- function(fn, expr, package_name, was_called = TRUE) { | ||
| fn <- substitute(fn) | ||
| if (is.call(fn) && identical(as.character(fn[[1]]), "not")) { | ||
| fn <- fn[[2]] | ||
| was_called <- FALSE | ||
| } | ||
| # Turn foo into package_name::foo | ||
| if (is.name(substitute(fn))) fn <- bquote(getFromNamespace(.(deparse(fn)), .(package_name))) | ||
| else fn <- as.call(list(quote(getFromNamespace), as.character(fn[[3]]), as.character(fn[[2]]))) | ||
|
|
||
| grab <- function(i) as.character(as.list(fn)[[i]]) | ||
|
|
||
| copy_of_fn <- duplicate(eval(fn)) | ||
| env <- list2env(list(called = 0), parent = emptyenv()) | ||
|
|
||
| mocked_fn <- function(...) { | ||
| env$called <- env$called + 1 | ||
| copy_of_fn(...) | ||
| } | ||
|
|
||
| result <- testthatsomemore::package_stub(grab(3), grab(2), mocked_fn, force(expr)) | ||
| expect_identical(was_called, as.logical(env$called)) | ||
|
|
||
| invisible(TRUE) | ||
| } | ||
|
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //shamelessly ripped off from @jimhester | ||
| #define USE_RINTERNALS | ||
| #include <R.h> | ||
| #include <Rdefines.h> | ||
| #include <R_ext/Error.h> | ||
|
|
||
| SEXP duplicate_testthatsomemore_(SEXP x) { | ||
| return duplicate(x); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| context("duplicate") | ||
|
|
||
| test_that('it duplicates a function', { | ||
| fn <- function(x) cat(x, "\n") | ||
| fn_copy <- duplicate(fn) | ||
| expect_equal(fn, fn_copy) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| context('expect_called') | ||
|
|
||
|
|
||
| test_that('it finds called function', { | ||
| f <- function() sum(2, 2) | ||
| expect_true(expect_called(base::sum, f())) | ||
| }) | ||
|
|
||
| test_that('it detects function was not called', { | ||
| f <- function() 10 | ||
| expect_true(expect_called(base::sum, f(), was_called = FALSE )) | ||
| }) | ||
|
|
||
| test_that('Package setting works', { | ||
| f <- function() sum(1, 2) | ||
| expect_true(expect_called(sum, f(), "base")) | ||
| }) | ||
|
|
||
| # neither test_that nor describe wrappers work here? | ||
| # test_that('it detects errors when function is expected but actually not called', { | ||
| # f <- function() 10 | ||
| # expect_error( | ||
| # expect_called(base::sum, f() ), | ||
| # "is not identical" | ||
| # ) | ||
| # }) | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@danilito19 What was the problem?