File: test-stringify-expression.R

package info (click to toggle)
r-cran-reprex 1.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,584 kB
  • sloc: sh: 13; makefile: 2
file content (122 lines) | stat: -rw-r--r-- 2,548 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
test_that("simple statements are stringified", {
  expect_identical(stringify_expression(1:5), "1:5")
  expect_identical(stringify_expression({1:5}), "1:5")
  expect_identical(stringify_expression(quote(mean(x))), "mean(x)")
})

## it is very difficult to create quoted multi-line expressions in tests
## that mimic what a user can create interactively re: the srcrefs
## therefore, I executed this interactively to create expressions.rds
if (FALSE) {
  e <- new.env()
  e$e01 <- quote({
    1:5
  })
  e$e02 <- quote({1:5
  })
  e$e03 <- quote({
    1:5})
  e$e04 <- quote({1:3;4:6})
  e$e05 <- quote({
    #' Leading comment
    x <- rnorm(3)
    #' Embedded comment
    mean(x)
    #' Trailing comment
  })
  e$e06 <- quote({mean(1:4) # comment
  })
  e$e07 <- quote({
    #' Leading comment
    y <- 1:4 # comment
    #' Trailing comment
  }
  )
  saveRDS(e, rprojroot::find_testthat_root_file("expressions.rds"))
}

e <- readRDS(rprojroot::find_testthat_root_file("expressions.rds"))

test_that("one statement, brackets, multiple lines, take 1", {
  # quote({
  #   1:5
  # })
  expect_identical(
    stringify_expression(e$e01),
    "1:5"
  )
})

test_that("one statement, brackets, multiple lines, take 2", {
  # expr <- quote({1:5
  # })
  expect_identical(
    stringify_expression(e$e02),
    "1:5"
  )
})

test_that("one statement, brackets, multiple lines, take 3", {
  # expr <- quote({
  #   1:5})
  expect_identical(
    stringify_expression(e$e03),
    "1:5"
  )
})

test_that("multiple statements, brackets, semicolon", {
  # quote({1:3;4:6})
  expect_identical(
    stringify_expression(e$e04),
    "1:3;4:6"
  )
})

test_that("leading, embedded, trailing comment, #89", {
  # expr <- quote({
  #   #' Leading comment
  #   x <- rnorm(3)
  #   #' Embedded comment
  #   mean(x)
  #   #' Trailing comment
  # })
  out <- c(
    "#' Leading comment",
    "x <- rnorm(3)",
    "#' Embedded comment",
    "mean(x)",
    "#' Trailing comment"
  )
  expect_identical(
    stringify_expression(e$e05),
    out
  )
})

test_that("trailing inline comment, #91", {
  # expr <- quote({mean(1:4) # comment
  # })
  out <- "mean(1:4) # comment"
  expect_identical(
    stringify_expression(e$e06),
    out
  )
})

test_that("trailing inline comment AND trailing comment line", {
  # expr <- quote({
  #   #' Leading comment
  #   y <- 1:4 # comment
  #   #' Trailing comment
  # }
  out <- c(
    "#' Leading comment",
    "y <- 1:4 # comment",
    "#' Trailing comment"
  )
  expect_identical(
    stringify_expression(e$e07),
    out
  )
})