public void AddDirective_ExistingFeature_UsesFeature() { // Arrange var expected = new DefaultRazorDirectiveFeature(); var engine = RazorEngine.CreateEmpty(b => { b.Features.Add(expected); // Act b.AddDirective(DirectiveDescriptor.CreateDirective("test", DirectiveKind.SingleLine)); }); // Assert var actual = Assert.Single(engine.Features.OfType <IRazorDirectiveFeature>()); Assert.Same(expected, actual); var directive = Assert.Single(actual.Directives); Assert.Equal("test", directive.Directive); }
public void Lower_WithImports_IgnoresBlockDirective() { // Arrange var source = TestRazorSourceDocument.Create("<p>Hi!</p>"); var imports = new[] { TestRazorSourceDocument.Create("@block token { }"), }; var codeDocument = TestRazorCodeDocument.Create(source, imports); // Act var documentNode = Lower(codeDocument, b => { b.AddDirective(DirectiveDescriptor.CreateDirective("block", DirectiveKind.RazorBlock, d => d.AddMemberToken())); }); // Assert Children( documentNode, n => Html("<p>Hi!</p>", n)); }
public void Execute_AutomaticallyOverridesImportedSingleLineSinglyOccurringDirective_MultipleImports() { // Arrange var directive = DirectiveDescriptor.CreateSingleLineDirective( "custom", builder => { builder.AddStringToken(); builder.Usage = DirectiveUsage.FileScopedSinglyOccurring; }); var phase = new DefaultRazorIntermediateNodeLoweringPhase(); var engine = RazorProjectEngine.CreateEmpty(b => { b.Phases.Add(phase); b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false)); b.AddDirective(directive); }); var options = RazorParserOptions.Create(builder => builder.Directives.Add(directive)); var importSource1 = TestRazorSourceDocument.Create("@custom \"hello\"", filePath: "import1.cshtml"); var importSource2 = TestRazorSourceDocument.Create("@custom \"world\"", filePath: "import2.cshtml"); var codeDocument = TestRazorCodeDocument.Create("<p>NonDirective</p>"); codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source, options)); codeDocument.SetImportSyntaxTrees(new[] { RazorSyntaxTree.Parse(importSource1, options), RazorSyntaxTree.Parse(importSource2, options) }); // Act phase.Execute(codeDocument); // Assert var documentNode = codeDocument.GetDocumentIntermediateNode(); var customDirectives = documentNode.FindDirectiveReferences(directive); var customDirective = (DirectiveIntermediateNode)Assert.Single(customDirectives).Node; var stringToken = Assert.Single(customDirective.Tokens); Assert.Equal("\"world\"", stringToken.Content); }
public void Execute_DoesNotImportNonFileScopedSinglyOccurringDirectives_Block() { // Arrange var codeBlockDirective = DirectiveDescriptor.CreateCodeBlockDirective("code", b => b.AddStringToken()); var razorBlockDirective = DirectiveDescriptor.CreateRazorBlockDirective("razor", b => b.AddStringToken()); var phase = new DefaultRazorIntermediateNodeLoweringPhase(); var engine = RazorProjectEngine.CreateEmpty(b => { b.Phases.Add(phase); b.Features.Add(new DefaultRazorCodeGenerationOptionsFeature(designTime: false)); b.AddDirective(codeBlockDirective); b.AddDirective(razorBlockDirective); }); var options = RazorParserOptions.Create(builder => { builder.Directives.Add(codeBlockDirective); builder.Directives.Add(razorBlockDirective); }); var importSource = TestRazorSourceDocument.Create( @"@code ""code block"" { } @razor ""razor block"" { }", filePath: "testImports.cshtml"); var codeDocument = TestRazorCodeDocument.Create("<p>NonDirective</p>"); codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source, options)); codeDocument.SetImportSyntaxTrees(new[] { RazorSyntaxTree.Parse(importSource, options) }); // Act phase.Execute(codeDocument); // Assert var documentNode = codeDocument.GetDocumentIntermediateNode(); var directives = documentNode.Children.OfType <DirectiveIntermediateNode>(); Assert.Empty(directives); }
/// <summary> /// Adds the specified <see cref="DirectiveDescriptor"/>. /// </summary> /// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param> /// <param name="directive">The <see cref="DirectiveDescriptor"/> to add.</param> /// <returns>The <see cref="RazorProjectEngineBuilder"/>.</returns> public static RazorProjectEngineBuilder AddDirective(this RazorProjectEngineBuilder builder, DirectiveDescriptor directive) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (directive == null) { throw new ArgumentNullException(nameof(directive)); } var directiveFeature = GetDirectiveFeature(builder); directiveFeature.Directives.Add(directive); return(builder); }
public void Configure(RazorParserOptionsBuilder options) { options.Directives.Add(DirectiveDescriptor.CreateDirective("test", DirectiveKind.SingleLine)); }
/// <summary> /// Adds the specified <see cref="DirectiveDescriptor"/> for the provided file kind. /// </summary> /// <param name="builder">The <see cref="RazorProjectEngineBuilder"/>.</param> /// <param name="directive">The <see cref="DirectiveDescriptor"/> to add.</param> /// <param name="fileKinds">The file kinds, for which to register the directive. See <see cref="FileKinds"/>.</param> /// <returns>The <see cref="RazorProjectEngineBuilder"/>.</returns> public static RazorProjectEngineBuilder AddDirective(this RazorProjectEngineBuilder builder, DirectiveDescriptor directive, params string[] fileKinds) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (directive == null) { throw new ArgumentNullException(nameof(directive)); } if (fileKinds == null) { throw new ArgumentNullException(nameof(fileKinds)); } var directiveFeature = GetDirectiveFeature(builder); foreach (var fileKind in fileKinds) { if (!directiveFeature.DirectivesByFileKind.TryGetValue(fileKind, out var directives)) { directives = new List <DirectiveDescriptor>(); directiveFeature.DirectivesByFileKind.Add(fileKind, directives); } directives.Add(directive); } return(builder); }