knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

testwhat uses the pipe operator (%>%) from the magrittr package to 'chain together' SCT functions. Every chain starts with the ex() function call, which holds the exercise state. This exercise state contains all the information that is required to check if an exercise is correct, which are:

As SCT functions are chained together with %>%, the exercise state is copied and adapted into so-called child states to zoom in on particular parts of the code.

Example

Consider the following snippet of markdown that represents part of an exercise:

`@solution`
```r
x <- 4
if (x > 0) {
  print("x is positive")
}
```

`@sct`
```r
ex() %>% check_if_else() %>% {
    check_cond(.) %>% check_code(c("x\\s+>\\s+0", "0\\s+<\\s+x")) # chain A
    check_if(.) %>% check_function("print") %>% check_arg("x") %>% check_equal() # chain B
}
```

To further explain this example, assume the following student submission:

x <- 4
if (x < 0) {
  print("x is negative")
}

In chain A, this is what happens:

```r # solution if (x > 0) { print("x is positive") }

# student if (x < 0) { print("x is negative") } ```

```r # solution x > 0

# student x < 0 ```

Assume now that the student corrects the mistake, and submits the following (which is still not correct):

x <- 4
if (x > 0) {
  print("x is negative")
}

Chain A will go through the same steps and will pass this time as x > 0 in the student submission now matches one of the regexes. In Chain B, this is what happens:

```r # solution print("x is positive")

# student print("x is negative") ```

``` # solution { "x": "x is positive" }

# student { "x": "x is negative" } ```

```r # solution "x is positive"

# student "x is negative" ```



datacamp/testwhat documentation built on June 26, 2022, 9:07 a.m.