Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -36,4 +36,4 @@ Collate:
'patch.R'
'squish.R'
'utils.R'
RoxygenNote: 5.0.1
RoxygenNote: 5.0.1.9000
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export(commit)
export(commits)
export(deletions)
export(environment)
export(exists)
export(force_push)
export(get)
export(is.patch)
Expand Down
7 changes: 7 additions & 0 deletions R/tracked_environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ rm <- function(..., envir = as.environment(-1)) {
if (is.tracked_environment(envir)) environment(envir) else envir)
}

#' @export
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
as.environment <- function(...) UseMethod('as.environment')
#' @method as.environment tracked_environment
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-tracked_environment.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,31 @@ 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))
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", {
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))
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))
})
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice tests 👍