tests/testthat/test-match_col_class.R

# ---------------------------------------------------------------------------- #
test_that("match_col_class matches character: stringsAsFactors = FALSE", {
  df <- data.frame(
    id = c("a", "b"), size = c(1.2, 3.5), num = c(1, 2),
    numl = c(10L, 20L), stringsAsFactors = FALSE
  )

  expect_equal(match_col_class(df, "character"), c(id = 1))
})

# ---------------------------------------------------------------------------- #
test_that("match_col_class matches character: stringsAsFactors = TRUE", {
  df <- data.frame(
    id = c("a", "b"), size = c(1.2, 3.5), num = c(1, 2),
    numl = c(10L, 20L), stringsAsFactors = TRUE
  )

  expect_equal(match_col_class(df, "character"), integer(0), ignore_attr = TRUE)
})

# ---------------------------------------------------------------------------- #
test_that("match_col_class matches factor", {
  df <- data.frame(
    id = c("a", "b"), size = c(1.2, 3.5), num = c(1, 2),
    numl = c(10L, 20L), stringsAsFactors = TRUE
  )

  expect_equal(match_col_class(df, "factor"), c(id = 1), ignore_attr = TRUE)
})

# ---------------------------------------------------------------------------- #
test_that("match_col_class matches numeric", {
  df <- data.frame(
    id = c("a", "b"), size = c(1.2, 3.5), num = c(1, 2),
    numl = c(10L, 20L)
  )

  expect_equal(match_col_class(df, "numeric"), c(size = 2, num = 3),
    ignore_attr = TRUE
  )
})

# ---------------------------------------------------------------------------- #
test_that("match_col_class matches integer", {
  df <- data.frame(
    id = c("a", "b"), size = c(1.2, 3.5), num = c(1, 2),
    numl = c(10L, 20L)
  )

  expect_equal(match_col_class(df, "integer"), c(numl = 4), ignore_attr = TRUE)
})

# ---------------------------------------------------------------------------- #
toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.