public void OnSubscribed_AlreadyTrackingImport_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(); uint cookie; var callCount = 0; var fileChangeService = new Mock <IVsFileChangeEx>(); fileChangeService .Setup(f => f.AdviseFileChange(It.IsAny <string>(), It.IsAny <uint>(), It.IsAny <IVsFileChangeEvents>(), out cookie)) .Returns(VSConstants.S_OK) .Callback(() => callCount++); var manager = new DefaultImportDocumentManager(fileChangeService.Object, templateEngineFactoryService, Dispatcher, new DefaultErrorReporter()); 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 OnUnsubscribed_AnotherDocumentTrackingImport_DoesNotStopTrackingImport() { // 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(); uint cookie; var fileChangeService = new Mock <IVsFileChangeEx>(); fileChangeService .Setup(f => f.AdviseFileChange(It.IsAny <string>(), It.IsAny <uint>(), It.IsAny <IVsFileChangeEvents>(), out cookie)) .Returns(VSConstants.S_OK); fileChangeService .Setup(f => f.UnadviseFileChange(It.IsAny <uint>())) .Returns(VSConstants.S_OK) .Callback(() => throw new InvalidOperationException()); var manager = new DefaultImportDocumentManager(fileChangeService.Object, templateEngineFactoryService, Dispatcher, new DefaultErrorReporter()); 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); }
public void OnUnsubscribed_StopsTrackingImport() { // 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(); uint cookie = 100; var fileChangeService = new Mock <IVsFileChangeEx>(MockBehavior.Strict); fileChangeService .Setup(f => f.AdviseFileChange("C:\\path\\to\\project\\_ViewImports.cshtml", It.IsAny <uint>(), It.IsAny <IVsFileChangeEvents>(), out cookie)) .Returns(VSConstants.S_OK) .Verifiable(); fileChangeService .Setup(f => f.UnadviseFileChange(cookie)) .Returns(VSConstants.S_OK) .Verifiable(); var manager = new DefaultImportDocumentManager(fileChangeService.Object, templateEngineFactoryService, Dispatcher, new DefaultErrorReporter()); manager.OnSubscribed(tracker); // Start tracking the import. // Act manager.OnUnsubscribed(tracker); // Assert fileChangeService.Verify(); }
public void OnFilesChanged_WithSpecificFlags_InvokesChangedHandler_WithExpectedArguments(uint fileChangeFlag, int expectedKind) { // 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 anotherFilePath = "C:\\path\\to\\project\\anotherFile.cshtml"; var anotherTracker = Mock.Of <VisualStudioDocumentTracker>(t => t.FilePath == anotherFilePath && t.ProjectPath == projectPath); uint cookie; var fileChangeService = new Mock <IVsFileChangeEx>(); fileChangeService .Setup(f => f.AdviseFileChange(It.IsAny <string>(), It.IsAny <uint>(), It.IsAny <IVsFileChangeEvents>(), out cookie)) .Returns(VSConstants.S_OK); var manager = new DefaultImportDocumentManager(fileChangeService.Object, templateEngineFactoryService, Dispatcher, new DefaultErrorReporter()); manager.OnSubscribed(tracker); manager.OnSubscribed(anotherTracker); var called = false; manager.Changed += (sender, args) => { called = true; Assert.Same(sender, manager); Assert.Equal("C:\\path\\to\\project\\_ViewImports.cshtml", args.FilePath); Assert.Equal((ImportChangeKind)expectedKind, args.Kind); Assert.Collection( args.AssociatedDocuments, f => Assert.Equal(filePath, f), f => Assert.Equal(anotherFilePath, f)); }; // Act manager.OnFilesChanged(fileCount: 1, filePaths: new[] { "C:\\path\\to\\project\\_ViewImports.cshtml" }, fileChangeFlags: new[] { fileChangeFlag }); // Assert Assert.True(called); }