Skip to content

Commit 1a4ba2b

Browse files
committed
Small improvements in the refcmp helper package
1 parent 9bb6796 commit 1a4ba2b

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,4 @@ install.tmp.gnur
156156
lib.install.packages.fastr
157157
lib.install.packages.gnur
158158
/com.oracle.truffle.r.release/doc/
159+
.Rproj.user

com.oracle.truffle.r.pkgs/refcmp/R/snapshot.R

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ refcmpEnv$snapshot_dir <- 'snapshots'
2626
refcmpEnv$snapshot_id <- 0L
2727
refcmpEnv$equals <- all.equal
2828

29+
log <- function(msg) cat("SNAPSHOT: ", msg, "\n")
30+
2931
snapshot.isFastR <- function() {
3032
length(grep('FastR', R.Version()$version.string)) != 0
3133
}
@@ -35,14 +37,17 @@ snapshot.isFastR <- function() {
3537
#' @param dir The directory where to store snapshot to (default: 'snapshots')
3638
#' @param is_reference_run A function returning TRUE if snapshots should be taken and FALSE if values should be compared (default: snapshot dir does not exist)
3739
#' @param equalityFunction The function to use for comparing actual values to snapshotted values (default: 'all.equal')
40+
#' @param browserOnDiff Calls the "browser()" function on each difference to the reference run, this opens the
41+
#' interactive command line debugger where you can further inspect the state.
3842
#' @examples
3943
#' # Only comparing using snapshots in directory "my/snapshot/dir" and using function 'identical' to compare values.
4044
#' snapshot.init(dir = "my/snapshot/dir", referenceRunPredicate = function() FALSE, equalityFunction = identical)
4145
#'
4246
#' # This should do the job in most cases
4347
#' snapshot.init()
4448
#' @export
45-
snapshot.init <- function (dir, is_reference_run, equalityFunction) {
49+
snapshot.init <- function (dir, is_reference_run, equalityFunction, browserOnDiff = T) {
50+
refcmpEnv$browser <- browserOnDiff
4651
if (!missing(dir)) {
4752
refcmpEnv$snaphost_dir <- dir
4853
}
@@ -51,6 +56,11 @@ snapshot.init <- function (dir, is_reference_run, equalityFunction) {
5156
} else {
5257
refcmpEnv$is_reference_run <- !file.exists(refcmpEnv$snapshot_dir)
5358
}
59+
if (refcmpEnv$is_reference_run) {
60+
log(paste0(" Mode is reference run. The data will be saved to ", refcmpEnv$snapshot_dir))
61+
} else {
62+
log(paste0(" Mode is check run. The data will be compared to reference run data from ", refcmpEnv$snapshot_dir))
63+
}
5464
if (!missing(equalityFunction)) {
5565
refcmpEnv$equals <- equalityFunction
5666
}
@@ -73,12 +83,12 @@ snapshot.init <- function (dir, is_reference_run, equalityFunction) {
7383
#'
7484
#' snapshot(x, y, z)
7585
#' @export
76-
snapshot <- function(...) {
86+
snapshot <- function(..., ignore = F) {
7787
# the actual parameter expessions
7888
actParExprs <- as.list(match.call()[-1])
7989
valueList <- actParsToList(actParExprs, parent.frame())
8090
try({
81-
snapshot.id(refcmpEnv$snapshot_id, valueList)
91+
snapshot.id(refcmpEnv$snapshot_id, valueList, ignore)
8292
refcmpEnv$snapshot_id <- refcmpEnv$snapshot_id + 1
8393
})
8494
}
@@ -95,23 +105,27 @@ snapshot <- function(...) {
95105
#' I contrast to function 'snapshot', this function does not try to automatically determine the name of a value.
96106
#' It uses the names as provided in the arguments.
97107
#'
108+
#' The ignore parameter allows to skip snapshots that were examined, but still increments the ID sequence so that following comparisons can work.
109+
#'
98110
#' @examples
99111
#' snapshot.named(a = 10 + 20, b = 30, c = function() print("hello"))
100112
#' @export
101-
snapshot.named <- function (...) {
113+
snapshot.named <- function (..., ignore = F) {
102114
args <- list(...)
103115
valueList <- args[names(args) != ""]
104116
try({
105-
snapshot.id(refcmpEnv$snapshot_id, valueList)
117+
snapshot.id(refcmpEnv$snapshot_id, valueList, ignore)
106118
refcmpEnv$snapshot_id <- refcmpEnv$snapshot_id + 1
107119
})
108120
}
109121

110-
snapshot.id <- function(id, valueList) {
111-
if(refcmpEnv$is_reference_run) {
112-
snapshot.record(id, valueList)
113-
} else {
122+
snapshot.id <- function(id, valueList, ignore = F) {
123+
if(!refcmpEnv$is_reference_run) {
124+
log(paste0(" Checking: id ", id, " values named: ", paste(names(valueList), collapse=",")))
114125
snapshot.check(id, valueList)
126+
} else if (!ignore) {
127+
log(paste0(" Recording: id ", id, " values named: ", paste(names(valueList), collapse=",")))
128+
snapshot.record(id, valueList)
115129
}
116130
}
117131

@@ -142,10 +156,18 @@ snapshot.check <- function(id, valueList) {
142156
expectedVal <- restoredVars[[var_names[[i]]]]
143157
equalsResult <- refcmpEnv$equals(expectedVal, actualVal)
144158
if(!is.logical(equalsResult) || !equalsResult) {
145-
stop(paste0("Value of variable '", var_names[[i]], "' differs. Expected ", expectedVal, " but was ", actualVal))
159+
cat("-------------------------------\n")
160+
cat("ERROR: Value of variable '", var_names[[i]], "' differs.\n")
161+
cat("Expected:\n"); str(expectedVal)
162+
cat("\n---\nActual:\n"); str(actualVal)
163+
cat("\n---\nEquality function result: \n")
164+
print(equalsResult)
165+
cat("-------------------------------\n")
166+
if (refcmpEnv$browser) { browser() }
146167
}
147168
} else {
148-
stop(paste0("Missing variable '", var_names[[i]], "' in recorded variables"))
169+
cat("ERROR: Missing variable '", var_names[[i]], "' in recorded variables\n")
170+
if (refcmpEnv$browser) { browser() }
149171
}
150172
}
151173
}

0 commit comments

Comments
 (0)