diff --git a/R/utils-get_code_dependency.R b/R/utils-get_code_dependency.R index 7f555e4c..8596ae42 100644 --- a/R/utils-get_code_dependency.R +++ b/R/utils-get_code_dependency.R @@ -521,6 +521,9 @@ get_call_breaks <- function(code) { } )) call_breaks <- call_breaks[-nrow(call_breaks), , drop = FALSE] # breaks in between needed only + if (nrow(call_breaks) == 0L) { + call_breaks <- matrix(numeric(0), ncol = 2) + } colnames(call_breaks) <- c("line", "col") call_breaks } diff --git a/tests/testthat/test-qenv_constructor.R b/tests/testthat/test-qenv_constructor.R index 064ef980..992397ed 100644 --- a/tests/testthat/test-qenv_constructor.R +++ b/tests/testthat/test-qenv_constructor.R @@ -54,14 +54,14 @@ testthat::test_that("constructor returns qenv", { testthat::expect_identical(q@code, list()) }) -testthat::describe("parent of qenv environment is the parent of .GlobalEnv", { +testthat::describe("grand parent of qenv environment is the parent of .GlobalEnv", { testthat::it("via slot", { q <- qenv() - testthat::expect_identical(parent.env(q@.xData), parent.env(.GlobalEnv)) + testthat::expect_identical(parent.env(parent.env(q@.xData)), parent.env(.GlobalEnv)) }) testthat::it("via qenv directly", { q <- qenv() - testthat::expect_identical(parent.env(q), parent.env(.GlobalEnv)) + testthat::expect_identical(parent.env(parent.env(q)), parent.env(.GlobalEnv)) }) }) diff --git a/tests/testthat/test-qenv_eval_code.R b/tests/testthat/test-qenv_eval_code.R index cf769a00..9a6075a6 100644 --- a/tests/testthat/test-qenv_eval_code.R +++ b/tests/testthat/test-qenv_eval_code.R @@ -136,44 +136,12 @@ testthat::test_that("comments fall into proper calls", { testthat::expect_identical(get_code(q), code) }) -testthat::test_that("comments alone are pasted to the next/following call element", { - code <- c("x <- 5", "# comment", "y <- 6") - q <- eval_code(qenv(), code) - testthat::expect_identical( - as.character(q@code)[2], - paste(code[2:3], collapse = "\n") - ) - testthat::expect_identical( - get_code(q), - paste(code, collapse = "\n") - ) -}) - -testthat::test_that("comments at the end of src are added to the previous call element", { - code <- c("x <- 5", "# comment") - q <- eval_code(qenv(), code) - testthat::expect_identical( - as.character(q@code), - paste(code[1:2], collapse = "\n") - ) - testthat::expect_identical( - get_code(q), - paste(code, collapse = "\n") - ) -}) - testthat::test_that("comments from the same line are associated with it's call", { code <- c("x <- 5", " y <- 4 # comment", "z <- 5") q <- eval_code(qenv(), code) testthat::expect_identical(as.character(q@code)[2], code[2]) }) -testthat::test_that("alone comments at the end of the source are considered as continuation of the last call", { - code <- c("x <- 5\n", "y <- 10\n# comment") - q <- eval_code(eval_code(qenv(), code[1]), code[2]) - testthat::expect_identical(as.character(q@code)[2], code[2]) -}) - testthat::test_that("comments passed alone to eval_code that contain @linksto tag have detected dependency", { code <- c("x <- 5", "# comment @linksto x") q <- eval_code(eval_code(qenv(), code[1]), code[2]) @@ -201,3 +169,4 @@ testthat::test_that("plot output is stored as recordedplot in the 'outputs' attr q <- eval_code(qenv(), "plot(1)") testthat::expect_s3_class(attr(q@code[[1]], "outputs")[[1]], "recordedplot") }) + diff --git a/tests/testthat/test-qenv_get_code.R b/tests/testthat/test-qenv_get_code.R index 825d9769..a5dc2975 100644 --- a/tests/testthat/test-qenv_get_code.R +++ b/tests/testthat/test-qenv_get_code.R @@ -581,7 +581,7 @@ testthat::test_that("detects occurrence of a function definition when a formal i }) testthat::test_that("detects occurrence of a function definition with a @linksto usage", { - code <- c( + code <- trimws(c( " foo <- function() { env <- parent.frame() @@ -589,7 +589,7 @@ testthat::test_that("detects occurrence of a function definition with a @linksto }", "foo() # @linksto x", "y <- x" - ) + )) q <- eval_code(qenv(), code) testthat::expect_identical( get_code(q, names = "x"), @@ -601,7 +601,7 @@ testthat::test_that("detects occurrence of a function definition with a @linksto # for loop -------------------------------------------------------------------------------------------------------- testthat::test_that("objects in for loop are extracted if passed as one character", { - code <- " + code <- trimws(" some_other_dataset <- mtcars original_dataset <- iris[, 1:4] count <- 1 @@ -610,11 +610,11 @@ testthat::test_that("objects in for loop are extracted if passed as one characte count <- count + 1 } output <- rlang::list2(x = original_dataset) - " + ") q <- eval_code(qenv(), code) testthat::expect_identical( get_code(q, names = "output"), - gsub("\n some_other_dataset <- mtcars\n", "", code, fixed = TRUE) + gsub("some_other_dataset <- mtcars\n", "", code, fixed = TRUE) ) })