From 2ca063a593a94b9b80d039e7679ec71d7c0998af Mon Sep 17 00:00:00 2001 From: RobertZK Date: Fri, 28 Oct 2016 13:25:20 -0500 Subject: [PATCH 1/4] Add failing test case for exists function. --- tests/testthat/test-tracked_environment.R | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/testthat/test-tracked_environment.R b/tests/testthat/test-tracked_environment.R index 3844578..8d65b68 100644 --- a/tests/testthat/test-tracked_environment.R +++ b/tests/testthat/test-tracked_environment.R @@ -167,3 +167,8 @@ test_that("it allows parent.env assignment", { expect_identical(parent.env(environment(x)), w) }) +test_that("it correctly uses the exists function", { + x <- tracked_environment(list2env(list(y = 1))) + expect_true(exists("y", envir = x)) +}) + From 0bcee7821c5ace26186be50ae8fa62e5abb38023 Mon Sep 17 00:00:00 2001 From: RobertZK Date: Fri, 28 Oct 2016 13:27:42 -0500 Subject: [PATCH 2/4] Implement shadowed version of exists method for tracked_environment objects. --- CHANGELOG.md | 4 ++++ DESCRIPTION | 4 ++-- NAMESPACE | 1 + R/tracked_environment.R | 5 +++++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 083eb11..74f796f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# Version 0.2.3.9002 + +* Implement an `exists` method for `tracked_environment` objects. + # Version 0.2.3.9001 * Added an optimization to `replay` that avoids re-building an environment diff --git a/DESCRIPTION b/DESCRIPTION index ce49eff..14076f8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: objectdiff Type: Package Title: Objectdiff -Version: 0.2.3.9001 +Version: 0.2.3.9002 Description: Objectdiff is an R package for measuring differences between arbitrary R objects using patches, a la Git. For example, if you have slightly modified a data.frame, and you would like to @@ -36,4 +36,4 @@ Collate: 'patch.R' 'squish.R' 'utils.R' -RoxygenNote: 5.0.1 +RoxygenNote: 5.0.1.9000 diff --git a/NAMESPACE b/NAMESPACE index 74d845e..d4fce3e 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -44,6 +44,7 @@ export(commit) export(commits) export(deletions) export(environment) +export(exists) export(force_push) export(get) export(is.patch) diff --git a/R/tracked_environment.R b/R/tracked_environment.R index 5f5aaff..4b76004 100644 --- a/R/tracked_environment.R +++ b/R/tracked_environment.R @@ -77,6 +77,11 @@ rm <- function(..., envir = as.environment(-1)) { if (is.tracked_environment(envir)) environment(envir) else envir) } +#' @export +exists <- function(..., envir = as.environment(-1)) { + base::exists(..., envir = if (is.tracked_environment(envir)) environment(envir) else envir) +} + #' @export as.environment <- function(...) UseMethod('as.environment') #' @method as.environment tracked_environment From 3840add986348b505706ce575896da3ca98a43b9 Mon Sep 17 00:00:00 2001 From: RobertZK Date: Fri, 28 Oct 2016 13:35:48 -0500 Subject: [PATCH 3/4] Add additional tests for exists method and use same definition as base::exists. --- R/tracked_environment.R | 6 ++++-- tests/testthat/test-tracked_environment.R | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/R/tracked_environment.R b/R/tracked_environment.R index 4b76004..5b32599 100644 --- a/R/tracked_environment.R +++ b/R/tracked_environment.R @@ -78,8 +78,10 @@ rm <- function(..., envir = as.environment(-1)) { } #' @export -exists <- function(..., envir = as.environment(-1)) { - base::exists(..., envir = if (is.tracked_environment(envir)) environment(envir) else envir) +exists <- function(x, where = -1, envir = as.environment(-1), frame, mode = "any", inherits = TRUE) { + base::exists(x, where, + if (is.tracked_environment(envir)) environment(envir) else envir, + frame, mode, inherits) } #' @export diff --git a/tests/testthat/test-tracked_environment.R b/tests/testthat/test-tracked_environment.R index 8d65b68..35ab8ea 100644 --- a/tests/testthat/test-tracked_environment.R +++ b/tests/testthat/test-tracked_environment.R @@ -170,5 +170,20 @@ test_that("it allows parent.env assignment", { test_that("it correctly uses the exists function", { x <- tracked_environment(list2env(list(y = 1))) expect_true(exists("y", envir = x)) + expect_false(exists("z", envir = x)) +}) + +test_that("it correctly uses the exists function with inherits = FALSE", { + x <- tracked_environment(list2env(list(y = 1), parent = list2env(list(z = 1)))) + expect_true(exists("z", envir = x)) + expect_false(exists("z", envir = x, inherits = FALSE)) +}) + +test_that("it correctly uses the exists function for vanilla environments", { + x <- list2env(list(y = 1), parent = list2env(list(z = 1))) + expect_true(exists("y", envir = x)) + expect_false(exists("w", envir = x)) + expect_true(exists("z", envir = x)) + expect_false(exists("z", envir = x, inherits = FALSE)) }) From 8e924b195e6d49eff7175525b875325a081b6c4c Mon Sep 17 00:00:00 2001 From: RobertZK Date: Fri, 28 Oct 2016 13:36:54 -0500 Subject: [PATCH 4/4] Check for argument name matching in exists function. --- tests/testthat/test-tracked_environment.R | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/testthat/test-tracked_environment.R b/tests/testthat/test-tracked_environment.R index 35ab8ea..d7c4620 100644 --- a/tests/testthat/test-tracked_environment.R +++ b/tests/testthat/test-tracked_environment.R @@ -171,12 +171,16 @@ test_that("it correctly uses the exists function", { x <- tracked_environment(list2env(list(y = 1))) expect_true(exists("y", envir = x)) expect_false(exists("z", envir = x)) + expect_true(exists("y", env = x)) + expect_false(exists("z", env = x)) }) test_that("it correctly uses the exists function with inherits = FALSE", { x <- tracked_environment(list2env(list(y = 1), parent = list2env(list(z = 1)))) expect_true(exists("z", envir = x)) expect_false(exists("z", envir = x, inherits = FALSE)) + expect_true(exists("z", env = x)) + expect_false(exists("z", env = x, inherits = FALSE)) }) test_that("it correctly uses the exists function for vanilla environments", { @@ -185,5 +189,9 @@ test_that("it correctly uses the exists function for vanilla environments", { expect_false(exists("w", envir = x)) expect_true(exists("z", envir = x)) expect_false(exists("z", envir = x, inherits = FALSE)) + expect_true(exists("y", env = x)) + expect_false(exists("w", env = x)) + expect_true(exists("z", env = x)) + expect_false(exists("z", env = x, inherits = FALSE)) })