Skip to content

Commit

Permalink
Custom omnitest which clears e$delta upon failure.
Browse files Browse the repository at this point in the history
  • Loading branch information
WilCrofter committed Jun 10, 2014
1 parent 69c0832 commit c918b8e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
45 changes: 45 additions & 0 deletions R_Programming/Matrices_and_Data_Frames/customTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,48 @@ calculates_same_value <- function(expr){
if(!passed)e$delta <- list()
return(passed)
}

omnitest <- function(correctExpr=NULL, correctVal=NULL, strict=FALSE){
e <- get("e", parent.frame())
# Trivial case
if(is.null(correctExpr) && is.null(correctVal))return(TRUE)
# Testing for correct expression only
if(!is.null(correctExpr) && is.null(correctVal)){
passed <- expr_identical_to(correctExpr)
if(!passed)e$delta <- list()
return(passed)
}
# Testing for both correct expression and correct value
# Value must be character or single number
valGood <- NULL
if(!is.null(correctVal)){
if(is.character(e$val)){
valResults <- expectThat(e$val,
is_equivalent_to(correctVal, label=correctVal),
label=(e$val))
if(is(e, "dev") && !valResults$passed)swirl_out(valResults$message)
valGood <- valResults$passed
# valGood <- val_matches(correctVal)
} else if(!is.na(e$val) && is.numeric(e$val) && length(e$val) == 1){
cval <- try(as.numeric(correctVal), silent=TRUE)
valResults <- expectThat(e$val,
equals(cval, label=correctVal),
label=toString(e$val))
if(is(e, "dev") && !valResults$passed)swirl_out(valResults$message)
valGood <- valResults$passed
}
}
exprGood <- ifelse(is.null(correctExpr), TRUE, expr_identical_to(correctExpr))
if(valGood && exprGood){
return(TRUE)
} else if (valGood && !exprGood && !strict){
swirl_out("That's not the expression I expected but it works.")
swirl_out("I've executed the correct expression in case the result is needed in an upcoming question.")
eval(parse(text=correctExpr),globalenv())
return(TRUE)
} else {
e$delta <- list()
return(FALSE)
}
}

44 changes: 44 additions & 0 deletions R_Programming/Vectors/customTests.R
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,47 @@ expr_creates_var <- function(correctName=NULL){
}
return(results$passed)
}

omnitest <- function(correctExpr=NULL, correctVal=NULL, strict=FALSE){
e <- get("e", parent.frame())
# Trivial case
if(is.null(correctExpr) && is.null(correctVal))return(TRUE)
# Testing for correct expression only
if(!is.null(correctExpr) && is.null(correctVal)){
passed <- expr_identical_to(correctExpr)
if(!passed)e$delta <- list()
return(passed)
}
# Testing for both correct expression and correct value
# Value must be character or single number
valGood <- NULL
if(!is.null(correctVal)){
if(is.character(e$val)){
valResults <- expectThat(e$val,
is_equivalent_to(correctVal, label=correctVal),
label=(e$val))
if(is(e, "dev") && !valResults$passed)swirl_out(valResults$message)
valGood <- valResults$passed
# valGood <- val_matches(correctVal)
} else if(!is.na(e$val) && is.numeric(e$val) && length(e$val) == 1){
cval <- try(as.numeric(correctVal), silent=TRUE)
valResults <- expectThat(e$val,
equals(cval, label=correctVal),
label=toString(e$val))
if(is(e, "dev") && !valResults$passed)swirl_out(valResults$message)
valGood <- valResults$passed
}
}
exprGood <- ifelse(is.null(correctExpr), TRUE, expr_identical_to(correctExpr))
if(valGood && exprGood){
return(TRUE)
} else if (valGood && !exprGood && !strict){
swirl_out("That's not the expression I expected but it works.")
swirl_out("I've executed the correct expression in case the result is needed in an upcoming question.")
eval(parse(text=correctExpr),globalenv())
return(TRUE)
} else {
e$delta <- list()
return(FALSE)
}
}

0 comments on commit c918b8e

Please sign in to comment.