File: test-f-interp.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 (68 lines) | stat: -rw-r--r-- 1,691 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
context("f_interp")

test_that("protected against bad inputs", {
  f <- ~ x + 1
  attr(f, ".Environment") <- 10
  expect_error(f_interp(f), "must be an environment")
})

test_that("interp produces single string for character inputs", {
  x <- interp("aaaaaaaaaaaaaa + bbbbbbbbbbbbbbb + ccccccccccccccccc + dddddddddddddddd + eeeeeeeeeeeeeee")
  expect_is(x, "character")
  expect_equal(length(x), 1)
})


test_that("can interpolate from environment", {
  env <- new.env(parent = emptyenv())
  env$a <- 10

  out <- interp(~ f(a), .values = env)
  expect_identical(out, ~f(10))
})


# uq ----------------------------------------------------------------------

test_that("evaluates contents of uq()", {
  expect_equal(f_interp(~ uq(1 + 2)), ~ 3)
})


test_that("unquoted formulas are interpolated first", {
  f <- function(n) {
    ~ x + uq(n)
  }
  n <- 100

  expect_equal(f_interp(~ uq(f(10))), ~ x + 10)
})


# uqs ---------------------------------------------------------------------

test_that("contents of uqs() must be a vector", {
  expr <- ~ 1 + uqs(environment())
  expect_error(f_interp(expr), "`x` must be a vector")
})

test_that("values of uqs() spliced into expression", {
  expr <- ~ f(a, uqs(list(quote(b), quote(c))), d)
  expect_identical(f_interp(expr), ~ f(a, b, c, d))
})

test_that("names within uqs() are preseved", {
  expr <- ~ f(uqs(list(a = quote(b))))
  expect_identical(f_interp(expr), ~ f(a = b))
})


# uqf ---------------------------------------------------------------------

test_that("requires formula", {
  expect_error(f_interp(~ uqf(10)), "must be a formula")
})

test_that("interpolates formula", {
  expect_equal(f_interp(~ uqf(x ~ y)), ~ (x ~ y))
})