Ejemplo n.º 1
0
  def it_should_detect_name_conflict_in_same_scope(self):

    def InjectValue():
      ioc.Injectable.value(val=42)

    InjectValue()
    expect(InjectValue).toRaise(ValueError)
Ejemplo n.º 2
0
  def it_should_allow_calling_injectables_for_testability(self):

    @ioc.Injectable
    def foo(val=ioc.IN):
      return val

    expect(foo(val=99)).toBe(99)
Ejemplo n.º 3
0
    def it_should_track_recursive_dep_and_attach_to_the_deepest(self):
      @ioc.Injectable
      def parent_val(leaf_val=ioc.IN):
        return leaf_val

      @ioc.Inject
      def GetSingleton(parent_singleton=ioc.IN):
        return parent_singleton

      @ioc.Scope
      def ParentScope():

        @ioc.Scope
        def LeafScope():
          ioc.Injectable.value(leaf_val=object())
          expect(GetSingleton()).toBe(GetSingleton())
          return GetSingleton()

        expect(LeafScope()).notToBeNone()
        # parent_singleton should attach to leaf because parent_val depends on
        # leaf_val
        expect(GetSingleton).toRaise(ValueError)

      ParentScope()
      expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 4
0
 def NewScope():
   expect(len(ioc._MyScopes())).toBe(2)
   ioc.Injectable.value(bar='baz')
   t = T()
   t.start()
   t.join()
   return t.name
Ejemplo n.º 5
0
    def it_should_track_recursive_dep_and_attach_to_the_deepest(self):
      @ioc.Injectable
      def parent_val(leaf_val=ioc.IN):
        return leaf_val

      @ioc.Inject
      def GetSingleton(parent_singleton=ioc.IN):
        return parent_singleton

      @ioc.Scope
      def ParentScope():

        @ioc.Scope
        def LeafScope():
          ioc.Injectable.value(leaf_val=object())
          expect(GetSingleton()).toBe(GetSingleton())
          return GetSingleton()

        expect(LeafScope()).notToBeNone()
        # parent_singleton should attach to leaf because parent_val depends on
        # leaf_val
        expect(GetSingleton).toRaise(ValueError)

      ParentScope()
      expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 6
0
 def NewScope():
   expect(len(ioc._MyScopes())).toBe(2)
   ioc.Injectable.value(bar='baz')
   t = T()
   t.start()
   t.join()
   return t.name
Ejemplo n.º 7
0
  def it_should_detect_name_conflict_in_same_scope(self):

    def InjectValue():
      ioc.Injectable.value(val=42)

    InjectValue()
    expect(InjectValue).toRaise(ValueError)
Ejemplo n.º 8
0
  def it_should_allow_calling_injectables_for_testability(self):

    @ioc.Injectable
    def foo(val=ioc.IN):
      return val

    expect(foo(val=99)).toBe(99)
Ejemplo n.º 9
0
  def it_should_not_mangle_classes(self):

    @ioc.Inject
    class Bar(object):
      def __init__(self):
        super(Bar, self).__init__()

    expect(Bar).notToRaise(TypeError)
Ejemplo n.º 10
0
  def it_should_not_mangle_classes(self):

    @ioc.Inject
    class Bar(object):
      def __init__(self):
        super(Bar, self).__init__()

    expect(Bar).notToRaise(TypeError)
Ejemplo n.º 11
0
      def ParentScope():
        ioc.Injectable.value(parent_val=object())

        @ioc.Scope
        def LeafScope():
          # Get parent_singleton for the first time inside leaf scope.
          return GetSingleton()
        # The singleton should stay the same even leaf scope is popped.
        expect(LeafScope()).toBe(GetSingleton())
Ejemplo n.º 12
0
      def ParentScope():
        ioc.Injectable.value(parent_val=object())

        @ioc.Scope
        def LeafScope():
          # Get parent_singleton for the first time inside leaf scope.
          return GetSingleton()
        # The singleton should stay the same even leaf scope is popped.
        expect(LeafScope()).toBe(GetSingleton())
