Skip to content

Commit e28b937

Browse files
committed
extend comments testing and remove |>
1 parent b4e119c commit e28b937

File tree

5 files changed

+114
-56
lines changed

5 files changed

+114
-56
lines changed

R/qenv-get_code.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
#'
1919
#' _Case 1: Usual assignments._
2020
#' ```r
21-
#' q1 <- qenv() |>
22-
#' within({
21+
#' q1 <-
22+
#' within(qenv(), {
2323
#' foo <- function(x) {
2424
#' x + 1
2525
#' }
@@ -33,8 +33,8 @@
3333
#'
3434
#' _Case 2: Some objects are created by a function's side effects._
3535
#' ```r
36-
#' q2 <- qenv() |>
37-
#' within({
36+
#' q2 <-
37+
#' within(qenv(){
3838
#' foo <- function() {
3939
#' x <<- x + 1
4040
#' }
@@ -52,8 +52,8 @@
5252
#' In order to include comments in code one must use the `eval_code` function instead.
5353
#'
5454
#' ```r
55-
#' q3 <- qenv() |>
56-
#' eval_code("
55+
#' q3 <-
56+
#' eval_code(qenv(), "
5757
#' foo <- function() {
5858
#' x <<- x + 1
5959
#' }

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Below is the showcase of the example usage
5656

5757
```r
5858
library(teal.code)
59-
my_qenv <- qenv() |> eval_code("x <- 5")
59+
my_qenv <- eval_code(qenv(), "x <- 5")
6060
my_qenv
6161
#> <environment: 0x00000225cc85c7a0> [L]
6262
#> Parent: <environment: package:teal.code>
@@ -69,7 +69,8 @@ ls(get_env(my_qenv))
6969
```
7070

