{"action":"created","comment":{"url":"https://api.github.com/repos/hadley/testthat/pulls/comments/69731378","id":69731378,"diff_hunk":"@@ -0,0 +1,174 @@\n+\n+#' Create a mock object.\n+#'\n+#' Mock object's primary use is to record calls that are made on the\n+#' mocked function.\n+#'\n+#' Optionally values/expressions can be passed via \\code{...} for the\n+#' mock object to return them upon subsequent calls. Expressions are\n+#' evaluated in environment \\code{envir} before being returned. If no\n+#' value is passed in \\code{...} then \\code{NULL} is returned.\n+#'\n+#' Passing an expression or a function call via \\code{...} is also a\n+#' way to implement side effects: keep track of the state of code\n+#' under testing, throw an exception when a condition is met, etc.\n+#'\n+#' @param ... Values returned upon subsequent calls.\n+#' @param cycle Whether to cycle over the return values. If \\code{FALSE},\n+#'        will fail if called too many times.\n+#' @param envir Where to evaluate the expressions being returned.\n+#' @return A mock object.\n+#'\n+#' @export\n+#' @examples\n+#' m <- mock(1)\n+#' with_mock(summary = m, {\n+#'   expect_equal(summary(iris), 1)\n+#'   expect_length(m, 1)\n+#'   expect_call(m, 1, summary(iris))\n+#'   expect_args(m, 1, iris)\n+#' })\n+#'\n+mock <- function (..., cycle = FALSE, envir = parent.frame()) {\n+  stopifnot(is.environment(envir))\n+\n+  return_values     <- eval(substitute(alist(...)))\n+  return_values_env <- envir\n+  call_no           <- 0\n+  calls             <- list()\n+  args              <- list()\n+\n+  mock_impl <- function (...)\n+  {\n+    call_no <<- call_no + 1\n+    calls[[call_no]] <<- match.call()\n+\n+    args[[call_no]] <<- list(...)\n+\n+    if (length(return_values)) {\n+      if (call_no > length(return_values) && !cycle)\n+        fail(\"too many calls to mock object and cycle set to FALSE\")\n+\n+      value <- return_values[[(call_no - 1) %% length(return_values) + 1]]\n+      return(eval(value, envir = return_values_env))\n+    }\n+\n+    # TODO maybe it should the mock object itself?\n+    invisible(NULL)\n+  }\n+\n+  class(mock_impl) <- 'mock'\n+  mock_impl\n+}\n+\n+\n+is_mock <- function (object) inherits(object, 'mock')\n+\n+\n+#' \\code{$.mock} returns one of two lists: \\code{calls} or \\code{args}.\n+#' @param m A \\code{\\link{mock}} object.\n+#' @param n List name.\n+#' @return \\code{calls} is a list of call signatures and \\code{args} is\n+#'         a respective list of lists of arguments' values.\n+#' @rdname mock\n+#' @export\n+#' @examples\n+#' m <- mock()\n+#' m(x = 1)\n+#' m(y = 2)\n+#' expect_equal(length(m), 2)\n+#' expect_equal(m$calls[[1]], quote(m(x = 1)))\n+#' expect_equal(m$calls[[2]], quote(m(y = 2)))\n+`$.mock` <- function (m, n) {\n+  stopifnot(is_mock(m))\n+  switch(n,\n+         calls = environment(m)$calls,\n+         args  = environment(m)$args,\n+         fail(paste(\"unknown member:\", n)))\n+}\n+\n+\n+#' @rdname mock\n+#' @param x A \\code{mock} object.\n+#' @return Number of calls invoked on \\code{m}.\n+#' @export\n+length.mock <- function (x)\n+{\n+  length(environment(x)$calls)\n+}\n+\n+\n+#' Expectation: does the given call match the expected?\n+#'\n+#' @inheritParams expect_that\n+#' @param mock_object A \\code{\\link{mock}} object.\n+#' @param n Call number.\n+#' @param expected_call Expected call.\n+#' @family expectations\n+#' @export\n+#' @examples\n+#' \\dontrun{\n+#' m <- mock()\n+#' with_mock(summary = m, summary(iris))\n+#' expect_call(m, 1, summary(iris))\n+#' }\n+expect_call <- function (mock_object, n, expected_call) {\n+  stopifnot(is_mock(mock_object))\n+\n+  expect(\n+    0 < n && n <= length(mock_object),\n+    sprintf(\"call number %s not found in mock object\", toString(n))\n+  )\n+\n+  expected_call <- substitute(expected_call)\n+  mocked_call <- mock_object$calls[[n]]\n+\n+  expect(\n+    mocked_call == expected_call,","path":"R/mock-object.R","position":127,"original_position":127,"commit_id":"c092e5c99296594bdb58c708950b5eced33560a1","original_commit_id":"c092e5c99296594bdb58c708950b5eced33560a1","user":{"login":"hadley","id":4196,"avatar_url":"https://avatars.githubusercontent.com/u/4196?v=3","gravatar_id":"","url":"https://api.github.com/users/hadley","html_url":"https://github.com/hadley","followers_url":"https://api.github.com/users/hadley/followers","following_url":"https://api.github.com/users/hadley/following{/other_user}","gists_url":"https://api.github.com/users/hadley/gists{/gist_id}","starred_url":"https://api.github.com/users/hadley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadley/subscriptions","organizations_url":"https://api.github.com/users/hadley/orgs","repos_url":"https://api.github.com/users/hadley/repos","events_url":"https://api.github.com/users/hadley/events{/privacy}","received_events_url":"https://api.github.com/users/hadley/received_events","type":"User","site_admin":false},"body":"`==` is vectorised - you should be using `identical()` to compare calls here.","created_at":"2016-07-06T13:41:00Z","updated_at":"2016-07-06T13:41:00Z","html_url":"https://github.com/hadley/testthat/pull/472#discussion_r69731378","pull_request_url":"https://api.github.com/repos/hadley/testthat/pulls/472","_links":{"self":{"href":"https://api.github.com/repos/hadley/testthat/pulls/comments/69731378"},"html":{"href":"https://github.com/hadley/testthat/pull/472#discussion_r69731378"},"pull_request":{"href":"https://api.github.com/repos/hadley/testthat/pulls/472"}}},"pull_request":{"url":"https://api.github.com/repos/hadley/testthat/pulls/472","id":69773739,"html_url":"https://github.com/hadley/testthat/pull/472","diff_url":"https://github.com/hadley/testthat/pull/472.diff","patch_url":"https://github.com/hadley/testthat/pull/472.patch","issue_url":"https://api.github.com/repos/hadley/testthat/issues/472","number":472,"state":"open","locked":false,"title":"adding a mock object API","user":{"login":"lbartnik","id":5261280,"avatar_url":"https://avatars.githubusercontent.com/u/5261280?v=3","gravatar_id":"","url":"https://api.github.com/users/lbartnik","html_url":"https://github.com/lbartnik","followers_url":"https://api.github.com/users/lbartnik/followers","following_url":"https://api.github.com/users/lbartnik/following{/other_user}","gists_url":"https://api.github.com/users/lbartnik/gists{/gist_id}","starred_url":"https://api.github.com/users/lbartnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lbartnik/subscriptions","organizations_url":"https://api.github.com/users/lbartnik/orgs","repos_url":"https://api.github.com/users/lbartnik/repos","events_url":"https://api.github.com/users/lbartnik/events{/privacy}","received_events_url":"https://api.github.com/users/lbartnik/received_events","type":"User","site_admin":false},"body":"this is a proposal of a new functionality in testthat - mock objects;\r\nI haven't really made it complete yet and it is meant to serve as an example of what could be done\r\n\r\nmy goal here is to see whether this is interesting for other developers at all ","created_at":"2016-05-12T03:11:38Z","updated_at":"2016-07-06T13:41:00Z","closed_at":null,"merged_at":null,"merge_commit_sha":"50455b5f38e8a78e6aa6c66e4a8d7bf6dc5b756a","assignee":null,"assignees":[],"milestone":{"url":"https://api.github.com/repos/hadley/testthat/milestones/2","html_url":"https://github.com/hadley/testthat/milestones/1.0.3","labels_url":"https://api.github.com/repos/hadley/testthat/milestones/2/labels","id":1862229,"number":2,"title":"1.0.3","description":"Issues and PRs for point release","creator":{"login":"krlmlr","id":1741643,"avatar_url":"https://avatars.githubusercontent.com/u/1741643?v=3","gravatar_id":"","url":"https://api.github.com/users/krlmlr","html_url":"https://github.com/krlmlr","followers_url":"https://api.github.com/users/krlmlr/followers","following_url":"https://api.github.com/users/krlmlr/following{/other_user}","gists_url":"https://api.github.com/users/krlmlr/gists{/gist_id}","starred_url":"https://api.github.com/users/krlmlr/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/krlmlr/subscriptions","organizations_url":"https://api.github.com/users/krlmlr/orgs","repos_url":"https://api.github.com/users/krlmlr/repos","events_url":"https://api.github.com/users/krlmlr/events{/privacy}","received_events_url":"https://api.github.com/users/krlmlr/received_events","type":"User","site_admin":false},"open_issues":9,"closed_issues":6,"state":"open","created_at":"2016-07-04T01:10:22Z","updated_at":"2016-07-06T07:33:10Z","due_on":null,"closed_at":null},"commits_url":"https://api.github.com/repos/hadley/testthat/pulls/472/commits","review_comments_url":"https://api.github.com/repos/hadley/testthat/pulls/472/comments","review_comment_url":"https://api.github.com/repos/hadley/testthat/pulls/comments{/number}","comments_url":"https://api.github.com/repos/hadley/testthat/issues/472/comments","statuses_url":"https://api.github.com/repos/hadley/testthat/statuses/c092e5c99296594bdb58c708950b5eced33560a1","head":{"label":"lbartnik:master","ref":"master","sha":"c092e5c99296594bdb58c708950b5eced33560a1","user":{"login":"lbartnik","id":5261280,"avatar_url":"https://avatars.githubusercontent.com/u/5261280?v=3","gravatar_id":"","url":"https://api.github.com/users/lbartnik","html_url":"https://github.com/lbartnik","followers_url":"https://api.github.com/users/lbartnik/followers","following_url":"https://api.github.com/users/lbartnik/following{/other_user}","gists_url":"https://api.github.com/users/lbartnik/gists{/gist_id}","starred_url":"https://api.github.com/users/lbartnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lbartnik/subscriptions","organizations_url":"https://api.github.com/users/lbartnik/orgs","repos_url":"https://api.github.com/users/lbartnik/repos","events_url":"https://api.github.com/users/lbartnik/events{/privacy}","received_events_url":"https://api.github.com/users/lbartnik/received_events","type":"User","site_admin":false},"repo":{"id":58026848,"name":"testthat","full_name":"lbartnik/testthat","owner":{"login":"lbartnik","id":5261280,"avatar_url":"https://avatars.githubusercontent.com/u/5261280?v=3","gravatar_id":"","url":"https://api.github.com/users/lbartnik","html_url":"https://github.com/lbartnik","followers_url":"https://api.github.com/users/lbartnik/followers","following_url":"https://api.github.com/users/lbartnik/following{/other_user}","gists_url":"https://api.github.com/users/lbartnik/gists{/gist_id}","starred_url":"https://api.github.com/users/lbartnik/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lbartnik/subscriptions","organizations_url":"https://api.github.com/users/lbartnik/orgs","repos_url":"https://api.github.com/users/lbartnik/repos","events_url":"https://api.github.com/users/lbartnik/events{/privacy}","received_events_url":"https://api.github.com/users/lbartnik/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/lbartnik/testthat","description":"An R package to make testing fun","fork":true,"url":"https://api.github.com/repos/lbartnik/testthat","forks_url":"https://api.github.com/repos/lbartnik/testthat/forks","keys_url":"https://api.github.com/repos/lbartnik/testthat/keys{/key_id}","collaborators_url":"https://api.github.com/repos/lbartnik/testthat/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/lbartnik/testthat/teams","hooks_url":"https://api.github.com/repos/lbartnik/testthat/hooks","issue_events_url":"https://api.github.com/repos/lbartnik/testthat/issues/events{/number}","events_url":"https://api.github.com/repos/lbartnik/testthat/events","assignees_url":"https://api.github.com/repos/lbartnik/testthat/assignees{/user}","branches_url":"https://api.github.com/repos/lbartnik/testthat/branches{/branch}","tags_url":"https://api.github.com/repos/lbartnik/testthat/tags","blobs_url":"https://api.github.com/repos/lbartnik/testthat/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/lbartnik/testthat/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/lbartnik/testthat/git/refs{/sha}","trees_url":"https://api.github.com/repos/lbartnik/testthat/git/trees{/sha}","statuses_url":"https://api.github.com/repos/lbartnik/testthat/statuses/{sha}","languages_url":"https://api.github.com/repos/lbartnik/testthat/languages","stargazers_url":"https://api.github.com/repos/lbartnik/testthat/stargazers","contributors_url":"https://api.github.com/repos/lbartnik/testthat/contributors","subscribers_url":"https://api.github.com/repos/lbartnik/testthat/subscribers","subscription_url":"https://api.github.com/repos/lbartnik/testthat/subscription","commits_url":"https://api.github.com/repos/lbartnik/testthat/commits{/sha}","git_commits_url":"https://api.github.com/repos/lbartnik/testthat/git/commits{/sha}","comments_url":"https://api.github.com/repos/lbartnik/testthat/comments{/number}","issue_comment_url":"https://api.github.com/repos/lbartnik/testthat/issues/comments{/number}","contents_url":"https://api.github.com/repos/lbartnik/testthat/contents/{+path}","compare_url":"https://api.github.com/repos/lbartnik/testthat/compare/{base}...{head}","merges_url":"https://api.github.com/repos/lbartnik/testthat/merges","archive_url":"https://api.github.com/repos/lbartnik/testthat/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/lbartnik/testthat/downloads","issues_url":"https://api.github.com/repos/lbartnik/testthat/issues{/number}","pulls_url":"https://api.github.com/repos/lbartnik/testthat/pulls{/number}","milestones_url":"https://api.github.com/repos/lbartnik/testthat/milestones{/number}","notifications_url":"https://api.github.com/repos/lbartnik/testthat/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/lbartnik/testthat/labels{/name}","releases_url":"https://api.github.com/repos/lbartnik/testthat/releases{/id}","deployments_url":"https://api.github.com/repos/lbartnik/testthat/deployments","created_at":"2016-05-04T06:02:44Z","updated_at":"2016-05-04T06:02:45Z","pushed_at":"2016-06-09T07:00:10Z","git_url":"git://github.com/lbartnik/testthat.git","ssh_url":"git@github.com:lbartnik/testthat.git","clone_url":"https://github.com/lbartnik/testthat.git","svn_url":"https://github.com/lbartnik/testthat","homepage":"","size":2282,"stargazers_count":0,"watchers_count":0,"language":"R","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"hadley:master","ref":"master","sha":"728cac2dba21d02ee705220fcc03856b8e907458","user":{"login":"hadley","id":4196,"avatar_url":"https://avatars.githubusercontent.com/u/4196?v=3","gravatar_id":"","url":"https://api.github.com/users/hadley","html_url":"https://github.com/hadley","followers_url":"https://api.github.com/users/hadley/followers","following_url":"https://api.github.com/users/hadley/following{/other_user}","gists_url":"https://api.github.com/users/hadley/gists{/gist_id}","starred_url":"https://api.github.com/users/hadley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadley/subscriptions","organizations_url":"https://api.github.com/users/hadley/orgs","repos_url":"https://api.github.com/users/hadley/repos","events_url":"https://api.github.com/users/hadley/events{/privacy}","received_events_url":"https://api.github.com/users/hadley/received_events","type":"User","site_admin":false},"repo":{"id":295311,"name":"testthat","full_name":"hadley/testthat","owner":{"login":"hadley","id":4196,"avatar_url":"https://avatars.githubusercontent.com/u/4196?v=3","gravatar_id":"","url":"https://api.github.com/users/hadley","html_url":"https://github.com/hadley","followers_url":"https://api.github.com/users/hadley/followers","following_url":"https://api.github.com/users/hadley/following{/other_user}","gists_url":"https://api.github.com/users/hadley/gists{/gist_id}","starred_url":"https://api.github.com/users/hadley/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hadley/subscriptions","organizations_url":"https://api.github.com/users/hadley/orgs","repos_url":"https://api.github.com/users/hadley/repos","events_url":"https://api.github.com/users/hadley/events{/privacy}","received_events_url":"https://api.github.com/users/hadley/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/hadley/testthat","description":"An R package to make testing fun","fork":false,"url":"https://api.github.com/repos/hadley/testthat","forks_url":"https://api.github.com/repos/hadley/testthat/forks","keys_url":"https://api.github.com/repos/hadley/testthat/keys{/key_id}","collaborators_url":"https://api.github.com/repos/hadley/testthat/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/hadley/testthat/teams","hooks_url":"https://api.github.com/repos/hadley/testthat/hooks","issue_events_url":"https://api.github.com/repos/hadley/testthat/issues/events{/number}","events_url":"https://api.github.com/repos/hadley/testthat/events","assignees_url":"https://api.github.com/repos/hadley/testthat/assignees{/user}","branches_url":"https://api.github.com/repos/hadley/testthat/branches{/branch}","tags_url":"https://api.github.com/repos/hadley/testthat/tags","blobs_url":"https://api.github.com/repos/hadley/testthat/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/hadley/testthat/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/hadley/testthat/git/refs{/sha}","trees_url":"https://api.github.com/repos/hadley/testthat/git/trees{/sha}","statuses_url":"https://api.github.com/repos/hadley/testthat/statuses/{sha}","languages_url":"https://api.github.com/repos/hadley/testthat/languages","stargazers_url":"https://api.github.com/repos/hadley/testthat/stargazers","contributors_url":"https://api.github.com/repos/hadley/testthat/contributors","subscribers_url":"https://api.github.com/repos/hadley/testthat/subscribers","subscription_url":"https://api.github.com/repos/hadley/testthat/subscription","commits_url":"https://api.github.com/repos/hadley/testthat/commits{/sha}","git_commits_url":"https://api.github.com/repos/hadley/testthat/git/commits{/sha}","comments_url":"https://api.github.com/repos/hadley/testthat/comments{/number}","issue_comment_url":"https://api.github.com/repos/hadley/testthat/issues/comments{/number}","contents_url":"https://api.github.com/repos/hadley/testthat/contents/{+path}","compare_url":"https://api.github.com/repos/hadley/testthat/compare/{base}...{head}","merges_url":"https://api.github.com/repos/hadley/testthat/merges","archive_url":"https://api.github.com/repos/hadley/testthat/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/hadley/testthat/downloads","issues_url":"https://api.github.com/repos/hadley/testthat/issues{/number}","pulls_url":"https://api.github.com/repos/hadley/testthat/pulls{/number}","milestones_url":"https://api.github.com/repos/hadley/testthat/milestones{/number}","notifications_url":"https://api.github.com/repos/hadley/testthat/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/hadley/testthat/labels{/name}","releases_url":"https://api.github.com/repos/hadley/testthat/releases{/id}","deployments_url":"https://api.github.com/repos/hadley/testthat/deployments","created_at":"2009-09-02T12:51:44Z","updated_at":"2016-07-04T11:59:45Z","pushed_at":"2016-07-05T21:30:30Z","git_url":"git://github.com/hadley/testthat.git","ssh_url":"git@github.com:hadley/testthat.git","clone_url":"https://github.com/hadley/testthat.git","svn_url":"https://github.com/hadley/testthat","homepage":"","size":2765,"stargazers_count":330,"watchers_count":330,"language":"R","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":146,"mirror_url":null,"open_issues_count":23,"forks":146,"open_issues":23,"watchers":330,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/hadley/testthat/pulls/472"},"html":{"href":"https://github.com/hadley/testthat/pull/472"},"issue":{"href":"https://api.github.com/repos/hadley/testthat/issues/472"},"comments":{"href":"https://api.github.com/repos/hadley/testthat/issues/472/comments"},"review_comments":{"href":"https://api.github.com/repos/hadley/testthat/pulls/472/comments"},"review_comment":{"href":"https://api.github.com/repos/hadley/testthat/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/hadley/testthat/pulls/472/commits"},"statuses":{"href":"https://api.github.com/repos/hadley/testthat/statuses/c092e5c99296594bdb58c708950b5eced33560a1"}}}}
{"actor":{"display_login":"hadley"}}