Beispiel #1
0
        public void OnUnsubscribed_StopsFileChangeTracker()
        {
            // Arrange
            var filePath    = "C:\\path\\to\\project\\file.cshtml";
            var projectPath = "C:\\path\\to\\project\\project.csproj";
            var tracker     = Mock.Of <VisualStudioDocumentTracker>(t => t.FilePath == filePath && t.ProjectPath == projectPath);
            var templateEngineFactoryService = GetTemplateEngineFactoryService();

            var fileChangeTracker = new Mock <FileChangeTracker>();

            fileChangeTracker.Setup(f => f.StopListening()).Verifiable();
            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>(MockBehavior.Strict);

            fileChangeTrackerFactory
            .Setup(f => f.Create("C:\\path\\to\\project\\_ViewImports.cshtml"))
            .Returns(fileChangeTracker.Object)
            .Verifiable();

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object, templateEngineFactoryService);

            manager.OnSubscribed(tracker); // Start tracking the import.

            // Act
            manager.OnUnsubscribed(tracker);

            // Assert
            fileChangeTrackerFactory.Verify();
            fileChangeTracker.Verify();
        }
Beispiel #2
0
        public void OnUnsubscribed_AnotherDocumentTrackingImport_DoesNotStopFileChangeTracker()
        {
            // Arrange
            var filePath    = "C:\\path\\to\\project\\file.cshtml";
            var projectPath = "C:\\path\\to\\project\\project.csproj";
            var tracker     = Mock.Of <VisualStudioDocumentTracker>(t => t.FilePath == filePath && t.ProjectPath == projectPath);
            var templateEngineFactoryService = GetTemplateEngineFactoryService();

            var fileChangeTracker = new Mock <FileChangeTracker>();

            fileChangeTracker
            .Setup(f => f.StopListening())
            .Throws(new InvalidOperationException());
            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>();

            fileChangeTrackerFactory
            .Setup(f => f.Create(It.IsAny <string>()))
            .Returns(fileChangeTracker.Object);

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object, templateEngineFactoryService);

            manager.OnSubscribed(tracker); // Starts tracking import for the first document.

            var anotherFilePath = "C:\\path\\to\\project\\anotherFile.cshtml";
            var anotherTracker  = Mock.Of <VisualStudioDocumentTracker>(t => t.FilePath == anotherFilePath && t.ProjectPath == projectPath);

            manager.OnSubscribed(anotherTracker); // Starts tracking import for the second document.

            // Act & Assert (Does not throw)
            manager.OnUnsubscribed(tracker);
        }
Beispiel #3
0
        public void OnSubscribed_AlreadySubscribed_DoesNothing()
        {
            // Arrange
            var filePath    = "C:\\path\\to\\project\\file.cshtml";
            var projectPath = "C:\\path\\to\\project\\project.csproj";
            var tracker     = Mock.Of <VisualStudioDocumentTracker>(t => t.FilePath == filePath && t.ProjectPath == projectPath);
            var templateEngineFactoryService = GetTemplateEngineFactoryService();

            var callCount = 0;
            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>();

            fileChangeTrackerFactory
            .Setup(f => f.Create(It.IsAny <string>()))
            .Returns(Mock.Of <FileChangeTracker>())
            .Callback(() => callCount++);

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object, templateEngineFactoryService);

            manager.OnSubscribed(tracker); // Start tracking the import.

            var anotherFilePath = "C:\\path\\to\\project\\anotherFile.cshtml";
            var anotherTracker  = Mock.Of <VisualStudioDocumentTracker>(t => t.FilePath == anotherFilePath && t.ProjectPath == projectPath);

            // Act
            manager.OnSubscribed(anotherTracker);

            // Assert
            Assert.Equal(1, callCount);
        }
        public void OnSubscribed_AlreadySubscribed_DoesNothing()
        {
            // Arrange
            var tracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == Path.Combine(DirectoryPath, "file.cshtml") &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine && p.GetDocument(It.IsAny <string>()) == null, MockBehavior.Strict), MockBehavior.Strict);

            var anotherTracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == Path.Combine(DirectoryPath, "anotherFile.cshtml") &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine && p.GetDocument(It.IsAny <string>()) == null, MockBehavior.Strict), MockBehavior.Strict);

            var callCount = 0;
            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>(MockBehavior.Strict);
            var fileChangeTracker        = new Mock <FileChangeTracker>(MockBehavior.Strict);

            fileChangeTracker.Setup(t => t.StartListening()).Verifiable();
            fileChangeTrackerFactory
            .Setup(f => f.Create(It.IsAny <string>()))
            .Returns(fileChangeTracker.Object)
            .Callback(() => callCount++);

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object);

            manager.OnSubscribed(tracker); // Start tracking the import.

            // Act
            manager.OnSubscribed(anotherTracker);

            // Assert
            Assert.Equal(1, callCount);
        }
        public void OnUnsubscribed_AnotherDocumentTrackingImport_DoesNotStopFileChangeTracker()
        {
            // Arrange
            var tracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == Path.Combine(DirectoryPath, "file.cshtml") &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine && p.GetDocument(It.IsAny <string>()) == null, MockBehavior.Strict), MockBehavior.Strict);

            var anotherTracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == Path.Combine(DirectoryPath, "anotherFile.cshtml") &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine && p.GetDocument(It.IsAny <string>()) == null, MockBehavior.Strict), MockBehavior.Strict);

            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>(MockBehavior.Strict);
            var fileChangeTracker        = new Mock <FileChangeTracker>(MockBehavior.Strict);

            fileChangeTracker.Setup(f => f.StartListening()).Verifiable();
            fileChangeTracker
            .Setup(f => f.StopListening())
            .Throws(new InvalidOperationException());
            fileChangeTrackerFactory
            .Setup(f => f.Create(It.IsAny <string>()))
            .Returns(fileChangeTracker.Object);

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object);

            manager.OnSubscribed(tracker);        // Starts tracking import for the first document.

            manager.OnSubscribed(anotherTracker); // Starts tracking import for the second document.

            // Act & Assert (Does not throw)
            manager.OnUnsubscribed(tracker);
            manager.OnUnsubscribed(tracker);
        }
        public void OnUnsubscribed_StopsFileChangeTracker()
        {
            // Arrange
            var tracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == Path.Combine(DirectoryPath, "file.cshtml") &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine && p.GetDocument(It.IsAny <string>()) == null, MockBehavior.Strict), MockBehavior.Strict);

            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>(MockBehavior.Strict);
            var fileChangeTracker        = new Mock <FileChangeTracker>(MockBehavior.Strict);

            fileChangeTracker.Setup(f => f.StartListening()).Verifiable();
            fileChangeTracker
            .Setup(f => f.StopListening())
            .Verifiable();
            fileChangeTrackerFactory
            .Setup(f => f.Create(Path.Combine(DirectoryPath, "_ViewImports.cshtml")))
            .Returns(fileChangeTracker.Object)
            .Verifiable();

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object);

            manager.OnSubscribed(tracker); // Start tracking the import.

            // Act
            manager.OnUnsubscribed(tracker);

            // Assert
            fileChangeTrackerFactory.Verify();
            fileChangeTracker.Verify();
        }
