tidyverse / funs

Collection of low-level functions for working with vctrs
Other
34 stars 7 forks source link

Implement if_else #37

Closed hadley closed 4 years ago

hadley commented 5 years ago

Start at https://vctrs.r-lib.org/articles/stability.html#ifelse

romainfrancois commented 4 years ago

Just dumping here tests that fail in the current version of dplyr::if_else(). They can probably be made to work with proper casting.

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`"
    )
  })
})