library(testthat) # most common expectations: # equality: expect_equal() and expect_identical() # regexp: expect_match() # catch-all: expect_true() and expect_false() # console output: expect_output() # messages: expect_message() # warning: expect_warning() # errors: expect_error() escapeString <- function(s) { t <- gsub("(\\\\)", "\\\\\\\\", s) t <- gsub("(\n)", "\\\\n", t) t <- gsub("(\r)", "\\\\r", t) t <- gsub("(\")", "\\\\\"", t) return(t) } prepStr <- function(s) { t <- escapeString(s) u <- eval(parse(text=paste0("\"", t, "\""))) if(s!=u) stop("Unable to escape string!") t <- paste0("\thtml <- \"", t, "\"") utils::writeClipboard(t) return(invisible()) } evaluationMode <- "sequential" processingLibrary <- "dplyr" description <- "test: sequential dplyr" countFunction <- "n()" isDevelopmentVersion <- (length(strsplit(packageDescription("pivottabler")$Version, "\\.")[[1]]) > 3) testScenarios <- function(description="test", releaseEvaluationMode="batch", releaseProcessingLibrary="dplyr", runAllForReleaseVersion=FALSE) { isDevelopmentVersion <- (length(strsplit(packageDescription("pivottabler")$Version, "\\.")[[1]]) > 3) if(isDevelopmentVersion||runAllForReleaseVersion) { evaluationModes <- c("sequential", "batch") processingLibraries <- c("dplyr", "data.table") } else { evaluationModes <- releaseEvaluationMode processingLibraries <- releaseProcessingLibrary } testCount <- length(evaluationModes)*length(processingLibraries) c1 <- character(testCount) c2 <- character(testCount) c3 <- character(testCount) c4 <- character(testCount) testCount <- 0 for(evaluationMode in evaluationModes) for(processingLibrary in processingLibraries) { testCount <- testCount + 1 c1[testCount] <- evaluationMode c2[testCount] <- processingLibrary c3[testCount] <- paste0(description, ": ", evaluationMode, " ", processingLibrary) c4[testCount] <- ifelse(processingLibrary=="data.table", ".N", "n()") } df <- data.frame(evaluationMode=c1, processingLibrary=c2, description=c3, countFunction=c4, stringsAsFactors=FALSE) return(df) } context("QUICK-PIVOT TESTS") test_that("quick-pivot tests: qpvt pivot 1", { skip_on_cran() library(pivottabler) pt <- qpvt(bhmtrains, "TOC", "TrainCategory", "n()", compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales30798303909
CrossCountry228656322928
London Midland144873379248279
Virgin Trains85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) }) test_that("quick-pivot tests: qpvt pivot 2", { library(pivottabler) pt <- qpvt(bhmtrains, c("=", "TOC"), c("TrainCategory", "PowerType"), c("Number of Trains"="n()", "Maximum Speed"="max(SchedSpeedMPH, na.rm=TRUE)"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE, noDataGroupNBSP=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
DMUEMUHSTTotalDMUEMUTotal
Number of TrainsArriva Trains Wales307930798308303909
CrossCountry2213373222865636322928
London Midland56388849144875591282013379248279
Virgin Trains2137645785948594
Total3298715306732490256484282013468583710
Maximum SpeedArriva Trains Wales9090909090
CrossCountry125125125100100125
London Midland100110110100100100110
Virgin Trains125125125125
Total125125125125100100100125
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 505565) expect_identical(as.character(pt$getHtml()), html) }) if(isDevelopmentVersion) { test_that("quick-pivot tests: qpvt pivot format 1 - single calc", { library(pivottabler) pt <- qpvt(bhmtrains, "TOC", "TrainCategory", "mean(SchedSpeedMPH, na.rm=TRUE)", format="%.0f", compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains Wales908990
CrossCountry113100113
London Midland989193
Virgin Trains125125
Total10991101
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 1426.677, tolerance=1e-2) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: qpvt pivot format 2 - two calcs basic", { library(pivottabler) pt <- qpvt(bhmtrains, "TOC", "TrainCategory", c("Mean Speed"="mean(SchedSpeedMPH, na.rm=TRUE)", "Std Dev Speed"="sd(SchedSpeedMPH, na.rm=TRUE)"), formats=list("%.0f", "%.1f"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Mean SpeedStd Dev SpeedMean SpeedStd Dev SpeedMean SpeedStd Dev Speed
Arriva Trains Wales902.1893.8902.6
CrossCountry11312.51000.011312.5
London Midland987.4918.1938.5
Virgin Trains1250.01250.0
Total10914.2918.110115.1
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 1521.537, tolerance=1e-2) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: qpvt pivot format 3 - two calcs list", { library(pivottabler) pt <- qpvt(bhmtrains, "TOC", "TrainCategory", c("Mean Speed"="mean(SchedSpeedMPH, na.rm=TRUE)", "Std Dev Speed"="sd(SchedSpeedMPH, na.rm=TRUE)"), formats=list(list(digits = 3, nsmall=1), "%.1f"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Mean SpeedStd Dev SpeedMean SpeedStd Dev SpeedMean SpeedStd Dev Speed
Arriva Trains Wales89.72.189.03.889.52.6
CrossCountry112.912.5100.00.0112.912.5
London Midland97.67.490.88.192.98.5
Virgin Trains125.00.0125.00.0
Total109.114.290.88.1101.515.1
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 1521.537, tolerance=1e-2) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: qpvt pivot format 3 - two calcs function", { library(pivottabler) qf <- function(x) { return(paste0("**", sprintf("%.0f", x), "**"))} pt <- qpvt(bhmtrains, "TOC", "TrainCategory", c("Mean Speed"="mean(SchedSpeedMPH, na.rm=TRUE)", "Std Dev Speed"="sd(SchedSpeedMPH, na.rm=TRUE)"), formats=list(qf, "%.1f"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Mean SpeedStd Dev SpeedMean SpeedStd Dev SpeedMean SpeedStd Dev Speed
Arriva Trains Wales**90**2.1**89**3.8**90**2.6
CrossCountry**113**12.5**100**0.0**113**12.5
London Midland**98**7.4**91**8.1**93**8.5
Virgin Trains**125**0.0**125**0.0
Total**109**14.2**91**8.1**101**15.1
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 1521.537, tolerance=1e-2) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: qlpvt pivot 1", { library(pivottabler) lchr <- qlpvt(bhmtrains, "TOC", "TrainCategory", "n()", caption="my caption", label="mylabel", compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # prepStr(lchr) latex <- "\\begin{table}[h!]\n \\centering\n \\caption{my caption}\n \\label{tab:mylabel}\n \\begin{tabular}{|l|rrr|}\n \\hline\n & Express Passenger & Ordinary Passenger & Total\\\\\n \\hline\n Arriva Trains Wales & 3079 & 830 & 3909\\\\\n CrossCountry & 22865 & 63 & 22928\\\\\n London Midland & 14487 & 33792 & 48279\\\\\n Virgin Trains & 8594 & & 8594\\\\\n Total & 49025 & 34685 & 83710\\\\\n \\hline\n \\end{tabular}\n\\end{table}" expect_identical(lchr, latex) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: totals base", { library(pivottabler) pt <- qpvt(bhmtrains, c("TOC", "PowerType"), "TrainCategory", "n()", compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE, noDataGroupNBSP=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains WalesDMU30798303909
Total30798303909
CrossCountryDMU221336322196
HST732732
Total228656322928
London MidlandDMU5638559111229
EMU88492820137050
Total144873379248279
Virgin TrainsDMU21372137
EMU64576457
Total85948594
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 502260) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: no totals", { library(pivottabler) pt <- qpvt(bhmtrains, c("TOC", "PowerType"), "TrainCategory", "n()", totals=NULL, compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary Passenger
Arriva Trains WalesDMU3079830
CrossCountryDMU2213363
HST732
London MidlandDMU56385591
EMU884928201
Virgin TrainsDMU2137
EMU6457
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 83710) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: 1 total out of 3 (chr)", { library(pivottabler) pt <- qpvt(bhmtrains, c("TOC", "PowerType"), "TrainCategory", "n()", totals="TOC", compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE, noDataGroupNBSP=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary Passenger
Arriva Trains WalesDMU3079830
CrossCountryDMU2213363
HST732
London MidlandDMU56385591
EMU884928201
Virgin TrainsDMU2137
EMU6457
Total4902534685
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 167420) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: 2 totals out of 3 (chr)", { library(pivottabler) pt <- qpvt(bhmtrains, c("TOC", "PowerType"), "TrainCategory", "n()", totals=c("TOC", "TrainCategory"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE, noDataGroupNBSP=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerTotal
Arriva Trains WalesDMU30798303909
CrossCountryDMU221336322196
HST732732
London MidlandDMU5638559111229
EMU88492820137050
Virgin TrainsDMU21372137
EMU64576457
Total490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: 1 total out of 3 (list)", { library(pivottabler) pt <- qpvt(bhmtrains, c("TOC", "PowerType"), "TrainCategory", "n()", totals=list("TOC"="All TOCs"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE, noDataGroupNBSP=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary Passenger
Arriva Trains WalesDMU3079830
CrossCountryDMU2213363
HST732
London MidlandDMU56385591
EMU884928201
Virgin TrainsDMU2137
EMU6457
All TOCs4902534685
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 167420) expect_identical(as.character(pt$getHtml()), html) }) } if(isDevelopmentVersion) { test_that("quick-pivot tests: 2 totals out of 3 (list)", { library(pivottabler) pt <- qpvt(bhmtrains, c("TOC", "PowerType"), "TrainCategory", "n()", totals=list("TOC"="All TOCs", "TrainCategory"="All Categories"), compatibility=list(totalStyleIsCellStyle=TRUE, explicitHeaderSpansOfOne=TRUE, noDataGroupNBSP=TRUE)) # pt$renderPivot() # sum(pt$cells$asMatrix(), na.rm=TRUE) # prepStr(as.character(pt$getHtml())) html <- "\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
 Express PassengerOrdinary PassengerAll Categories
Arriva Trains WalesDMU30798303909
CrossCountryDMU221336322196
HST732732
London MidlandDMU5638559111229
EMU88492820137050
Virgin TrainsDMU21372137
EMU64576457
All TOCs490253468583710
" expect_equal(sum(pt$cells$asMatrix(), na.rm=TRUE), 334840) expect_identical(as.character(pt$getHtml()), html) }) }