Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
Fix up tests for change in all.equal behavior
Browse files Browse the repository at this point in the history
R-devel (will be R 3.2.0) has a change in behavior of all.equal for environments.
This change fixes the tests for this new behavior.
  • Loading branch information
wch committed Sep 18, 2014
1 parent aaaf579 commit 62306ee
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
23 changes: 22 additions & 1 deletion tests/testthat/test-ggvis.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
context("ggvis")
# Recursively crawl a list and replace any environments with a special
# environment. This is a workaround for a change in behavior in R 3.2.0
# for all.equal when given two environments.
blank_envs <- function(x) {
replacement_env <- new.env(parent = emptyenv())

# Use `x[]<-` to preserve any attributes on x
x[] <- lapply(x, function(val) {
if (is.environment(val)) replacement_env
else if (is.list(val)) blank_envs(val)
else val
})

x
}

test_that("plot the same regardless of order data/props added", {

df <- data.frame(x = 1:10, y = 10:1)
plots <- list(
df %>% ggvis(~x, ~y) %>% layer_points(),
Expand All @@ -9,12 +25,17 @@ test_that("plot the same regardless of order data/props added", {
ggvis() %>% add_data(df) %>% add_props(~x, ~y) %>% layer_points(),
ggvis() %>% add_data(df) %>% add_props(~x) %>% layer_points(y = ~y)
)
plots <- blank_envs(plots)

marks <- lapply(plots, function(x) x$marks[[1]])
props <- lapply(marks, "[[", "props")
data <- lapply(marks, function(x) x$data())

for (i in seq_along(marks)) {
expect_equal(props[[i]][c("x.update", "y.update")], props(~x, ~y))
expect_equal(
props[[i]][c("x.update", "y.update")],
blank_envs(props(~x, ~y))
)
expect_equal(data[[i]], df)
}
})
30 changes: 24 additions & 6 deletions tests/testthat/test-props.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
context("props")
require(shiny)

# Recursively crawl a list and replace any environments with a special
# environment. This is a workaround for a change in behavior in R 3.2.0
# for all.equal when given two environments.
blank_envs <- function(x) {
replacement_env <- new.env(parent = emptyenv())

# Use `x[]<-` to preserve any attributes on x
x[] <- lapply(x, function(val) {
if (is.environment(val)) replacement_env
else if (is.list(val)) blank_envs(val)
else val
})

x
}

test_prop <- function(p, property, value, scale, event = "update") {
expect_identical(p$property, property)
expect_identical(p$value, value)
Expand Down Expand Up @@ -225,14 +241,16 @@ test_that("merging props", {
# Utility function: sort props by name
sortp <- function(p) p[sort(names(p))]

p_i <- props(x=~a, z:="red")
q_i <- props(y=~b, z:="blue")
q_ni <- props(y=~b, z:="blue", inherit=FALSE)
p_i <- blank_envs(props(x=~a, z:="red"))
q_i <- blank_envs(props(y=~b, z:="blue"))
q_ni <- blank_envs(props(y=~b, z:="blue", inherit=FALSE))

expect_equal(sortp(merge_props(p_i, q_i)), props(x=~a, y=~b, z:="blue"))
expect_equal(sortp(merge_props(p_i, q_i)), blank_envs(props(x=~a, y=~b, z:="blue")))
expect_equal(sortp(merge_props(p_i, q_ni)), q_ni)
expect_equal(sortp(merge_props(q_ni, p_i)),
props(x=~a, y=~b, z:="red", inherit = FALSE))
expect_equal(
sortp(merge_props(q_ni, p_i)),
blank_envs(props(x=~a, y=~b, z:="red", inherit = FALSE))
)
})

test_that("prop_event_sets splits up props properly", {
Expand Down

0 comments on commit 62306ee

Please sign in to comment.