Ejemplo n.º 13
0
  def it_should_allow_calling_injectables(self):

    @ioc.Injectable
    def foo(bar=ioc.IN):
      return 'foo %s' % bar

    ioc.Injectable.value(bar='bar')

    expect(foo()).toEqual('foo bar')
    expect(foo(bar='candybar')).toEqual('foo candybar')
Ejemplo n.º 14
0
      def ParentScope():

        @ioc.Scope
        def LeafScope():
          ioc.Injectable.value(leaf_val=object())
          return GetSingleton()

        expect(LeafScope()).notToBeNone()
        # leaf_singleton should have been popped.
        expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 15
0
      def ParentScope():

        @ioc.Scope
        def LeafScope():
          ioc.Injectable.value(leaf_val=object())
          return GetSingleton()

        expect(LeafScope()).notToBeNone()
        # leaf_singleton should have been popped.
        expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 16
0
  def it_should_allow_calling_injectables(self):

    @ioc.Injectable
    def foo(bar=ioc.IN):
      return 'foo %s' % bar

    ioc.Injectable.value(bar='bar')

    expect(foo()).toEqual('foo bar')
    expect(foo(bar='candybar')).toEqual('foo candybar')
Ejemplo n.º 17
0
  def it_should_inject(self):

    @ioc.Injectable
    def foo():  # pylint: disable=unused-variable
      return 42

    @ioc.Inject
    def bar(foo=ioc.IN):
      return foo

    expect(bar()).toBe(42)
Ejemplo n.º 18
0
  def it_should_inject(self):

    @ioc.Injectable
    def foo():  # pylint: disable=unused-variable
      return 42

    @ioc.Inject
    def bar(foo=ioc.IN):
      return foo

    expect(bar()).toBe(42)
Ejemplo n.º 19
0
  def it_should_support_naming_injectables(self):

    @ioc.Injectable.named('bar')
    def foo():  # pylint: disable=unused-variable
      return 42

    @ioc.Inject
    def bar(bar=ioc.IN):
      return bar

    expect(bar()).toBe(42)
Ejemplo n.º 20
0
  def it_should_allow_overwriting_injectables(self):

    @ioc.Injectable
    def foo():  # pylint: disable=unused-variable
      return 42

    @ioc.Inject
    def bar(foo=ioc.IN):
      return foo

    expect(bar(foo=99)).toBe(99)
Ejemplo n.º 21
0
  def it_should_allow_overwriting_injectables(self):

    @ioc.Injectable
    def foo():  # pylint: disable=unused-variable
      return 42

    @ioc.Inject
    def bar(foo=ioc.IN):
      return foo

    expect(bar(foo=99)).toBe(99)
Ejemplo n.º 22
0
  def it_should_support_naming_injectables(self):

    @ioc.Injectable.named('bar')
    def foo():  # pylint: disable=unused-variable
      return 42

    @ioc.Inject
    def bar(bar=ioc.IN):
      return bar

    expect(bar()).toBe(42)
Ejemplo n.º 23
0
      def ParentScope():

        @ioc.Scope
        def LeafScope():
          ioc.Injectable.value(leaf_val=object())
          expect(GetSingleton()).toBe(GetSingleton())
          return GetSingleton()

        expect(LeafScope()).notToBeNone()
        # parent_singleton should attach to leaf because parent_val depends on
        # leaf_val
        expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 24
0
      def ParentScope():

        @ioc.Scope
        def LeafScope():
          ioc.Injectable.value(leaf_val=object())
          expect(GetSingleton()).toBe(GetSingleton())
          return GetSingleton()

        expect(LeafScope()).notToBeNone()
        # parent_singleton should attach to leaf because parent_val depends on
        # leaf_val
        expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 25
0
  def it_should_work_with_super_classes_multiple_times(self):

    class Foo(object):
      def __init__(self):
        self.baz = 42

    class Bar(Foo):
      def __init__(self):
        super(Bar, self).__init__()

    expect(Bar().baz).toBe(42)
    expect(Bar().baz).toBe(42)
