File: test-htmlTable_total.R

package info (click to toggle)
r-cran-htmltable 2.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,600 kB
  • sloc: javascript: 6,797; makefile: 2
file content (74 lines) | stat: -rw-r--r-- 3,015 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
library(testthat)

context("htmlTable - the total argument")
test_that("Throws errors",{
  mx <- matrix(1, ncol=3, nrow=6)
  expect_error(htmlTable(mx, total = c(TRUE, TRUE)))
  expect_error(htmlTable(mx, total = c(TRUE, TRUE),
                         tspanner = letters[1:3], n.tspanner = rep(2, times = 3)))
  expect_error(htmlTable(mx, total = -1))
  expect_error(htmlTable(mx, total = nrow(mx) + 1))
  expect_error(htmlTable(mx, total = "asdasd"))
})

test_that("Correct rows",{
  mx <- matrix(1:6, ncol=3, nrow=6)
  table_str <- htmlTable(mx,
                         css.total = "color: red",
                         total=TRUE)
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>6</td>")

  table_str <- htmlTable(mx,
                         css.total = "color: red",
                         total=4)
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>4</td>")

  table_str <- htmlTable(mx,
                         css.total = "color: red",
                         total=c(4, 2))
  expect_false(grepl("<tr[^>]*>[^>]+color: red[^>]+>[1356789]+</td>", table_str))
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>2</td>")
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>4</td>")

  table_str <- htmlTable(mx,
                         css.total = "color: red",
                         total=c(4, 2))
  expect_false(grepl("<tr[^>]*>[^>]+color: red[^>]+>[1356789]+</td>", table_str))
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>2</td>")
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>4</td>")
})

test_that("Check tspanner", {
  mx <- matrix(1:6, ncol=3, nrow=6)
  table_str <- htmlTable(mx, tspanner = letters[1:2], n.tspanner = c(3, 3),
                         css.total = "color: red",
                         total="tspanner")
  expect_false(grepl("<tr[^>]*>[^>]+color: red[^>]+>[1245789]+</td>", table_str))
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>3</td>")
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>6</td>")
})

test_that("Check choosing css.style", {
  mx <- matrix(1:6, ncol=3, nrow=6)
  table_str <- htmlTable(mx, tspanner = letters[1:2], n.tspanner = c(3, 3),
                         css.total = c("color: red", "color: green"),
                         total="tspanner")
  expect_false(grepl("<tr[^>]*>[^>]+color: red[^>]+>[1245789]+</td>", table_str))
  expect_match(table_str, "<tr[^>]*>[^>]+color: red[^>]+>3</td>")
  expect_match(table_str, "<tr[^>]*>[^>]+color: green[^>]+>6</td>")
})

test_that("The total should be added to the output if used with addmargins", {
  var1 <- LETTERS[1:3]
  var2 <- LETTERS[c(4:5, 5)]
  total_out <-
    table(var1, var2) %>%
    addmargins %>%
    htmlTable(css.total = "background: purple")

  expect_match(total_out, "<td[^>]+background: purple[^>]+>[^>]*Sum</td>",
               info = "Expect the variable name to appear as a cgroup")

  expect_match(total_out, "<th colspan='2'[^>]*>var2",
               info = "Expect the variable name to appear as a cgroup")
})