def test_keys_handling_opt_specs_properly(value, conformed): """ When a key entry is an opt-value with a default, that default should be used. When a key entry is an opt-value without a default, the entry will be None. """ spec = s.keys({1: s.opt(s.str), 2: s.opt(s.str, "<default>")}) assert conformed == s.conform(spec, value)
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)
def __init__(self, conf): conf_c = s.conform(GW_CONF_PARSER_SPEC, conf) if conf_c == s.Invalid: raise RuntimeError("LOLCAEK") if not s.valid(GW_CONF_PARSER_SPEC, conf): raise SpecError(GW_CONF_PARSER_SPEC, conf) self.open = conf['open'] self.close = conf['close'] self.processes = conf['processes'] self.temp_file_suffix = conf['temp_file_suffix'] self.include_patterns = conf['include_patterns'] self.ignore_patterns = conf['ignore_patterns'] self.ignore_dir_patterns = conf['ignore_dir_patterns'] self.search_paths = conf['search_paths'] self.post_process_fn = conf['post_process_fn']
def load(project: Path): conf_path = project.joinpath(CONF_NAME) try: with open(str(conf_path), 'r') as f: config = yaml.safe_load(f) if not s.valid(GW_CONF_SPEC, config): raise ConfigurationFileInvalidError(GW_CONF_SPEC, config) except FileNotFoundError as e: raise ConfigurationNotFoundError(project) from e # except PermissionError config_c = s.conform(GW_CONF_SPEC, config) if config_c == s.Invalid: # Should never happen here - we already validated the spec ConfigurationFileInvalidError(GW_CONF_SPEC, config) pp_log_conf(config_c) return Configuration(project, config_c)
def test_bool_conform(value, exp): assert s.conform(s.bool, value) == exp, "unexpected"
def test_float_conform(value, exp): assert s.conform(s.float, value) == exp, "unexpected"
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_any_explain(msg, value, spec, result): assert s.conform(s.keys(spec), value) == result, msg
def test_any_conform(value): assert s.conform(s.any(), value) == value, "should always yield the value itself"
def test_opt_explain(msg, value, spec, result): assert s.conform(spec, value) == result, msg
def test_mapof_conform(value, exp): spec = s.mapof(StrSpec(), IntSpec()) assert s.conform(spec, value) == exp, "unexpected"
def test_inst_conform(value, element_spec, exp): spec = s.seqof(element_spec) assert s.conform(spec, value) == exp, "unexpected"
def test_inst_explain(value, exp): spec = s.seqof(s.predicate(lambda v: int(v))) assert s.conform(spec, value) == exp, "unexpected"
def test_str_conform(value, exp): assert s.conform(s.str, value) == exp, "unexpected"
def test_inseq_conform(msg, value, seq, result): assert s.conform(s.inseq(seq), value) == result, msg
def test_inst_valid(value, element_spec, exp): spec = s.seqof(element_spec) assert s.conform(spec, value) == exp, "call from C-code to resolve correctly"
def test_int_conform(value, exp): assert s.conform(s.int, value) == exp, "unexpected"
def test_typ_explain(msg, value, typ, result): assert s.conform(s.type(typ), value) == result, msg