def test_isboolclass(strictboolclass, strictboolobj): """Test :func:`moretools.isboolclass` check. """ # first make sure that strictboolobj fixture # is really a StrictBool instance # (no common use case, as StrictBool.__new__ # normally returns builint bools) assert not isinstance(strictboolobj, bool) # then perform actual tests for class_ in bool, StrictBool, strictboolclass: assert isboolclass(class_) for class_ in object, int: assert not isboolclass(class_) for obj in object(), False, 1, 'True', 'bool', strictboolobj: with pytest.raises(TypeError) as exc: isboolclass(obj) assert str(exc.value) == "isboolclass() arg must be a class"
def register_bool_class(cls_or_name, name=None, true=None, false=None, ignore=None, caseless=True, spaceless=True): if isstring(cls_or_name): name = cls_or_name Bool = normboolclass(name, true=true, false=false, ignore=ignore, caseless=caseless, spaceless=spaceless) else: if not isboolclass(cls_or_name): raise TypeError("No bool class: %s" % repr(cls_or_name)) Bool = cls_or_name if not name: name = Bool.__name__ BOOL_CLASSES[name] = Bool
def convert_to_bool(self, value, *true_false, **options): if true_false: lists = NormalizedDict({'true': [], 'false': []}) # choose the first list to fill with items # based on given TRUE or FALSE specifier: try: t_or_f_list = lists[true_false[0]] except KeyError: raise ValueError("Expected TRUE or FALSE, not: %s" % repr(true_false[0])) for item in true_false[1:]: if item in lists: #==> is new TRUE or FALSE specifier #==> switch to corresponding list t_or_f_list = lists[item] if t_or_f_list: raise ValueError("Multiple %s lists specfied." % normalize(item).upper()) else: t_or_f_list.append(item) for key, items in lists.items(): if not items: raise ValueError("No %s list specified." % key.upper()) if RobotBool(options.get('normalized', True)): boolcls = normboolclass(**lists) else: boolcls = boolclass(**lists) else: boolcls = options.get('boolclass') or options.get('booltype') if not boolcls: # fallback to robot's default bool conversion return BUILTIN.convert_to_boolean(value) if isstring(boolcls): try: # is a registered bool class name? boolcls = BOOL_CLASSES[boolcls] except KeyError: if '.' not in boolcls: raise ValueError( "No such bool class registered: '%s'" % boolcls) modname, clsname = boolcls.rsplit('.', 1) try: # is an importable 'module.class' string? boolcls = getattr(__import__(modname), clsname) except (ImportError, AttributeError): raise ValueError( "Can't import bool class: '%s'" % boolcls) elif not isboolclass(boolcls): raise TypeError("No bool class: %s" % repr(boolcls)) BUILTIN._log_types(value) return boolcls(value)
def convert_to_bool(self, value, *true_false, **options): if true_false: lists = NormalizedDict({'true': [], 'false': []}) # choose the first list to fill with items # based on given TRUE or FALSE specifier: try: t_or_f_list = lists[true_false[0]] except KeyError: raise ValueError("Expected TRUE or FALSE, not: %s" % repr(true_false[0])) for item in true_false[1:]: if item in lists: #==> is new TRUE or FALSE specifier #==> switch to corresponding list t_or_f_list = lists[item] if t_or_f_list: raise ValueError("Multiple %s lists specfied." % normalize(item).upper()) else: t_or_f_list.append(item) for key, items in lists.items(): if not items: raise ValueError("No %s list specified." % key.upper()) if RobotBool(options.get('normalized', True)): boolcls = normboolclass(**lists) else: boolcls = boolclass(**lists) else: boolcls = options.get('boolclass') or options.get('booltype') if not boolcls: # fallback to robot's default bool conversion return BUILTIN.convert_to_boolean(value) if isstring(boolcls): try: # is a registered bool class name? boolcls = BOOL_CLASSES[boolcls] except KeyError: if '.' not in boolcls: raise ValueError( "No such bool class registered: '%s'" % boolcls) modname, clsname = boolcls.rsplit('.', 1) try: # is an importable 'module.class' string? boolcls = getattr(__import__(modname), clsname) except (ImportError, AttributeError): raise ValueError("Can't import bool class: '%s'" % boolcls) elif not isboolclass(boolcls): raise TypeError("No bool class: %s" % repr(boolcls)) BUILTIN._log_types(value) return boolcls(value)