1 2# object ------------------------------------------------------------------ 3 4test_that("has thoughtful print method", { 5 text <- 6 "#' This is a title 7 #' 8 #' @param x,y A number 9 #' @export 10 f <- function(x, y) x + y 11 " 12 block <- parse_text(text)[[1]] 13 verify_output(test_path("test-block-print.txt"), block) 14}) 15 16# description block ------------------------------------------------------- 17 18test_that("title and description taken from first line if only one", { 19 out <- roc_proc_text(rd_roclet(), " 20 #' title 21 #' @name a 22 NULL")[[1]] 23 expect_equal(out$get_value("description"), "title") 24 expect_equal(out$get_value("title"), "title") 25}) 26 27test_that("description taken from multiple titles if merged", { 28 out <- roc_proc_text(rd_roclet(), " 29 #' T1 30 #' @name a 31 NULL 32 33 #' T2 34 #' @name a 35 NULL 36 ")[[1]] 37 expect_equal(out$get_value("title"), c("T1", "T2")) 38 expect_equal(out$get_value("description"), c("T1", "T2")) 39}) 40 41test_that("title, description and details extracted correctly", { 42 out <- roc_proc_text(rd_roclet(), " 43 #' title 44 #' 45 #' description 46 #' 47 #' details 48 #' @name a 49 NULL")[[1]] 50 expect_equal(out$get_value("description"), "description") 51 expect_equal(out$get_value("details"), "details") 52}) 53 54test_that("title taken from first paragraph", { 55 out <- roc_proc_text(rd_roclet(), " 56 #' Description with sentence. 57 #' 58 #' That continueth. 59 #' @name a 60 NULL")[[1]] 61 expect_equal(out$get_value("title"), "Description with sentence.") 62 expect_equal(out$get_value("description"), "That continueth.") 63}) 64 65test_that("@title overrides default title", { 66 out <- roc_proc_text(rd_roclet(), " 67 #' Would be title 68 #' @title Overridden title 69 #' @name a 70 NULL")[[1]] 71 expect_equal(out$get_value("title"), "Overridden title") 72 expect_equal(out$get_value("description"), "Would be title") 73}) 74 75test_that("docs parsed correctly if no blank text", { 76 out <- roc_proc_text(rd_roclet(), " 77 #' @title My title 78 #' @description My description 79 #' @param x value 80 a <- function(x) {}")[[1]] 81 82 expect_equal(out$get_value("title"), "My title") 83 expect_equal(out$get_value("description"), "My description") 84}) 85 86test_that("question mark ends sentence", { 87 out <- roc_proc_text(rd_roclet(), " 88 #' Is a number odd? 89 is.odd <- function(a) {}")[[1]] 90 expect_equal(out$get_value("title"), "Is a number odd?") 91 92}) 93 94test_that("no ending punctuation does not produce ellipsis", { 95 out <- roc_proc_text(rd_roclet(), " 96 #' Whether a number is odd 97 is.odd <- function(a) {}")[[1]] 98 expect_equal(out$get_value("title"), "Whether a number is odd") 99}) 100 101test_that("details are merged if needed", { 102 out <- roc_proc_text(rd_roclet(), " 103 #' Title 104 #' 105 #' Description 106 #' 107 #' Details1 108 #' 109 #' Details2 110 #' 111 #' @details Details3 112 #' 113 #' Details4 114 foo <- function(x) {}")[[1]] 115 116 expect_equal( 117 out$get_value("details"), 118 "Details1\n\nDetails2\n\nDetails3\n\nDetails4" 119 ) 120}) 121 122test_that("whitespace is not detected as details", { 123 expect_silent( 124 out <- roc_proc_text( 125 rd_roclet(), " 126 #' Title 127 #' 128 #' 129 #' Description 130 #' 131 #' 132 #' 133 foo <- function(x) {}" 134 )[[1]] 135 ) 136 137 expect_null(out$get_value("details")) 138}) 139 140 141test_that("@description and @details are merged", { 142 out <- roc_proc_text(rd_roclet(), " 143 #' Foo 144 #' 145 #' This. 146 #' 147 #' OBTW. 148 foo <- function(x = '%') x 149 150 #' @rdname foo 151 #' @description And that. 152 #' @details ORLY? 153 bar <- function(y = '%') y 154 ")[[1]] 155 156 expect_equal(out$get_value("description"), c("This.", "And that.")) 157 expect_equal(out$get_value("details"), c("OBTW.", "ORLY?")) 158}) 159 160test_that("empty description block is silently removed", { 161 expect_warning( 162 roc_proc_text(rd_roclet(), " 163 #' 164 #' 165 f <- function() {} 166 " 167 ), 168 NA 169 ) 170}) 171 172test_that("description block preserves whitespace", { 173 out <- parse_text(" 174 #' Title 175 #' 176 #' Line 1 177 #' Line 2 178 #' 179 #' Line 1 180 #' Line 2 181 f <- function() {} 182 " 183 )[[1]] 184 185 expect_equal(block_get_tag_value(out, "description"), "Line 1\n Line 2") 186 expect_equal(block_get_tag_value(out, "details"), "Line 1\n Line 2") 187}) 188 189 190test_that("line numbers offset correctly", { 191 out <- parse_text( 192 "#' Title 193 #' 194 #' Line 3 195 #' Line 4 196 #' Line 5 197 #' 198 #' Line 7 199 #' Line 8 200 f <- function() {} 201 " 202 )[[1]] 203 204 expect_equal(out$tags[[1]]$line, 1) 205 expect_equal(out$tags[[2]]$line, 3) 206 expect_equal(out$tags[[3]]$line, 7) 207}) 208 209test_that("even with explicit title/description", { 210 out <- parse_text( 211 "#' Line 1 212 #' Line 2 213 #' Line 3 214 #' 215 #' Line 5 216 #' Line 6 217 #' @title This is a title 218 f <- function() {} 219 " 220 )[[1]] 221 222 expect_equal(out$tags[[1]]$line, 1) 223 expect_equal(out$tags[[2]]$line, 5) 224 expect_equal(out$tags[[3]]$line, 7) 225}) 226 227# evaluate ---------------------------------------------------------------- 228 229test_that("evaluation occurs during parsing", { 230 out <- roc_proc_text(rd_roclet(), " 231 foo <- function() c('@name a', '@title a') 232 #' @eval foo() 233 NULL")[[1]] 234 235 expect_equal(out$get_value("title"), "a") 236 expect_equal(out$filename, "a.Rd") 237}) 238 239test_that("errors are propagated", { 240 expect_warning( 241 roc_proc_text(rd_roclet(), " 242 foo <- function() stop('Uhoh') 243 #' @eval foo() 244 NULL" 245 ), 246 "failed with error" 247 ) 248}) 249 250test_that("must return non-NA string", { 251 expect_warning( 252 roc_proc_text(rd_roclet(), " 253 foo <- function() NA 254 #' @eval foo() 255 NULL" 256 ), 257 "did not evaluate to a string" 258 ) 259 260 expect_warning( 261 roc_proc_text(rd_roclet(), " 262 foo <- function() NA_character_ 263 #' @eval foo() 264 NULL" 265 ), 266 "result contained NA" 267 ) 268}) 269 270test_that("also works with namespace roclet", { 271 out <- roc_proc_text(namespace_roclet(), " 272 foo <- function() '@export a' 273 #' @eval foo() 274 #' @name a 275 #' @title a 276 NULL") 277 278 expect_equal(out, "export(a)") 279}) 280 281