Result validation

Apart from standard result validation there are also other methods of validating correctness of function call.

Throws

If function is expected to raise an exception for particular input you can use throws statement.

def factorial(n):
    """
    spec:
        title: Factorial
        domain -1:
            title: Should be incalculable for negative values
            description:
                Function raises proper exception
                when called with negative value
            throws ValueError('factorial() not defined for negative values')
        domain 0:
            title: Should return 1 on boundary
            description: Function should return 0 when called with 0
            results 1
        domain 1: results 1
        domain 3: results 6
        domain 4: results 24
        domain 5: results 120
    """
    if n < 0:
        raise ValueError("factorial() not defined for negative values")

    if n == 0:
        return 1
    return n * factorial(n - 1)

Between

If your function returns inaccurate results you can use between to validate if result is in desired range.

def square_derivative(x):
    """
    spec:
        domain 10:
            result between (19.9, 20.1)
    """
    eps = 0.01
    f = lambda x: x * x
    return (f(x + eps) - f(x)) / eps

Validate by function

The most generic way of validating result. At first you have to define predicate in plain Python. The predicate is of type Result -> Bool ie. takes the result of function call and returns boolean. The boolean value indicates whether test passed or not.

def got_name(result):
    return result.get("name")

You can either define the predicate in the same file or import it for the sake of test/code separation as shown below. To validate result using function use result <foo_name> statement.

def check_by_function():
    """
    spec:
        from examples.result_validation.check_by_function.test_utils import got_name
        domain:
            result got_name
    """
    return {"name": "example", "value": 1}