Skip to content

Commit

Permalink
Fix exercise print method (#733)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Sep 19, 2022
1 parent 02c99ae commit 6508e36
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 7 deletions.
16 changes: 9 additions & 7 deletions R/exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -848,15 +848,17 @@ exercise_code_chunks_user <- function(exercise) {
exercise_code_chunks(user_chunk)
}

exercise_code_chunks <- function(chunks) {
vapply(chunks, function(x) {
opts <- x$opts[setdiff(names(x$opts), "label")]
exercise_code_chunks <- function(chunks, engine = "r") {
vapply(chunks, function(chunk) {
opts <- chunk$opts[setdiff(names(chunk$opts), "label")]
opts <- paste(names(opts), unname(opts), sep = "=")
chunk_engine <- chunk$engine %||% engine %||% "r"
chunk_opts <- paste0(c(dput_to_string(chunk$label), opts), collapse = ", ")
paste(
sep = "\n",
# we quote the label to ensure that it is treated as a label and not a symbol for instance
sprintf("```{%s %s}", x$engine, paste0(c(dput_to_string(x$label), opts), collapse = ", ")),
paste0(x$code, collapse = "\n"),
sprintf("```{%s %s}", chunk_engine, chunk_opts),
paste0(chunk$code, collapse = "\n"),
"```"
)
}, character(1))
Expand Down Expand Up @@ -1534,12 +1536,12 @@ format.tutorial_exercise <- function (x, ..., setup_chunk_only = FALSE) {
support_chunk <- mock_chunk(
label = paste0(label, "-", sub("_", "-", chunk)),
code = x[[chunk]],
engine = if (chunk == "solution") x$engine
engine = x$engine
)
x$chunks <- c(x$chunks, list(support_chunk))
}
}
chunks <- exercise_code_chunks(x$chunks)
chunks <- exercise_code_chunks(x$chunks, x$engine)
paste(chunks, collapse = "\n\n")
}

Expand Down
25 changes: 25 additions & 0 deletions tests/testthat/_snaps/exercise.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,28 @@
get("___sql_result")
```

# exercise print method

Code
example_exercise
Output
```{r "ex", exercise=TRUE}
1 + 1
```
```{r "ex-solution"}
2 + 2
```
```{r "ex-code-check"}
3 + 3
```
```{r "ex-check"}
5 + 5
```
```{r "ex-error-check"}
4 + 4
```

17 changes: 17 additions & 0 deletions tests/testthat/test-exercise.R
Original file line number Diff line number Diff line change
Expand Up @@ -1432,3 +1432,20 @@ test_that("render_exercise_prepare() removes forced default chunk options from e
res <- evaluate_exercise(ex, new.env())
expect_equal(res$feedback$checker_args$last_value, 2)
})


# Exercise Print Method ---------------------------------------------------

test_that("exercise print method", {
local_edition(3)

example_exercise <- mock_exercise(
user_code = "1 + 1",
solution_code = "2 + 2",
code_check = "3 + 3",
error_check = "4 + 4",
check = "5 + 5"
)

expect_snapshot(example_exercise)
})

0 comments on commit 6508e36

Please sign in to comment.