Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix data sources methods
Shouldn't inherit from original class, and fix names and length methods.
  • Loading branch information
lionel- committed Feb 14, 2017
commit 098b0c103031dad82114f7afdc5e88868318cbb5
6 changes: 3 additions & 3 deletions R/tidy-sources.r
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ data_source.data.frame <- function(x, lookup_msg = NULL) {

new_data_source <- function(x, lookup_msg) {
msg <- lookup_msg %||% "Object '%s' not found in pronoun"
class <- c("data_source", class(x))
class <- "data_source"
structure(list(src = x, lookup_msg = msg), class = class)
}

Expand All @@ -65,11 +65,11 @@ new_data_source <- function(x, lookup_msg) {
}
#' @export
names.data_source <- function(x) {
names(x$src)
names(unclass(x)$src)
}
#' @export
length.data_source <- function(x) {
length(x$src)
length(unclass(x)$src)
}

has_binding <- function(x, name) {
Expand Down
41 changes: 0 additions & 41 deletions tests/testthat/test-tidy-eval.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,47 +174,6 @@ test_that("evaluation env is cleaned up", {
})


context("data_source") # ---------------------------------------------

test_that("can't access non-existent list members", {
x1 <- list(y = 1)
x2 <- data_source(x1)

expect_equal(x2$y, 1)
expect_error(x2$z, "Object 'z' not found in pronoun")
expect_error(x2[["z"]], "Object 'z' not found in pronoun")
})

test_that("can't access non-existent environment components", {
x1 <- list2env(list(y = 1))
x2 <- data_source(x1)

expect_equal(x2$y, 1)
expect_error(x2$z, "Object 'z' not found in environment")
expect_error(x2[["z"]], "Object 'z' not found in environment")
})

test_that("can't use non-character vectors", {
x <- data_source(list(y = 1))

expect_error(x[[1]], "subset with a string")
expect_error(x[[c("a", "b")]], "subset with a string")
})

test_that("data_source doesn't taint env class", {
x1 <- list2env(list(y = 1))
x2 <- data_source(x1)

expect_equal(class(x1), "environment")
expect_equal(class(x2), c("data_source", "environment"))
})

test_that("subsetting .data pronoun fails when not supplied", {
f <- tidy_quote(.data$foo)
expect_error(tidy_eval(f), "not found in pronoun")
})


context("invoke") # --------------------------------------------------

test_that("invoke() buries arguments", {
Expand Down
37 changes: 37 additions & 0 deletions tests/testthat/test-tidy-sources.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
context("tidy sources")

test_that("can't access non-existent list members", {
x1 <- list(y = 1)
x2 <- data_source(x1)

expect_equal(x2$y, 1)
expect_error(x2$z, "Object 'z' not found in pronoun")
expect_error(x2[["z"]], "Object 'z' not found in pronoun")
})

test_that("can't access non-existent environment components", {
x1 <- list2env(list(y = 1))
x2 <- data_source(x1)

expect_equal(x2$y, 1)
expect_error(x2$z, "Object 'z' not found in environment")
expect_error(x2[["z"]], "Object 'z' not found in environment")
})

test_that("can't use non-character vectors", {
x <- data_source(list(y = 1))

expect_error(x[[1]], "subset with a string")
expect_error(x[[c("a", "b")]], "subset with a string")
})

test_that("subsetting .data pronoun fails when not supplied", {
f <- tidy_quote(.data$foo)
expect_error(tidy_eval(f), "not found in pronoun")
})

test_that("names() and length() methods", {
x <- data_source(mtcars)
expect_identical(names(x), names(mtcars))
expect_identical(length(x), length(mtcars))
})