File: util.R

package info (click to toggle)
r-cran-sourcetools 0.1.5-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 300 kB
  • sloc: cpp: 1,983; ansic: 481; sh: 10; makefile: 2
file content (57 lines) | stat: -rw-r--r-- 1,294 bytes parent folder | download | duplicates (2)
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
.sourcetools <- new.env(parent = emptyenv())
.sourcetools$gctorture <- TRUE

with_gctorture <- function(expr) {
  gctorture(.sourcetools$gctorture)
  result <- expr
  gctorture(FALSE)
  result
}

check_parse <- function(R, S = R) {
  lhs <- base::parse(text = R, keep.source = FALSE)
  rhs <- with_gctorture(parse_string(S))
  check_parse_impl(lhs, rhs)
}

check_parse_impl <- function(lhs, rhs) {

  lhsType <- typeof(lhs)
  rhsType <- typeof(rhs)

  onError <- function(format, ...) {
    message <- c(
      sprintf(format, ...),
      sprintf("R: '%s'", deparse(lhs)),
      sprintf("S: '%s'", deparse(rhs))
    )
    stop(paste(message, collapse = "\n"), call. = FALSE)
  }

  if (lhsType != rhsType)
    onError("TypeError: '%s' != '%s'", lhsType, rhsType)

  if (length(lhs) != length(rhs))
    onError("LengthError: %s != %s", length(lhs), length(rhs))

  if (is.call(lhs) || is.expression(lhs)) {
    lapply(seq_along(lhs), function(i) {
      check_parse_impl(lhs[[i]], rhs[[i]])
    })
  }

  if (!identical(lhs, rhs))
    onError("IdenticalError: '%s' != '%s'", lhs, rhs)

  TRUE
}

expect_parse <- function(R, S = R) {
  testthat::expect_true(check_parse(R, S))
}

search_objects <- function() {
  lapply(seq_along(search()), function(i) {
    ls(pos = i, all.names = TRUE)
  })
}