def test_popped_layer_is_no_longer_used(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(top_canary.touched).to(be_false)
def test_pops_top_layer_off_the_stack(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(bottom_canary.touched).to(be_true)
def test_does_not_use_object_injected_lower_in_stack(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(bottom_canary.touched).to(be_false)
def test_uses_object_injected_at_top_of_stack(self): bottom_canary = Canary() top_canary = Canary() with dependency_context() as bottom_context: bottom_context.inject(Canary, SingletonClass(bottom_canary)) with dependency_context() as top_context: top_context.inject(Canary, SingletonClass(top_canary)) touch_a_canary() expect(top_canary.touched).to(be_true)
def inject_as_class(self, dependency, injected): """ Inject an object as though it were a class. When the victim requests the class, the injector returns a SingletonClass which wraps the injected object. """ self.inject(dependency, SingletonClass(injected))
def test_simple_injection(self): my_canary = Canary() context = open_dependency_context() context.inject(Canary, SingletonClass(my_canary)) touch_a_canary() context.close() expect(my_canary.touched).to(be_true)
def test_injection_does_not_affect_another_thread(self): my_canary = Canary() with dependency_context() as context: context.inject(Canary, SingletonClass(my_canary)) t = Thread(target=touch_a_canary) t.start() t.join() expect(my_canary.touched).to(be_false)