tests/testthat/test-regex.R

context("Regular expression translations")

test_that("grepl() returns expected result", {
  check_impala()
  test_op <- function(x) {
    x %>%
      filter(grepl("^v", species)) %>%
      tally() %>%
      collect() %>%
      as.integer()
  }
  expect_true(
    compare(test_op(tbl(impala, "iris")), 100L)
  )
})

test_that("grepl() ignores case by default and returns expected result", {
  check_impala()
  test_op <- function(x) {
    x %>%
      filter(grepl("^V", species)) %>%
      tally() %>%
      collect() %>%
      pull(1) %>%
      as.integer()
  }
  expect_true(
    compare(test_op(tbl(impala, "iris")), 0L)
  )
})

test_that("grepl() returns expected result with ignore.case = TRUE", {
  check_impala()
  test_op <- function(x) {
    x %>%
      filter(grepl("^V", species, ignore.case = TRUE)) %>%
      tally() %>%
      collect() %>%
      pull(1) %>%
      as.integer()
  }
  expect_true(
    compare(test_op(tbl(impala, "iris")), 100L)
  )
})

test_that("gsub() returns expected result", {
  check_impala()
  test_op <- function(x) {
    x %>%
      transmute(mangled = gsub("v", "b", species)) %>%
      distinct(mangled) %>%
      arrange(desc(mangled)) %>%
      collect() %>%
      pull(1)
  }
  expect_true(
    compare(test_op(tbl(impala, "iris")), c("setosa", "birginica", "bersicolor"))
  )
})

Try the implyr package in your browser

Any scripts or data that you put into this service are public.

implyr documentation built on March 29, 2021, 5:09 p.m.