public void TryComputeNamespaceAndClass_PrefersOptionsFromCodeDocument_ComputesNamespaceAndClass() { // Arrange var sourceDocument = TestRazorSourceDocument.Create(filePath: "C:\\Hello\\Components\\Test.cshtml", relativePath: "\\Components\\Test.cshtml"); var codeDocument = TestRazorCodeDocument.Create(sourceDocument, Array.Empty <RazorSourceDocument>()); codeDocument.SetCodeGenerationOptions(RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "World"; })); var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hello"; }) }; codeDocument.SetDocumentIntermediateNode(documentNode); // Act codeDocument.TryComputeNamespaceAndClass(out var @namespace, out var @class); // Assert Assert.Equal("World.Components", @namespace); Assert.Equal("Test", @class); }
public void TryComputeNamespace_RespectsNamespaceDirective() { // Arrange var sourceDocument = TestRazorSourceDocument.Create( content: "@namespace My.Custom.NS", filePath: "C:\\Hello\\Components\\Test.cshtml", relativePath: "\\Components\\Test.cshtml"); var codeDocument = TestRazorCodeDocument.Create(sourceDocument, Array.Empty <RazorSourceDocument>()); codeDocument.SetFileKind(FileKinds.Component); codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(sourceDocument, RazorParserOptions.Create(options => { options.Directives.Add(NamespaceDirective.Directive); }))); var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hello.World"; }) }; codeDocument.SetDocumentIntermediateNode(documentNode); // Act codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var @namespace); // Assert Assert.Equal("My.Custom.NS", @namespace); }
public void TryComputeNamespace_ComputesNamespace() { // Arrange var sourceDocument = TestRazorSourceDocument.Create(filePath: "C:\\Hello\\Components\\Test.cshtml", relativePath: "\\Components\\Test.cshtml"); var codeDocument = TestRazorCodeDocument.Create(sourceDocument, Array.Empty <RazorSourceDocument>()); codeDocument.SetCodeGenerationOptions(RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hello"; })); // Act codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var @namespace); // Assert Assert.Equal("Hello.Components", @namespace); }
public void TryComputeNamespace_SanitizesNamespaceName() { // Arrange var sourceDocument = TestRazorSourceDocument.Create(filePath: "C:\\Hello\\Components with space\\Test$name.cshtml", relativePath: "\\Components with space\\Test$name.cshtml"); var codeDocument = TestRazorCodeDocument.Create(sourceDocument, Array.Empty <RazorSourceDocument>()); var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hel?o.World"; }) }; codeDocument.SetDocumentIntermediateNode(documentNode); // Act codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var @namespace); // Assert Assert.Equal("Hel_o.World.Components_with_space", @namespace); }
public void TryComputeNamespace_NoRootNamespaceFallback_ReturnsNull() { // Arrange var sourceDocument = TestRazorSourceDocument.Create(filePath: "C:\\Hello\\Components\\Test.cshtml", relativePath: "\\Components\\Test.cshtml"); var codeDocument = TestRazorCodeDocument.Create(sourceDocument, Array.Empty <RazorSourceDocument>()); var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hello"; }) }; codeDocument.SetDocumentIntermediateNode(documentNode); // Act codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var @namespace); // Assert Assert.Null(@namespace); }
public RazorCodeGenerationOptions GetOptions() { return(_designTime ? RazorCodeGenerationOptions.CreateDesignTime(ConfigureOptions) : RazorCodeGenerationOptions.Create(ConfigureOptions)); }