testwhat (version 4.2.2)

test_control: Test if student coded a control statement correctly

Description

Test if student coded a control statement correctly

Usage

test_if_else(index = 1, if_cond_test = NULL, if_expr_test = NULL, else_expr_test = NULL, not_found_msg = NULL, missing_else_msg = NULL)
test_while_loop(index = 1, cond_test = NULL, expr_test = NULL, not_found_msg = NULL)
test_for_loop(index = 1, cond_test = NULL, expr_test = NULL, not_found_msg = NULL)
check_if_else(state, index = 1, not_found_msg = NULL, append = TRUE)
check_while(state, index = 1, not_found_msg = NULL, append = TRUE)
check_for(state, index = 1, not_found_msg = NULL, append = TRUE)
check_cond(state)
"check_body"(state, ...)
check_if(state)
check_else(state, not_found_msg = NULL, append = TRUE)

Arguments

index
Number of that particular control statement to check
if_cond_test
sub-SCT to perform in the if condition part (for test_if_else
if_expr_test
sub-SCT to perform in the if expression part (for test_if_else)
else_expr_test
sub-SCT to perform in the else expression (for test_if_else)
not_found_msg
Custom message in case the control statement was not found
missing_else_msg
Custom message in case the else part is missing (for test_if_else)
cond_test
sub-SCT to perform on the condition part (for test_for_loop or test_while_loop)
expr_test
sub-SCT to perform on the expression part (for test_for_loop or test_while_loop)
state
state to start from (for check_ functions)
append
Whether or not to append the feedback to feedback built in previous states
...
S3 stuff

Examples

Run this code
## Not run: 
# # Example 1: if else
# vec <- c("a", "b", "c")
# if("a" %in% vec) {
#  print("a in here")
# } else if(any("b" > vec)) {
#  cat("b not smallest")
# } else {
#  str(vec)
# }
# 
# # SCT option 1
# test_if_else(1, 
#              if_cond_test = {
#                test_student_typed("%in%")
#                test_student_typed("vec")
#              },
#              if_expr_test = test_function("print")
#              else_expr_test = test_if_else(1,
#                                            if_cond_test = test_student_typed(">"),
#                                            if_expr_test = test_function("cat"),
#                                            else_expr_test = test_function("str")))
#                                            
# # SCT option 2
# ifelse <- check_if_else(1)
# ifelse %>% check_cond() %>% check_code("%in%")
# ifelse %>% check_cond() %>% check_code("vec")
# ifelse %>% check_if() %>% check_function("print")
# nestedifelse <- ifelse %>% check_else() %>% check_if_else(1)
# nestedifelse %>% check_cond() %>% check_code(">")
# nestedifelse %>% check_if() %>% check_function("cat")
# nestedifelse %>% check_else() %>% check_function("str")
# 
# # Example 2: while loop
# while(x < 18) {
#  x <- x + 5
#  print(x)
# }
# 
# # SCT Option 1
# test_while_loop(1, 
#                cond_test = test_student_typed(c("< 18", "18 >")),
#                expr_test = {
#                  test_student_typed(c("x + 5", "5 + x"))
#                  # no actual value matching possible!!
#                  test_function("print", args = 'x', eval = FALSE)
#                })
# 
# # SCT Option 2
# w <- check_while(1)
# w %>% check_cond() %>% check_code(c("< 18", "18 >"))
# w %>% check_body() %>% check_code(c("x + 5", "5 + x"))
# w %>% check_body() %>% check_function("print") %>% test_arg("x")
# 
# # Example 3: for loop
# for(i in 1:5) {
#  print("hurray!") 
# }
# 
# # SCT Option 1
# test_for_loop(1, 
#               cond_test = {
#                 test_student_typed("in")
#                 test_student_typed("1")
#                 test_student_typed("5")
#               }, 
#               expr_test = test_function("print"), args = "x")
# 
# # SCT Option 2
# f <- ex() %>% check_for(1)
# cond <- f %>% check_cond() 
# cond %>% check_code("in")
# cond %>% check_code("1")
# cond %>% check_code("5")
# f %>% check_body() %>% check_function("print") %>% check_arg("x") %>% check_equal()
# ## End(Not run)

Run the code above in your browser using DataLab