Beispiel #1
0
        public void Fetch_ClearsCachedExtension_AndFetchesNewExtension_WhenUnderlyingCollectionChanged()
        {
            // Arrange
            int           revision             = 1;
            var           createdExtension     = new TestExtension();
            var           replacementExtension = new TestExtension();
            TestExtension capturedExtension    = null;
            var           collection           = CreateCollection <TestExtension>(
                onRevision: () => revision,
                extension: null,
                onSet: e => capturedExtension = e);
            var refs = new WorkContextExtensionReferences <ExtensionCache>(collection);

            refs.Fetch <TestExtension>(ref refs.Cache.Test, e => createdExtension);

            // Act
            revision++;
            var extension = refs.Fetch <TestExtension>(ref refs.Cache.Test, e => replacementExtension);

            // Assert
            Assert.NotNull(extension);
            Assert.Same(replacementExtension, extension);
            Assert.Equal(2, refs.Revision);
            Assert.NotNull(capturedExtension);
            Assert.Same(replacementExtension, capturedExtension);
        }
Beispiel #2
0
        public void Constructor_SetsProperties()
        {
            // Arrange
            var collection = CreateCollection <object>(revision: 1);

            // Act
            var refs = new WorkContextExtensionReferences <object>(collection);

            // Assert
            Assert.Equal(1, refs.Revision);
            Assert.Same(collection, refs.Extensions);
        }
Beispiel #3
0
        public void Fetch_FetchesExtension_FromCollection_WhenNotCached_AndExtensionExists()
        {
            // Arrange
            var           existingExtension = new TestExtension();
            TestExtension capturedExtension = null;
            var           collection        = CreateCollection <TestExtension>(
                revision: 1,
                extension: existingExtension,
                onSet: e => capturedExtension = e);
            var refs = new WorkContextExtensionReferences <ExtensionCache>(collection);

            // Act
            var extension = refs.Fetch <TestExtension>(ref refs.Cache.Test, e => new TestExtension());

            // Assert
            Assert.NotNull(extension);
            Assert.Same(existingExtension, extension);
            Assert.Null(capturedExtension);
        }
Beispiel #4
0
        public void Fetch_ReturnsCachedExtension_WhenCollectionUnchanged()
        {
            // Arrange
            int           revision          = 1;
            var           createdExtension  = new TestExtension();
            TestExtension capturedExtension = null;
            var           collection        = CreateCollection <TestExtension>(
                onRevision: () => revision,
                onGet: () => createdExtension,
                onSet: e => capturedExtension = e);
            var refs = new WorkContextExtensionReferences <ExtensionCache>(collection);

            refs.Fetch <TestExtension>(ref refs.Cache.Test, e => createdExtension);

            // Act
            var extension = refs.Fetch <TestExtension>(ref refs.Cache.Test, e => new TestExtension());

            // Assert
            Assert.NotNull(extension);
            Assert.Same(createdExtension, extension);
            Assert.Equal(1, refs.Revision);
        }
Beispiel #5
0
        public void Fetch_CreatesExtension_UsingFactory_WhenNotCached_AndExtensionDoesNotExist()
        {
            // Arrange
            int           revision          = 1;
            var           existingExtension = new TestExtension();
            TestExtension capturedExtension = null;
            var           collection        = CreateCollection <TestExtension>(
                onRevision: () => revision,
                extension: null,
                onSet: e => capturedExtension = e);
            var refs = new WorkContextExtensionReferences <ExtensionCache>(collection);

            // Act
            revision++;
            var extension = refs.Fetch <TestExtension>(ref refs.Cache.Test, e => new TestExtension());

            // Assert
            Assert.NotNull(extension);
            Assert.NotSame(existingExtension, extension);
            Assert.NotNull(capturedExtension);
            Assert.Equal(2, refs.Revision);
        }
Beispiel #6
0
 /// <summary>
 /// Initialises a new instance of <see cref="WorkContext"/>.
 /// </summary>
 /// <param name="extensions"></param>
 public WorkContext(IWorkContextExtensionCollection extensions)
 {
     _extensions = new WorkContextExtensionReferences <StandardExtensions>(extensions);
 }