ninject /src/Ninject.Tests/Unit/CacheTests.cs

Language C# Lines 139
MD5 Hash afb5002505c3b69372bfe08236d7d25d
Repository https://github.com/developingchris/ninject.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using Moq;
using Ninject.Activation;
using Ninject.Activation.Caching;
using Ninject.Infrastructure;
using Ninject.Planning.Bindings;
using Ninject.Tests.Fakes;
using Xunit;
using Xunit.Should;

namespace Ninject.Tests.Unit.CacheTests
{
	public class CacheContext
	{
		protected Mock<IPipeline> activatorMock;
		protected Mock<ICachePruner> cachePrunerMock;
		protected Mock<IBinding> bindingMock;
		protected Cache cache;

		public CacheContext()
		{
			activatorMock = new Mock<IPipeline>();
			cachePrunerMock = new Mock<ICachePruner>();
			bindingMock = new Mock<IBinding>();
			cache = new Cache(activatorMock.Object, cachePrunerMock.Object);
		}
	}

	public class WhenTryGetInstanceIsCalled : CacheContext
	{
		[Fact]
		public void ReturnsNullIfNoInstancesHaveBeenAddedToCache()
		{
			var scope = new object();

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

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

			instance.ShouldBeNull();
		}

		[Fact]
		public void ReturnsCachedInstanceIfOneHasBeenAddedWithinSpecifiedScope()
		{
			var scope = new object();
			var reference = new InstanceReference { Instance = new Sword() };

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

			cache.Remember(contextMock1.Object, reference);

			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(reference.Instance);
		}

		[Fact]
		public void ReturnsNullIfNoInstancesHaveBeenAddedWithinSpecifiedScope()
		{
			var reference = new InstanceReference { Instance = new Sword() };

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

			cache.Remember(contextMock1.Object, reference);

			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();
		}
	}

	public class WhenTryGetInstanceIsCalledForContextWithGenericInference : CacheContext
	{
		[Fact]
		public void ReturnsInstanceIfOneHasBeenCachedWithSameGenericParameters()
		{
			var scope = new object();
			var reference = new InstanceReference { Instance = new Sword() };

			var contextMock1 = new Mock<IContext>();
			contextMock1.SetupGet(x => x.Binding).Returns(bindingMock.Object);
			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, reference);

			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(reference.Instance);
		}

		[Fact]
		public void ReturnsNullIfInstanceAddedToCacheHasDifferentGenericParameters()
		{
			var scope = new object();
			var reference = new InstanceReference { Instance = new Sword() };

			var contextMock1 = new Mock<IContext>();
			contextMock1.SetupGet(x => x.Binding).Returns(bindingMock.Object);
			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, reference);

			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();
		}
	}
}
Back to Top