|
| 1 | +library(progressify) |
| 2 | +options(progressify.debug = TRUE) |
| 3 | + |
| 4 | +message("*** Internals") |
| 5 | + |
| 6 | +# -------------------------------------------------------------------- |
| 7 | +# Debug functions |
| 8 | +# -------------------------------------------------------------------- |
| 9 | +message("debug_indent()") |
| 10 | +oopts <- options(warn = 2L) |
| 11 | +res <- tryCatch(progressify:::debug_indent(delta = -1L), error = identity) |
| 12 | +print(res) |
| 13 | +stopifnot(inherits(res, "error")) |
| 14 | +options(oopts) |
| 15 | + |
| 16 | + |
| 17 | +# -------------------------------------------------------------------- |
| 18 | +# .onLoad() |
| 19 | +# -------------------------------------------------------------------- |
| 20 | +message(".onLoad()") |
| 21 | +progressify:::.onLoad("progressify", "progressify") |
| 22 | + |
| 23 | + |
| 24 | +# -------------------------------------------------------------------- |
| 25 | +# bquote_compile() and bquote_apply() |
| 26 | +# -------------------------------------------------------------------- |
| 27 | +message("bquote_compile() and bquote_apply()") |
| 28 | +bquote_compile <- progressify:::bquote_compile |
| 29 | +bquote_apply <- progressify:::bquote_apply |
| 30 | + |
| 31 | +tmpl <- bquote_compile(function(a = .(X), b = .(Y)) { a + b }) |
| 32 | +expr <- bquote_apply(tmpl, X = 42, Y = NULL) |
| 33 | +f <- formals(eval(expr)) |
| 34 | +stopifnot(identical(f$a, 42)) |
| 35 | +stopifnot(is.null(f$b)) |
| 36 | + |
| 37 | + |
| 38 | +# -------------------------------------------------------------------- |
| 39 | +# import_progressr() and import_from() |
| 40 | +# -------------------------------------------------------------------- |
| 41 | +message("import_progressr() and import_from()") |
| 42 | +import_from <- progressify:::import_from |
| 43 | +import_progressr <- progressify:::import_progressr |
| 44 | + |
| 45 | +fcn <- import_progressr("progressor") |
| 46 | +stopifnot(is.function(fcn)) |
| 47 | + |
| 48 | +fcn <- import_progressr("nonExistingFcn", default = identity) |
| 49 | +stopifnot(identical(fcn, identity)) |
| 50 | + |
| 51 | +res <- tryCatch( |
| 52 | + import_progressr("nonExistingFcn"), |
| 53 | + error = identity |
| 54 | +) |
| 55 | +stopifnot(inherits(res, "error")) |
| 56 | + |
| 57 | +fcn <- import_from("lapply", package = "base") |
| 58 | +stopifnot(identical(fcn, base::lapply)) |
| 59 | + |
| 60 | +fcn <- import_from("nonExisting", package = "base", default = sum) |
| 61 | +stopifnot(identical(fcn, sum)) |
| 62 | + |
| 63 | +res <- tryCatch( |
| 64 | + import_from("nonExisting", package = "base"), |
| 65 | + error = identity |
| 66 | +) |
| 67 | +stopifnot(inherits(res, "error")) |
| 68 | + |
| 69 | + |
| 70 | +# -------------------------------------------------------------------- |
| 71 | +# S3 dispatching |
| 72 | +# -------------------------------------------------------------------- |
| 73 | +message("S3 dispatch") |
| 74 | +is_s3_generic <- progressify:::is_s3_generic |
| 75 | +find_s3_method <- progressify:::find_s3_method |
| 76 | + |
| 77 | +my_generic <- function(x) UseMethod("my_generic") |
| 78 | +stopifnot(is_s3_generic(my_generic)) |
| 79 | +stopifnot(!is_s3_generic(lapply)) |
| 80 | +stopifnot(!is_s3_generic(sum)) |
| 81 | +stopifnot(!is_s3_generic(function() NULL)) |
| 82 | + |
| 83 | +my_generic.default <- function(x) "default" |
| 84 | +my_generic.my_class <- function(x) "my_class" |
| 85 | + |
| 86 | +obj <- structure(list(), class = "my_class") |
| 87 | +res <- find_s3_method(my_generic, "my_generic", quote(my_generic(obj)), envir = environment()) |
| 88 | +stopifnot(identical(res$name, "my_generic.my_class")) |
| 89 | + |
| 90 | +fempty <- function() {} |
| 91 | +res_empty <- find_s3_method(fempty, "fempty", quote(fempty()), envir = environment()) |
| 92 | +stopifnot(is.null(res_empty)) |
| 93 | + |
| 94 | +fdots <- function(...) {} |
| 95 | +res_dots <- find_s3_method(fdots, "fdots", quote(fdots()), envir = environment()) |
| 96 | +stopifnot(is.null(res_dots)) |
| 97 | + |
| 98 | +res_unmatched <- find_s3_method(my_generic, "my_generic", quote(my_generic(a, b, c)), envir = environment()) |
| 99 | +stopifnot(is.null(res_unmatched)) |
| 100 | + |
| 101 | +res_lit <- find_s3_method(my_generic, "my_generic", quote(my_generic(1)), envir = environment()) |
| 102 | +stopifnot(is.null(res_lit)) |
| 103 | + |
| 104 | +res_null <- find_s3_method(my_generic, "my_generic", quote(my_generic(nonexistent_obj)), envir = environment()) |
| 105 | +stopifnot(is.null(res_null)) |
| 106 | + |
| 107 | + |
| 108 | +# -------------------------------------------------------------------- |
| 109 | +# S4 dispatching |
| 110 | +# -------------------------------------------------------------------- |
| 111 | +message("S4 dispatch") |
| 112 | +find_s4_method <- progressify:::find_s4_method |
| 113 | + |
| 114 | +methods::setClass("MyS4Class", slots = list(name = "character")) |
| 115 | +methods::setGeneric("my_s4_generic", function(x) standardGeneric("my_s4_generic")) |
| 116 | +methods::setMethod("my_s4_generic", signature = "MyS4Class", function(x) "s4_method") |
| 117 | + |
| 118 | +obj <- methods::new("MyS4Class", name = "test") |
| 119 | +res <- find_s4_method(my_s4_generic, "my_s4_generic", quote(my_s4_generic(obj)), envir = environment()) |
| 120 | +stopifnot(identical(res$name, "my_s4_generic")) |
| 121 | + |
| 122 | +res_nons4 <- find_s4_method(identity, "identity", quote(identity(1)), envir = environment()) |
| 123 | +stopifnot(is.null(res_nons4)) |
| 124 | + |
| 125 | + |
| 126 | +# -------------------------------------------------------------------- |
| 127 | +# Transpiler registry |
| 128 | +# -------------------------------------------------------------------- |
| 129 | +message("Transpiler registry") |
| 130 | +transpilers_for_package <- progressify:::transpilers_for_package |
| 131 | +transpiler_packages <- progressify:::transpiler_packages |
| 132 | +list_transpilers <- progressify:::list_transpilers |
| 133 | + |
| 134 | +transpilers_for_package(type = "test-type", package = "test-pkg", action = "reset") |
| 135 | +transpilers_for_package(type = "test-type", package = "test-pkg", fcn = function() "test-pkg", action = "add") |
| 136 | + |
| 137 | +res_get <- transpilers_for_package(type = "test-type", package = "test-pkg", action = "get") |
| 138 | +stopifnot(length(res_get) == 1L) |
| 139 | + |
| 140 | +res_list <- transpilers_for_package(type = "test-type", action = "list") |
| 141 | +stopifnot(length(res_list) >= 1L) |
| 142 | + |
| 143 | +res_make <- transpilers_for_package(type = "test-type", package = "test-pkg", action = "make") |
| 144 | +stopifnot(identical(res_make, "test-pkg")) |
| 145 | + |
| 146 | +df_pkgs <- transpiler_packages(classes = "test-type") |
| 147 | +stopifnot(is.data.frame(df_pkgs)) |
| 148 | + |
| 149 | +df_list <- list_transpilers(class = "progressify::built-in") |
| 150 | +stopifnot(is.data.frame(df_list)) |
| 151 | + |
| 152 | +df_filtered <- list_transpilers(pattern = "^ba", class = "progressify::built-in") |
| 153 | +stopifnot(is.data.frame(df_filtered)) |
| 154 | + |
| 155 | + |
| 156 | +# -------------------------------------------------------------------- |
| 157 | +# parse_call() and transpile() edge cases |
| 158 | +# -------------------------------------------------------------------- |
| 159 | +message("parse_call and transpile edge cases") |
| 160 | +parse_call <- progressify:::parse_call |
| 161 | +transpile <- progressify:::transpile |
| 162 | + |
| 163 | +res <- tryCatch({ |
| 164 | + parse_call(quote(1)) |
| 165 | +}, error = function(e) e) |
| 166 | +stopifnot(inherits(res, "error")) |
| 167 | + |
| 168 | +append_call_arguments <- progressify:::append_call_arguments |
| 169 | +res_appended <- append_call_arguments(quote(my_fcn(x)), y = 2) |
| 170 | +stopifnot(identical(res_appended, quote(my_fcn(x, y = 2)))) |
| 171 | + |
| 172 | +register_all_transpilers <- progressify:::register_all_transpilers |
| 173 | +register_all_transpilers() |
0 commit comments