Ejemplo n.º 26
0
  def it_should_support_singleton_functions(self):

    @ioc.Injectable
    @ioc.Singleton
    def singleton():  # pylint: disable=unused-variable
      return object()

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton

    expect(ReturnSingleton()).toBe(ReturnSingleton())
Ejemplo n.º 27
0
  def it_should_work_with_super_classes_multiple_times(self):

    class Foo(object):
      def __init__(self):
        self.baz = 42

    class Bar(Foo):
      def __init__(self):
        super(Bar, self).__init__()

    expect(Bar().baz).toBe(42)
    expect(Bar().baz).toBe(42)
Ejemplo n.º 28
0
  def it_should_allow_subclassing_injectables(self):

    @ioc.Inject
    class Foo(object):
      def __init__(self, bar=ioc.IN):
        self.bar = bar

    @ioc.Injectable
    class Bar(Foo): pass

    ioc.Injectable.value(bar=3)
    expect(Bar().bar).toBe(3)
Ejemplo n.º 29
0
  def it_should_allow_subclassing_injectables(self):

    @ioc.Inject
    class Foo(object):
      def __init__(self, bar=ioc.IN):
        self.bar = bar

    @ioc.Injectable
    class Bar(Foo): pass

    ioc.Injectable.value(bar=3)
    expect(Bar().bar).toBe(3)
Ejemplo n.º 30
0
  def it_should_support_singleton_functions(self):

    @ioc.Injectable
    @ioc.Singleton
    def singleton():  # pylint: disable=unused-variable
      return object()

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton

    expect(ReturnSingleton()).toBe(ReturnSingleton())
Ejemplo n.º 31
0
  def it_should_support_multiple_threads(self):

    class T(ioc.threading.Thread):
      @ioc.Inject
      def run(self, bar=ioc.IN):
        self.setName(bar)

    ioc.Injectable.value(bar='baz')
    t = T()
    t.start()
    t.join()

    expect(t.name).toEqual('baz')
Ejemplo n.º 32
0
  def it_should_support_multiple_threads(self):

    class T(ioc.threading.Thread):
      @ioc.Inject
      def run(self, bar=ioc.IN):
        self.setName(bar)

    ioc.Injectable.value(bar='baz')
    t = T()
    t.start()
    t.join()

    expect(t.name).toEqual('baz')
Ejemplo n.º 33
0
  def it_should_override_name_in_parent_scope(self):
    ioc.Injectable.value(val=42)

    @ioc.Inject
    def GetVal(val=ioc.IN):
      return val

    @ioc.Scope
    def ScopedFunc():
      ioc.Injectable.value(val=32)
      return GetVal()

    expect(GetVal()).toBe(42)
    expect(ScopedFunc()).toBe(32)
Ejemplo n.º 34
0
  def it_should_support_multiple_scopes(self):
    ioc.Injectable.value(root=99)

    @ioc.Inject
    def InjectedFunc(scoped=ioc.IN, root=ioc.IN):
      return scoped, root

    @ioc.Scope
    def ScopedFunc():
      ioc.Injectable.value(scoped=32)
      return InjectedFunc()

    expect(ScopedFunc()).toEqual((32, 99))
    expect(InjectedFunc).toRaise(ioc.InjectionMissingError)
Ejemplo n.º 35
0
  def it_should_support_multiple_scopes(self):
    ioc.Injectable.value(root=99)

    @ioc.Inject
    def InjectedFunc(scoped=ioc.IN, root=ioc.IN):
      return scoped, root

    @ioc.Scope
    def ScopedFunc():
      ioc.Injectable.value(scoped=32)
      return InjectedFunc()

    expect(ScopedFunc()).toEqual((32, 99))
    expect(InjectedFunc).toRaise(ioc.InjectionMissingError)
