tests/testthat/test_add_margins.R

context('Test that add_margins works properly')

test_that('add_margins works properly', {
  df <- data.frame(a = 'text', b = 1:5, c = 1:5, d = 'text',
                   stringsAsFactors = FALSE)

  expect_error(df %>% add_margins())

  df %<>% dplyr::select(-d)
  added <- df %>% add_margins()

  expect_equal(as.numeric(added$Total), c(2 * 1:5, sum(2 * 1:5)))
  expect_equal(as.character(tail(added, 1)), c('Total', '15', '15', '30'))

  added <- df %>% add_margins(rowsum = FALSE)

  expect_false('Total' %in% names(added))
})
kimjam/qrcutils documentation built on May 20, 2019, 10:21 p.m.