tests/testthat/test-histogram.R

library(palmerpenguins)
test_that("histogram() returns one chart", {
  plots_list <- histogram(penguins, c("body_mass_g"))
  expect_equal(1, length(plots_list))

  hist_plot <- plots_list[[1]]
  expect_true("GeomBar" %in% c(class(hist_plot$layers[[1]]$geom)))
  expect_true("body_mass_g"  == rlang::get_expr(hist_plot$mapping$x))
})

test_that("histogram() returns multiple charts", {
  plots_list <- histogram(penguins, c("body_mass_g", "flipper_length_mm"))
  expect_equal(2, length(plots_list))

  hist_plot <- plots_list[[1]]
  expect_true("GeomBar" %in% c(class(hist_plot$layers[[1]]$geom)))
  expect_true("body_mass_g"  == rlang::get_expr(hist_plot$mapping$x))

  hist_plot <- plots_list[[2]]
  expect_true("GeomBar" %in% c(class(hist_plot$layers[[1]]$geom)))
  expect_true("flipper_length_mm"  == rlang::get_expr(hist_plot$mapping$x))
})

test_that("Input data should be a data frame", {
  input_vector <- c(1,2,3,4)
  expect_error(histogram(input_vector, c("col_a")),
               "Please provide a data frame as the first argument to the function")
})

test_that("Columns should be a character vector", {
  expect_error(histogram(penguins, penguins),
               "Please provide a single column name or character vector")
})

test_that("Each column should exist in data frame", {
  expect_error(histogram(penguins, c("fake_col")),
               "Column does not exist in data frame")
})
UBC-MDS/slimreda documentation built on Feb. 7, 2022, 9:12 a.m.