partial_test: Partial test of a function

Description Usage Arguments Details Note Examples

View source: R/partial_test.R

Description

Partially evaluates a function, returning only the objects which exist after the last line of partial evaluation and compares them to expected output

Usage

1
partial_test(fun, args, eval_point, compare_fun = NULL, compare_object = NULL)

Arguments

fun

A function to partially evaluate (either the function object, or a string).

args

A list of the arguments necessary for the function to execute, or nothing. If nothing default parameter values must be specified in the function signature.

eval_point

The function line from which to return the result. A line number in the body of the function, or a character string quoting a part of the function. See details.

compare_fun

A predicate function to compare to, defaults to NULL.

compare_object

An object to compare to, defaults to NULL.

Details

Parameter **args** can be safely ignored for functions which take no arguments explicitly, or for functions that have all their arguments set. **eval_point** stands for the line in the function body to be replaced with a return - this line is temporarily overwritten so a return can be made from it. The object to return is then compared, either using any form of predicate function, or using an object to compare to. If both function and object are used, the object is compared to the result of the partial evaluation using the same function. See examples for usage.

Note

If regex matching fails and your expression does not evaluate to anything valid, please try shortening it or supplying a different part of it. (Or specifying the correct line number.)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# create a dummy function
dummy_function <- function( x,     y = 2,    z,
                            a = 5, b = TRUE, c = 10 )
{
  x_2 <- x + y - z
  TRUTHFULLY <- b
  negative <- (c-a) > 0
  return(y)
  }

# works with function body line number
partial_test( fun = dummy_function,
              args = list(x = 10,z = FALSE, b = FALSE),
              eval_point = 1,
              compare_object =  NA)

partial_test( fun = dummy_function,
              args = list(x = 10,z = FALSE, b = FALSE),
              eval_point = 3,
              compare_object = FALSE)


# works with partial string matching
partial_test( fun = dummy_function,
              args = list(x = 10,z = FALSE, b = FALSE),
              eval_point = "negat",
              compare_object = TRUE )

# and semi-full string matching
partial_test( fun = dummy_function,
              args = list(x = 10,z = FALSE, b = FALSE),
              eval_point = "negative <- ",
              compare_object = TRUE )

JSzitas/recovery documentation built on April 3, 2021, 3:16 p.m.