Beispiel #7
0
        public void Changed_TrackerChanged_ResultsInChangedHavingCorrectArgs()
        {
            // Arrange
            var testImportsPath = "C:\\path\\to\\project\\_ViewImports.cshtml";

            var tracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == "C:\\path\\to\\project\\Views\\Home\\file.cshtml" &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine));

            var anotherTracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == "C:\\path\\to\\project\\anotherFile.cshtml" &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine));

            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>();
            var fileChangeTracker        = new Mock <FileChangeTracker>();

            fileChangeTracker
            .Setup(f => f.FilePath)
            .Returns(testImportsPath);
            fileChangeTrackerFactory
            .Setup(f => f.Create(testImportsPath))
            .Returns(fileChangeTracker.Object);

            fileChangeTrackerFactory
            .Setup(f => f.Create("C:\\path\\to\\project\\Views\\_ViewImports.cshtml"))
            .Returns(Mock.Of <FileChangeTracker>());
            fileChangeTrackerFactory
            .Setup(f => f.Create("C:\\path\\to\\project\\Views\\Home\\_ViewImports.cshtml"))
            .Returns(Mock.Of <FileChangeTracker>());

            var called  = false;
            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object);

            manager.OnSubscribed(tracker);
            manager.OnSubscribed(anotherTracker);
            manager.Changed += (sender, args) =>
            {
                called = true;
                Assert.Same(sender, manager);
                Assert.Equal(testImportsPath, args.FilePath);
                Assert.Equal(FileChangeKind.Changed, args.Kind);
                Assert.Collection(
                    args.AssociatedDocuments,
                    f => Assert.Equal("C:\\path\\to\\project\\Views\\Home\\file.cshtml", f),
                    f => Assert.Equal("C:\\path\\to\\project\\anotherFile.cshtml", f));
            };

            // Act
            fileChangeTracker.Raise(t => t.Changed += null, new FileChangeEventArgs(testImportsPath, FileChangeKind.Changed));

            // Assert
            Assert.True(called);
        }
        public void OnSubscribed_StartsFileChangeTrackers()
        {
            // Arrange
            var tracker = Mock.Of <VisualStudioDocumentTracker>(
                t => t.FilePath == "C:\\path\\to\\project\\Views\\Home\\file.cshtml" &&
                t.ProjectPath == ProjectPath &&
                t.ProjectSnapshot == Mock.Of <ProjectSnapshot>(p => p.GetProjectEngine() == ProjectEngine));

            var fileChangeTrackerFactory = new Mock <FileChangeTrackerFactory>();
            var fileChangeTracker1       = new Mock <FileChangeTracker>();

            fileChangeTracker1
            .Setup(f => f.StartListening())
            .Verifiable();
            fileChangeTrackerFactory
            .Setup(f => f.Create("C:\\path\\to\\project\\Views\\Home\\_ViewImports.cshtml"))
            .Returns(fileChangeTracker1.Object)
            .Verifiable();
            var fileChangeTracker2 = new Mock <FileChangeTracker>();

            fileChangeTracker2
            .Setup(f => f.StartListening())
            .Verifiable();
            fileChangeTrackerFactory
            .Setup(f => f.Create("C:\\path\\to\\project\\Views\\_ViewImports.cshtml"))
            .Returns(fileChangeTracker2.Object)
            .Verifiable();
            var fileChangeTracker3 = new Mock <FileChangeTracker>();

            fileChangeTracker3.Setup(f => f.StartListening()).Verifiable();
            fileChangeTrackerFactory
            .Setup(f => f.Create("C:\\path\\to\\project\\_ViewImports.cshtml"))
            .Returns(fileChangeTracker3.Object)
            .Verifiable();

            var manager = new DefaultImportDocumentManager(Dispatcher, new DefaultErrorReporter(), fileChangeTrackerFactory.Object);

            // Act
            manager.OnSubscribed(tracker);

            // Assert
            fileChangeTrackerFactory.Verify();
            fileChangeTracker1.Verify();
            fileChangeTracker2.Verify();
            fileChangeTracker3.Verify();
        }