Example #1
0
        public void Execute_AutomaticallyOverridesImportedSingleLineSinglyOccurringDirective_MainDocument()
        {
            // 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 importSource = TestRazorSourceDocument.Create("@custom \"hello\"", filePath: "import.cshtml");
            var codeDocument = TestRazorCodeDocument.Create("@custom \"world\"");

            codeDocument.SetSyntaxTree(RazorSyntaxTree.Parse(codeDocument.Source, options));
            codeDocument.SetImportSyntaxTrees(new[] { RazorSyntaxTree.Parse(importSource, 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);
        }
Example #2
0
        public void Build_ValidatesDirectiveKeyword_InvalidCharacter()
        {
            // Arrange & Act
            var ex = Assert.Throws <InvalidOperationException>(() => DirectiveDescriptor.CreateSingleLineDirective("test_directive"));

            // Assert
            Assert.Equal("Invalid directive keyword 'test_directive'. Directives must have a non-empty keyword that consists only of letters.", ex.Message);
        }
Example #3
0
        public void CreateSingleLineDirective_CreatesSingleLineDirective()
        {
            // Arrange & Act
            var directive = DirectiveDescriptor.CreateSingleLineDirective("test");

            // Assert
            Assert.Equal("test", directive.Directive);
            Assert.Equal(DirectiveKind.SingleLine, directive.Kind);
        }
Example #4
0
        public void Build_ValidatesDirectiveName_NonOptionalTokenFollowsOptionalToken()
        {
            // Arrange & Act
            var ex = Assert.Throws <InvalidOperationException>(
                () => DirectiveDescriptor.CreateSingleLineDirective("test", b => { b.AddOptionalMemberToken(); b.AddMemberToken(); }));

            // Assert
            Assert.Equal("A non-optional directive token cannot follow an optional directive token.", ex.Message);
        }
Example #5
0
        public void CreateSingleLineDirective_WithConfigure_CreatesSingleLineDirective()
        {
            // Arrange
            var called = false;
            Action <IDirectiveDescriptorBuilder> configure = b => { called = true; };

            // Act
            var directive = DirectiveDescriptor.CreateSingleLineDirective("test", configure);

            // Assert
            Assert.Equal("test", directive.Directive);
            Assert.Equal(DirectiveKind.SingleLine, directive.Kind);
            Assert.True(called);
        }