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

Language C# Lines 198
MD5 Hash 48b19a97b1f9125efc313e91c9c0e2d8
Repository https://github.com/daniyord/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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
using System;
using Moq;
using Ninject.Activation;
using Ninject.Activation.Caching;
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();
		}
	}

	public class WhenReleaseIsCalled : CacheContext
	{
		[Fact]
		public void ReturnsFalseIfInstanceIsNotTracked()
		{
			bool result = cache.Release(new object());
			result.ShouldBeFalse();
		}

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

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