This file is indexed.

/usr/share/doc/r-cran-rlang/tests/testthat/test-formula.R is in r-cran-rlang 0.2.0-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
context("formula")

# Creation ----------------------------------------------------------------

test_that("is_formula works", {
  expect_true(is_formula(~10))
  expect_false(is_formula(10))
})


# Getters -----------------------------------------------------------------

test_that("throws errors for bad inputs", {
  expect_error(f_rhs(1), "must be a formula")
  expect_error(f_rhs(`~`()), "Invalid formula")
  expect_error(f_rhs(`~`(1, 2, 3)), "Invalid formula")

  expect_error(f_lhs(1), "must be a formula")
  expect_error(f_lhs(`~`()), "Invalid formula")
  expect_error(f_lhs(`~`(1, 2, 3)), "Invalid formula")

  expect_error(f_env(1), "must be a formula")
})

test_that("extracts call, name, or scalar", {
  expect_identical(f_rhs(~ x), quote(x))
  expect_identical(f_rhs(~ f()), quote(f()))
  expect_identical(f_rhs(~ 1L), 1L)
})


# Setters -----------------------------------------------------------------

test_that("can replace RHS of one-sided formula", {
  f <- ~ x1
  f_rhs(f) <- quote(x2)

  expect_equal(f, ~ x2)
})

test_that("can replace both sides of two-sided formula", {
  f <- x1 ~ y1
  f_lhs(f) <- quote(x2)
  f_rhs(f) <- quote(y2)

  expect_equal(f, x2 ~ y2)
})

test_that("can remove lhs of two-sided formula", {
  f <- x ~ y
  f_lhs(f) <- NULL

  expect_equal(f, ~ y)
})

test_that("can modify environment", {
  f <- x ~ y
  env <- new.env()
  f_env(f) <- env

  expect_equal(f_env(f), env)
})

test_that("setting RHS preserves attributes", {
  attrs <- list(foo = "bar", class = "baz")

  f <- set_attrs(~foo, !!! attrs)
  f_rhs(f) <- quote(bar)

  expect_identical(f, set_attrs(~bar, !!! attrs))
})

test_that("setting LHS preserves attributes", {
  attrs <- list(foo = "bar", class = "baz")

  f <- set_attrs(~foo, !!! attrs)
  f_lhs(f) <- quote(bar)

  expect_identical(f, set_attrs(bar ~ foo, !!! attrs))

  f_lhs(f) <- quote(baz)
  expect_identical(f, set_attrs(baz ~ foo, !!! attrs))
})

test_that("setting environment preserves attributes", {
  attrs <- list(foo = "bar", class = "baz")
  env <- env()

  f <- set_attrs(~foo, !!! attrs)
  f_env(f) <- env
  expect_identical(f, set_attrs(~foo, !!! attrs, .Environment = env))
})


# Utils --------------------------------------------------------------

test_that("quosures are not recognised as bare formulas", {
  expect_false(is_bare_formula(quo(foo)))
})

test_that("lhs is inspected", {
  expect_true(is_formula(~foo))

  expect_false(is_formula(~foo, lhs = TRUE))
  expect_true(is_formula(~foo, lhs = FALSE))

  expect_true(is_formula(foo ~ bar, lhs = TRUE))
  expect_false(is_formula(foo ~ bar, lhs = FALSE))
})

test_that("definitions are not formulas but are formulaish", {
  expect_false(is_formula(quote(foo := bar)))
  expect_true(is_formulaish(quote(foo := bar), lhs = TRUE))
  expect_false(is_formulaish(quote(foo := bar), lhs = FALSE))

  `:=` <- `~`
  expect_false(is_formulaish(foo := bar, scoped = TRUE, lhs = FALSE))
  expect_false(is_formulaish(foo := bar, scoped = FALSE, lhs = TRUE))
})