示例#1
0
    def test_get__should_succeed_for_known_object(self):
        lazy = LazyObject("behave.importer", "LazyObject")
        value = lazy.get()
        assert value is LazyObject

        lazy2 = LazyObject("behave.importer:LazyObject")
        value2 = lazy2.get()
        assert value2 is LazyObject

        lazy3 = LazyObject("behave.formatter.steps", "StepsFormatter")
        value3 = lazy3.get()
        assert issubclass(value3, Formatter)
def register_as(name, formatter_class):
    """
    Register formatter class with given name.

    :param name:  Name for this formatter (as identifier).
    :param formatter_class:  Formatter class to register.

    .. since:: 1.2.5
        Parameter ordering starts with name.
    """
    if not isinstance(name, six.string_types):
        # -- REORDER-PARAMS: Used old ordering before behave-1.2.5 (2015).
        warnings.warn("Use parameter ordering: name, formatter_class (for: %s)"\
                      % formatter_class)
        _formatter_class = name
        name = formatter_class
        formatter_class = _formatter_class

    if isinstance(formatter_class, six.string_types):
        # -- SPEEDUP-STARTUP: Only import formatter_class when used.
        scoped_formatter_class_name = formatter_class
        formatter_class = LazyObject(scoped_formatter_class_name)
    assert (isinstance(formatter_class, LazyObject)
            or issubclass(formatter_class, Formatter))
    _formatter_registry[name] = formatter_class
示例#3
0
    def test_load_module__should_succeed_for_existing_module(self):
        module_name = "behave.textutil"
        self.theory.ensure_module_is_not_imported(module_name)

        module = LazyObject.load_module(module_name)
        self.theory.assert_module_with_name(module, module_name)
        self.theory.assert_module_is_imported(module_name)
示例#4
0
    def test_load_module__should_succeed_for_existing_module(self):
        module_name = "behave.textutil"
        self.theory.ensure_module_is_not_imported(module_name)

        module = LazyObject.load_module(module_name)
        self.theory.assert_module_with_name(module, module_name)
        self.theory.assert_module_is_imported(module_name)
示例#5
0
    def test_load_module__should_succeed_for_already_imported_module(self):
        module_name = "behave.importer"
        self.theory.assert_module_is_imported(module_name)

        module = LazyObject.load_module(module_name)
        self.theory.assert_module_with_name(module, module_name)
        self.theory.assert_module_is_imported(module_name)
示例#6
0
    def test_load_module__should_succeed_for_already_imported_module(self):
        module_name = "behave.importer"
        self.theory.assert_module_is_imported(module_name)

        module = LazyObject.load_module(module_name)
        self.theory.assert_module_with_name(module, module_name)
        self.theory.assert_module_is_imported(module_name)
示例#7
0
    def test_lazy_item_access__should_load_object(self):
        ImportModuleTheory.ensure_module_is_not_imported("inspect")
        lazy_dict = LazyDict({"alice": LazyObject("inspect:ismodule")})
        self.theory.assert_item_is_lazy(lazy_dict, "alice")
        self.theory.assert_item_is_lazy(lazy_dict, "alice")

        value = lazy_dict["alice"]
        self.theory.assert_is_not_lazy_object(value)
        self.theory.assert_item_is_not_lazy(lazy_dict, "alice")
示例#8
0
    def test_get__should_succeed_for_known_object(self):
        lazy = LazyObject("behave.importer", "LazyObject")
        value = lazy.get()
        assert value is LazyObject

        lazy2 = LazyObject("behave.importer:LazyObject")
        value2 = lazy2.get()
        assert value2 is LazyObject

        lazy3 = LazyObject("behave.formatter.steps", "StepsFormatter")
        value3 = lazy3.get()
        assert issubclass(value3, Formatter)
示例#9
0
 def test_get__should_fail_for_unknown_object_in_module(self):
     lazy = LazyObject("behave.textutil", "xxx")
     assert_raises(ImportError, lazy.get)
示例#10
0
 def test_get__should_fail_for_unknown_module(self):
     lazy = LazyObject("__unknown_module__", "xxx")
     assert_raises(ImportError, lazy.get)
示例#11
0
 def test_lazy_item_access__should_fail_with_unknown_object(self):
     lazy_dict = LazyDict(
         {"bob": LazyObject("behave.importer", "XUnknown")})
     item_access = lambda key: lazy_dict[key]
     assert_raises(ImportError, item_access, "bob")
示例#12
0
 def test_lazy_item_access__should_fail_with_unknown_module(self):
     lazy_dict = LazyDict({"bob": LazyObject("__unknown_module__", "xxx")})
     item_access = lambda key: lazy_dict[key]
     assert_raises(ImportError, item_access, "bob")
示例#13
0
 def test_get__should_fail_for_unknown_object_in_module(self):
     lazy = LazyObject("test._importer_candidate", "xxx")
     assert_raises(ImportError, lazy.get)
示例#14
0
 def test_get__should_fail_for_unknown_module(self):
     lazy = LazyObject("__unknown_module__", "xxx")
     with pytest.raises(ImportError):
         lazy.get()