11testthat :: test_that(" eval_code evaluates the code in the qenvs environment" , {
22 q <- qenv()
3- q1 <- eval_code(q , quote(iris1 <- iris ))
4- q2 <- eval_code(q1 , quote(b <- nrow( iris1 ) ))
5- testthat :: expect_identical (q2 $ b , 150L )
3+ q1 <- eval_code(q , quote(a <- 1L ))
4+ q2 <- eval_code(q1 , quote(b <- 1 ))
5+ testthat :: expect_equal (q2 , list2env( list ( a = 1L , b = 1 )) )
66})
77
88testthat :: test_that(" eval_code locks the environment" , {
99 q <- eval_code(qenv(), quote(iris1 <- iris ))
10- testthat :: expect_true(environmentIsLocked(q @ .xData ))
10+ testthat :: expect_true(environmentIsLocked(q ))
1111})
1212
1313testthat :: test_that(" eval_code doesn't have access to environment where it's called" , {
@@ -36,21 +36,29 @@ testthat::test_that("getting object from the package namespace works even if lib
3636testthat :: test_that(" eval_code works with character" , {
3737 q1 <- eval_code(qenv(), " a <- 1" )
3838 testthat :: expect_identical(get_code(q1 ), " a <- 1" )
39- testthat :: expect_equal(q1 @ .xData , list2env(list (a = 1 )))
39+ testthat :: expect_equal(q1 , list2env(list (a = 1 )))
4040})
4141
4242testthat :: test_that(" eval_code works with expression" , {
43- q1 <- eval_code(qenv(), as.expression(quote(a <- 1 )))
43+ q1 <- eval_code(qenv(), expression(a <- 1 , b <- 2 ))
44+ testthat :: expect_identical(get_code(q1 ), " a <- 1\n b <- 2" )
45+ testthat :: expect_equal(q1 , list2env(list (a = 1 , b = 2 )))
46+ })
4447
45- testthat :: expect_identical(get_code(q1 ), " a <- 1" )
46- testthat :: expect_equal(q1 @ .xData , list2env(list (a = 1 )))
48+ testthat :: test_that(" eval_code preserves original formatting when `srcref` is present in the expression" , {
49+ code <- " # comment
50+ a <- 1L"
51+ expr <- parse(text = code , keep.source = TRUE )
52+ q1 <- eval_code(qenv(), expr )
53+ testthat :: expect_identical(get_code(q1 ), code )
54+ testthat :: expect_equal(q1 , list2env(list (a = 1L )))
4755})
4856
4957testthat :: test_that(" eval_code works with quoted" , {
5058 q1 <- eval_code(qenv(), quote(a <- 1 ))
5159
5260 testthat :: expect_identical(get_code(q1 ), " a <- 1" )
53- testthat :: expect_equal(q1 @ .xData , list2env(list (a = 1 )))
61+ testthat :: expect_equal(q1 , list2env(list (a = 1 )))
5462})
5563
5664testthat :: test_that(" eval_code works with quoted code block" , {
@@ -66,7 +74,7 @@ testthat::test_that("eval_code works with quoted code block", {
6674 get_code(q1 ),
6775 c(" a <- 1\n b <- 2" )
6876 )
69- testthat :: expect_equal(q1 @ .xData , list2env(list (a = 1 , b = 2 )))
77+ testthat :: expect_equal(q1 , list2env(list (a = 1 , b = 2 )))
7078})
7179
7280testthat :: test_that(" eval_code fails with unquoted expression" , {
@@ -172,3 +180,8 @@ testthat::test_that("comments passed alone to eval_code that contain @linksto ta
172180 " x"
173181 )
174182})
183+
184+ testthat :: test_that(" Code executed with integer shorthand (1L) is the same as original" , {
185+ q <- within(qenv(), a <- 1L )
186+ testthat :: expect_identical(get_code(q ), " a <- 1L" )
187+ })
0 commit comments