Ejemplo n.º 1
0
        public void InstanceIsRemovedFromCache()
        {
            var scope = new object();
            var sword = new Sword();
            var reference = new InstanceReference { Instance = sword };

            var writeContext = new Mock<IContext>();
            writeContext.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            writeContext.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            writeContext.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(int) });
            writeContext.Setup(x => x.GetScope()).Returns(scope);

            cache.Remember(writeContext.Object, reference);

            var readContext = new Mock<IContext>();
            readContext.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            readContext.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            readContext.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(int) });
            readContext.Setup(x => x.GetScope()).Returns(scope);

            object instance1 = cache.TryGet(readContext.Object);
            instance1.ShouldBeSameAs(reference.Instance);

            bool result = cache.Release(instance1);
            result.ShouldBeTrue();

            object instance2 = cache.TryGet(readContext.Object);
            instance2.ShouldBeNull();
        }
Ejemplo n.º 2
0
        public void ProviderInvokesCallbackToRetrieveValue()
        {
            var sword = new Sword();
            provider = new CallbackProvider<Sword>(c => sword);

            var result = provider.Create(contextMock.Object);

            result.Should().BeSameAs(sword);
        }
        public void CallsConstructor()
        {
            var sword = new Sword();

            var samurai = injector.Invoke(new[] { sword }) as Samurai;

            samurai.Should().NotBeNull();
            samurai.Weapon.Should().BeSameAs(sword);
        }
Ejemplo n.º 4
0
        public void ProviderReturnsConstantValue()
        {
            var sword = new Sword();
            provider = new ConstantProvider<Sword>(sword);

            var result = provider.Create(contextMock.Object);

            result.ShouldBeSameAs(sword);
        }
        public void SetsPropertyValue()
        {
            var samurai = new Samurai(null);
            var sword = new Sword();

            injector.Invoke(samurai, sword);

            samurai.Weapon.Should().BeSameAs(sword);
        }
Ejemplo n.º 6
0
        public void factory_uses_kernel()
        {
            var expectedWeapon = new Sword();

            kernel.Bind<IWeapon>().ToConstant(expectedWeapon);

            FactoryProvider<IWeapon> factoryProvider = new FactoryProvider<IWeapon>();

            var result = (Func<IWeapon>)factoryProvider.Create(contextMock.Object);

            Assert.Equal(expectedWeapon, result());
        }
Ejemplo n.º 7
0
        public void UncollectedScopeInstancesAreNotRemoved()
        {
            var sword = new Sword();
            var swordWeakReference = new WeakReference(sword);
            var context = CreateContextMock(new TestObject(42), this.bindingConfigurationMock.Object);

            this.Remember(sword, context);
            GC.Collect();
            bool swordCollected = !swordWeakReference.IsAlive;

            swordCollected.Should().BeFalse();
        }
Ejemplo n.º 8
0
        public void GivenAMixtureOfBindings_OnlyNonImplicitBindingsWillResolve()
        {
            var shortSword = new ShortSword();
            var sword = new Sword();
            var shuriken = new Shuriken();

            kernel.Bind<IWeapon>().ToConstant(shortSword);
            kernel.Bind<IWeapon>().ToConstant(sword);
            kernel.Bind<IWeapon>().ToConstant(shuriken).Binding.IsImplicit = true;
            var result = kernel.GetAll<IWeapon>();
            result.Should().Contain(shortSword);
            result.Should().Contain(sword);
            result.Should().NotContain(shuriken);
        }
Ejemplo n.º 9
0
        public void WhenNoScopeIsDefinedAllEntriesAreReleased()
        {
            var sword = new Sword();
            var reference = new InstanceReference { Instance = sword };
            var context1 = CreateContextMock(new object(), this.bindingConfigurationMock.Object);
            var context2 = CreateContextMock(new object(), this.bindingConfigurationMock.Object);

            cache.Remember(context1.Object, reference);
            cache.Remember(context2.Object, reference);
            cache.Clear();
            var instance1 = cache.TryGet(context1.Object);
            var instance2 = cache.TryGet(context2.Object);

            instance1.Should().BeNull();
            instance2.Should().BeNull();
        }