Ejemplo n.º 36
0
  def it_should_override_name_in_parent_scope(self):
    ioc.Injectable.value(val=42)

    @ioc.Inject
    def GetVal(val=ioc.IN):
      return val

    @ioc.Scope
    def ScopedFunc():
      ioc.Injectable.value(val=32)
      return GetVal()

    expect(GetVal()).toBe(42)
    expect(ScopedFunc()).toBe(32)
Ejemplo n.º 37
0
    def it_should_attach_to_root(self):
      @ioc.Inject
      def GetSingleton(root_singleton=ioc.IN):
        return root_singleton

      @ioc.Scope
      def ParentScope():
        @ioc.Scope
        def LeafScope():
          # Get root_singleton for the first time inside leaf scope.
          return GetSingleton()
        return LeafScope()

      # The singleton value should attach to the root scope.
      expect(ParentScope()).toBe(GetSingleton())
Ejemplo n.º 38
0
  def it_should_work_normally_for_non_injected_classes(self):

    class Foo(object):
      def __init__(self):
        self.baz = 42
      def method(self):
        return 99

    class Bar(Foo):
      def __init__(self):
        super(Bar, self).__init__()

    b = Bar()
    expect(b.baz).toBe(42)
    expect(b.method()).toBe(99)
Ejemplo n.º 39
0
  def it_should_support_injectable_arg_in_singleton_functions(self):

    @ioc.Injectable
    @ioc.Singleton
    def singleton(val=ioc.IN):
      return val

    @ioc.Inject
    def ReturnSingletonVal(singleton=ioc.IN):
      return singleton

    val = object()
    ioc.Injectable.value(val=val)

    expect(ReturnSingletonVal()).toBe(val)
Ejemplo n.º 40
0
  def it_should_support_eager_singletons(self):
    spy = create_spy('eager')

    @ioc.Injectable
    @ioc.Singleton.eager
    def singleton():  # pylint: disable=unused-variable
      spy()

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton
    ioc.Warmup()
    expect(spy.call_count).toBe(1)
    ReturnSingleton()
    expect(spy.call_count).toBe(1)
Ejemplo n.º 41
0
  def it_should_support_singleton_classes(self):
    # pylint: disable=unused-variable

    @ioc.Injectable
    @ioc.Singleton
    class singleton(object):
      def __init__(self):
        self.bar = object()

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton

    expect(ReturnSingleton()).toBe(ReturnSingleton())
    expect(ReturnSingleton().bar).toBe(ReturnSingleton().bar)
Ejemplo n.º 42
0
  def it_should_support_singleton_classes(self):
    # pylint: disable=unused-variable

    @ioc.Injectable
    @ioc.Singleton
    class singleton(object):
      def __init__(self):
        self.bar = object()

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton

    expect(ReturnSingleton()).toBe(ReturnSingleton())
    expect(ReturnSingleton().bar).toBe(ReturnSingleton().bar)
Ejemplo n.º 43
0
  def it_should_support_injectable_arg_in_singleton_functions(self):

    @ioc.Injectable
    @ioc.Singleton
    def singleton(val=ioc.IN):
      return val

    @ioc.Inject
    def ReturnSingletonVal(singleton=ioc.IN):
      return singleton

    val = object()
    ioc.Injectable.value(val=val)

    expect(ReturnSingletonVal()).toBe(val)
Ejemplo n.º 44
0
  def it_should_work_normally_for_non_injected_classes(self):

    class Foo(object):
      def __init__(self):
        self.baz = 42
      def method(self):
        return 99

    class Bar(Foo):
      def __init__(self):
        super(Bar, self).__init__()

    b = Bar()
    expect(b.baz).toBe(42)
    expect(b.method()).toBe(99)
Ejemplo n.º 45
0
  def it_should_support_eager_singletons(self):
    spy = create_spy('eager')

    @ioc.Injectable
    @ioc.Singleton.eager
    def singleton():  # pylint: disable=unused-variable
      spy()

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton
    ioc.Warmup()
    expect(spy.call_count).toBe(1)
    ReturnSingleton()
    expect(spy.call_count).toBe(1)
