예제 #1
0
def test_walk(walk_test):
    datatype, actual, call_args_list = walk_test
    callback_mock = Mock()

    result = walk(datatype, actual, callback_mock)
    assert callback_mock.call_args_list == call_args_list
    assert result == actual
예제 #2
0
def failures(datatype, value):
    """Return list of failures (if any) validating `value` against `datatype`.

    Params:
        `datatype`: Datatype to validate value against.  See README.markdown
            for examples.
        `value`: Value to validate
        `path`: Used internally for location of failures.

    Example:
        >>> failures('int', 'foo')
        ['expected int, got str']
    """
    fails = []

    def validate(path, *args):
        msg = '%s: %%s' % path if path else '%s'
        fails.extend(msg % m for m in validate_step(*args) or [])

    walk(datatype, value, validate)

    return fails
예제 #3
0
def failures(datatype, value):
    """Return list of failures (if any) validating `value` against `datatype`.

    Params:
        `datatype`: Datatype to validate value against.  See README.markdown
            for examples.
        `value`: Value to validate
        `path`: Used internally for location of failures.

    Example:
        >>> failures('int', 'foo')
        ['expected int, got str']
    """
    fails = []

    def validate(path, *args):
        msg = '%s: %%s' % path if path else '%s'
        fails.extend(msg % m for m in validate_step(*args) or [])

    walk(datatype, value, validate)

    return fails
예제 #4
0
def coerce_value(datatype, value):
    """Attempt to coerce value to requested datatype.

    Example:
        >>> coerce_value("int", "5")
        5

    If coercion is not possible, the incoercible portion is left changed.

    Example:
        >>> coerce_value(['int'], ['1', '2', 'c'])
        [1, 2, 'c']
    """
    return walk(datatype, value, coerce_step)
예제 #5
0
def coerce_value(datatype, value):
    """Attempt to coerce value to requested datatype.

    Example:
        >>> coerce_value("int", "5")
        5

    If coercion is not possible, the incoercible portion is left changed.

    Example:
        >>> coerce_value(['int'], ['1', '2', 'c'])
        [1, 2, 'c']
    """
    return walk(datatype, value, coerce_step)