Ejemplo n.º 10
0
        public void WhenScopeIsDefinedItsEntriesAreReleased()
        {
            var scope = new object();
            var sword = new Sword();
            var reference = new InstanceReference { Instance = sword };
            var context1 = CreateContextMock(scope, bindingMock.Object);
            var context2 = CreateContextMock(new object(), bindingMock.Object);

            cache.Remember(context1.Object, reference);
            cache.Remember(context2.Object, reference);
            cache.Clear(scope);
            var instance1 = cache.TryGet(context1.Object);
            var instance2 = cache.TryGet(context2.Object);

            instance1.Should().BeNull();
            instance2.Should().NotBeNull();
        }
Ejemplo n.º 11
0
        public void ReturnsTrueIfInstanceIsTracked()
        {
            var scope = new object();
            var instance = new Sword();
            var reference = new InstanceReference { Instance = instance };

            var writeContext = new Mock<IContext>();
            writeContext.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            writeContext.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            writeContext.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(int) });
            writeContext.Setup(x => x.GetScope()).Returns(scope);

            cache.Remember(writeContext.Object, reference);

            bool result = cache.Release(instance);
            result.ShouldBeTrue();
        }
Ejemplo n.º 12
0
        public void ReturnsNullIfNoInstancesHaveBeenAddedWithinSpecifiedScope()
        {
            var sword = new Sword();

            var contextMock1 = new Mock<IContext>();
            contextMock1.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock1.SetupGet(x => x.Instance).Returns(sword);
            contextMock1.Setup(x => x.GetScope()).Returns(new object());

            cache.Remember(contextMock1.Object);

            var contextMock2 = new Mock<IContext>();
            contextMock2.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock2.Setup(x => x.GetScope()).Returns(new object());

            object instance = cache.TryGet(contextMock2.Object);

            instance.ShouldBeNull();
        }
Ejemplo n.º 13
0
        public void ReturnsCachedInstanceIfOneHasBeenAddedWithinSpecifiedScope()
        {
            var scope = new object();
            var sword = new Sword();

            var contextMock1 = new Mock<IContext>();
            contextMock1.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock1.SetupGet(x => x.Instance).Returns(sword);
            contextMock1.Setup(x => x.GetScope()).Returns(scope);

            cache.Remember(contextMock1.Object);

            var contextMock2 = new Mock<IContext>();
            contextMock2.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock2.Setup(x => x.GetScope()).Returns(scope);

            object instance = cache.TryGet(contextMock2.Object);

            instance.ShouldBeSameAs(sword);
        }
Ejemplo n.º 14
0
        public void InstanceIsRemovedFromCache()
        {
            var scope = new object();
            var sword = new Sword();
            var reference = new InstanceReference { Instance = sword };
            var writeContext = CreateContextMock(scope, this.bindingConfigurationMock.Object, typeof(int));
            var readContext = CreateContextMock(scope, this.bindingConfigurationMock.Object, typeof(int));

            cache.Remember(writeContext.Object, reference);
            object instance1 = cache.TryGet(readContext.Object);
            bool result = cache.Release(instance1);
            object instance2 = cache.TryGet(readContext.Object);

            instance1.Should().BeSameAs(reference.Instance);
            result.Should().BeTrue();
            instance2.Should().BeNull();
        }
Ejemplo n.º 15
0
 private WeakReference Process()
 {
     var sword = new Sword();
     this.kernel.Get<FootSoldier>(new WeakPropertyValue("Weapon", sword));
     return new WeakReference(sword);
 }
Ejemplo n.º 16
0
        public void CollectedScopeInstancesAreRemoved()
        {
            var sword = new Sword();
            var swordWeakReference = new WeakReference(sword);
            var context = CreateContextMock(new object(), bindingMock.Object);

            this.Execute(sword, context);
            sword = null;
            context = null;
            GC.Collect();
            cache.Prune();
            GC.Collect();
            bool swordCollected = !swordWeakReference.IsAlive;

            swordCollected.Should().BeTrue();
        }
Ejemplo n.º 17
0
 private void Execute(Sword sword, IContext context)
 {
     this.cache.Remember(context, new InstanceReference { Instance = sword });
 }
Ejemplo n.º 18
0
        public void ReturnsInstanceIfOneHasBeenCachedWithSameGenericParameters()
        {
            var scope = new object();
            var sword = new Sword();

            var contextMock1 = new Mock<IContext>();
            contextMock1.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock1.SetupGet(x => x.Instance).Returns(sword);
            contextMock1.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            contextMock1.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(int) });
            contextMock1.Setup(x => x.GetScope()).Returns(scope);

            cache.Remember(contextMock1.Object);

            var contextMock2 = new Mock<IContext>();
            contextMock2.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock2.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            contextMock2.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(int) });
            contextMock2.Setup(x => x.GetScope()).Returns(scope);

            object instance = cache.TryGet(contextMock2.Object);

            instance.ShouldBeSameAs(sword);
        }
