Skip to content

Commit

Permalink
implement test_lesson and test_lesson_by_name
Browse files Browse the repository at this point in the history
  • Loading branch information
ChihChengLiang committed Oct 2, 2015
1 parent 66a0101 commit e6dbffa
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions R/yaml_writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -440,3 +440,45 @@ rule <- function(title = "") {
width <- getOption("width") - nchar(title) - 1
message("\n", title, paste(rep("-", width, collapse = "")), "\n")
}

#' Test all cmd questions of current lesson.
#'
#' @export
test_lesson <- function(){
test_lesson_by_name(getOption("swirlify_lesson_dir_name"))
}

This comment has been minimized.

Copy link
@wush978

wush978 Oct 3, 2015

#' @param lesson_dir_name path. The path of the lesson (yaml? or directory?)
test_lesson <- function(lesson_name = NULL) {
  if (is.null(lesson_name)) lesson_name <- getOption("swirlify_lesson_dir_name")
  test_lesson_by_name(lesson_name)
}
#' Test all cmd questions of any lesson of current course.
#'
#' @param lesson_dir_name
#' @importFrom yaml yaml.load_file
test_lesson_by_name <- function(lesson_dir_name){

print(paste("####Begin testing:", lesson_dir_name))
.e <- environment(swirl:::any_of_exprs)
attach(.e)
on.exit(detach(.e))
e <- new.env()

course_dir_path <- getOption("swirlify_course_dir_path")
lesson_dir_path <- file.path(course_dir_path, lesson_dir_name)
les <- yaml.load_file(file.path(lesson_dir_path, "lesson.yaml"))

for (R_file in c("customTests.R", "initLesson.R")){
R_file_path <- file.path(lesson_dir_path, R_file)
if(file.exists(R_file_path)) source(R_file_path,local = e)
}

for (question in les){
if(!is.null(question$CorrectAnswer) && question$Class == "cmd_question"){
print(paste(">", question$CorrectAnswer))
suppressWarnings({
eval(parse(text=question$CorrectAnswer), envir = e)

This comment has been minimized.

Copy link
@wush978

wush978 Oct 3, 2015

e$val <- eval(parse(text=question$CorrectAnswer), envir = e)

e$expr <- parse(text = question$CorrectAnswer)[[1]]
stopifnot(eval(parse(text=question$AnswerTests), envir = e))
})
}

This comment has been minimized.

Copy link
@wush978

wush978 Oct 3, 2015

      switch(question$Class, 
        "cmd_question" = {
 # ...
        },
        "mult_question" = {
          e$val <- as.character(question$CorrectAnswer)
          stopifnot(eval(parse(text = question$AnswerTests), envir = e))
        })
}

print(paste("-----Testing", lesson_dir_name, "Done"))
}

0 comments on commit e6dbffa

Please sign in to comment.