torture.R 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. # Kernel↔wasm R parity torture fixture — every hook branch, extractCall shape,
  2. # and known-gap behavior from docs/design/r-kernel-port-checklist.md. Parses
  3. # CLEAN (no ERROR/MISSING) — deferral shapes live in the test file, and the
  4. # CRLF variant is derived in-memory by kernel-r-parity.test.ts.
  5. #' Roxygen title for top_fn (dropped — R nodes never carry docstrings)
  6. #' @param a first
  7. top_fn <- function(a, b = 2, ...) {
  8. a + b
  9. }
  10. # plain comment run above eq_fn (also dropped)
  11. # second line
  12. eq_fn = function(x) x * 2
  13. lam <- \(x) x + 1
  14. gfun <<- function() 0
  15. (function(x) x * 3) -> trpl
  16. function(y) y - 1 -> ghost
  17. MAX_RETRIES <- 3L
  18. A.CONST = 2.5
  19. lower_var <- "hello"
  20. dotted.var <- 1
  21. x2 <<- 4
  22. 9 -> right_var
  23. 10 ->> RIGHT.CONST
  24. chain_a <- chain_b <- 5
  25. nester <- function(x) {
  26. inner <- function(y) {
  27. innermost <- function(z) z + 1
  28. innermost(y)
  29. }
  30. CAPS_LOCAL <- 99
  31. z <- inner(x)
  32. log_it(z)
  33. z
  34. }
  35. if (TRUE) f_in_if <- function() 1
  36. {
  37. hidden_var <- 42
  38. braced_fn <- function() 2
  39. }
  40. top_call(nested_call(1))
  41. x %>% p_one() %>% p_two()
  42. z |> p_three()
  43. res <- data %>% p_four()
  44. # --- imports -----------------------------------------------------------------
  45. library(dplyr)
  46. require(stats)
  47. requireNamespace("jsonlite")
  48. loadNamespace("tools")
  49. source("helpers.R")
  50. source(file.path("R", "dyn.R"))
  51. library()
  52. suppressPackageStartupMessages(library(quietpkg))
  53. base::library(magrittr)
  54. library(help = docpkg)
  55. requireNamespace(quietly = TRUE, package = "namedpkg")
  56. library("")
  57. pkg::fn_q(1)
  58. pkg:::fn_h(2)
  59. use_it <- function() {
  60. library(inside_fn)
  61. fn_q(3)
  62. }
  63. # --- classes -----------------------------------------------------------------
  64. setClass("Patient", representation(name = "character"), contains = "Person")
  65. setGeneric("describe", function(obj) standardGeneric("describe"))
  66. setMethod("describe", "Patient", function(obj) {
  67. fmt(obj)
  68. })
  69. Account <- setRefClass("Account",
  70. fields = list(balance = "numeric"),
  71. contains = "BaseAccount",
  72. methods = list(
  73. deposit = function(x) {
  74. balance <<- balance + x
  75. audit(x)
  76. },
  77. withdraw = function(x) balance <<- balance - x
  78. ))
  79. Stack <- R6Class("Stack",
  80. inherit = AbstractCollection,
  81. public = list(
  82. items = NULL,
  83. push = function(x) {
  84. self$items <- c(self$items, x)
  85. invisible(self)
  86. }
  87. ),
  88. private = list(
  89. validate_it = function() TRUE
  90. ),
  91. active = list(
  92. size = function() length(private$items)
  93. ))
  94. GeomX <- ggproto("GeomX", Geom,
  95. extra_param = "no",
  96. draw_panel = function(data, panel) {
  97. render_geom(data)
  98. }
  99. )
  100. methods::setClass("QualClass", contains = "QBase")
  101. R6::R6Class("QualR6", public = list(qm = function() do_q()))
  102. Gen <- R6Class(GenName, public = list(gm = function() 1))
  103. BadGG <- ggproto(NULL, Geom, draw_key = function(x) render_key(x))
  104. NoInherit <- R6Class("NoInherit", inherit = pkg::Parent)
  105. factory <- function() {
  106. Local <- setRefClass("LocalCls", methods = list(lm = function() lcall()))
  107. Local
  108. }
  109. Empty <- R6Class("Empty", public = list())
  110. Pos <- setRefClass("PosCls", methods = list(function() 1))
  111. s3.method <- print.myclass <- NULL
  112. print.data.frame2 <- function(x, ...) {
  113. format_it(x)
  114. }
  115. env$attached <- function(x) side_call(x)
  116. "strname" <- function() 1
  117. setMethod("show", signature("Cls"), function(object) cat_it(object))
  118. setGeneric("area")
  119. setValidity("Cls", function(object) TRUE)
  120. Late <- R6Class("Late",
  121. public = list(pm = function() p_call()),
  122. inherit = LateBase)
  123. # --- call zoo & lhs shapes ---------------------------------------------------
  124. "strassign" <- 6
  125. x[1] <- 7
  126. attr(x, "who") <- 8
  127. names(x) <- c("a")
  128. obj$field <- 9
  129. obj@slot <- 10
  130. assign("via_assign", 11)
  131. delayedAssign("lazy_one", compute_it())
  132. makeActiveBinding("active_one", function() 1, environment())
  133. obj$meth(3)
  134. lst$a$b(4)
  135. o@s$m(5)
  136. Negate(`%in%`)(6)
  137. "strfn"(7)
  138. (handler)(8)
  139. lst[[1]](9)
  140. `weird name` <- 12
  141. `%+%` <- function(a, b) paste(a, b)
  142. result <- if (cond) f_yes() else f_no()
  143. for (i in seq_len(10)) body_call(i)
  144. while (keep_going()) step_once()
  145. repeat break
  146. local({
  147. local_hidden <- 13
  148. local_fn <- function() 14
  149. })
  150. try(risky_call())
  151. Recall(1)
  152. UseMethod("generic_dispatch")
  153. do.call("dyn_target", list(1))
  154. do.call(real_target, list(2))
  155. match.fun("fun_by_name")(3)
  156. stopifnot(is_ok(x))
  157. on.exit(cleanup_fn())
  158. invisible(NULL)
  159. # --- return-as-named-node, duplicates, right-assign in a body ----------------
  160. f <- function(x) {
  161. if (x > 0) return(g(x))
  162. h(x)
  163. }
  164. ret_val <- return
  165. setMethod("area", "Sq", area_impl)
  166. require(pkg2, quietly = TRUE)
  167. dupline <- function() 1; dupline <- function() 2
  168. dup_var <- 1; dup_var <- 2
  169. runner <- function() {
  170. fetch() -> got
  171. got
  172. }
  173. dt[, b := compute_b(a)]
  174. # --- parse-clean battery shapes (must NOT defer) -----------------------------
  175. rs <- r"(no \escape here)"
  176. rs2 <- r"#(one "quoted" bit)#"
  177. plot(1, )
  178. sliced <- x[1, ]
  179. piped <- x |> f_ph(y = _)
  180. # --- UTF-16 columns ----------------------------------------------------------
  181. msg <- "héllo 🎉"
  182. emoji_caller <- function() after_emoji("🎉🎉", target_fn())