Skip to content

Commit 88a8c04

Browse files
committed
fix tests
1 parent a32939c commit 88a8c04

File tree

5 files changed

+48
-41
lines changed

5 files changed

+48
-41
lines changed

R/qenv-eval_code.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ setGeneric("eval_code", function(object, code) standardGeneric("eval_code"))
2929

3030
setMethod("eval_code", signature = c("qenv", "character"), function(object, code) {
3131
parsed_code <- parse(text = code, keep.source = TRUE)
32+
if (length(parsed_code) == 0) return(object)
33+
3234
id <- sample.int(.Machine$integer.max, size = length(parsed_code))
3335

3436
object@id <- c(object@id, id)
3537
object@env <- rlang::env_clone(object@env, parent = parent.env(.GlobalEnv))
3638

37-
code_split <- split_code(code)
39+
code_split <- split_code(paste(code, collapse = "\n"))
3840
object@code <- c(object@code, unlist(code_split))
3941

4042
current_warnings <- rep("", length(parsed_code))

R/utils-get_code_dependency.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,12 @@ get_line_ids <- function(pd) {
491491
last_comment_ids <- NULL
492492
}
493493

494+
# If NUM_CONST is the last element, we need to reorder rows.
495+
# Last 2 rows
496+
n <- nrow(pd)
497+
if (pd$token[n-1] == "NUM_CONST" && pd$parent[n] == 0) {
498+
pd <- rbind(pd[-(n-1), ], pd[n-1, ])
499+
}
494500

495501
calls_start <- which(pd$parent == 0)
496502
calls_end <- c(which(pd$parent == 0)[-1] - 1, nrow(pd))
@@ -526,6 +532,7 @@ get_line_ids <- function(pd) {
526532
split_code <- function(code) {
527533
parsed_code <- parse(text = code, keep.source = TRUE)
528534
pd <- utils::getParseData(parsed_code)
535+
pd <- normalize_pd(pd)
529536
pd <- pd[pd$token != "';'", ]
530537
lines_ids <- get_line_ids(pd)
531538

@@ -540,7 +547,11 @@ split_code <- function(code) {
540547
# in case only indentantion is changed, do not trim the indentation
541548
if (!identical(code_lines_candidate, trimws(code_lines))) {
542549
# case of multiple calls in one line, keep the original indentation
543-
indentation <- gsub("^(\\s+).*", "\\1", code_lines)
550+
indentation <- if (grepl("^\\s+", code_lines)) {
551+
gsub("^(\\s+).*", "\\1", code_lines)
552+
} else {
553+
""
554+
}
544555
code_lines <- paste0(indentation, code_lines_candidate)
545556
}
546557
} else {

tests/testthat/test-qenv_extract.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ testthat::test_that("`[.` extract proper code", {
1515
qs <- q[object_names]
1616
testthat::expect_identical(
1717
qs@code,
18-
c("x <- 1", "a <- 1")
18+
c("x<-1", "a<-1")
1919
)
2020
})
2121

@@ -26,7 +26,7 @@ testthat::test_that("`[.` preservers comments in the code", {
2626
qs <- q[c("x", "a")]
2727
testthat::expect_identical(
2828
qs@code,
29-
c("x <- 1 #comment", "a <- 1")
29+
c("x<-1 #comment", "a<-1")
3030
)
3131
})
3232

tests/testthat/test-qenv_get_code.R

Lines changed: 29 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ testthat::test_that("handles the code included in curly brackets", {
7676
code <- "{1 + 1;a <- 5}"
7777

7878
testthat::expect_identical(
79+
# TODO: to be fixed
7980
get_code(eval_code(qenv(), code), names = "a"),
8081
"a <- 5"
8182
)
@@ -87,7 +88,7 @@ testthat::test_that("handles the code of length > 1 when at least one is enclose
8788

8889
testthat::expect_identical(
8990
get_code(q, names = "a"),
90-
"a <- 5"
91+
"a<-5"
9192
)
9293
})
9394

@@ -212,7 +213,7 @@ testthat::test_that("detects every assign calls even if not evaluated, if there
212213
q <- eval_code(qenv(), code)
213214
testthat::expect_identical(
214215
get_code(q, names = "b"),
215-
c("b <- 2", "eval(expression({\n b <- b + 2\n}))")
216+
code[2:3]
216217
)
217218
})
218219

@@ -295,16 +296,11 @@ testthat::test_that("extracts the code for assign() where \"x\" is a literal str
295296
q <- eval_code(qenv(), code)
296297
testthat::expect_identical(
297298
get_code(q, names = "b"),
298-
c("assign(\"b\", 5)", "b <- b + 2")
299+
code[c(2, 5)]
299300
)
300301
testthat::expect_identical(
301302
get_code(q, names = "c"),
302-
c(
303-
"assign(\"b\", 5)",
304-
"assign(value = 7, x = \"c\")",
305-
"b <- b + 2",
306-
"c <- b"
307-
)
303+
code[c(2, 3, 5, 6)]
308304
)
309305
testthat::expect_identical(
310306
get_code(q, names = "d"),
@@ -355,11 +351,11 @@ testthat::test_that("detects function usage of the assignment operator", {
355351

356352
testthat::expect_identical(
357353
get_code(q, names = "y"),
358-
c(code[1], "y <- x")
354+
code
359355
)
360356
testthat::expect_identical(
361357
get_code(q2, names = "y"),
362-
"y <- x <- 2"
358+
code2
363359
)
364360
})
365361

@@ -390,7 +386,7 @@ testthat::test_that("@linksto makes a line being returned for an affected bindin
390386
q <- eval_code(qenv(), code)
391387
testthat::expect_identical(
392388
get_code(q, names = "b"),
393-
c("a <- 1 # @linksto b", "b <- 2")
389+
c(" a <- 1 # @linksto b", " b <- 2")
394390
)
395391
})
396392

@@ -443,7 +439,7 @@ testthat::test_that(
443439
q <- eval_code(qenv(), code)
444440
testthat::expect_identical(
445441
get_code(q, names = "classes"),
446-
c("iris2 <- iris[1:5, ]", code[2:4])
442+
code
447443
)
448444
}
449445
)
@@ -467,11 +463,10 @@ testthat::test_that("comments fall into proper calls", {
467463
q <- qenv() |> eval_code(code)
468464
testthat::expect_identical(
469465
get_code(q),
470-
c(
471-
"a <- 1 # initial comment",
472-
"b <- 2 # inline comment",
473-
"c <- 3 # inbetween comment",
474-
"d <- 4 # finishing comment"
466+
c(" # initial comment\n a <- 1",
467+
" b <- 2 # inline comment",
468+
" c <- 3\n # inbetween comment",
469+
" d <- 4\n # finishing comment"
475470
)
476471
)
477472
})
@@ -493,11 +488,10 @@ testthat::test_that("comments get pasted when they fall into calls", {
493488
q <- qenv() |> eval_code(code)
494489
testthat::expect_identical(
495490
get_code(q),
496-
c(
497-
"a <- 1 # initial comment # A comment",
498-
"b <- 2 # inline comment",
499-
"c <- 3 # C comment # inbetween comment",
500-
"d <- 4 # finishing comment"
491+
c(" # initial comment\n a <- 1 # A comment",
492+
" b <- 2 # inline comment",
493+
" c <- 3 # C comment\n # inbetween comment",
494+
" d <- 4\n # finishing comment"
501495
)
502496
)
503497
})
@@ -516,7 +510,7 @@ testthat::test_that("ignores occurrence in a function definition", {
516510
)
517511
testthat::expect_identical(
518512
get_code(q, names = "foo"),
519-
"foo <- function(b) {\n b <- b + 2\n}"
513+
code[2]
520514
)
521515
})
522516

@@ -528,11 +522,11 @@ testthat::test_that("ignores occurrence in a function definition that has functi
528522
q <- eval_code(qenv(), code)
529523
testthat::expect_identical(
530524
get_code(q, names = "b"),
531-
"b <- 2"
525+
code[1]
532526
)
533527
testthat::expect_identical(
534528
get_code(q, names = "foo"),
535-
"foo <- function(b) {\n function(c) {\n b <- c + 2\n }\n}"
529+
code[2]
536530
)
537531
})
538532

@@ -550,7 +544,7 @@ testthat::test_that("ignores occurrence in a function definition if there is mul
550544
)
551545
testthat::expect_identical(
552546
get_code(q, names = "foo"),
553-
"foo <- function(b) {\n function(c) {\n b <- c + 2\n }\n}"
547+
code[2]
554548
)
555549
})
556550

@@ -587,7 +581,7 @@ testthat::test_that("does not ignore occurrence in function body if object exsit
587581
testthat::test_that("ignores occurrence in function definition without { curly brackets", {
588582
code <- c(
589583
"b <- 2",
590-
"foo <- function(b) b <- b + 2 "
584+
"foo <- function(b) b <- b + 2"
591585
)
592586
q <- eval_code(qenv(), code)
593587
testthat::expect_identical(
@@ -610,7 +604,7 @@ testthat::test_that("detects occurrence of the function object", {
610604
q <- eval_code(qenv(), code)
611605
testthat::expect_identical(
612606
get_code(q, names = "b"),
613-
c("a <- 1", "b <- 2", "foo <- function(b) {\n b <- b + 2\n}", "b <- foo(a)")
607+
code
614608
)
615609
})
616610

@@ -623,7 +617,7 @@ testthat::test_that("detects occurrence of a function definition when a formal i
623617
q <- eval_code(qenv(), code)
624618
testthat::expect_identical(
625619
get_code(q, names = "a"),
626-
c("x <- 1", "foo <- function(foo = 1) \"text\"", "a <- foo(x)")
620+
code
627621
)
628622
})
629623

@@ -640,10 +634,8 @@ testthat::test_that("detects occurrence of a function definition with a @linksto
640634
q <- eval_code(qenv(), code)
641635
testthat::expect_identical(
642636
get_code(q, names = "x"),
643-
c(
644-
"foo <- function() {\n env <- parent.frame()\n env$x <- 0\n}",
645-
"foo() # @linksto x"
646-
)
637+
c(" foo <- function() {\n env <- parent.frame()\n env$x <- 0\n }",
638+
"foo() # @linksto x")
647639
)
648640
})
649641
# $ ---------------------------------------------------------------------------------------------------------------
@@ -696,11 +688,11 @@ testthat::test_that("understands @ usage and do not treat rhs of @ as objects (o
696688
q@code <- code # we don't use eval_code so the code is not run
697689
testthat::expect_identical(
698690
get_code(q, names = "x"),
699-
gsub("'", "\"", code[1:2], fixed = TRUE)
691+
code[1:2]
700692
)
701693
testthat::expect_identical(
702694
get_code(q, names = "a"),
703-
gsub("'", "\"", code, fixed = TRUE)
695+
code
704696
)
705697
})
706698

@@ -752,7 +744,7 @@ testthat::test_that("data() call is returned when data name is provided as a cha
752744
q <- eval_code(qenv(), code)
753745
testthat::expect_identical(
754746
get_code(q, names = "z"),
755-
gsub("'", "\"", code[-1], fixed = TRUE)
747+
code[-1]
756748
)
757749
})
758750

tests/testthat/test-qenv_within.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ testthat::test_that("within.qenv renturns a `qenv` where `@env` is a deep copy o
6060
qq <- within(q, {})
6161
testthat::expect_equal(q@env, qq@env)
6262
testthat::expect_false(identical(q@env, qq@env))
63+
# TODO: fix
64+
# dunno what's going on yet
6365
})
6466

6567
testthat::test_that("within.qenv renturns qenv.error even if evaluation raises error", {

0 commit comments

Comments
 (0)