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("MERGED CELLS TESTS") test_that("merge cells test", { # data for the table saleIds <- c(5334, 5336, 5338, 5339) items <- c("Apple", "Orange", "Banana", "Grapefruit") quantities <- c(5, 8, 6, 2) prices <- c(0.34452354, 0.4732543, 1.3443243, 0.5628432) status <- c("Good", "OK", "Bad", "OK") # construct the table library(basictabler) tbl <- BasicTable$new() tbl$addData(data.frame(saleIds, items, quantities, prices, status), firstColumnAsRowHeaders=TRUE, explicitColumnHeaders=c("Sale ID", "Item", "Quantity", "Price", "Status"), columnFormats=list(NULL, NULL, NULL, "%.2f", NULL)) # merge the cells and specify new heading tbl$mergeCells(rFrom=1, cFrom=2, rSpan=1, cSpan=2) cell <- tbl$cells$getCell(1, 2) cell$rawValue <- "Item & Qty" cell$formattedValue <- "Item & Qty" tbl$mergeCells(rFrom=3, cFrom=3, rSpan=2, cSpan=2) cell <- tbl$cells$getCell(3, 3) cell$rawValue <- "??" cell$formattedValue <- "??" # tbl$renderTable() # prepStr(tbl$print(asCharacter=TRUE), "str") # prepStr(as.character(tbl$getHtml())) str <- "Sale ID Item & Qty Quantity Price Status \n 5334 Apple 5 0.34 Good \n 5336 Orange ?? 0.47 OK \n 5338 Banana 6 1.34 Bad \n 5339 Grapefruit 2 0.56 OK " 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
Sale IDItem & QtyPriceStatus
5334Apple50.34Good
5336Orange??OK
5338BananaBad
5339Grapefruit20.56OK
" expect_identical(tbl$print(asCharacter=TRUE), str) expect_identical(as.character(tbl$getHtml()), html) }) test_that("merge cells test (using rowNumbers/columnNumbers and then from/to)", { # data for the table saleIds <- c(5334, 5336, 5338, 5339) items <- c("Apple", "Orange", "Banana", "Grapefruit") quantities <- c(5, 8, 6, 2) prices <- c(0.34452354, 0.4732543, 1.3443243, 0.5628432) status <- c("Good", "OK", "Bad", "OK") # construct the table library(basictabler) tbl <- BasicTable$new() tbl$addData(data.frame(saleIds, items, quantities, prices, status), firstColumnAsRowHeaders=TRUE, explicitColumnHeaders=c("Sale ID", "Item", "Quantity", "Price", "Status"), columnFormats=list(NULL, NULL, NULL, "%.2f", NULL)) # merge the cells and specify new heading tbl$mergeCells(rowNumbers=1, columnNumbers=c(2,3)) cell <- tbl$cells$getCell(1, 2) cell$rawValue <- "Item & Qty" cell$formattedValue <- "Item & Qty" tbl$mergeCells(rFrom=3, cFrom=3, rTo=4, cTo=4) cell <- tbl$cells$getCell(3, 3) cell$rawValue <- "??" cell$formattedValue <- "??" # tbl$renderTable() # prepStr(tbl$print(asCharacter=TRUE), "str") # prepStr(as.character(tbl$getHtml())) str <- "Sale ID Item & Qty Quantity Price Status \n 5334 Apple 5 0.34 Good \n 5336 Orange ?? 0.47 OK \n 5338 Banana 6 1.34 Bad \n 5339 Grapefruit 2 0.56 OK " 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
Sale IDItem & QtyPriceStatus
5334Apple50.34Good
5336Orange??OK
5338BananaBad
5339Grapefruit20.56OK
" expect_identical(tbl$print(asCharacter=TRUE), str) expect_identical(as.character(tbl$getHtml()), html) }) test_that("merge cells test (compatibility mode)", { # data for the table saleIds <- c(5334, 5336, 5338, 5339) items <- c("Apple", "Orange", "Banana", "Grapefruit") quantities <- c(5, 8, 6, 2) prices <- c(0.34452354, 0.4732543, 1.3443243, 0.5628432) status <- c("Good", "OK", "Bad", "OK") # construct the table library(basictabler) tbl <- BasicTable$new(compatibility=list(headerCellsAsTD=TRUE, explicitHeaderSpansOfOne=TRUE)) tbl$addData(data.frame(saleIds, items, quantities, prices, status), firstColumnAsRowHeaders=TRUE, explicitColumnHeaders=c("Sale ID", "Item", "Quantity", "Price", "Status"), columnFormats=list(NULL, NULL, NULL, "%.2f", NULL)) # merge the cells and specify new heading tbl$mergeCells(rFrom=1, cFrom=2, rSpan=1, cSpan=2) cell <- tbl$cells$getCell(1, 2) cell$rawValue <- "Item & Qty" cell$formattedValue <- "Item & Qty" tbl$mergeCells(rFrom=3, cFrom=3, rSpan=2, cSpan=2) cell <- tbl$cells$getCell(3, 3) cell$rawValue <- "??" cell$formattedValue <- "??" # tbl$renderTable() # prepStr(tbl$print(asCharacter=TRUE), "str") # prepStr(as.character(tbl$getHtml())) str <- "Sale ID Item & Qty Quantity Price Status \n 5334 Apple 5 0.34 Good \n 5336 Orange ?? 0.47 OK \n 5338 Banana 6 1.34 Bad \n 5339 Grapefruit 2 0.56 OK " 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
Sale IDItem & QtyPriceStatus
5334Apple50.34Good
5336Orange??OK
5338BananaBad
5339Grapefruit20.56OK
" expect_identical(tbl$print(asCharacter=TRUE), str) expect_identical(as.character(tbl$getHtml()), html) }) test_that("merge and manipulate test", { # data for the table ints0 <- 0:29 ints1 <- 100:129 ints2 <- 200:229 ints3 <- 300:329 ints4 <- 400:429 ints5 <- 500:529 ints6 <- 600:629 ints7 <- 700:729 ints8 <- 800:829 ints9 <- 900:929 df <- data.frame(ints0, ints1, ints2, ints3, ints4, ints5, ints6, ints7, ints8, ints9) colNames <- c("0-99", "100-199", "200-299", "300-399", "400-499", "500-599", "600-699", "700-799", "800-899", "900-999") # construct the table library(basictabler) tbl <- BasicTable$new(compatibility=list(headerCellsAsTD=TRUE, explicitHeaderSpansOfOne=TRUE)) # populate the table tbl$addData(df, explicitColumnHeaders=colNames) # do some merges! tbl$mergeCells(rFrom=10, cFrom=4, rSpan=5, cSpan=3) tbl$cells$setValue(10, 4, rawValue="Merge 1") tbl$mergeCells(rFrom=17, cFrom=6, rSpan=7, cSpan=2) tbl$cells$setValue(17, 6, rawValue="Merge 2!") tbl$mergeCells(rFrom=6, cFrom=9, rSpan=7, cSpan=1) tbl$cells$setValue(6, 9, rawValue="Merge 3!") # do some manipulation tbl$cells$insertRow(4) tbl$cells$setValue(4, 5, rawValue="NEW") tbl$cells$insertRow(8) tbl$cells$setValue(8, 5, rawValue="NEW") tbl$cells$deleteRow(13) tbl$cells$deleteRow(12) tbl$cells$deleteRow(14) tbl$cells$deleteColumn(4) tbl$cells$insertColumn(6) # tbl$renderTable() # prepStr(tbl$print(asCharacter=TRUE), "str") # prepStr(as.character(tbl$getHtml())) str <- "0-99 100-199 200-299 400-499 500-599 600-699 700-799 800-899 900-999 \n 0 100 200 400 500 600 700 800 900 \n 1 101 201 401 501 601 701 801 901 \n NEW \n 2 102 202 402 502 602 702 802 902 \n 3 103 203 403 503 603 703 803 903 \n 4 104 204 404 504 604 704 Merge 3! 904 \n NEW \n 5 105 205 405 505 605 705 805 905 \n 6 106 206 406 506 606 706 806 906 \n 7 107 207 407 507 607 707 807 907 \n 10 110 210 410 510 610 710 810 910 \n 11 111 211 411 511 611 711 811 911 \n 13 113 213 413 513 613 713 813 913 \n 14 114 214 414 514 614 714 814 914 \n 15 115 215 415 Merge 2! 615 715 815 915 \n 16 116 216 416 516 616 716 816 916 \n 17 117 217 417 517 617 717 817 917 \n 18 118 218 418 518 618 718 818 918 \n 19 119 219 419 519 619 719 819 919 \n 20 120 220 420 520 620 720 820 920 \n 21 121 221 421 521 621 721 821 921 \n 22 122 222 422 522 622 722 822 922 \n 23 123 223 423 523 623 723 823 923 \n 24 124 224 424 524 624 724 824 924 \n 25 125 225 425 525 625 725 825 925 \n 26 126 226 426 526 626 726 826 926 \n 27 127 227 427 527 627 727 827 927 \n 28 128 228 428 528 628 728 828 928 \n 29 129 229 429 529 629 729 829 929 " 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 \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
0-99100-199200-299400-499500-599600-699700-799800-899900-999
0100200400500600700800900
1101201401501601701801901
NEW
2102202402502602702802902
3103203403503603703803903
4104204404504604704Merge 3!904
NEW
5105205405505605705905
6106206406506606706906
7107207407507607707907
10110210410610710910
11111211611711811911
13113213413513613713813913
14114214414514614714814914
15115215415Merge 2!715815915
16116216416716816916
17117217417717817917
18118218418718818918
19119219419719819919
20120220420720820920
21121221421721821921
22122222422522622722822922
23123223423523623723823923
24124224424524624724824924
25125225425525625725825925
26126226426526626726826926
27127227427527627727827927
28128228428528628728828928
29129229429529629729829929
" expect_identical(tbl$print(asCharacter=TRUE), str) expect_identical(as.character(tbl$getHtml()), html) })