Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use native pipe in testthat #179

Merged
merged 2 commits into from
Jun 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Suggests:
rmarkdown,
stringi,
testthat,
tidyr,
xml2
Config/testthat/edition: 3
Roxygen: list(markdown = TRUE)
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-developer-testing-rtf_convert_format.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ test_that("Report ERROR if libreoffice7.1 is not avaiable", {
skip_on_cran()
skip_on_os(c("windows", "mac", "solaris"))
file <- file.path(tempdir(), "tmp.rtf")
iris %>%
head() %>%
rtf_body() %>%
rtf_encode() %>%
iris |>
head() |>
rtf_body() |>
rtf_encode() |>
write_rtf(file)
if (system("which libreoffice7.1", ignore.stderr = TRUE, ignore.stdout = TRUE) == 1) {
expect_error(rtf_convert_format(input = file, output_dir = tempdir()))
Expand Down
16 changes: 8 additions & 8 deletions tests/testthat/test-independent-testing-as_rtf_colheader.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
test_that("Case when colheader equals to NULL", {
x <- iris %>% rtf_colheader(colheader = "")
x <- iris |> rtf_colheader(colheader = "")
expect_null(as_rtf_colheader(x))

x <- iris %>% rtf_colheader(colheader = "", border_color_left = "red")
x <- iris |> rtf_colheader(colheader = "", border_color_left = "red")
expect_null(as_rtf_colheader(x))
})

test_that("Case when colheader is not NULL and border color is specified", {
x <- iris %>% rtf_colheader(colheader = "a | b| c", border_color_left = "green")
x <- iris |> rtf_colheader(colheader = "a | b| c", border_color_left = "green")
expect_snapshot_output(as_rtf_colheader(x))

x <- iris %>% rtf_colheader(colheader = "a | b| c", border_color_left = "red", border_color_right = "red")
x <- iris |> rtf_colheader(colheader = "a | b| c", border_color_left = "red", border_color_right = "red")
expect_snapshot_output(as_rtf_colheader(x))
})

test_that("case when colheader is not NULL and border type is specified", {
x <- iris %>% rtf_colheader(colheader = "a | b| c", border_right = "double")
x <- iris |> rtf_colheader(colheader = "a | b| c", border_right = "double")
expect_snapshot_output(as_rtf_colheader(x))

x <- iris %>% rtf_colheader(colheader = "a | b| c", border_top = "dot dash")
x <- iris |> rtf_colheader(colheader = "a | b| c", border_top = "dot dash")
expect_snapshot_output(as_rtf_colheader(x))
})

test_that("case when colheader is not NULL and cell text formats is specified", {
x <- iris %>% rtf_colheader(colheader = "a | b| c", text_format = "i")
x <- iris |> rtf_colheader(colheader = "a | b| c", text_format = "i")
expect_snapshot_output(as_rtf_colheader(x))

x <- iris %>% rtf_colheader(colheader = "a | b| c", text_format = "b")
x <- iris |> rtf_colheader(colheader = "a | b| c", text_format = "b")
expect_snapshot_output(as_rtf_colheader(x))
})
80 changes: 40 additions & 40 deletions tests/testthat/test-independent-testing-as_rtf_pageby.R
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
library(dplyr)
svg(tempfile("tmp.svg"))
test_that("Test for case when output has title, footnote and source", {
x <- iris[1:2, ] %>%
rtf_title("Title") %>%
rtf_footnote("Footnote") %>%
rtf_source("DataSource") %>%
rtf_body() %>%
rtf_pageby(page_by = "Species", new_page = TRUE, pageby_header = TRUE) %>%
x <- iris[1:2, ] |>
rtf_title("Title") |>
rtf_footnote("Footnote") |>
rtf_source("DataSource") |>
rtf_body() |>
rtf_pageby(page_by = "Species", new_page = TRUE, pageby_header = TRUE) |>
as_rtf_pageby()

expect_snapshot_output(x)
})

test_that("Test the pageby rows are the last rows of a page", {
x <- iris %>%
rtf_title("Title") %>%
rtf_footnote("Footnote") %>%
rtf_source("DataSource") %>%
rtf_body() %>%
x <- iris |>
rtf_title("Title") |>
rtf_footnote("Footnote") |>
rtf_source("DataSource") |>
rtf_body() |>
rtf_pageby(page_by = "Species", new_page = TRUE, pageby_header = FALSE)

y <- as.data.frame(attributes(as_rtf_pageby(x))) %>%
group_by(info.id) %>%
summarise_all(last) %>%
y <- as.data.frame(attributes(as_rtf_pageby(x))) |>
group_by(info.id) |>
summarise_all(last) |>
mutate(row = substr(as.character(info.page), 1, 1))

z <- as.data.frame(table(x$Species)) %>%
z <- as.data.frame(table(x$Species)) |>
mutate(cu = cumsum(Freq))
z <- z %>% mutate(cum = cu + as.numeric(rownames(z)))
z <- z |> mutate(cum = cu + as.numeric(rownames(z)))

expect_equal(y$row, rownames(y))
expect_equal(y$info.index, z$cum)
expect_false(all(y$info.pageby))
})

test_that("Test if border type/color for first/last row are assigned correctly", {
x <- iris %>%
x <- iris |>
rtf_body(
border_first = c("single", "dash", "dot", "triple", ""),
border_last = c("single", "dash", "dot", "triple", ""),
border_color_top = c("black", "gold", "ivory", "blue", "white"),
border_color_bottom = c("black", "gold", "ivory", "blue", "white"),
border_color_first = c("azure", "gold", "ivory", "bisque", "white"),
border_color_last = c("red", "gold", "ivory", "bisque", "white")
) %>%
rtf_pageby(page_by = "Species", new_page = TRUE, pageby_header = TRUE) %>%
) |>
rtf_pageby(page_by = "Species", new_page = TRUE, pageby_header = TRUE) |>
as_rtf_pageby()

attr(x, "info") <- NULL
Expand All @@ -53,8 +53,8 @@ test_that("Test if border type/color for first/last row are assigned correctly",
})

test_that("Test if page_dict attribute is created for tbl", {
x <- iris %>%
rtf_body() %>%
x <- iris |>
rtf_body() |>
rtf_pageby(page_by = "Species", new_page = TRUE, pageby_header = FALSE)

y <- as_rtf_pageby(x)
Expand All @@ -65,11 +65,11 @@ test_that("Test if page_dict attribute is created for tbl", {


test_that("Test for more than one page_by var", {
x <- iris %>%
mutate(cat = rep(1:5, 30)) %>%
arrange(Species, cat) %>%
rtf_page(nrow = 5) %>%
rtf_body() %>%
x <- iris |>
mutate(cat = rep(1:5, 30)) |>
arrange(Species, cat) |>
rtf_page(nrow = 5) |>
rtf_body() |>
rtf_pageby(page_by = c("Species", "cat"), new_page = TRUE, pageby_header = TRUE)

y <- as_rtf_pageby(x)
Expand All @@ -81,8 +81,8 @@ test_that("Test for more than one page_by var", {
test_that("Test if new_page is FALSE and group_by is NOT NULL", {
iris1 <- iris
iris1$cat <- iris1$Species
x <- iris1 %>%
rtf_body(group_by = "cat") %>%
x <- iris1 |>
rtf_body(group_by = "cat") |>
rtf_pageby(page_by = "Species", new_page = FALSE, pageby_header = FALSE)
y <- as_rtf_pageby(x)

Expand All @@ -92,46 +92,46 @@ test_that("Test if new_page is FALSE and group_by is NOT NULL", {
})

test_that("Test whether lines with '-----' were removed correctly", {
x <- distinct(iris %>% subset(Species != "virginica"), Species, .keep_all = T) %>%
mutate(Species = ifelse(Species == "setosa", "-----", Species)) %>%
x <- distinct(iris |> subset(Species != "virginica"), Species, .keep_all = T) |>
mutate(Species = ifelse(Species == "setosa", "-----", Species)) |>
rtf_colheader("Sepal.Length | Sepal.Width | Petal.Length | Petal.Width",
col_rel_width = rep(1, 4)
) %>%
) |>
rtf_body(
page_by = "Species",
text_justification = c(rep("c", 4), "l"),
border_top = c(rep("", 4), "single"),
border_bottom = c(rep("", 4), "single")
) %>%
) |>
rtf_encode()

expect_snapshot_output(x$body)
})


test_that("Test when using subline_by and page_by together in rtf_body", {
tbl1 <- iris[c(1:4, 51:54), 3:5] %>%
mutate(s2 = paste0(Species, 1:2), s3 = s2) %>%
arrange(Species, s2) %>%
tbl1 <- iris[c(1:4, 51:54), 3:5] |>
mutate(s2 = paste0(Species, 1:2), s3 = s2) |>
arrange(Species, s2) |>
rtf_body(
subline_by = "s2",
page_by = "Species"
) %>%
) |>
rtf_encode()

expect_snapshot_output(tbl1$body)
})


test_that("Test when using subline_by and page_by with pageby_row = 'first_row' in rtf_body", {
tbl2 <- iris[c(1:4, 51:54), 3:5] %>%
mutate(s2 = paste0(Species, 1:2), s3 = s2) %>%
arrange(Species, s2) %>%
tbl2 <- iris[c(1:4, 51:54), 3:5] |>
mutate(s2 = paste0(Species, 1:2), s3 = s2) |>
arrange(Species, s2) |>
rtf_body(
subline_by = "s2",
page_by = "Species",
pageby_row = "first_row"
) %>%
) |>
rtf_encode()

expect_snapshot_output(tbl2$body)
Expand Down
12 changes: 6 additions & 6 deletions tests/testthat/test-independent-testing-as_rtf_paragraph.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
tbl <- iris[1, ] %>%
rtf_body() %>%
tbl <- iris[1, ] |>
rtf_body() |>
rtf_title("title")
teststr <- attr(tbl, "rtf_title")

test_that("test if title is converted to RTF correctly", {
expect_snapshot_output(as_rtf_paragraph(teststr))
})

tbl <- iris[1, ] %>%
rtf_body() %>%
tbl <- iris[1, ] |>
rtf_body() |>
rtf_footnote("footnote")
teststr <- attr(tbl, "rtf_footnote")

test_that("test if footnote is converted to RTF correctly", {
expect_snapshot_output(as_rtf_paragraph(teststr))
})

tbl <- iris[1, ] %>%
rtf_body() %>%
tbl <- iris[1, ] |>
rtf_body() |>
rtf_source("source")
teststr <- attr(tbl, "rtf_source")

Expand Down
48 changes: 24 additions & 24 deletions tests/testthat/test-independent-testing-as_rtf_table.R
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
test_that("Case in defalut", {
x <- iris[1:1, ] %>%
dplyr::select(1) %>%
rtf_body() %>%
as_rtf_table() %>%
x <- iris[1:1, ] |>
dplyr::select(1) |>
rtf_body() |>
as_rtf_table() |>
strsplit("\n")

expect_snapshot_output(x[[1]])
})

test_that("Case for border_color_left and border_color_top", {
x <- iris[1:1, ] %>%
dplyr::select(1) %>%
rtf_body(border_color_left = "red", border_color_top = "blue") %>%
as_rtf_table() %>%
x <- iris[1:1, ] |>
dplyr::select(1) |>
rtf_body(border_color_left = "red", border_color_top = "blue") |>
as_rtf_table() |>
strsplit("\n")

expect_snapshot_output(x[[1]])
})

test_that("Case for having group_by without page_by", {
x <- iris[1:2, 4:5] %>%
rtf_body(group_by = "Species") %>%
as_rtf_table() %>%
x <- iris[1:2, 4:5] |>
rtf_body(group_by = "Species") |>
as_rtf_table() |>
strsplit("\n")

expect_snapshot_output(x[[1]])
Expand All @@ -30,40 +30,40 @@ test_that("Case for having group_by without page_by", {

# add additional test to increase coverage and for new feature
test_that("Test case when subline is not NULL", {
x <- iris[c(1:4, 51:54), 3:5] %>%
mutate(s2 = paste0(Species, 1:2), s3 = s2) %>%
arrange(Species, s2) %>%
x <- iris[c(1:4, 51:54), 3:5] |>
mutate(s2 = paste0(Species, 1:2), s3 = s2) |>
arrange(Species, s2) |>
rtf_body(
subline_by = "Species",
page_by = "s2",
) %>%
as_rtf_table() %>%
) |>
as_rtf_table() |>
strsplit("\n")

expect_snapshot_output(x[[1]])


data(r2rtf_adae)
ae <- r2rtf_adae[200:260, ] %>%
ae <- r2rtf_adae[200:260, ] |>
arrange(SITEID, TRTA, USUBJID, ASTDY)

ae <- ae %>%
ae <- ae |>
mutate(
AEDECODNUM = as.character(rownames(ae)),
SUBLINEBY = paste0(
"Trial Number: ", STUDYID,
", Site Number: ", SITEID
),
) %>%
select(USUBJID, ASTDY, AEDECODNUM, TRTA, SUBLINEBY) %>%
arrange(SUBLINEBY, TRTA, USUBJID, ASTDY) %>%
rtf_colheader("Subject| Rel Day | Adverse Code|") %>%
) |>
select(USUBJID, ASTDY, AEDECODNUM, TRTA, SUBLINEBY) |>
arrange(SUBLINEBY, TRTA, USUBJID, ASTDY) |>
rtf_colheader("Subject| Rel Day | Adverse Code|") |>
rtf_body(
subline_by = "SUBLINEBY",
page_by = c("TRTA"),
group_by = c("USUBJID", "ASTDY"),
) %>%
as_rtf_table() %>%
) |>
as_rtf_table() |>
strsplit("\n")

expect_snapshot_output(ae[[1]])
Expand Down
14 changes: 7 additions & 7 deletions tests/testthat/test-independent-testing-assemble.R
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# test bulletproofing
file <- replicate(2, tempfile(fileext = ".rtf"))
file1 <- head(iris) %>%
rtf_body() %>%
rtf_encode() %>%
file1 <- head(iris) |>
rtf_body() |>
rtf_encode() |>
write_rtf(file[1])
file2 <- head(cars) %>%
rtf_page(orientation = "landscape") %>%
rtf_body() %>%
rtf_encode() %>%
file2 <- head(cars) |>
rtf_page(orientation = "landscape") |>
rtf_body() |>
rtf_encode() |>
write_rtf(file[2])

test_that("rtf_assemble: bulletproofing argument landscape", {
Expand Down
Loading