File: test-f-eval.R

package info (click to toggle)
r-cran-lazyeval 0.2.0-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 1,276 kB
  • sloc: ansic: 268; sh: 9; makefile: 2
file content (83 lines) | stat: -rw-r--r-- 1,800 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
context("f_eval")

test_that("first argument must be a function", {
  expect_error(f_eval(10), "`f` is not a formula")
})

test_that("f_eval uses formula's environment", {
  x <- 10
  f <- local({
    y <- 100
    ~ x + y
  })

  expect_equal(f_eval(f), 110)
})

test_that("data needs to be a list", {
  expect_error(f_eval(~ x, 10), "Do not know how to find data")
})

test_that("looks first in `data`", {
  x <- 10
  data <- list(x = 100)
  expect_equal(f_eval(~ x, data), 100)
})

test_that("pronouns resolve ambiguity looks first in `data`", {
  x <- 10
  data <- list(x = 100)
  expect_equal(f_eval(~ .data$x, data), 100)
  expect_equal(f_eval(~ .env$x, data), 10)
})

test_that("pronouns complain about missing values", {
  expect_error(f_eval(~ .data$x, list()), "Variable 'x' not found in data")
  expect_error(f_eval(~ .env$`__`, list()), "Object '__' not found in environment")
})

test_that("f_eval does quasiquoting", {
  x <- 10
  expect_equal(f_eval(~ uq(quote(x))), 10)
})


test_that("unquoted formulas look in their own env", {
  f <- function() {
    n <- 100
    ~ n
  }

  n <- 10
  expect_equal(f_eval(~ uq(f())), 10)
})

test_that("unquoted formulas can use data", {
  f1 <- function() {
    z <- 100
    ~ x + z
  }
  f2 <- function() {
    z <- 100
    ~ .data$x + .env$z
  }

  z <- 10
  expect_equal(f_eval(~ uq(f1()), data = list(x = 1)), 101)
  expect_equal(f_eval(~ uq(f2()), data = list(x = 1)), 101)
})

test_that("f_eval_lhs uses lhs", {
  f <- 1 ~ 2

  expect_equal(f_eval_lhs(f), 1)
})


# find_data ---------------------------------------------------------------

test_that("find data works for NULL, lists, and data frames", {
  expect_equal(find_data(NULL), list())
  expect_equal(find_data(list(x = 1)), list(x = 1))
  expect_equal(find_data(mtcars), mtcars)
})