Ejemplo n.º 19
0
        public void ReturnsNullIfInstanceAddedToCacheHasDifferentGenericParameters()
        {
            var scope = new object();
            var sword = new Sword();

            var contextMock1 = new Mock<IContext>();
            contextMock1.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock1.SetupGet(x => x.Instance).Returns(sword);
            contextMock1.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            contextMock1.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(int) });
            contextMock1.Setup(x => x.GetScope()).Returns(scope);

            cache.Remember(contextMock1.Object);

            var contextMock2 = new Mock<IContext>();
            contextMock2.SetupGet(x => x.Binding).Returns(bindingMock.Object);
            contextMock2.SetupGet(x => x.HasInferredGenericArguments).Returns(true);
            contextMock2.SetupGet(x => x.GenericArguments).Returns(new[] { typeof(double) });
            contextMock2.Setup(x => x.GetScope()).Returns(scope);

            object instance = cache.TryGet(contextMock2.Object);

            instance.ShouldBeNull();
        }
Ejemplo n.º 20
0
        public void CachedObjectsAreReleased()
        {
            var scope = new object();
            var scopeOfScope = new object();
            var sword = new Sword();
            var context = CreateContextMock(scope, this.bindingConfigurationMock.Object);

            cache.Remember(context.Object, new InstanceReference { Instance = sword });
            cache.Remember(CreateContextMock(scopeOfScope, this.bindingConfigurationMock.Object).Object, new InstanceReference { Instance = scope });
            cache.Clear(scopeOfScope);
            var instance = cache.TryGet(context.Object);

            instance.Should().BeNull();
        }
Ejemplo n.º 21
0
        public void WhenScopeIsDefinedItsEntriesAreReleased()
        {
            var scope = new TestObject(42);
            var sword = new Sword();
            var reference = new InstanceReference { Instance = sword };
            var context1 = CreateContext(scope, this.bindingConfiguration);
            var context2 = CreateContext(new TestObject(42), this.bindingConfiguration);

            cache.Remember(context1, reference);
            cache.Remember(context2, reference);
            cache.Clear(scope);
            var instance1 = cache.TryGet(context1);
            var instance2 = cache.TryGet(context2);

            instance1.Should().BeNull();
            instance2.Should().NotBeNull();
        }
Ejemplo n.º 22
0
 private WeakReference Process()
 {
     var sword = new Sword();
     this.kernel.Get<Barracks>(new WeakConstructorArgument("weapon", sword));
     return new WeakReference(sword);
 }
Ejemplo n.º 23
0
        public void CachedObjectsAreReleased()
        {
            var scopeMock = new Mock<INotifyWhenDisposed>();
            var sword = new Sword();
            var reference = new InstanceReference { Instance = sword };
            var context = CreateContextMock(scopeMock.Object, this.bindingConfigurationMock.Object);

            cache.Remember(context.Object, reference);
            scopeMock.Raise(scope => scope.Disposed += null, EventArgs.Empty);
            object instance = cache.TryGet(context.Object);

            instance.Should().BeNull();
        }
Ejemplo n.º 24
0
        public void UncollectedScopeInstancesAreNotRemoved()
        {
            var sword = new Sword();
            var swordWeakReference = new WeakReference(sword);
            var context = CreateContextMock(new object(), bindingMock.Object);

            this.Execute(sword, context);
            GC.Collect();
            bool swordCollected = !swordWeakReference.IsAlive;

            swordCollected.ShouldBeFalse();
        }
Ejemplo n.º 25
0
 public ClassWithSelfBindableType(Sword sword)
 {
     this.Sword = sword;
 }
Ejemplo n.º 26
0
        public void ReturnsTrueIfInstanceIsTracked()
        {
            var scope = new object();
            var instance = new Sword();
            var reference = new InstanceReference { Instance = instance };
            var writeContext = CreateContextMock(scope, this.bindingConfigurationMock.Object, typeof(int));

            cache.Remember(writeContext.Object, reference);
            bool result = cache.Release(instance);

            result.Should().BeTrue();
        }