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, varName="html") { t <- escapeString(s) u <- eval(parse(text=paste0("\"", t, "\""))) if(s!=u) stop("Unable to escape string!") if(is.null(varName)) varName <- "html" t <- paste0("\t", varName, " <- \"", t, "\"") utils::writeClipboard(t) return(invisible()) } context("GET VALUES TESTS") test_that("retrieving cell value", { # data for the table saleIds <- c(5334, 5336, 5338) items <- c("Apple", "Orange", "Banana") quantities <- c(5, 8, 6) prices <- c(0.34452354, 0.4732543, 1.3443243) # construct the table library(basictabler) tbl <- BasicTable$new(compatibility=list(headerCellsAsTD=TRUE)) tbl$addData(data.frame(saleIds, items, quantities, prices, stringsAsFactors=FALSE), firstColumnAsRowHeaders=TRUE, explicitColumnHeaders=c("Sale ID", "Item", "Quantity", "Price"), columnFormats=list(NULL, NULL, NULL, "%.2f")) v1 <- tbl$cells$getValue(2, 4) v2 <- tbl$cells$getValue(2, 4, formattedValue=TRUE) rowValues <- tbl$cells$getRowValues(2, asList=TRUE) rowValues <- paste0(class(rowValues), ": ", paste(rowValues, collapse=", ")) columnValues <- tbl$cells$getColumnValues(3) columnValues <- paste0(class(columnValues), ": ", paste(columnValues, collapse=", ")) # tbl$renderTable() # prepStr(tbl$print(asCharacter=TRUE), "str") # prepStr(as.character(tbl$getHtml())) str <- "Sale ID Item Quantity Price \n 5334 Apple 5 0.34 \n 5336 Orange 8 0.47 \n 5338 Banana 6 1.34 " 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
Sale IDItemQuantityPrice
5334Apple50.34
5336Orange80.47
5338Banana61.34
" expect_identical(tbl$print(asCharacter=TRUE), str) expect_identical(as.character(tbl$getHtml()), html) expect_equal(v1, 0.34452354) expect_identical(v2, "0.34") expect_identical(rowValues, "list: 5334, Apple, 5, 0.34452354") expect_identical(columnValues, "numeric: 5, 8, 6") })