Ejemplo n.º 46
0
    def it_should_attach_to_root(self):
      @ioc.Inject
      def GetSingleton(root_singleton=ioc.IN):
        return root_singleton

      @ioc.Scope
      def ParentScope():
        @ioc.Scope
        def LeafScope():
          # Get root_singleton for the first time inside leaf scope.
          return GetSingleton()
        return LeafScope()

      # The singleton value should attach to the root scope.
      expect(ParentScope()).toBe(GetSingleton())
Ejemplo n.º 47
0
  def it_should_support_injectable_arg_in_singleton_classes(self):
    # pylint: disable=unused-variable

    @ioc.Injectable
    @ioc.Singleton
    class singleton(object):
      def __init__(self, val=ioc.IN):
        self.val = val

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton

    val = object()
    ioc.Injectable.value(val=val)

    expect(ReturnSingleton()).toBe(ReturnSingleton())
    expect(ReturnSingleton().val).toBe(val)
Ejemplo n.º 48
0
  def it_should_support_injectable_arg_in_singleton_classes(self):
    # pylint: disable=unused-variable

    @ioc.Injectable
    @ioc.Singleton
    class singleton(object):
      def __init__(self, val=ioc.IN):
        self.val = val

    @ioc.Inject
    def ReturnSingleton(singleton=ioc.IN):
      return singleton

    val = object()
    ioc.Injectable.value(val=val)

    expect(ReturnSingleton()).toBe(ReturnSingleton())
    expect(ReturnSingleton().val).toBe(val)
Ejemplo n.º 49
0
  def it_should_tolerate_layering_injection_wrappers(self):

    def InjectInjectable():
      @ioc.Inject
      @ioc.Injectable
      def foo(baz=ioc.IN):  # pylint: disable=unused-variable
        return baz

    def InjectableInject():
      @ioc.Injectable
      @ioc.Inject
      def bar(baz=ioc.IN):  # pylint: disable=unused-variable
        return baz

    ioc.Injectable.value(baz=42)  # To ensure that foo is wrapped.

    expect(InjectInjectable).notToRaise(AssertionError)
    expect(InjectableInject).notToRaise(AssertionError)
Ejemplo n.º 50
0
  def it_should_tolerate_layering_injection_wrappers(self):

    def InjectInjectable():
      @ioc.Inject
      @ioc.Injectable
      def foo(baz=ioc.IN):  # pylint: disable=unused-variable
        return baz

    def InjectableInject():
      @ioc.Injectable
      @ioc.Inject
      def bar(baz=ioc.IN):  # pylint: disable=unused-variable
        return baz

    ioc.Injectable.value(baz=42)  # To ensure that foo is wrapped.

    expect(InjectInjectable).notToRaise(AssertionError)
    expect(InjectableInject).notToRaise(AssertionError)
Ejemplo n.º 51
0
    def it_should_attach_to_parent(self):
      @ioc.Inject
      def GetSingleton(parent_singleton=ioc.IN):
        return parent_singleton

      @ioc.Scope
      def ParentScope():
        ioc.Injectable.value(parent_val=object())

        @ioc.Scope
        def LeafScope():
          # Get parent_singleton for the first time inside leaf scope.
          return GetSingleton()
        # The singleton should stay the same even leaf scope is popped.
        expect(LeafScope()).toBe(GetSingleton())

      ParentScope()
      # parent_singleton should have been popped.
      expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 52
0
    def it_should_attach_to_parent(self):
      @ioc.Inject
      def GetSingleton(parent_singleton=ioc.IN):
        return parent_singleton

      @ioc.Scope
      def ParentScope():
        ioc.Injectable.value(parent_val=object())

        @ioc.Scope
        def LeafScope():
          # Get parent_singleton for the first time inside leaf scope.
          return GetSingleton()
        # The singleton should stay the same even leaf scope is popped.
        expect(LeafScope()).toBe(GetSingleton())

      ParentScope()
      # parent_singleton should have been popped.
      expect(GetSingleton).toRaise(ValueError)
