Skip to content

Commit

Permalink
add unit tests for statascii()
Browse files Browse the repository at this point in the history
  • Loading branch information
gvelasq committed Mar 21, 2018
1 parent 37df283 commit 2e5a237
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 25 deletions.
16 changes: 1 addition & 15 deletions R/statascii.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,14 @@ statascii <- function(df, ..., flavor = "oneway", padding = "stata", separators
rlang::abort("data frame must have at least two columns")
}
df <- as.matrix(purrr::modify(df, as.character))
if (ncol(df) == 1L) {
df <- t(df)
}
if (padding == "stata") {
colnames(df) <- stringr::str_pad(colnames(df), 9L, pad = " ")
}
if (padding == "summary") {
colnames(df) <- stringr::str_pad(colnames(df), 5L, pad = " ")
}
nchar_content <- apply(df, 2, function(x) {
if (getRversion() <= "3.2.0") {
max(crayon::col_nchar(x, type = "width"))
} else {
max(crayon::col_nchar(x, keepNA = FALSE))
}
max(crayon::col_nchar(x, type = "width"))
})
if (getRversion() <= "3.2.0") {
nchar_names <- crayon::col_nchar(colnames(df), type = "width")
Expand Down Expand Up @@ -107,13 +100,6 @@ statascii <- function(df, ..., flavor = "oneway", padding = "stata", separators
writeLines(table_line, con)
for (i in seq_len(nrow(df))) {
writeLines(add_row_twoway(df[i, ], M), con)
if (i > 0L & i < nrow(df)) {
if (separators) {
if (df[i, 1] != df[i + 1L, 1]) {
writeLines(group_dashes, con)
}
}
}
if (i == total_line) {
writeLines(table_line, con)
}
Expand Down
4 changes: 2 additions & 2 deletions tests/create-sample-data/create-sample-data.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# this R script creates sample tables for unit testing
# tables last created on: 18Mar18 (tabr version 0.0.0.9000)
# tables last created on: 20Mar18 (tabr version 0.0.0.9000)

# setup
library(dplyr)
Expand Down Expand Up @@ -37,7 +37,7 @@ e <- mtcars %>% group_by(gear) %>% summarize(
Min = min(gear),
Max = max(gear)
)
table_e <- capture.output(statascii(e, flavor = "summary"))
table_e <- capture.output(statascii(e, flavor = "summary", padding = "summary"))

# f. demonstrate wrapping feature for wide tables
f <- mtcars %>%
Expand Down
Binary file modified tests/testthat/sample-tables.rds
Binary file not shown.
19 changes: 11 additions & 8 deletions tests/testthat/test-statascii.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,52 @@ context("test-statascii.R")

tables <- read_rds("sample-tables.rds")

test_that("data frame must have at least two columns",
expect_error(statascii(as.data.frame(letters[1:3])))
)

test_that("data frame must have at least three columns for 'twoway' flavor",
expect_error(statascii(as.data.frame(letters[1:3]), flavor = "twoway"))
)

test_that("oneway flavor works", {
skip_on_appveyor()
# a. demonstrate 'oneway' flavor for one-way tables of frequencies
a <- mtcars %>% count(gear) %>% rename(Freq. = n)
a <- a %>% add_row(gear = "Total", Freq. = sum(a[, 2]))
expect_equal(tables[[1]], utils::capture.output(statascii(a, flavor = "oneway")))
})

test_that("oneway flavor with no padding works", {
skip_on_appveyor()
# b. demonstrate 'oneway' flavor with no padding
b <- mtcars %>% count(gear) %>% rename(Freq. = n)
b <- b %>% add_row(gear = "Total", Freq. = sum(b[, 2]))
expect_equal(tables[[2]], utils::capture.output(statascii(b, flavor = "oneway", padding = "none")))
})

test_that("twowway flavor works for 3-way table", {
skip_on_appveyor()
# c. demonstrate 'twoway' flavor for n-way tables of frequencies
c <- mtcars %>% count(gear, carb, am) %>% rename(Freq. = n)
c <- c %>% ungroup() %>% add_row(gear = "Total", carb = "", am = "", Freq. = sum(c[, 4]))
expect_equal(tables[[3]], utils::capture.output(statascii(c, flavor = "twoway")))
})

test_that("twoway flavor works with dashed group separator", {
test_that("twoway flavor works with dashed group separators", {
skip_on_appveyor()
# d. demonstrate 'twoway' flavor with dashed group separator
d <- mtcars %>% count(gear, carb, am) %>% rename(Freq. = n)
d <- d %>% ungroup() %>% add_row(gear = "Total", carb = "", am = "", Freq. = sum(d[, 4]))
expect_equal(tables[[4]], utils::capture.output(statascii(d, flavor = "twoway", separators = TRUE)))
})

test_that("summary flavor works", {
test_that("summary flavor with summary padding works", {
skip_on_appveyor()
# e. demonstrate 'summary' flavor for summary statistics
e <- mtcars %>% group_by(gear) %>% summarize(
Obs = n(),
Mean = mean(gear),
"Std. Dev." = sd(gear),
Min = min(gear),
Max = max(gear)
)
expect_equal(tables[[5]], utils::capture.output(statascii(e, flavor = "summary")))
expect_equal(tables[[5]], utils::capture.output(statascii(e, flavor = "summary", padding = "summary")))
})

test_that("wrap_tbl() works", {
Expand Down

0 comments on commit 2e5a237

Please sign in to comment.