diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index bba220b..523b46a 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -22,7 +22,7 @@ // search for APT R packages with: `apt-cache search "^r-.*" | sort` ,"ghcr.io/rocker-org/devcontainer-features/apt-packages:1.0.2": { - "packages": "bash-completion,pandoc,qpdf,r-cran-assertthat,r-cran-debugme,r-cran-cli,r-cran-covr,r-cran-dt,r-cran-htmltools,r-cran-huxtable,r-cran-igraph,r-cran-scales" + "packages": "bash-completion,pandoc,qpdf,r-cran-assertthat,r-cran-debugme,r-cran-cli,r-cran-clitable,r-cran-covr,r-cran-dt,r-cran-htmltools,r-cran-igraph" } }, diff --git a/DESCRIPTION b/DESCRIPTION index 11c09f9..d654a0b 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -9,21 +9,20 @@ Description: Manage a collection/library of R source packages. Discover, documen License: GPL (>= 3) Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.3.1 +RoxygenNote: 7.3.2 URL: https://github.com/kforner/srcpkgs BugReports: https://github.com/kforner/srcpkgs/issues Imports: cli, + clitable, devtools, pkgload, testthat, stats, utils Suggests: - huxtable, knitr, rmarkdown, - scales, withr Config/testthat/edition: 3 VignetteBuilder: knitr diff --git a/NAMESPACE b/NAMESPACE index 43739bb..493a939 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -30,5 +30,7 @@ export(pkgs_test) export(reset) export(settings) export(unhack_r_loaders) +importFrom(cli,cli_text) +importFrom(clitable,cli_table) importFrom(testthat,test_dir) importFrom(utils,getFromNamespace) diff --git a/R/1_imports.R b/R/1_imports.R new file mode 100644 index 0000000..6b10cf6 --- /dev/null +++ b/R/1_imports.R @@ -0,0 +1,3 @@ +#' @importFrom cli cli_text +#' @importFrom clitable cli_table +NULL diff --git a/R/pkg_test.R b/R/pkg_test.R index 91c02a8..e402773 100644 --- a/R/pkg_test.R +++ b/R/pkg_test.R @@ -8,7 +8,7 @@ #' @param export_all passed to [pkg_load()]. Enables the test functions to easily access to non-exported #' functions. Caveat: If the pkg is already loaded and up-to-date with export_all=FALSE, it will not work. #' @param ... passed to `testthat::test_dir()` -#' @return the results as a `pkg_test` object, or NULL if no tests found +#' @return the results as a `pkg_test` object, which is an empty listL if no tests were found #' @importFrom testthat test_dir #' @export #' @examples @@ -32,16 +32,22 @@ pkg_test <- function(pkgid, filter = NULL, src_pkgs = get_srcpkgs(), export_all pkg_load(pkg, src_pkgs, quiet = quiet, export_all = export_all) test_path <- file.path(pkg$path, "tests/testthat") - if (!dir.exists(test_path) || length(dir(test_path)) == 0) return(invisible()) - res <- testthat::test_dir(test_path, filter = filter, stop_on_failure = FALSE, ...) + if (!dir.exists(test_path) || length(dir(test_path)) == 0) { + # no tests found, return an empty pkg_test + return(invisible(new_pkg_test(pkg))) + } - attr(res, 'pkg') <- pkg - class(res) <- c('pkg_test', class(res)) + res <- testthat::test_dir(test_path, filter = filter, stop_on_failure = FALSE, ...) - invisible(res) + invisible(new_pkg_test(pkg, res)) } +new_pkg_test <- function(pkg_name, test_results = list()) { + attr(test_results, 'pkg') <- pkg_name + class(test_results) <- c('pkg_test', class(test_results)) + test_results +} # tells if the test is successful # N.B: this is not a roxygen comment ON PURPOSE @@ -80,14 +86,32 @@ fortify_pkg_test <- function(df) { #' @export as.data.frame.pkg_test <- function(x, ...) { + if (!length(x)) { + return(data.frame( + file = character(0), + test = character(0), + nb = integer(0), + failed = integer(0), + passed = integer(0), + skipped = logical(0), + error = logical(0), + warning = integer(0), + time = numeric(0)) + ) + } fortify_pkg_test(NextMethod()) } #' @export print.pkg_test <- function(x, ...) { pkg <- attr(x, 'pkg') - df <- as.data.frame(x) + if (!length(x)) { + cli::cli_h1(paste0("package ", pkg$package, " has no tests")) + return(invisible()) + } + + df <- as.data.frame(x) results <- df$result ### by test @@ -115,6 +139,19 @@ print.pkg_test <- function(x, ...) { # N.B: this is not a roxygen comment ON PURPOSE #' @export summary.pkg_test <- function(object, col = 'file', ...) { + if (!length(object)) { + return(data.frame( + file = character(0), + nb = integer(0), + failed = integer(0), + passed = integer(0), + skipped = logical(0), + error = logical(0), + warning = integer(0), + time = numeric(0)) + ) + } + df <- as.data.frame(object) stop_unless(length(col) <= 1, "col must be a column name or NULL") diff --git a/R/pkgs_test.R b/R/pkgs_test.R index 3c7bf10..afc5ede 100644 --- a/R/pkgs_test.R +++ b/R/pkgs_test.R @@ -24,6 +24,10 @@ pkgs_test <- function(pkgids = names(filter_srcpkgs(src_pkgs, filter)), src_pkgs #' @export as.data.frame.pkgs_test <- function(x, ...) { .row <- function(test_res) { + if (!length(test_res)) { + return(data.frame(nb = 0L, failed = 0L, passed = 0L, skipped = 0L, error = 0L, warning = 0L, time = 0)) + } + if (is_error(test_res)) { # trick to create an empty data frame like test_results diff --git a/R/text_table.R b/R/text_table.R index 8819c12..e15bc10 100644 --- a/R/text_table.R +++ b/R/text_table.R @@ -1,68 +1,6 @@ -print_text_table_with_huxtable <- function(df, title = NULL, footnote = NULL, - heatmap_columns = NULL, hilite_rows = NULL, - styler = huxtable_text_table_default_styler, ...) -{ - ### build the huxtable - tt <- huxtable::as_hux(df) - if (length(footnote)) tt <- huxtable::add_footnote(tt, footnote) - if (length(title)) tt <- huxtable::set_caption(tt, title) - - - if (length(styler)) tt <- styler(tt, heatmap_columns = heatmap_columns, hilite_rows = hilite_rows) - - # now print - lines <- huxtable::to_screen(tt, max_width = Inf, colnames = FALSE) - cat(lines, sep = '\n') - - invisible(tt) -} - -huxtable_text_table_default_styler <- function(tt, - heatmap_columns = NULL, heatmap_colorspace = c('green', 'red'), - hilite_rows = NULL, hilite_bg = 'red' -, fg = 'black', bg = '#f7f7f7') -{ - ### set the title position: top - huxtable::caption_pos(tt) <- 'top' - ### make the header line BOLD - tt <- huxtable::set_bold(tt, 1, huxtable::everywhere, TRUE) - - ### colors - tt <- huxtable::set_background_color(tt, 1, huxtable::everywhere, bg) - tt <- huxtable::set_text_color(tt, 1, huxtable::everywhere, fg) - - ### hilite - tt <- huxtable::set_background_color(tt, hilite_rows + 1, huxtable::everywhere, hilite_bg) - - ### heatmap - if (length(heatmap_columns)) - for (col in heatmap_columns) - tt <- huxtable::map_background_color(tt, huxtable::everywhere, col, huxtable::by_colorspace(heatmap_colorspace)) - - - ### borders - tt <- huxtable::set_bottom_border(tt, 1, huxtable::everywhere, 1) - tt <- huxtable::set_left_border(tt, 1) - tt <- huxtable::set_outer_borders(tt, 1) - tt <- huxtable::set_bottom_border(tt, huxtable::final(1), huxtable::everywhere, 1) - - ### alignment - tt <- huxtable::set_align(tt, value = 'left') - - tt -} - -print_text_table_with_base <- function(df, ...) { - print(df) - invisible() -} - -print_text_table <- function(df, ...) { - if (!requireNamespace("huxtable", quietly = TRUE)) { - print_text_table_with_base(df, ...) - } else { - print_text_table_with_huxtable(df, ...) - } - invisible() -} +print_text_table <- function(df, title = NULL, header_style = "bold", border_style = "double-single", ...) { + if (length(title)) cli::cli_h1(title) + ct <- cli_table(df, header_style = header_style, border_style = border_style, ...) + cat(ct, sep = "\n") +} \ No newline at end of file diff --git a/dev.R b/dev.R index b2706df..67c62c7 100644 --- a/dev.R +++ b/dev.R @@ -1,9 +1,13 @@ library(devtools) - check_man() test() -test(filter = "pkg_load") +test(filter = "config") test(filter = "pkg_check") +test(filter = "pkg_load") +test(filter = "pkg_test") +test(filter = "pkgs_test") check() + +options(width = Sys.getenv("COLUMNS", 80)) diff --git a/man/pkg_test.Rd b/man/pkg_test.Rd index a2ecf5d..8283ce7 100644 --- a/man/pkg_test.Rd +++ b/man/pkg_test.Rd @@ -28,7 +28,7 @@ functions. Caveat: If the pkg is already loaded and up-to-date with export_all=F \item{...}{passed to \code{testthat::test_dir()}} } \value{ -the results as a \code{pkg_test} object, or NULL if no tests found +the results as a \code{pkg_test} object, which is an empty listL if no tests were found } \description{ This function will test a source package using \code{testthat}, diff --git a/tests/testthat/_snaps/pkg_test.md b/tests/testthat/_snaps/pkg_test.md index 2b11048..10fa756 100644 --- a/tests/testthat/_snaps/pkg_test.md +++ b/tests/testthat/_snaps/pkg_test.md @@ -2,43 +2,46 @@ Code print(fix_test_result_timings(res)) - Output - Test results by test for package AA - ┌─────────┬─────────┬────┬────────┬────────┬─────────┬───────┬─────────┬──────┐ - │ file    │ test    │ nb │ failed │ passed │ skipped │ error │ warning │ time │ - ├─────────┼─────────┼────┼────────┼────────┼─────────┼───────┼─────────┼──────┤ - │ error   │ stop    │ 1  │ 0      │ 1      │ FALSE   │ TRUE  │ 0       │ 0    │ - │ failure │ failure │ 1  │ 1      │ 0      │ FALSE   │ FALSE │ 0       │ 0    │ - │ misc    │ misc1   │ 2  │ 1      │ 1      │ FALSE   │ FALSE │ 0       │ 0    │ - │ misc    │ misc2   │ 2  │ 1      │ 0      │ TRUE    │ FALSE │ 0       │ 0    │ - │ misc │ misc3 │ 2 │ 0 │ 2 │ FALSE │ FALSE │ 0 │ 0    │ - │ misc    │ misc4   │ 2  │ 0      │ 1      │ FALSE   │ TRUE  │ 1       │ 0    │ - │ mixed   │ mixed   │ 2  │ 1      │ 1      │ FALSE   │ FALSE │ 0       │ 0    │ - │ skip │ skip │ 1 │ 0 │ 0 │ TRUE │ FALSE │ 0 │ 0    │ - │ success │ success │ 1 │ 0 │ 1 │ FALSE │ FALSE │ 0 │ 0    │ - │ warning │ warn    │ 3  │ 2      │ 0      │ FALSE   │ FALSE │ 1       │ 0    │ - └─────────┴─────────┴────┴────────┴────────┴─────────┴───────┴─────────┴──────┘ + Message - Test results by file for package AA - ┌─────────┬────┬────────┬────────┬─────────┬───────┬─────────┬──────┐ - │ file    │ nb │ failed │ passed │ skipped │ error │ warning │ time │ - ├─────────┼────┼────────┼────────┼─────────┼───────┼─────────┼──────┤ - │ error   │ 1  │ 0      │ 1      │ 0       │ 1     │ 0       │ 0    │ - │ failure │ 1  │ 1      │ 0      │ 0       │ 0     │ 0       │ 0    │ - │ misc    │ 8  │ 2      │ 4      │ 1       │ 1     │ 1       │ 0    │ - │ mixed   │ 2  │ 1      │ 1      │ 0       │ 0     │ 0       │ 0    │ - │ skip │ 1 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0    │ - │ success │ 1 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0    │ - │ warning │ 3  │ 2      │ 0      │ 0       │ 0     │ 1       │ 0    │ - └─────────┴────┴────────┴────────┴─────────┴───────┴─────────┴──────┘ + ── Test results by test for package AA ───────────────────────────────────────── + Output + ╒═══════╤═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │ file │ test │nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ error │ stop │ 1│ 0 │ 1 │ FALSE │ TRUE│ 0 │ 0 │ + │failure│failure│ 1│ 1 │ 0 │ FALSE │FALSE│ 0 │ 0 │ + │ misc │ misc1 │ 2│ 1 │ 1 │ FALSE │FALSE│ 0 │ 0 │ + │ misc │ misc2 │ 2│ 1 │ 0 │ TRUE │FALSE│ 0 │ 0 │ + │ misc │ misc3 │ 2│ 0 │ 2 │ FALSE │FALSE│ 0 │ 0 │ + │ misc │ misc4 │ 2│ 0 │ 1 │ FALSE │ TRUE│ 1 │ 0 │ + │ mixed │ mixed │ 2│ 1 │ 1 │ FALSE │FALSE│ 0 │ 0 │ + │ skip │ skip │ 1│ 0 │ 0 │ TRUE │FALSE│ 0 │ 0 │ + │success│success│ 1│ 0 │ 1 │ FALSE │FALSE│ 0 │ 0 │ + │warning│ warn │ 3│ 2 │ 0 │ FALSE │FALSE│ 1 │ 0 │ + ╘═══════╧═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + Message - Test results overview for package AA - ┌────┬────────┬────────┬─────────┬───────┬─────────┬──────┐ - │ nb │ failed │ passed │ skipped │ error │ warning │ time │ - ├────┼────────┼────────┼─────────┼───────┼─────────┼──────┤ - │ 17 │ 6 │ 7 │ 2 │ 2 │ 2 │ 0 │ - ├────┴────────┴────────┴─────────┴───────┴─────────┴──────┤ - │ Test failed :( ... │ - └─────────────────────────────────────────────────────────┘ + ── Test results by file for package AA ───────────────────────────────────────── + Output + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │ file │nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ error │ 1│ 0 │ 1 │ 0 │ 1 │ 0 │ 0 │ + │failure│ 1│ 1 │ 0 │ 0 │ 0 │ 0 │ 0 │ + │ misc │ 8│ 2 │ 4 │ 1 │ 1 │ 1 │ 0 │ + │ mixed │ 2│ 1 │ 1 │ 0 │ 0 │ 0 │ 0 │ + │ skip │ 1│ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ + │success│ 1│ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ + │warning│ 3│ 2 │ 0 │ 0 │ 0 │ 1 │ 0 │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + Message + ── Test results overview for package AA ──────────────────────────────────────── + Output + ╒══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │nb│failed│passed│skipped│error│warning│time│ + ╞══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │17│ 6 │ 7 │ 2 │ 2 │ 2 │ 0 │ + ╘══╧══════╧══════╧═══════╧═════╧═══════╧════╛ diff --git a/tests/testthat/_snaps/pkgs_test.md b/tests/testthat/_snaps/pkgs_test.md index e4bffd6..b9efa76 100644 --- a/tests/testthat/_snaps/pkgs_test.md +++ b/tests/testthat/_snaps/pkgs_test.md @@ -1,23 +1,78 @@ +# pkgs_test_when_no_tests + + Code + print(res) + Message + + ── Test results by package ───────────────────────────────────────────────────── + Output + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │package│nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ AA │ 0│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ + │ BB │ 0│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + Message + + ── Test results overview ─────────────────────────────────────────────────────── + Output + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │package│nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ 2 │ 0│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + + SUCCESS + +--- + + Code + print(fix_pkg_tests_results_timings(res)) + Message + + ── Test results by package ───────────────────────────────────────────────────── + Output + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │package│nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ AA │ 0│ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ + │ BB │17│ 6 │ 7 │ 2 │ 2 │ 2 │ 0 │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + Message + + ── Test results overview ─────────────────────────────────────────────────────── + Output + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │package│nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ 2 │17│ 6 │ 7 │ 2 │ 2 │ 2 │ 0 │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + + FAILED + # pkgs_test Code - print(fix_pkg_tests_results__timings(res)) + print(fix_pkg_tests_results_timings(res)) + Message + + ── Test results by package ───────────────────────────────────────────────────── Output - Test results by package - ┌─────────┬────┬────────┬────────┬─────────┬───────┬─────────┬──────┐ - │ package │ nb │ failed │ passed │ skipped │ error │ warning │ time │ - ├─────────┼────┼────────┼────────┼─────────┼───────┼─────────┼──────┤ - │ AA │ 17 │ 6 │ 7 │ 2 │ 2 │ 2 │ 0 │ - │ BB │ │ │ │ │ 1 │ │ │ - └─────────┴────┴────────┴────────┴─────────┴───────┴─────────┴──────┘ - - Test results overview - ┌─────────┬────┬────────┬────────┬─────────┬───────┬─────────┬──────┐ - │ package │ nb │ failed │ passed │ skipped │ error │ warning │ time │ - ├─────────┼────┼────────┼────────┼─────────┼───────┼─────────┼──────┤ - │ 2 │ 17 │ 6 │ 7 │ 2 │ 3 │ 2 │ 0 │ - └─────────┴────┴────────┴────────┴─────────┴───────┴─────────┴──────┘ + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │package│nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ AA │17│ 6 │ 7 │ 2 │ 2 │ 2 │ 0 │ + │ BB │NA│ NA │ NA │ NA │ 1 │ NA │ NA │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ + Message + ── Test results overview ─────────────────────────────────────────────────────── + Output + ╒═══════╤══╤══════╤══════╤═══════╤═════╤═══════╤════╕ + │package│nb│failed│passed│skipped│error│warning│time│ + ╞═══════╪══╪══════╪══════╪═══════╪═════╪═══════╪════╡ + │ 2 │17│ 6 │ 7 │ 2 │ 3 │ 2 │ 0 │ + ╘═══════╧══╧══════╧══════╧═══════╧═════╧═══════╧════╛ FAILED diff --git a/tests/testthat/_snaps/text_table.md b/tests/testthat/_snaps/text_table.md deleted file mode 100644 index 7cf84fc..0000000 --- a/tests/testthat/_snaps/text_table.md +++ /dev/null @@ -1,115 +0,0 @@ -# print_text_table_with_base - - Code - print_text_table_with_base(df) - Output - Sepal.Length Sepal.Width Petal.Length Petal.Width Species - 1 5.1 3.5 1.4 0.2 setosa - 2 4.9 3.0 1.4 0.2 setosa - 3 4.7 3.2 1.3 0.2 setosa - 4 4.6 3.1 1.5 0.2 setosa - 5 5.0 3.6 1.4 0.2 setosa - 6 5.4 3.9 1.7 0.4 setosa - -# print_text_table_with_huxtable - - Code - print_text_table_with_huxtable(df) - Output - ┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┐ - │ Sepal.Length │ Sepal.Width │ Petal.Length │ Petal.Width │ Species │ - ├──────────────┼─────────────┼──────────────┼─────────────┼─────────┤ - │ 5.1 │ 3.5 │ 1.4 │ 0.2 │ setosa │ - │ 4.9 │ 3 │ 1.4 │ 0.2 │ setosa │ - │ 4.7 │ 3.2 │ 1.3 │ 0.2 │ setosa │ - │ 4.6 │ 3.1 │ 1.5 │ 0.2 │ setosa │ - │ 5 │ 3.6 │ 1.4 │ 0.2 │ setosa │ - │ 5.4 │ 3.9 │ 1.7 │ 0.4 │ setosa │ - └──────────────┴─────────────┴──────────────┴─────────────┴─────────┘ - - ---- - - Code - print_text_table_with_huxtable(df, styler = NULL) - Output - Sepal.Length Sepal.Width Petal.Length Petal.Width Species - 5.1 3.5 1.4 0.2 setosa - 4.9 3   1.4 0.2 setosa - 4.7 3.2 1.3 0.2 setosa - 4.6 3.1 1.5 0.2 setosa - 5   3.6 1.4 0.2 setosa - 5.4 3.9 1.7 0.4 setosa - - ---- - - Code - print_text_table_with_huxtable(df, title = "Title") - Output - Title - ┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┐ - │ Sepal.Length │ Sepal.Width │ Petal.Length │ Petal.Width │ Species │ - ├──────────────┼─────────────┼──────────────┼─────────────┼─────────┤ - │ 5.1 │ 3.5 │ 1.4 │ 0.2 │ setosa │ - │ 4.9 │ 3 │ 1.4 │ 0.2 │ setosa │ - │ 4.7 │ 3.2 │ 1.3 │ 0.2 │ setosa │ - │ 4.6 │ 3.1 │ 1.5 │ 0.2 │ setosa │ - │ 5 │ 3.6 │ 1.4 │ 0.2 │ setosa │ - │ 5.4 │ 3.9 │ 1.7 │ 0.4 │ setosa │ - └──────────────┴─────────────┴──────────────┴─────────────┴─────────┘ - - ---- - - Code - print_text_table_with_huxtable(df, footnote = "Footnote") - Output - ┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┐ - │ Sepal.Length │ Sepal.Width │ Petal.Length │ Petal.Width │ Species │ - ├──────────────┼─────────────┼──────────────┼─────────────┼─────────┤ - │ 5.1 │ 3.5 │ 1.4 │ 0.2 │ setosa │ - │ 4.9 │ 3 │ 1.4 │ 0.2 │ setosa │ - │ 4.7 │ 3.2 │ 1.3 │ 0.2 │ setosa │ - │ 4.6 │ 3.1 │ 1.5 │ 0.2 │ setosa │ - │ 5 │ 3.6 │ 1.4 │ 0.2 │ setosa │ - │ 5.4 │ 3.9 │ 1.7 │ 0.4 │ setosa │ - ├──────────────┴─────────────┴──────────────┴─────────────┴─────────┤ - │ Footnote │ - └───────────────────────────────────────────────────────────────────┘ - - ---- - - Code - print_text_table_with_huxtable(df, heatmap_columns = c(1, 3)) - Output - ┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┐ - │ Sepal.Length │ Sepal.Width │ Petal.Length │ Petal.Width │ Species │ - ├──────────────┼─────────────┼──────────────┼─────────────┼─────────┤ - │ 5.1          │ 3.5 │ 1.4          │ 0.2 │ setosa │ - │ 4.9          │ 3 │ 1.4          │ 0.2 │ setosa │ - │ 4.7          │ 3.2 │ 1.3          │ 0.2 │ setosa │ - │ 4.6          │ 3.1 │ 1.5          │ 0.2 │ setosa │ - │ 5            │ 3.6 │ 1.4          │ 0.2 │ setosa │ - │ 5.4          │ 3.9 │ 1.7          │ 0.4 │ setosa │ - └──────────────┴─────────────┴──────────────┴─────────────┴─────────┘ - - ---- - - Code - print_text_table_with_huxtable(df, hilite_rows = c(1, 3)) - Output - ┌──────────────┬─────────────┬──────────────┬─────────────┬─────────┐ - │ Sepal.Length │ Sepal.Width │ Petal.Length │ Petal.Width │ Species │ - ├──────────────┼─────────────┼──────────────┼─────────────┼─────────┤ - │ 5.1          │ 3.5         │ 1.4          │ 0.2         │ setosa  │ - │ 4.9 │ 3 │ 1.4 │ 0.2 │ setosa │ - │ 4.7          │ 3.2         │ 1.3          │ 0.2         │ setosa  │ - │ 4.6 │ 3.1 │ 1.5 │ 0.2 │ setosa │ - │ 5 │ 3.6 │ 1.4 │ 0.2 │ setosa │ - │ 5.4 │ 3.9 │ 1.7 │ 0.4 │ setosa │ - └──────────────┴─────────────┴──────────────┴─────────────┴─────────┘ - - diff --git a/tests/testthat/helper-functions.R b/tests/testthat/helper-functions.R index 5aa5e44..b599f7d 100644 --- a/tests/testthat/helper-functions.R +++ b/tests/testthat/helper-functions.R @@ -66,7 +66,7 @@ restore_init <- function(previous) { } } -fix_pkg_tests_results__timings <- function(res, time = 0) { +fix_pkg_tests_results_timings <- function(res, time = 0) { for (i in seq_along(res)) { if (is_error(res[[i]])) next res[[i]] <- fix_test_result_timings(res[[i]], time) diff --git a/tests/testthat/test-pkg_test.R b/tests/testthat/test-pkg_test.R index ad6c59b..038eb65 100644 --- a/tests/testthat/test-pkg_test.R +++ b/tests/testthat/test-pkg_test.R @@ -16,8 +16,25 @@ test_that("pkg_test", { src_pkgs <- examples_srcpkgs_basic() ### pkg with no tests - expect_null(pkg_test('AA', src_pkgs = src_pkgs, reporter = "silent")) - expect_null(pkg_test('BB', src_pkgs = src_pkgs, reporter = "silent")) + is_empty_pkg_test <- function(x) inherits(x, "pkg_test") && is.list(x) && length(x) == 0 + + res <- pkg_test('AA', src_pkgs = src_pkgs, reporter = "silent") + expect_true(is_empty_pkg_test(res)) + expect_true(is_empty_pkg_test(pkg_test('BB', src_pkgs = src_pkgs, reporter = "silent"))) + + # as.data.frame + df <- as.data.frame(res) + + expect_identical(names(df), c("file", "test", "nb", "failed", "passed", "skipped", "error", "warning", "time")) + expect_equal(nrow(df) ,0) + + # no tests --> no failure + expect_true(as.logical(res)) + expect_match(capture.output(print(res), type = "message"), "package AA has no tests", fixed = TRUE, all = FALSE) + + sdf <- summary(res) + expect_identical(names(sdf), c("file", "nb", "failed", "passed", "skipped", "error", "warning", "time")) + expect_equal(nrow(sdf) ,0) ###### with tests add_dummy_test_to_srcpkgs(src_pkgs) @@ -49,7 +66,7 @@ test_that("pkg_test", { .pkg_test_s3_methods <- test_that("pkg_test_s3_methods", { - local_reproducible_output(crayon = TRUE) + local_reproducible_output(crayon = TRUE, unicode = TRUE) src_pkgs <- examples_srcpkgs_complex_deps() add_dummy_test_to_srcpkgs(src_pkgs) diff --git a/tests/testthat/test-pkgs_test.R b/tests/testthat/test-pkgs_test.R index af4f3b0..4698e6a 100644 --- a/tests/testthat/test-pkgs_test.R +++ b/tests/testthat/test-pkgs_test.R @@ -1,4 +1,64 @@ +.pkgs_test_when_no_tests <- +test_that("pkgs_test_when_no_tests", { + src_pkgs <- examples_srcpkgs_basic() + + ### no tests at all + res <- pkgs_test(src_pkgs = src_pkgs, reporter = "silent", quiet = TRUE) + + expect_s3_class(res, "pkgs_test") + expect_identical(names(res), c("AA", "BB")) + + ## all empty since no tests + expect_s3_class(res$AA, "pkg_test") + expect_s3_class(res$BB, "pkg_test") + expect_length(res$AA, 0) + expect_length(res$BB, 0) + + # as.data.frame + df <- as.data.frame(res) + + expect_identical(df$package, c("AA", "BB")) + expect_true(all(df[, -1] == 0)) + + # as.logical + expect_true(as.logical(res)) + + # summary + sdf <- summary(res) + expect_equal(sdf$package, 2) + expect_true(all(sdf[, -1] == 0)) + + # print + local_reproducible_output(crayon = TRUE, unicode = TRUE) + expect_snapshot(print(res)) + + ### only BB has tests + add_dummy_test_to_srcpkg(src_pkgs$BB) + + res <- pkgs_test(src_pkgs = src_pkgs, reporter = "silent", quiet = TRUE) + expect_s3_class(res, "pkgs_test") + + # as.data.frame + df <- as.data.frame(res) + + expect_identical(df$package, c("AA", "BB")) + expect_true(all(df[1, -1] == 0)) + expect_equal(df["BB", "nb"], 17) + + # as.logical + expect_false(as.logical(res)) + + # summary + sdf <- summary(res) + expect_equal(sdf$package, 2) + expect_equal(sdf[, -1], df[2, -1], ignore_attr = TRUE) + + # print + local_reproducible_output(crayon = TRUE, unicode = TRUE) + expect_snapshot(print(fix_pkg_tests_results_timings(res))) +}) + .pkgs_test <- test_that("pkgs_test", { @@ -100,7 +160,8 @@ test_that("pkgs_test", { expect_identical(sdf, expected) ### print - expect_snapshot(print(fix_pkg_tests_results__timings(res))) + local_reproducible_output(crayon = TRUE, unicode = TRUE) + expect_snapshot(print(fix_pkg_tests_results_timings(res))) }) diff --git a/tests/testthat/test-text_table.R b/tests/testthat/test-text_table.R deleted file mode 100644 index 29cfe7d..0000000 --- a/tests/testthat/test-text_table.R +++ /dev/null @@ -1,28 +0,0 @@ - - -test_that("print_text_table_with_base", { - df <- head(iris) - expect_snapshot(print_text_table_with_base(df)) -}) - -test_that("print_text_table_with_huxtable", { - # we enable crayon and color output for this test - local_reproducible_output(crayon = TRUE) - df <- head(iris) - - withr::local_options(list(cli.num_colors = 256)) - - expect_snapshot(print_text_table_with_huxtable(df)) - expect_snapshot(print_text_table_with_huxtable(df, styler = NULL)) - - expect_snapshot(print_text_table_with_huxtable(df, title = "Title")) - - expect_snapshot(print_text_table_with_huxtable(df, footnote = "Footnote")) - - expect_snapshot(print_text_table_with_huxtable(df, heatmap_columns = c(1, 3))) - - expect_snapshot(print_text_table_with_huxtable(df, hilite_rows = c(1, 3))) - -}) - -