7171
```r
72-
qenv_2 <- eval_code(my_qenv, "y <- x * 2") |> eval_code("z <- y * 2")
72+
qenv_2 <- eval_code(my_qenv, "y <- x * 2")
73+
qenv_2 <- eval_code(qenv_2, "z <- y * 2")
7374
qenv_2
7475
#> <environment: 0x00000225ca866d68> [L]
7576
#> Parent: <environment: package:teal.code>

tests/testthat/test-qenv_eval_code.R

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,104 @@ testthat::test_that(
170170
)
171171
}
172172
)
173+
174+
175+
176+
# comments --------------------------------------------------------------------------------------------------------
177+
178+
179+
# comments --------------------------------------------------------------------------------------------------------
180+
181+
testthat::test_that("comments fall into proper calls", {
182+
# If comment is on top, it gets moved to the first call.
183+
# Any other comment gets moved to the call above.
184+
code <- "
185+
# initial comment
186+
a <- 1
187+
b <- 2 # inline comment
188+
c <- 3
189+
# inbetween comment
190+
d <- 4
191+
# finishing comment
192+
"
193+
194+
q <- eval_code(qenv(), code)
195+
testthat::expect_identical(
196+
get_code(q),
197+
code
198+
)
199+
})
200+
201+
testthat::test_that("comments get pasted when they fall into calls", {
202+
# If comment is on top, it gets moved to the first call.
203+
# Any other comment gets moved to the call above.
204+
# Comments get pasted if there are two assigned to the same call.
205+
code <- "
206+
# initial comment
207+
a <- 1 # A comment
208+
b <- 2 # inline comment
209+
c <- 3 # C comment
210+
# inbetween comment
211+
d <- 4
212+
# finishing comment
213+
"
214+
215+
q <- eval_code(qenv(), code)
216+
testthat::expect_identical(
217+
get_code(q),
218+
code
219+
)
220+
})
221+
222+
testthat::test_that("comments alone are pasted to the next/following call element",{
223+
code <- c("x <- 5", "# comment", "y <- 6")
224+
q <- eval_code(qenv(), code)
225+
testthat::expect_identical(
226+
unlist(q@code)[2],
227+
pasten(code[2:3])
228+
)
229+
testthat::expect_identical(
230+
get_code(q),
231+
pasten(code)
232+
)
233+
})
234+
235+
testthat::test_that("comments at the end of src are added to the previous call element",{
236+
code <- c("x <- 5", "# comment")
237+
q <- eval_code(qenv(), code)
238+
testthat::expect_identical(
239+
unlist(q@code),
240+
pasten(code[1:2])
241+
)
242+
testthat::expect_identical(
243+
get_code(q),
244+
pasten(code)
245+
)
246+
})
247+
248+
testthat::test_that("comments from the same line are associated with it's call",{
249+
code <- c("x <- 5", " y <- 4 # comment", "z <- 5")
250+
q <- eval_code(qenv(), code)
251+
testthat::expect_identical(
252+
unlist(q@code)[2],
253+
paste0(code[2], "\n")
254+
)
255+
testthat::expect_identical(
256+
get_code(q),
257+
pasten(code)
258+
)
259+
})
260+
261+
testthat::test_that("comments alone passed to eval_code are skipped",{
262+
code <- c("x <- 5", "# comment")
263+
q <- eval_code(eval_code(qenv(), code[1]), code[2])
264+
testthat::expect_identical(
265+
unlist(q@code),
266+
pasten(code[1:2])
267+
)
268+
testthat::expect_identical(
269+
get_code(q),
270+
pasten(code)
271+
)
272+
})
273+

tests/testthat/test-qenv_extract.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ testthat::test_that("`[.` extracts the code only needed to recreate objects pass
4141
)
4242
})
4343

44-
testthat::test_that("`[.` preservers comments in the code", {
44+
testthat::test_that("`[.` comments are preserved in the code and associated with the following call", {
4545
q <- qenv()
4646
code <- c("x<-1 #comment", "a<-1;b<-2")
4747
q <- eval_code(q, code)

tests/testthat/test-qenv_get_code.R

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
pasten <- function(...) paste(..., collapse = "\n")
22

33
testthat::test_that("get_code returns code (character by default) of qenv object", {
4-
q <- qenv() |>
5-
eval_code(quote(x <- 1)) |>
6-
eval_code(quote(y <- x))
4+
q <- qenv()
5+
q <- eval_code(q, quote(x <- 1))
6+
q <- eval_code(q, quote(y <- x))
77
testthat::expect_equal(get_code(q), pasten(c("x <- 1", "y <- x")))
88
})
99

@@ -456,50 +456,6 @@ testthat::test_that(
456456
}
457457
)
458458

459-
460-
# comments --------------------------------------------------------------------------------------------------------
461-
462-
testthat::test_that("comments fall into proper calls", {
463-
# If comment is on top, it gets moved to the first call.
464-
# Any other comment gets moved to the call above.
465-
code <- "
466-
# initial comment
467-
a <- 1
468-
b <- 2 # inline comment
469-
c <- 3
470-
# inbetween comment
471-
d <- 4
472-
# finishing comment
473-
"
474-
475-
q <- eval_code(qenv(), code)
476-
testthat::expect_identical(
477-
get_code(q),
478-
code
479-
)
480-
})
481-
482-
testthat::test_that("comments get pasted when they fall into calls", {
483-
# If comment is on top, it gets moved to the first call.
484-
# Any other comment gets moved to the call above.
485-
# Comments get pasted if there are two assigned to the same call.
486-
code <- "
487-
# initial comment
488-
a <- 1 # A comment
489-
b <- 2 # inline comment
490-
c <- 3 # C comment
491-
# inbetween comment
492-
d <- 4
493-
# finishing comment
494-
"
495-
496-
q <- qenv() |> eval_code(code)
497-
testthat::expect_identical(
498-
get_code(q),
499-
code
500-
)
501-
})
502-
503459
# functions -------------------------------------------------------------------------------------------------------
504460

505461
testthat::test_that("ignores occurrence in a function definition", {

0 commit comments

Comments
 (0)