Skip to content

Commit

Permalink
Add a basic check to test_course()/test_lesson() to identify potentia…
Browse files Browse the repository at this point in the history
…l course name inconsistencies in lesson.yaml files, and also include a test for this test function. This resolves issue swirldev#33.
  • Loading branch information
jonmcalder committed Oct 21, 2016
1 parent 7a04c1b commit 5334eac
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
10 changes: 10 additions & 0 deletions R/test_lesson.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ test_lesson_by_name <- function(){
lesson_dir_path <- getOption("swirlify_lesson_dir_path")
les <- yaml.load_file(getOption("swirlify_lesson_file_path"))

# Get name of course folder from path
# and replace underscores with spaces
course_folder_name <- gsub(pattern = "_", replacement = " ", basename(course_dir_path))

# 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)
Expand All @@ -87,6 +91,12 @@ test_lesson_by_name <- function(){
if(is.null(question[[i]])) message("Please provide a value for the ",
i, " key in the meta question.")
}
if(question[["Course"]] != course_folder_name) {
message("Course name '" , question[["Course"]] , "' for ",
getOption("swirlify_lesson_name"), "\n",
"is inconsistent with the ", "(directory) course name: '",
course_folder_name, "'")
}
} else if(question$Class == "cmd_question"){
for(i in c("Output", "CorrectAnswer", "AnswerTests", "Hint")){
if(is.null(question[[i]])) message("Please provide a value for the ",
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/test_test_course.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
context("Test test_course()")

path <- tempdir()

setwd(path)

new_lesson("Test Lesson 1", "Test Course", open_lesson = FALSE)
add_license(author = "Test Author")
add_to_manifest()
wq_command("0", "0", "0", "0")
wq_message("0")

new_lesson("Test Lesson 2", "Test Course", open_lesson = FALSE)
add_to_manifest()
wq_command("0", "0", "0", "0")
wq_message("0")

test_lesson_2_path <- file.path(getOption("swirlify_lesson_dir_path"), "lesson.yaml")

# Overwrite lesson.yaml for Test Lesson 2
# replacing "Test Course" with "Inconsistent Course Name"
updated_yaml <- sub("Test Course", "Inconsistent Course Name",
readLines(test_lesson_2_path))
cat(updated_yaml, file = test_lesson_2_path, sep = "\n")

new_lesson("Test Lesson 3", "Test Course", open_lesson = FALSE)
add_to_manifest()
wq_command("0", "0", "0", "0")
wq_message("0")


zz <- file(file.path(path, "test.log"), open = "wt")
sink(zz)
sink(zz, type = "message")

test_course()

sink(type = "message")
sink()

correct_output <- c("##### Begin testing: Test Lesson 1 #####",
"##### End testing: Test Lesson 1 #####",
"",
"##### Begin testing: Test Lesson 2 #####",
"Course name 'Inconsistent Course Name' for Test Lesson 2",
"is inconsistent with the (directory) course name: 'Test Course'",
"##### End testing: Test Lesson 2 #####",
"",
"##### Begin testing: Test Lesson 3 #####",
"##### End testing: Test Lesson 3 #####")

test_that("test_course() passes with message about inconsistent course name", {
expect_true(all(correct_output %in% readLines(file.path(path, "test.log"))))
})

unlink(getOption("swirlify_course_dir_path"), recursive = TRUE, force = TRUE)

0 comments on commit 5334eac

Please sign in to comment.