public void Execute_AddsTheRestToMethod() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var builder = IntermediateNodeBuilder.Create(documentNode); builder.Add(new HtmlContentIntermediateNode()); builder.Add(new CSharpCodeIntermediateNode()); var pass = new TestDocumentClassifierPass(); pass.Engine = Engine; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert var @namespace = SingleChild <NamespaceDeclarationIntermediateNode>(documentNode); var @class = SingleChild <ClassDeclarationIntermediateNode>(@namespace); var method = SingleChild <MethodDeclarationIntermediateNode>(@class); Children( method, n => Assert.IsType <HtmlContentIntermediateNode>(n), n => Assert.IsType <CSharpCodeIntermediateNode>(n)); }
public void Execute_Match_AddsGlobalTargetExtensions() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var expected = new ICodeTargetExtension[] { new MyExtension1(), new MyExtension2(), }; var pass = new TestDocumentClassifierPass(); pass.Engine = RazorProjectEngine.CreateEmpty(b => { for (var i = 0; i < expected.Length; i++) { b.AddTargetExtension(expected[i]); } }).Engine; ICodeTargetExtension[] extensions = null; pass.CodeTargetCallback = (builder) => extensions = builder.TargetExtensions.ToArray(); // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert Assert.Equal(expected, extensions); }
public void Execute_AddsUsingsToNamespace() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var builder = IntermediateNodeBuilder.Create(documentNode); builder.Add(new UsingDirectiveIntermediateNode()); var pass = new TestDocumentClassifierPass(); pass.Engine = Engine; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert var @namespace = SingleChild <NamespaceDeclarationIntermediateNode>(documentNode); Children( @namespace, n => Assert.IsType <UsingDirectiveIntermediateNode>(n), n => Assert.IsType <ClassDeclarationIntermediateNode>(n)); }
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 Execute_CollatesIRDocumentDiagnosticsFromSourceDocument() { // Arrange var phase = new DefaultRazorCSharpLoweringPhase(); var engine = RazorProjectEngine.CreateEmpty(b => b.Phases.Add(phase)); var codeDocument = TestRazorCodeDocument.Create("<p class=@("); var options = RazorCodeGenerationOptions.CreateDefault(); var irDocument = new DocumentIntermediateNode() { DocumentKind = "test", Target = CodeTarget.CreateDefault(codeDocument, options), Options = options, }; var expectedDiagnostic = RazorDiagnostic.Create( new RazorDiagnosticDescriptor("1234", () => "I am an error.", RazorDiagnosticSeverity.Error), new SourceSpan("SomeFile.cshtml", 11, 0, 11, 1)); irDocument.Diagnostics.Add(expectedDiagnostic); codeDocument.SetDocumentIntermediateNode(irDocument); // Act phase.Execute(codeDocument); // Assert var csharpDocument = codeDocument.GetCSharpDocument(); var diagnostic = Assert.Single(csharpDocument.Diagnostics); Assert.Same(expectedDiagnostic, diagnostic); }
public static RazorCSharpDocument Create( string generatedCode, RazorCodeGenerationOptions options, IEnumerable <RazorDiagnostic> diagnostics, IEnumerable <SourceMapping> sourceMappings, IEnumerable <LinePragma> linePragmas) { if (generatedCode == null) { throw new ArgumentNullException(nameof(generatedCode)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } if (diagnostics == null) { throw new ArgumentNullException(nameof(diagnostics)); } if (sourceMappings == null) { throw new ArgumentNullException(nameof(sourceMappings)); } return(new DefaultRazorCSharpDocument(generatedCode, options, diagnostics.ToArray(), sourceMappings.ToArray(), linePragmas.ToArray())); }
public void Execute_Match_SetsDocumentType_AndCreatesStructure() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var pass = new TestDocumentClassifierPass(); pass.Engine = Engine; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert Assert.Equal("test", documentNode.DocumentKind); Assert.NotNull(documentNode.Target); var @namespace = SingleChild <NamespaceDeclarationIntermediateNode>(documentNode); var @class = SingleChild <ClassDeclarationIntermediateNode>(@namespace); var method = SingleChild <MethodDeclarationIntermediateNode>(@class); NoChildren(method); }
public void TryComputeNamespace_PrefersOptionsFromCodeDocument_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 = "World"; })); var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.Create(c => { c.RootNamespace = "Hello"; }) }; codeDocument.SetDocumentIntermediateNode(documentNode); // Act codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var @namespace); // Assert Assert.Equal("World.Components", @namespace); }
public static void SetCodeGenerationOptions(this RazorCodeDocument document, RazorCodeGenerationOptions codeGenerationOptions) { if (document == null) { throw new ArgumentNullException(nameof(document)); } document.Items[typeof(RazorCodeGenerationOptions)] = codeGenerationOptions; }
private CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options) { return(CodeTarget.CreateDefault(codeDocument, options, (builder) => { for (var i = 0; i < _targetExtensions.Length; i++) { builder.TargetExtensions.Add(_targetExtensions[i]); } ConfigureTarget(builder); })); }
// virtual to allow replacing the code target wholesale. protected virtual CodeTarget CreateTarget(RazorCodeDocument codeDocument, RazorCodeGenerationOptions options) { return(CodeTarget.CreateDefault(codeDocument, options, (builder) => { for (var i = 0; i < TargetExtensions.Count; i++) { builder.TargetExtensions.Add(TargetExtensions[i]); } ConfigureTarget(builder); })); }
public void SetCodeGenerationOptions_SetsSuccessfully() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCodeGenerationOptions.CreateDefault(); // Act codeDocument.SetCodeGenerationOptions(expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(RazorCodeGenerationOptions)]); }
public void SetCSharpDocument_SetsCSharpDocument() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCSharpDocument.Create("", RazorCodeGenerationOptions.CreateDefault(), Array.Empty <RazorDiagnostic>()); // Act codeDocument.SetCSharpDocument(expected); // Assert Assert.Same(expected, codeDocument.Items[typeof(RazorCSharpDocument)]); }
public static RazorHtmlDocument Create(string generatedHtml, RazorCodeGenerationOptions options) { if (generatedHtml == null) { throw new ArgumentNullException(nameof(generatedHtml)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } return(new DefaultRazorHtmlDocument(generatedHtml, options)); }
public void GetCodeGenerationOptions_ReturnsSuccessfully() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCodeGenerationOptions.CreateDefault(); codeDocument.Items[typeof(RazorCodeGenerationOptions)] = expected; // Act var actual = codeDocument.GetCodeGenerationOptions(); // Assert Assert.Same(expected, actual); }
public void GetCSharpDocument_ReturnsCSharpDocument() { // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); var expected = RazorCSharpDocument.Create("", RazorCodeGenerationOptions.CreateDefault(), Array.Empty <RazorDiagnostic>()); codeDocument.Items[typeof(RazorCSharpDocument)] = expected; // Act var actual = codeDocument.GetCSharpDocument(); // Assert Assert.Same(expected, actual); }
public DefaultRazorHtmlDocument( string generatedHtml, RazorCodeGenerationOptions options) { if (generatedHtml == null) { throw new ArgumentNullException(nameof(generatedHtml)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } _generatedHtml = generatedHtml; _options = options; }
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 static RazorCodeDocument Create( RazorSourceDocument source, IEnumerable <RazorSourceDocument> imports, RazorParserOptions parserOptions, RazorCodeGenerationOptions codeGenerationOptions) { if (source == null) { throw new ArgumentNullException(nameof(source)); } var codeDocument = new DefaultRazorCodeDocument(source, imports); codeDocument.SetParserOptions(parserOptions); codeDocument.SetCodeGenerationOptions(codeGenerationOptions); return(codeDocument); }
public void Execute_IgnoresDocumentsWithDocumentKind() { // Arrange var documentNode = new DocumentIntermediateNode() { DocumentKind = "ignore", Options = RazorCodeGenerationOptions.CreateDefault(), }; var pass = new DefaultDocumentClassifierPass(); pass.Engine = RazorProjectEngine.Create().Engine; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert Assert.Equal("ignore", documentNode.DocumentKind); NoChildren(documentNode); }
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 void Execute_NoMatch_IgnoresDocument() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var pass = new TestDocumentClassifierPass() { Engine = Engine, ShouldMatch = false, }; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert Assert.Null(documentNode.DocumentKind); NoChildren(documentNode); }
public DefaultRazorCSharpDocument( string generatedCode, RazorCodeGenerationOptions options, RazorDiagnostic[] diagnostics, SourceMapping[] sourceMappings) { if (generatedCode == null) { throw new ArgumentNullException(nameof(generatedCode)); } if (options == null) { throw new ArgumentNullException(nameof(options)); } _generatedCode = generatedCode; _options = options; _diagnostics = diagnostics ?? Array.Empty <RazorDiagnostic>(); _sourceMappings = sourceMappings ?? Array.Empty <SourceMapping>(); }
public void TryComputeNamespaceAndClass_UsesIROptions_ComputesNamespaceAndClass() { // 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.TryComputeNamespaceAndClass(out var @namespace, out var @class); // Assert Assert.Equal("Hello.Components", @namespace); Assert.Equal("Test", @class); }
public void Execute_CollatesSyntaxDiagnosticsFromImportDocuments() { // Arrange var phase = new DefaultRazorIntermediateNodeLoweringPhase(); var engine = RazorProjectEngine.CreateEmpty(b => { b.Phases.Add(phase); b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false)); }); var codeDocument = TestRazorCodeDocument.CreateEmpty(); codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source)); codeDocument.SetImportSyntaxTrees(new[] { RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("@ ")), RazorSyntaxTree.Parse(TestRazorSourceDocument.Create("<p @(")), }); var options = RazorCodeGenerationOptions.CreateDefault(); // Act phase.Execute(codeDocument); // Assert var documentNode = codeDocument.GetDocumentIntermediateNode(); Assert.Collection(documentNode.Diagnostics, diagnostic => { Assert.Equal(@"A space or line break was encountered after the ""@"" character. Only valid identifiers, keywords, comments, ""("" and ""{"" are valid at the start of a code block and they must occur immediately following ""@"" with no space in between.", diagnostic.GetMessage(CultureInfo.CurrentCulture)); }, diagnostic => { Assert.Equal(@"The explicit expression block is missing a closing "")"" character. Make sure you have a matching "")"" character for all the ""("" characters within this block, and that none of the "")"" characters are being interpreted as markup.", diagnostic.GetMessage(CultureInfo.CurrentCulture)); }); }
public void Execute_AddsPrimaryAnnotations() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var builder = IntermediateNodeBuilder.Create(documentNode); builder.Add(new HtmlContentIntermediateNode()); builder.Add(new CSharpCodeIntermediateNode()); var pass = new TestDocumentClassifierPass() { Engine = Engine, Namespace = "TestNamespace", Class = "TestClass", Method = "TestMethod", }; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert var @namespace = SingleChild <NamespaceDeclarationIntermediateNode>(documentNode); AnnotationEquals(@namespace, CommonAnnotations.PrimaryNamespace); var @class = SingleChild <ClassDeclarationIntermediateNode>(@namespace); AnnotationEquals(@class, CommonAnnotations.PrimaryClass); var method = SingleChild <MethodDeclarationIntermediateNode>(@class); AnnotationEquals(method, CommonAnnotations.PrimaryMethod); }
public void Execute_CanInitializeDefaults() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var builder = IntermediateNodeBuilder.Create(documentNode); builder.Add(new HtmlContentIntermediateNode()); builder.Add(new CSharpCodeIntermediateNode()); var pass = new TestDocumentClassifierPass() { Engine = Engine, Namespace = "TestNamespace", Class = "TestClass", Method = "TestMethod", }; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert var @namespace = SingleChild <NamespaceDeclarationIntermediateNode>(documentNode); Assert.Equal("TestNamespace", @namespace.Content); var @class = SingleChild <ClassDeclarationIntermediateNode>(@namespace); Assert.Equal("TestClass", @class.ClassName); var method = SingleChild <MethodDeclarationIntermediateNode>(@class); Assert.Equal("TestMethod", method.MethodName); }
public void Execute_CreatesClassStructure() { // Arrange var documentNode = new DocumentIntermediateNode() { Options = RazorCodeGenerationOptions.CreateDefault(), }; var pass = new DefaultDocumentClassifierPass(); pass.Engine = RazorProjectEngine.Create().Engine; // Act pass.Execute(TestRazorCodeDocument.CreateEmpty(), documentNode); // Assert Assert.Equal("default", documentNode.DocumentKind); var @namespace = SingleChild <NamespaceDeclarationIntermediateNode>(documentNode); var @class = SingleChild <ClassDeclarationIntermediateNode>(@namespace); var method = SingleChild <MethodDeclarationIntermediateNode>(@class); NoChildren(method); }
public RazorCodeGenerationOptions GetOptions() { return(_designTime ? RazorCodeGenerationOptions.CreateDesignTime(ConfigureOptions) : RazorCodeGenerationOptions.Create(ConfigureOptions)); }