public void AddHierarchicalImports_AddsViewImportSourceDocumentsOnDisk() { // Arrange var imports = new List <RazorSourceDocument>(); var testFileSystem = new TestRazorProjectFileSystem(new[] { new TestRazorProjectItem("/Index.cshtml"), new TestRazorProjectItem("/_ViewImports.cshtml"), new TestRazorProjectItem("/Contact/_ViewImports.cshtml"), new TestRazorProjectItem("/Contact/Index.cshtml"), }); var mvcImportFeature = new DefaultMvcImportFeature() { ProjectEngine = Mock.Of <RazorProjectEngine>(projectEngine => projectEngine.FileSystem == testFileSystem) }; // Act mvcImportFeature.AddHierarchicalImports("/Contact/Index.cshtml", imports); // Assert Assert.Collection(imports, import => Assert.Equal("/_ViewImports.cshtml", import.FilePath), import => Assert.Equal("/Contact/_ViewImports.cshtml", import.FilePath)); }
public void GetViewStartFactories_FindsFullHierarchy() { // Arrange var descriptor = new PageActionDescriptor() { RelativePath = "/Pages/Level1/Level2/Index.cshtml", FilterDescriptors = new FilterDescriptor[0], ViewEnginePath = "/Pages/Level1/Level2/Index.cshtml" }; var compiledPageDescriptor = new CompiledPageActionDescriptor(descriptor) { PageTypeInfo = typeof(object).GetTypeInfo(), }; var loader = new Mock <IPageLoader>(); loader .Setup(l => l.Load(It.IsAny <PageActionDescriptor>())) .Returns(compiledPageDescriptor); var fileProvider = new TestFileProvider(); fileProvider.AddFile("/_ViewStart.cshtml", "page content"); fileProvider.AddFile("/Pages/_ViewStart.cshtml", "page content"); fileProvider.AddFile("/Pages/Level1/_ViewStart.cshtml", "page content"); fileProvider.AddFile("/Pages/Level1/Level2/_ViewStart.cshtml", "page content"); fileProvider.AddFile("/Pages/Level1/Level3/_ViewStart.cshtml", "page content"); var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment); var mock = new Mock <IRazorPageFactoryProvider>(MockBehavior.Strict); mock .Setup(p => p.CreateFactory("/Pages/Level1/Level2/_ViewStart.cshtml")) .Returns(new RazorPageFactoryResult(new CompiledViewDescriptor(), () => null)) .Verifiable(); mock .Setup(p => p.CreateFactory("/Pages/Level1/_ViewStart.cshtml")) .Returns(new RazorPageFactoryResult(new CompiledViewDescriptor(), () => null)) .Verifiable(); mock .Setup(p => p.CreateFactory("/Pages/_ViewStart.cshtml")) .Returns(new RazorPageFactoryResult(new CompiledViewDescriptor(), () => null)) .Verifiable(); mock .Setup(p => p.CreateFactory("/_ViewStart.cshtml")) .Returns(new RazorPageFactoryResult(new CompiledViewDescriptor(), () => null)) .Verifiable(); var razorPageFactoryProvider = mock.Object; var invokerProvider = CreateInvokerProvider( loader.Object, CreateActionDescriptorCollection(descriptor), razorPageFactoryProvider: razorPageFactoryProvider, fileSystem: fileSystem); // Act var factories = invokerProvider.GetViewStartFactories(compiledPageDescriptor); // Assert mock.Verify(); }
private RazorComponentRenameEndpoint CreateEndpoint(LanguageServerFeatureOptions languageServerFeatureOptions = null) { var tagHelperDescriptors = new List <TagHelperDescriptor>(); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("First", "First.Components", "Component1")); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("First", "Test", "Component2")); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("Second", "Second.Components", "Component3")); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("Second", "Second.Components", "Component4")); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("First", "Test", "Component1337")); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("First", "Test.Components", "Directory1")); tagHelperDescriptors.AddRange(CreateRazorComponentTagHelperDescriptors("First", "Test.Components", "Directory2")); var item1 = CreateProjectItem("@namespace First.Components\n@using Test\n<Component2></Component2>", "c:/First/Component1.razor"); var item2 = CreateProjectItem("@namespace Test", "c:/First/Component2.razor"); var item3 = CreateProjectItem("@namespace Second.Components\n<Component3></Component3>", "c:/Second/Component3.razor"); var item4 = CreateProjectItem("@namespace Second.Components\n<Component3></Component3>\n<Component3></Component3>", "c:/Second/Component4.razor"); var itemComponentParam = CreateProjectItem("@namespace Second.Components\n<Component3 Title=\"Something\"></Component3>", "c:/Second/Component5.razor"); var item1337 = CreateProjectItem(string.Empty, "c:/First/Component1337.razor"); var indexItem = CreateProjectItem("@namespace First.Components\n@using Test\n<Component1337></Component1337>\n<Test.Component1337></Test.Component1337>", "c:/First/Index.razor"); var itemDirectory1 = CreateProjectItem("@namespace Test.Components\n<Directory2></Directory2>", "c:/Dir1/Directory1.razor"); var itemDirectory2 = CreateProjectItem("@namespace Test.Components\n<Directory1></Directory1>", "c:/Dir2/Directory2.razor"); var fileSystem = new TestRazorProjectFileSystem(new[] { item1, item2, item3, item4, itemComponentParam, indexItem, itemDirectory1, itemDirectory2 }); var projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder => { builder.AddDirective(NamespaceDirective.Directive); builder.AddTagHelpers(tagHelperDescriptors); }); var component1 = CreateRazorDocumentSnapshot(projectEngine, item1, "First.Components", tagHelperDescriptors); var component2 = CreateRazorDocumentSnapshot(projectEngine, item2, "Test", tagHelperDescriptors); var component3 = CreateRazorDocumentSnapshot(projectEngine, item3, "Second.Components", tagHelperDescriptors); var component4 = CreateRazorDocumentSnapshot(projectEngine, item4, "Second.Components", tagHelperDescriptors); var componentWithParam = CreateRazorDocumentSnapshot(projectEngine, itemComponentParam, "Second.Components", tagHelperDescriptors); var component1337 = CreateRazorDocumentSnapshot(projectEngine, item1337, "Test", tagHelperDescriptors); var index = CreateRazorDocumentSnapshot(projectEngine, indexItem, "First.Components", tagHelperDescriptors); var directory1Component = CreateRazorDocumentSnapshot(projectEngine, itemDirectory1, "Test.Components", tagHelperDescriptors); var directory2Component = CreateRazorDocumentSnapshot(projectEngine, itemDirectory2, "Test.Components", tagHelperDescriptors); var firstProject = Mock.Of <ProjectSnapshot>(p => p.FilePath == "c:/First/First.csproj" && p.DocumentFilePaths == new[] { "c:/First/Component1.razor", "c:/First/Component2.razor", itemDirectory1.FilePath, itemDirectory2.FilePath, component1337.FilePath } && p.GetDocument("c:/First/Component1.razor") == component1 && p.GetDocument("c:/First/Component2.razor") == component2 && p.GetDocument(itemDirectory1.FilePath) == directory1Component && p.GetDocument(itemDirectory2.FilePath) == directory2Component && p.GetDocument(component1337.FilePath) == component1337, MockBehavior.Strict); var secondProject = Mock.Of <ProjectSnapshot>(p => p.FilePath == "c:/Second/Second.csproj" && p.DocumentFilePaths == new[] { "c:/Second/Component3.razor", "c:/Second/Component4.razor", index.FilePath } && p.GetDocument("c:/Second/Component3.razor") == component3 && p.GetDocument("c:/Second/Component4.razor") == component4 && p.GetDocument("c:/Second/ComponentWithParam.razor") == componentWithParam && p.GetDocument(index.FilePath) == index, MockBehavior.Strict); var projectSnapshotManager = Mock.Of <ProjectSnapshotManagerBase>(p => p.Projects == new[] { firstProject, secondProject }, MockBehavior.Strict); var projectSnapshotManagerAccessor = new TestProjectSnapshotManagerAccessor(projectSnapshotManager); var documentResolver = Mock.Of <DocumentResolver>(d => d.TryResolveDocument("c:/First/Component1.razor", out component1) == true && d.TryResolveDocument("c:/First/Component2.razor", out component2) == true && d.TryResolveDocument("c:/Second/Component3.razor", out component3) == true && d.TryResolveDocument("c:/Second/Component4.razor", out component4) == true && d.TryResolveDocument("c:/Second/ComponentWithParam.razor", out componentWithParam) == true && d.TryResolveDocument(index.FilePath, out index) == true && d.TryResolveDocument(component1337.FilePath, out component1337) == true && d.TryResolveDocument(itemDirectory1.FilePath, out directory1Component) == true && d.TryResolveDocument(itemDirectory2.FilePath, out directory2Component) == true, MockBehavior.Strict); var searchEngine = new DefaultRazorComponentSearchEngine(Dispatcher, projectSnapshotManagerAccessor); languageServerFeatureOptions = languageServerFeatureOptions ?? Mock.Of <LanguageServerFeatureOptions>(options => options.SupportsFileManipulation == true, MockBehavior.Strict); var endpoint = new RazorComponentRenameEndpoint(Dispatcher, documentResolver, searchEngine, projectSnapshotManagerAccessor, languageServerFeatureOptions); return(endpoint); }
private void CreateEndpoint() { var tag1 = CreateRazorComponentTagHelperDescriptor("First", "First.Components", "Component1"); var tag2 = CreateRazorComponentTagHelperDescriptor("First", "Test", "Component2"); var tag3 = CreateRazorComponentTagHelperDescriptor("Second", "Second.Components", "Component3"); var tag4 = CreateRazorComponentTagHelperDescriptor("Second", "Second.Components", "Component4"); var directory1 = CreateRazorComponentTagHelperDescriptor("First", "Test.Components", "Directory1"); var directory2 = CreateRazorComponentTagHelperDescriptor("First", "Test.Components", "Directory2"); var tagHelperDescriptors = new[] { tag1, tag2, tag3, tag4, directory1, directory2 }; var item1 = CreateProjectItem("@namespace First.Components\n@using Test\n<Component2></Component2>", "c:/First/Component1.razor"); var item2 = CreateProjectItem("@namespace Test", "c:/First/Component2.razor"); var item3 = CreateProjectItem("@namespace Second.Components\n<Component3></Component3>", "c:/Second/Component3.razor"); var item4 = CreateProjectItem("@namespace Second.Components\n<Component3></Component3>\n<Component3></Component3>", "c:/Second/Component4.razor"); var itemDirectory1 = CreateProjectItem("@namespace Test.Components\n<Directory2></Directory2>", "c:/Dir1/Directory1.razor"); var itemDirectory2 = CreateProjectItem("@namespace Test.Components\n<Directory1></Directory1>", "c:/Dir2/Directory2.razor"); var fileSystem = new TestRazorProjectFileSystem(new[] { item1, item2, item3, item4, itemDirectory1, itemDirectory2 }); _projectEngine = RazorProjectEngine.Create(RazorConfiguration.Default, fileSystem, builder => { builder.AddDirective(NamespaceDirective.Directive); builder.AddTagHelpers(tagHelperDescriptors); }); var component1 = CreateRazorDocumentSnapshot(item1, "First.Components"); var component2 = CreateRazorDocumentSnapshot(item2, "Test"); var component3 = CreateRazorDocumentSnapshot(item3, "Second.Components"); var component4 = CreateRazorDocumentSnapshot(item4, "Second.Components"); var directory1Component = CreateRazorDocumentSnapshot(itemDirectory1, "Test.Components"); var directory2Component = CreateRazorDocumentSnapshot(itemDirectory2, "Test.Components"); var firstProject = Mock.Of <ProjectSnapshot>(p => p.FilePath == "c:/First/First.csproj" && p.DocumentFilePaths == new[] { "c:/First/Component1.razor", "c:/First/Component2.razor", itemDirectory1.FilePath, itemDirectory2.FilePath } && p.GetDocument("c:/First/Component1.razor") == component1 && p.GetDocument("c:/First/Component2.razor") == component2 && p.GetDocument(itemDirectory1.FilePath) == directory1Component && p.GetDocument(itemDirectory2.FilePath) == directory2Component); var secondProject = Mock.Of <ProjectSnapshot>(p => p.FilePath == "c:/Second/Second.csproj" && p.DocumentFilePaths == new[] { "c:/Second/Component3.razor", "c:/Second/Component4.razor" } && p.GetDocument("c:/Second/Component3.razor") == component3 && p.GetDocument("c:/Second/Component4.razor") == component4); var projectSnapshotManager = Mock.Of <ProjectSnapshotManagerBase>(p => p.Projects == new[] { firstProject, secondProject }); var projectSnapshotManagerAccessor = new TestProjectSnapshotManagerAccessor(projectSnapshotManager); var documentResolver = Mock.Of <DocumentResolver>(d => d.TryResolveDocument("c:/First/Component1.razor", out component1) == true && d.TryResolveDocument("c:/First/Component2.razor", out component2) == true && d.TryResolveDocument("c:/Second/Component3.razor", out component3) == true && d.TryResolveDocument("c:/Second/Component4.razor", out component4) == true && d.TryResolveDocument(itemDirectory1.FilePath, out directory1Component) == true && d.TryResolveDocument(itemDirectory2.FilePath, out directory2Component) == true); var searchEngine = new DefaultRazorComponentSearchEngine(Dispatcher, projectSnapshotManagerAccessor); _endpoint = new RazorComponentRenameEndpoint(Dispatcher, documentResolver, searchEngine, projectSnapshotManagerAccessor); }
public void OnProvidersExecuting_AddsPagesUnderAreas() { // Arrange var fileProvider = new TestFileProvider(); var file1 = fileProvider.AddFile("Categories.cshtml", "@page"); var file2 = fileProvider.AddFile("Index.cshtml", "@page"); var file3 = fileProvider.AddFile("List.cshtml", "@page \"{sortOrder?}\""); var file4 = fileProvider.AddFile("_ViewStart.cshtml", "@page"); var manageDir = fileProvider.AddDirectoryContent("/Areas/Products/Pages/Manage", new[] { file1 }); var pagesDir = fileProvider.AddDirectoryContent("/Areas/Products/Pages", new IFileInfo[] { manageDir, file2, file3, file4 }); var productsDir = fileProvider.AddDirectoryContent("/Areas/Products", new[] { pagesDir }); var areasDir = fileProvider.AddDirectoryContent("/Areas", new[] { productsDir }); var rootDir = fileProvider.AddDirectoryContent("/", new[] { areasDir }); var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment); var optionsManager = Options.Create(new RazorPagesOptions { AllowAreas = true }); var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, model => { Assert.Equal("/Areas/Products/Pages/Manage/Categories.cshtml", model.RelativePath); Assert.Equal("/Manage/Categories", model.ViewEnginePath); Assert.Collection(model.Selectors, selector => Assert.Equal("Products/Manage/Categories", selector.AttributeRouteModel.Template)); Assert.Collection(model.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Manage/Categories", kvp.Value); }); }, model => { Assert.Equal("/Areas/Products/Pages/Index.cshtml", model.RelativePath); Assert.Equal("/Index", model.ViewEnginePath); Assert.Collection(model.Selectors, selector => Assert.Equal("Products/Index", selector.AttributeRouteModel.Template), selector => Assert.Equal("Products", selector.AttributeRouteModel.Template)); Assert.Collection(model.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Index", kvp.Value); }); }, model => { Assert.Equal("/Areas/Products/Pages/List.cshtml", model.RelativePath); Assert.Equal("/List", model.ViewEnginePath); Assert.Collection(model.Selectors, selector => Assert.Equal("Products/List/{sortOrder?}", selector.AttributeRouteModel.Template)); Assert.Collection(model.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/List", kvp.Value); }); }); }
public void OnProvidersExecuting_DoesNotAddAreaAndNonAreaRoutesForAPage() { // Arrange var fileProvider = new TestFileProvider(); var conformingFileUnderAreasDirectory = fileProvider.AddFile("Categories.cshtml", "@page"); // We shouldn't add a route for this. var nonConformingFileUnderAreasDirectory = fileProvider.AddFile("Home.cshtml", "@page"); var rootFile = fileProvider.AddFile("About.cshtml", "@page"); var productsDir = fileProvider.AddDirectoryContent("/Areas/Products", new[] { conformingFileUnderAreasDirectory }); var areasDir = fileProvider.AddDirectoryContent("/Areas", new IFileInfo[] { productsDir, nonConformingFileUnderAreasDirectory }); var rootDir = fileProvider.AddDirectoryContent("/", new IFileInfo[] { areasDir, rootFile }); var fileSystem = new TestRazorProjectFileSystem(fileProvider, _hostingEnvironment); var optionsManager = Options.Create(new RazorPagesOptions { RootDirectory = "/", AreaRootDirectory = "/Areas", AllowAreas = true, }); var provider = new RazorProjectPageRouteModelProvider(fileSystem, optionsManager, NullLoggerFactory.Instance); var context = new PageRouteModelProviderContext(); // Act provider.OnProvidersExecuting(context); // Assert Assert.Collection(context.RouteModels, model => { Assert.Equal("/Areas/Products/Categories.cshtml", model.RelativePath); Assert.Equal("/Categories", model.ViewEnginePath); Assert.Collection(model.Selectors, selector => Assert.Equal("Products/Categories", selector.AttributeRouteModel.Template)); Assert.Collection(model.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("area", kvp.Key); Assert.Equal("Products", kvp.Value); }, kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/Categories", kvp.Value); }); }, model => { Assert.Equal("/About.cshtml", model.RelativePath); Assert.Equal("/About", model.ViewEnginePath); Assert.Collection(model.Selectors, selector => Assert.Equal("About", selector.AttributeRouteModel.Template)); Assert.Collection(model.RouteValues.OrderBy(k => k.Key), kvp => { Assert.Equal("page", kvp.Key); Assert.Equal("/About", kvp.Value); }); }); }