tests/testthat/test-cur-context.R

test_that("cur_column() works", {
  df <- tidytable(x = 1:3, y = 4:6)
  res <- df %>%
    mutate(
      across(c(x, y), ~ paste0(cur_column(), .x))
    )
  expect_equal(res$x, c("x1", "x2", "x3"))
  expect_equal(res$y, c("y4", "y5", "y6"))

  # Works in an anonymous function, #699
  res <- df %>%
    mutate(
      across(c(x, y), \(.x) paste0(cur_column(), .x))
    )
  expect_equal(res$x, c("x1", "x2", "x3"))
  expect_equal(res$y, c("y4", "y5", "y6"))
})

test_that("cur_data() works", {
  df <- tidytable(x = 1:3, y = c("a", "a", "b"))
  result_df <- df %>%
    summarize(data = list(cur_data()), .by = y)
  check_df <- df %>%
    nest_by(y)

  expect_equal(result_df, check_df)
})

test_that("cur_group_rows() works", {
  df <- tidytable(x = 1:3, y = c("a", "a", "b"))
  df <- df %>%
    mutate(rows1 = cur_group_rows(),
           rows2 = cur_group_rows.(),
           rows_check = 1:.N,
           .by = y)

  expect_equal(df$rows1, df$rows_check)
  expect_equal(df$rows2, df$rows_check)
})


test_that("cur_group_id() works", {
  df <- tidytable(x = 1:3, y = c("a", "a", "b"))
  df <- df %>%
    mutate(id1 = cur_group_id(),
            id2 = cur_group_id.(),
            id_check = .GRP,
            .by = y)

  expect_equal(df$id1, df$id_check)
  expect_equal(df$id2, df$id_check)
})

Try the tidytable package in your browser

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

tidytable documentation built on Oct. 5, 2023, 5:07 p.m.