Ejemplo n.º 53
0
  def it_should_support_super_class_testing(self):

    @ioc.Inject
    class Foo(object):
      def __init__(self, baz=ioc.IN):
        self.baz = baz
      def method(self):
        return 99

    @ioc.Injectable
    class Bar(Foo):
      def __init__(self):
        super(Bar, self).__init__()

    ioc.SetUpTestInjections(baz=32)
    ioc.Injectable.value(baz=42)

    b = Bar()
    expect(b.baz).toBe(32)
    expect(b.method()).toBe(99)
Ejemplo n.º 54
0
    def it_should_save_injectable_singleton_class(self):
      @ioc.Inject
      class RootInstance(object):
        def __init__(self, parent=ioc.IN):
          self.value = parent.value

      @ioc.Injectable.named('parent')
      @ioc.Singleton
      class ParentSingleton(object):
        def __init__(self, leaf_instance=ioc.IN):
          self.value = leaf_instance.value

      @ioc.Injectable.named('leaf_instance')
      class LeafInstance(object):
        value = 'Leaf Instance'

        def __init__(self):
          pass

      expect(RootInstance().value).toBe(LeafInstance.value)
Ejemplo n.º 55
0
  def it_should_support_super_class_testing(self):

    @ioc.Inject
    class Foo(object):
      def __init__(self, baz=ioc.IN):
        self.baz = baz
      def method(self):
        return 99

    @ioc.Injectable
    class Bar(Foo):
      def __init__(self):
        super(Bar, self).__init__()

    ioc.SetUpTestInjections(baz=32)
    ioc.Injectable.value(baz=42)

    b = Bar()
    expect(b.baz).toBe(32)
    expect(b.method()).toBe(99)
Ejemplo n.º 56
0
    def it_should_save_injectable_singleton_class(self):
      @ioc.Inject
      class RootInstance(object):
        def __init__(self, parent=ioc.IN):
          self.value = parent.value

      @ioc.Injectable.named('parent')
      @ioc.Singleton
      class ParentSingleton(object):
        def __init__(self, leaf_instance=ioc.IN):
          self.value = leaf_instance.value

      @ioc.Injectable.named('leaf_instance')
      class LeafInstance(object):
        value = 'Leaf Instance'

        def __init__(self):
          pass

      expect(RootInstance().value).toBe(LeafInstance.value)
Ejemplo n.º 57
0
  def it_should_support_the_main_thread_adding_scopes_for_children(self):

    class T(ioc.threading.Thread):

      @ioc.Inject
      def run(self, bar=ioc.IN):
        self.setName(bar)
        expect(len(ioc._MyScopes())).toBe(2)


    @ioc.Scope
    def NewScope():
      expect(len(ioc._MyScopes())).toBe(2)
      ioc.Injectable.value(bar='baz')
      t = T()
      t.start()
      t.join()
      return t.name

    expect(len(ioc._MyScopes())).toBe(1)
    expect(NewScope()).toEqual('baz')
Ejemplo n.º 58
0
  def it_should_support_the_main_thread_adding_scopes_for_children(self):

    class T(ioc.threading.Thread):

      @ioc.Inject
      def run(self, bar=ioc.IN):
        self.setName(bar)
        expect(len(ioc._MyScopes())).toBe(2)


    @ioc.Scope
    def NewScope():
      expect(len(ioc._MyScopes())).toBe(2)
      ioc.Injectable.value(bar='baz')
      t = T()
      t.start()
      t.join()
      return t.name

    expect(len(ioc._MyScopes())).toBe(1)
    expect(NewScope()).toEqual('baz')
Ejemplo n.º 59
0
 def it_should_hello(self):
     msg = example.hello(greet='Greeting',
                         app_name='Hello Method',
                         user='******')
     jazz.expect(msg).toEqual('<p>Hello Method: Greeting Test User</p>')
Ejemplo n.º 60
0
 def LeafScope():
   ioc.Injectable.value(leaf_val=object())
   expect(GetSingleton()).toBe(GetSingleton())
   return GetSingleton()