예제 #1
0
def test_predicate_int_explain(value, success):
    ps = s.predicate(lambda x: isinstance(x, int), "isint")
    if success:
        assert s.explain(ps,
                         value) is None, f"expected isint({value}) to succeed"
    else:
        assert s.explain(ps, value) == f"predicate 'isint' failed"
예제 #2
0
def test_natint(input, exp_valid, exp_conform, exp_explain):
    def _natint(value):
        value = int(value)
        return value if value > 0 else False

    spec = s.predicate(_natint, 'natural integer')
    assert s.valid(spec, input) == exp_valid, "validation failed"
    assert s.conform(spec, input) == exp_conform, "conform value is incorrect"
    assert s.explain(spec, input) == ("predicate 'natural integer' failed"
                                      if exp_explain else None)
예제 #3
0
파일: conf.py 프로젝트: jwdevantier/pyt
        if path and path.is_dir():
            return path.as_posix()
        return s.Invalid

    @staticmethod
    def _name():
        return "Directory"


GW_CONF_PARSER_SPEC = s.keys({
    'open':
    s.opt(s.str, '<@@'),
    'close':
    s.opt(s.str, '@@>'),
    'processes':
    s.opt(s.predicate(_natint, 'positive int'), cpu_count()),
    'temp_file_suffix':
    s.opt(s.str, '.gw.tmp'),
    'include_patterns':
    s.req(s.seqof(s.str)),
    'ignore_patterns':
    s.opt(s.seqof(s.str), []),
    'ignore_dir_patterns':
    s.opt(s.seqof(s.str), []),
    'search_paths':
    s.req(
        s.allof({
            'non-empty?': s.predicate(_nonempty, 'non-empty?'),
            'list of dirs?': s.seqof(Directory())
        })),
    'post_process_fn':
예제 #4
0
import pytest
from ghostwriter.utils import spec as s

# !! These tests rely on the 'Predicate' spec functioning


def isint(v):
    return isinstance(v, int)


p_isint = s.predicate(isint)


def isbig(v):
    return isinstance(v, int) and abs(v) >= 10


p_isbig = s.predicate(isbig)


def ispos(v):
    return isinstance(v, int) and v > 0


p_ispos = s.predicate(ispos)


def isneg(v):
    return isinstance(v, int) and v < 0

예제 #5
0
def test_predicate_int_conform(value, result):
    ps = s.predicate(lambda x: int(x), "intify")
    assert s.conform(
        ps, value) == result, f"conform({value}) failed - expected {result}"
예제 #6
0
def test_predicate_int_valid(value, result, txt):
    ps = s.predicate(lambda x: isinstance(x, int), "isint")
    assert s.valid(ps, value) == result, f"valid({value}): {txt}"
예제 #7
0
def test_inst_explain(value, exp):
    spec = s.seqof(s.predicate(lambda v: int(v)))
    assert s.conform(spec, value) == exp, "unexpected"
예제 #8
0
import typing as t

import pytest
from ghostwriter.utils import spec as s

p_isint = s.predicate(lambda v: isinstance(v, int))
p_isstr = s.predicate(lambda v: isinstance(v, str))


# TODO: move tests using IntSpec out to its own test file.
class IntSpec(s.SpecBase):
    @staticmethod
    def _valid(value: t.Any):
        return isinstance(value, int)

    @staticmethod
    def _explain(value: t.Any):
        if not isinstance(value, int):
            return f"expected 'int', got '{type(value)}'"

    @staticmethod
    def _conform(value: t.Any):
        try:
            return int(value)
        except (ValueError, TypeError):
            return s.Invalid

    @staticmethod
    def _name():
        return "Int"