testwhat (version 4.2.2)

test_fun_def: Check whether the student defined a function correctly

Description

Check whether the student defined a function correctly

Usage

test_function_definition(name, function_test = NULL, body_test = NULL, undefined_msg = NULL, incorrect_number_arguments_msg = NULL)
check_fun_def(state, name, undefined_msg = NULL, no_fundef_msg = NULL, append = TRUE)
check_arguments(state, incorrect_number_arguments_msg = NULL, append = TRUE)
"check_body"(state, not_found_msg = NULL, append = TRUE, ...)
check_call(state, ...)

Arguments

name
The name of the function to test
function_test
sub-SCT to perform on the function
body_test
sub-SCT to perform on the body of the function if the sub-SCT in function_test fails. Only able to test on strings here!
undefined_msg
Custom message in case the specified function was not defined
incorrect_number_arguments_msg
Optional feedback message in case the function does not have the correct number of arguments.
state
the state to start from
no_fundef_msg
Custom message in case the function specified in name is not a function.
append
Whether or not to append the feedback to feedback built in previous states
not_found_msg
Custom feedback message if function definition was not found.
...
arguments to pass to the user-defined function to test result, output or error in a later stage

Examples

Run this code
## Not run: 
# # Example:
# my_fun <- function(a, b) { 
#   stopifnot(is.numeric(a), is.numeric(b))
#   a + b 
# }
# 
# # SCT option 1
# test_function_definition("my_fun",
#                          function_test = {
#                            test_expression_result("my_fun(1,2)")
#                            test_expression_error("my_fun('c',3)")
#                          }, {
#                            test_student_typed("+")
#                          })
# 
# # SCT option 2
# fundef %>% check_fun_def("my_fun") 
# fundef %>% check_arguments()
# fundef %>% check_call(a = 1, b = 2) %>% check_result() %>% check_equal()
# fundef %>% check_call(a = 'c', b = 3) %>% check_error() %>% check_equal()
# fundef %>% check_body() %>% check_code("+")
# ## End(Not run)

Run the code above in your browser using DataCamp Workspace