Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement test_lesson and test_course functions #15

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export(setMeta)
export(set_lesson)
export(swirl2html)
export(swirlify)
export(test_course)
export(test_lesson)
export(testit)
export(txt)
export(vid)
Expand Down
75 changes: 74 additions & 1 deletion R/yaml_writer.R
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ hlp <- function(){
"testit(from) or test(from, to) -- See ?testit.",
"count_units() -- Count the number of units in current lesson.",
"find_units(regex) -- Get unit numbers for units matching regex.",
"add_to_manifest() -- Add current lesson to course manifest.")
"add_to_manifest() -- Add current lesson to course manifest.",
"test_lesson() -- Test all cmd and mult questions of current lesson.",
"test_course() -- Test all cmd and mult questions of current course.")
rule("Utilities")
message(paste0(" * ", utils, collapse="\n"))
units <-
Expand Down Expand Up @@ -440,3 +442,74 @@ rule <- function(title = "") {
width <- getOption("width") - nchar(title) - 1
message("\n", title, paste(rep("-", width, collapse = "")), "\n")
}

#' Test all cmd and mult questions of a lesson.
#'
#' @param lesson_dir_name The directory name of the lesson. Defaults to current lesson.
#' @export
test_lesson <- function(lesson_dir_name = NULL){
if (is.null(lesson_dir_name)) {
lesson_dir_name <- getOption("swirlify_lesson_dir_name")
}
test_lesson_by_name(lesson_dir_name)
}

#' Test all cmd and mult questions of current course.
#'
#' @export
test_course <- function(){
course_path <- getOption("swirlify_course_dir_path")
lesson_list <- list.dirs(course_path, recursive = FALSE,full.names = FALSE)
lesson_list <- grep("^[^\\.]",lesson_list, value = T)
for (lesson in lesson_list){
test_lesson_by_name(lesson)
}
}

# Test all cmd and mult questions of any lesson of current course.
test_lesson_by_name <- function(lesson_dir_name){

message(paste("##### Begin testing:", lesson_dir_name, "#####\n"))
.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) || !is.null(question$AnswerTests)){
print(paste(">", question$CorrectAnswer))
switch(question$Class,
"cmd_question" = {
suppressWarnings({
e$val <- eval(parse(text=question$CorrectAnswer), envir = e)
e$expr <- parse(text = question$CorrectAnswer)[[1]]
stopifnot(eval(parse(text=question$AnswerTests), envir = e))
})
},
"mult_question" = {
e$val <- as.character(question$CorrectAnswer)
stopifnot(eval(parse(text = question$AnswerTests), envir = e))
},
"script" = {
question$correctScript <- file.path(lesson_dir_path, "scripts", paste(tools::file_path_sans_ext(question$Script), "-correct.R", sep = ""))
if (file.exists(question$correctScript)) {
print(paste("Testing script:", question$correctScript))
file.copy(question$correctScript, e$script_temp_path <- file.path(tempdir(), question$Script), overwrite = TRUE)
source(question$correctScript, local = globalenv())
stopifnot(eval(parse(text = question$AnswerTests), envir = e))
}
})
}
}

message(paste("----- Testing:", lesson_dir_name, ", done. -----\n"))
}
11 changes: 11 additions & 0 deletions man/test_course.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
% Generated by roxygen2 (4.0.1): do not edit by hand
\name{test_course}
\alias{test_course}
\title{Test all cmd and mult questions of current course.}
\usage{
test_course()
}
\description{
Test all cmd and mult questions of current course.
}

14 changes: 14 additions & 0 deletions man/test_lesson.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
% Generated by roxygen2 (4.0.1): do not edit by hand
\name{test_lesson}
\alias{test_lesson}
\title{Test all cmd and mult questions of a lesson.}
\usage{
test_lesson(lesson_dir_name = NULL)
}
\arguments{
\item{lesson_dir_name}{The directory name of the lesson. Defaults to current lesson.}
}
\description{
Test all cmd and mult questions of a lesson.
}