r - object no found error in testthat tests -
here code:
setgeneric("iflet", function(sym, x, body1, body2, ...) { standardgeneric("iflet") }) #' @name iflet #' @export setmethod("iflet", signature(sym = "name", x = "any", body1 = "any", body2 = "any"), function(sym, x, body1, body2 = {}) { e = new.env() sym_str = deparse(substitute(sym)) iflet(sym_str, x, body1, body2) }) #' @rdname iflet #' @export setmethod("iflet", signature(sym = "character", x = "any", body1 = "any", body2 = "any"), function(sym, x, body1, body2 = {}) { stopifnot(length(sym) == 1) e = new.env() e[[sym]] = x if(e[[sym]]) { eval(substitute(body1), e) } else { eval(substitute(body2), e) } }) # test iflet("myvar", 3 > 2, { as.integer(myvar) * 3 }, { as.integer(myvar) + 1 }) == 3
the test passes in reple fails in testthat context, why?
here error msg:
1. error: iflet ---------------------------------------------------------------- object 'myvar' not found 1: withcallinghandlers(eval(code, new_test_environment), error = capture_calls) 2: eval(code, new_test_environment) 3: eval(expr, envir, enclos) 4: iflet("myvar", 3 > 2, { as.integer(myvar) * 3 }, { as.integer(myvar) + 1 }) @ test.bedinfo.r:60 5: iflet("myvar", 3 > 2, { as.integer(myvar) * 3 }, { as.integer(myvar) + 1 }) @ /users/kaiyin/eclipseworkspace/collapsabel/r/0_iflen.r:58 6: .local(sym, x, body1, body2, ...) 7: eval(substitute(body1), e) @ /users/kaiyin/eclipseworkspace/collapsabel/r/0_iflen.r:80 8: eval(expr, envir, enclos) 9: .handlesimpleerror(function (e) { e$calls <- head(sys.calls()[-seq_len(frame + 7)], -2) signalcondition(e) }, "object 'myvar' not found", quote(eval(expr, envir, enclos))) @ test.bedinfo.r:61
Comments
Post a Comment