/usr/share/doc/r-cran-dplyr/tests/testthat/test-if-else.R is in r-cran-dplyr 0.7.4-3.
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | context("if_else")
test_that("first argument must be logical", {
expect_error(
if_else(1:10, 1, 2),
"`condition` must be a logical, not integer",
fixed = TRUE
)
})
test_that("true and false must be same length as condition (or length 1)", {
expect_error(
if_else(1:3 < 2, 1:2, 1:3),
"`true` must be length 3 (length of `condition`) or one, not 2",
fixed = TRUE
)
expect_error(
if_else(1:3 < 2, 1:3, 1:2),
"`false` must be length 3 (length of `condition`) or one, not 2",
fixed = TRUE
)
})
test_that("true and false must be same type and same class", {
expect_error(
if_else(1:3 < 2, 1, 1L),
"`false` must be type double, not integer",
fixed = TRUE
)
x <- factor("x")
y <- ordered("x")
expect_error(
if_else(1:3 < 2, x, y),
"`false` must be factor, not ordered/factor",
fixed = TRUE
)
})
test_that("scalar true and false are vectorised", {
x <- c(TRUE, TRUE, FALSE, FALSE)
expect_equal(if_else(x, 1, 2), c(1, 1, 2, 2))
})
test_that("vector true and false are ok", {
x <- c(-1, 0, 1)
expect_equal(if_else(x < 0, x, 0), c(-1, 0, 0))
expect_equal(if_else(x > 0, x, 0), c(0, 0, 1))
})
test_that("missing values are missing", {
expect_equal(if_else(c(TRUE, NA, FALSE), -1, 1), c(-1, NA, 1))
})
test_that("works with lists", {
x <- list(1, 2, 3)
expect_equal(
if_else(c(TRUE, TRUE, FALSE), x, list(NULL)),
list(1, 2, NULL)
)
})
test_that("better factor support (#2197)", {
skip("Currently failing")
test_that("gives proper error messages for factor class (#2197)", {
x <- factor(1:3, labels = letters[1:3])
expect_error(
if_else(x == "a", "b", x),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", 1L, x),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", 1., x),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", TRUE, x),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", Sys.Date(), x),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", x, "b"),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", x, 1L),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", x, 1.),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", x, TRUE),
"asdf",
fixed = TRUE
)
expect_error(
if_else(x == "a", x, Sys.Date()),
"asdf",
fixed = TRUE
)
})
test_that("works with factors as both `true` and `false` (#2197)", {
x <- factor(1:3, labels = letters[1:3])
y <- factor(1:3, labels = letters[c(1, 2, 4)])
expect_equal(if_else(x == "a", x[[2]], x), x[c(2, 2, 3)])
expect_error(
if_else(x == "a", x, y),
"asdf levels in `false` don't match levels in `true`"
)
})
})
|