Skip to content

Commit bb65cda

Browse files
committed
Add tests
1 parent 6453535 commit bb65cda

File tree

1 file changed

+111
-0
lines changed

1 file changed

+111
-0
lines changed

tests/testthat/test-table-helper.R

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
test_that("tbl_qmd_span generates correct HTML with base64 encoding", {
2+
expect_match(
3+
tbl_qmd_span("**bold text**"),
4+
"<span data-qmd-base64=\"Kipib2xkIHRleHQqKg==\">**bold text**</span>",
5+
fixed = TRUE
6+
)
7+
expect_match(
8+
tbl_qmd_span("$\\alpha + \\beta$", display = "Greek formula"),
9+
"<span data-qmd-base64=\"JFxhbHBoYSArIFxiZXRhJA==\">Greek formula</span>",
10+
fixed = TRUE
11+
)
12+
expect_match(
13+
tbl_qmd_span("**bold text**", class = "highlight"),
14+
"<span data-qmd-base64=\"Kipib2xkIHRleHQqKg==\" class=\"highlight\">**bold text**</span>",
15+
fixed = TRUE
16+
)
17+
expect_match(
18+
tbl_qmd_span(
19+
"**Important note**",
20+
attrs = list(title = "Hover for more info")
21+
),
22+
"<span data-qmd-base64=\"KipJbXBvcnRhbnQgbm90ZSoq\" title=\"Hover for more info\">**Important note**</span>",
23+
fixed = TRUE
24+
)
25+
})
26+
27+
test_that("tbl_qmd_span_raw generates correct HTML with raw encoding", {
28+
expect_match(
29+
tbl_qmd_span_raw("Simple text"),
30+
"<span data-qmd=\"Simple text\">Simple text</span>",
31+
fixed = TRUE
32+
)
33+
})
34+
35+
test_that("tbl_qmd_div generates correct HTML with base64 encoding", {
36+
# Test with default base64 encoding
37+
expect_match(
38+
tbl_qmd_div("## Section Title\n\nContent here"),
39+
"<div data-qmd-base64=\"IyMgU2VjdGlvbiBUaXRsZQoKQ29udGVudCBoZXJl\">## Section Title\n\nContent here</div>",
40+
fixed = TRUE
41+
)
42+
43+
# Test with display text
44+
expect_match(
45+
tbl_qmd_div(
46+
"{{< video https://example.com >}}",
47+
display = "[Video content]"
48+
),
49+
"<div data-qmd-base64=\"e3s8IHZpZGVvIGh0dHBzOi8vZXhhbXBsZS5jb20gPn19\">[Video content]</div>",
50+
fixed = TRUE
51+
)
52+
53+
expect_match(
54+
tbl_qmd_div("Content here", class = "something"),
55+
"<div data-qmd-base64=\"Q29udGVudCBoZXJl\" class=\"something\">Content here</div>",
56+
fixed = TRUE
57+
)
58+
expect_match(
59+
tbl_qmd_div(
60+
"Content here",
61+
attrs = list(id = "special-note", tabindex = "0")
62+
),
63+
"<div data-qmd-base64=\"Q29udGVudCBoZXJl\" id=\"special-note\" tabindex=\"0\">Content here</div>",
64+
fixed = TRUE
65+
)
66+
})
67+
68+
test_that("tbl_qmd_div_raw generates correct HTML with raw encoding", {
69+
result <- tbl_qmd_div_raw("## Simple header")
70+
expect_true(grepl("<div data-qmd=", result))
71+
expect_false(grepl("<div data-qmd-base64=", result))
72+
})
73+
74+
test_that(".validate_tbl_qmd_input validates inputs correctly", {
75+
# Valid inputs should return TRUE
76+
expect_true(.validate_tbl_qmd_input("content"))
77+
expect_true(.validate_tbl_qmd_input("content", "display"))
78+
79+
# Invalid content should throw error
80+
expect_error(.validate_tbl_qmd_input(c("multiple", "strings")))
81+
expect_error(.validate_tbl_qmd_input(123))
82+
83+
# Invalid display should throw error
84+
expect_error(.validate_tbl_qmd_input("content", c("multiple", "displays")))
85+
expect_error(.validate_tbl_qmd_input("content", 123))
86+
})
87+
88+
test_that("tbl_qmd_div_base64 and tbl_qmd_span_base64 explicitly use base64 encoding", {
89+
expect_identical(
90+
tbl_qmd_div_base64("Content"),
91+
tbl_qmd_div("Content", use_base64 = TRUE)
92+
)
93+
expect_identical(
94+
tbl_qmd_span_base64("Content"),
95+
tbl_qmd_span("Content", use_base64 = TRUE)
96+
)
97+
})
98+
99+
test_that(".tbl_qmd_element correctly handles different tag types", {
100+
expect_match(
101+
.tbl_qmd_element("span", "content", "display", TRUE),
102+
"<span data-qmd-base64=\"Y29udGVudA==\">display</span>",
103+
fixed = TRUE
104+
)
105+
106+
expect_match(
107+
.tbl_qmd_element("div", "content", "display", TRUE),
108+
"<div data-qmd-base64=\"Y29udGVudA==\">display</div>",
109+
fixed = TRUE
110+
)
111+
})

0 commit comments

Comments
 (0)