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"
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)
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':
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
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}"
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}"
def test_inst_explain(value, exp): spec = s.seqof(s.predicate(lambda v: int(v))) assert s.conform(spec, value) == exp, "unexpected"
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"