blob: 1b4830bbec409d179051844bb15de73402caea39 [file] [log] [blame]
Damian Thomasf36f9602021-04-28 09:59:51 -04001context("add_indent")
2
3test_that("add_indent can add to 1 row", {
4 observed <- kable(mtcars[1:4, 1:3], "html") %>%
5 add_indent(1) %>%
6 as.character()
7 expected <- "<table>\n <thead>\n <tr>\n <th style=\"text-align:left;\"> </th>\n <th style=\"text-align:right;\"> mpg </th>\n <th style=\"text-align:right;\"> cyl </th>\n <th style=\"text-align:right;\"> disp </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> Mazda RX4 </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:right;\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Mazda RX4 Wag </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:right;\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Datsun 710 </td>\n <td style=\"text-align:right;\"> 22.8 </td>\n <td style=\"text-align:right;\"> 4 </td>\n <td style=\"text-align:right;\"> 108 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Hornet 4 Drive </td>\n <td style=\"text-align:right;\"> 21.4 </td>\n <td style=\"text-align:right;\"> 6 </td>\n <td style=\"text-align:right;\"> 258 </td>\n </tr>\n</tbody>\n</table>"
8 expect_equal(observed, expected)
9})
10
11test_that("add_indent can be added multiple times.", {
12 observed <- kable(mtcars[1:4, 1:3], "html") %>%
13 add_indent(1:3) %>%
14 add_indent(1) %>%
15 as.character()
16 expected <- "<table>\n <thead>\n <tr>\n <th style=\"text-align:left;\"> </th>\n <th style=\"text-align:right;\"> mpg </th>\n <th style=\"text-align:right;\"> cyl </th>\n <th style=\"text-align:right;\"> disp </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style=\"text-align:left;padding-left: 4em;\" indentlevel=\"2\"> Mazda RX4 </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:right;\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> Mazda RX4 Wag </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:right;\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> Datsun 710 </td>\n <td style=\"text-align:right;\"> 22.8 </td>\n <td style=\"text-align:right;\"> 4 </td>\n <td style=\"text-align:right;\"> 108 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Hornet 4 Drive </td>\n <td style=\"text-align:right;\"> 21.4 </td>\n <td style=\"text-align:right;\"> 6 </td>\n <td style=\"text-align:right;\"> 258 </td>\n </tr>\n</tbody>\n</table>"
17 expect_equal(observed, expected)
18})
19
20test_that("add_indent can add to an interior column.", {
21 cars <- mtcars[1:4, 1:3]
22 cars$cyl <- as.character(cars$cyl)
23 observed <- kable(cars, "html") %>%
24 add_indent(1:3, target_cols = 3) %>%
25 as.character()
26 expected <- "<table>\n <thead>\n <tr>\n <th style=\"text-align:left;\"> </th>\n <th style=\"text-align:right;\"> mpg </th>\n <th style=\"text-align:left;\"> cyl </th>\n <th style=\"text-align:right;\"> disp </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style=\"text-align:left;\"> Mazda RX4 </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Mazda RX4 Wag </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Datsun 710 </td>\n <td style=\"text-align:right;\"> 22.8 </td>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> 4 </td>\n <td style=\"text-align:right;\"> 108 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Hornet 4 Drive </td>\n <td style=\"text-align:right;\"> 21.4 </td>\n <td style=\"text-align:left;\"> 6 </td>\n <td style=\"text-align:right;\"> 258 </td>\n </tr>\n</tbody>\n</table>"
27 expect_equal(observed, expected)
28})
29
30test_that("add_indent can add to an interior column multiple times.", {
31 cars <- mtcars[1:4, 1:3]
32 cars$cyl <- as.character(cars$cyl)
33 observed <- kable(cars, "html") %>%
34 add_indent(1:3, target_cols = 3) %>%
35 add_indent(1, target_cols = 3) %>%
36 as.character()
37 expected <- "<table>\n <thead>\n <tr>\n <th style=\"text-align:left;\"> </th>\n <th style=\"text-align:right;\"> mpg </th>\n <th style=\"text-align:left;\"> cyl </th>\n <th style=\"text-align:right;\"> disp </th>\n </tr>\n </thead>\n<tbody>\n <tr>\n <td style=\"text-align:left;\"> Mazda RX4 </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:left;padding-left: 4em;\" indentlevel=\"2\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Mazda RX4 Wag </td>\n <td style=\"text-align:right;\"> 21.0 </td>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> 6 </td>\n <td style=\"text-align:right;\"> 160 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Datsun 710 </td>\n <td style=\"text-align:right;\"> 22.8 </td>\n <td style=\"text-align:left;padding-left: 2em;\" indentlevel=\"1\"> 4 </td>\n <td style=\"text-align:right;\"> 108 </td>\n </tr>\n <tr>\n <td style=\"text-align:left;\"> Hornet 4 Drive </td>\n <td style=\"text-align:right;\"> 21.4 </td>\n <td style=\"text-align:left;\"> 6 </td>\n <td style=\"text-align:right;\"> 258 </td>\n </tr>\n</tbody>\n</table>"
38 expect_equal(observed, expected)
39})