Пример #1
0
        public void SectionDirectiveAutoCompleteAtEOF()
        {
            // Arrange
            var chunkGenerator = new DirectiveChunkGenerator(SectionDirective.Directive);

            chunkGenerator.Diagnostics.Add(
                RazorDiagnostic.Create(
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
                        new SourceLocation(16, 0, 16),
                        length: 1)));

            // Act & Assert
            ParseBlockTest(
                "@section Header {",
                new[] { SectionDirective.Directive },
                new DirectiveBlock(chunkGenerator,
                                   Factory.CodeTransition(),
                                   Factory.MetaCode("section").Accepts(AcceptedCharactersInternal.None),
                                   Factory.Span(SpanKindInternal.Code, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.WhiteSpace),
                                   Factory.Span(SpanKindInternal.Code, "Header", CSharpSymbolType.Identifier)
                                   .AsDirectiveToken(SectionDirective.Directive.Tokens.First()),
                                   Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace),
                                   Factory.MetaCode("{").AutoCompleteWith("}", atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None),
                                   new MarkupBlock(
                                       Factory.EmptyHtml())));
        }
Пример #2
0
        public void InheritsBlockOutputsErrorIfInheritsNotFollowedByTypeButAcceptsEntireLineAsCode()
        {
            // Arrange
            var chunkGenerator = new DirectiveChunkGenerator(InheritsDirective.Directive);

            chunkGenerator.Diagnostics.Add(
                RazorDiagnostic.Create(
                    new RazorError(
                        LegacyResources.FormatDirectiveExpectsTypeName(InheritsDirective.Directive.Directive),
                        25, 0, 25, Environment.NewLine.Length)));

            // Act & Assert
            ParseDocumentTest(
                "@inherits                " + Environment.NewLine + "foo",
                new[] { InheritsDirective.Directive },
                new MarkupBlock(
                    Factory.EmptyHtml(),
                    new DirectiveBlock(chunkGenerator,
                                       Factory.CodeTransition(),
                                       Factory.MetaCode("inherits").Accepts(AcceptedCharactersInternal.None),
                                       Factory.Span(SpanKindInternal.Code, "                ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.WhiteSpace),
                                       Factory.Span(SpanKindInternal.Code, string.Empty, CSharpSymbolType.Unknown)
                                       .AsDirectiveToken(DirectiveTokenDescriptor.CreateToken(DirectiveTokenKind.Type))),
                    Factory.Markup(Environment.NewLine + "foo")));
        }
Пример #3
0
 public void VerbatimBlockAutoCompleteAtStartOfFile()
 {
     ParseBlockTest(
         "@{" + Environment.NewLine + "<p></p>",
         new StatementBlock(
             Factory.CodeTransition(),
             Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
             Factory.Code(Environment.NewLine)
             .AsStatement()
             .With(new AutoCompleteEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString)
     {
         AutoCompleteString = "}"
     }),
             new MarkupBlock(
                 new MarkupTagBlock(
                     Factory.Markup("<p>").Accepts(AcceptedCharactersInternal.None)),
                 new MarkupTagBlock(
                     Factory.Markup("</p>").Accepts(AcceptedCharactersInternal.None))),
             Factory.Span(SpanKindInternal.Code, new CSharpSymbol(string.Empty, CSharpSymbolType.Unknown))
             .With(new StatementChunkGenerator())
             ),
         new RazorError(
             LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
                 LegacyResources.BlockName_Code, "}", "{"),
             new SourceLocation(1, 0, 1),
             length: 1));
 }
Пример #4
0
 public void UnterminatedRazorCommentInVerbatimBlock()
 {
     ParseDocumentTest("@{@*",
                       new MarkupBlock(
                           Factory.EmptyHtml(),
                           new StatementBlock(
                               Factory.CodeTransition(),
                               Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                               Factory.EmptyCSharp()
                               .AsStatement()
                               .AutoCompleteWith("}"),
                               new CommentBlock(
                                   Factory.CodeTransition(CSharpSymbolType.RazorCommentTransition)
                                   .Accepts(AcceptedCharactersInternal.None),
                                   Factory.MetaCode("*", CSharpSymbolType.RazorCommentStar)
                                   .Accepts(AcceptedCharactersInternal.None),
                                   Factory.Span(SpanKindInternal.Comment, new CSharpSymbol(string.Empty, CSharpSymbolType.Unknown))
                                   .Accepts(AcceptedCharactersInternal.Any)))),
                       new RazorError(
                           LegacyResources.ParseError_RazorComment_Not_Terminated,
                           new SourceLocation(2, 0, 2),
                           length: 2),
                       new RazorError(
                           LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
                               LegacyResources.BlockName_Code, "}", "{"),
                           new SourceLocation(1, 0, 1),
                           length: 1));
 }
Пример #5
0
 public void UnterminatedRazorCommentInImplicitExpressionMethodCall()
 {
     ParseDocumentTest("@foo(@*",
                       new MarkupBlock(
                           Factory.EmptyHtml(),
                           new ExpressionBlock(
                               Factory.CodeTransition(),
                               Factory.Code("foo(")
                               .AsImplicitExpression(CSharpCodeParser.DefaultKeywords),
                               new CommentBlock(
                                   Factory.CodeTransition(CSharpSymbolType.RazorCommentTransition)
                                   .Accepts(AcceptedCharactersInternal.None),
                                   Factory.MetaCode("*", CSharpSymbolType.RazorCommentStar)
                                   .Accepts(AcceptedCharactersInternal.None),
                                   Factory.Span(SpanKindInternal.Comment, new CSharpSymbol(
                                                    string.Empty,
                                                    CSharpSymbolType.Unknown))
                                   .Accepts(AcceptedCharactersInternal.Any)))),
                       new RazorError(
                           LegacyResources.ParseError_RazorComment_Not_Terminated,
                           new SourceLocation(5, 0, 5),
                           length: 2),
                       new RazorError(
                           LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
                           new SourceLocation(4, 0, 4),
                           length: 1));
 }
Пример #6
0
        public void ParseBlockRequiresControlFlowStatementsToHaveBraces()
        {
            var expectedMessage = LegacyResources.FormatParseError_SingleLine_ControlFlowStatements_Not_Allowed("{", "<");

            ParseBlockTest("if(foo) <p>Bar</p> else if(bar) <p>Baz</p> else <p>Boz</p>",
                           new StatementBlock(
                               Factory.Code("if(foo) ").AsStatement(),
                               new MarkupBlock(
                                   BlockFactory.MarkupTagBlock("<p>", AcceptedCharactersInternal.None),
                                   Factory.Markup("Bar"),
                                   BlockFactory.MarkupTagBlock("</p>", AcceptedCharactersInternal.None),
                                   Factory.Markup(" ").Accepts(AcceptedCharactersInternal.None)),
                               Factory.Code("else if(bar) ").AsStatement(),
                               new MarkupBlock(
                                   BlockFactory.MarkupTagBlock("<p>", AcceptedCharactersInternal.None),
                                   Factory.Markup("Baz"),
                                   BlockFactory.MarkupTagBlock("</p>", AcceptedCharactersInternal.None),
                                   Factory.Markup(" ").Accepts(AcceptedCharactersInternal.None)),
                               Factory.Code("else ").AsStatement(),
                               new MarkupBlock(
                                   BlockFactory.MarkupTagBlock("<p>", AcceptedCharactersInternal.None),
                                   Factory.Markup("Boz"),
                                   BlockFactory.MarkupTagBlock("</p>", AcceptedCharactersInternal.None)),
                               Factory.EmptyCSharp().AsStatement()
                               ),
                           new RazorError(expectedMessage, 8, 0, 8, 1),
                           new RazorError(expectedMessage, 32, 0, 32, 1),
                           new RazorError(expectedMessage, 48, 0, 48, 1));
        }
Пример #7
0
 public void ParseBlockStopsBalancingParenthesesAtEOF()
 {
     ImplicitExpressionTest(
         "foo(()", "foo(()",
         acceptedCharacters: AcceptedCharactersInternal.Any,
         errors: new RazorError(
             LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
             new SourceLocation(4, 0, 4),
             length: 1));
 }
Пример #8
0
 public void ParseBlockStopsParsingMidEmptyTagIfEOFReached()
 {
     ParseBlockTest("<br/",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup("<br/"))),
                    new RazorError(
                        LegacyResources.FormatParseError_UnfinishedTag("br"),
                        new SourceLocation(1, 0, 1),
                        length: 2));
 }
Пример #9
0
 public void ParseBlockTerminatesAtEOF()
 {
     ParseBlockTest("<foo>",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup("<foo>").Accepts(AcceptedCharactersInternal.None))),
                    new RazorError(
                        LegacyResources.FormatParseError_MissingEndTag("foo"),
                        new SourceLocation(1, 0, 1),
                        length: 3));
 }
Пример #10
0
 public void ParseBlockReportsErrorIfFinallyBlockUnterminatedAtEOF()
 {
     ParseBlockTest("try { baz(); } finally { var foo = bar; if(foo != null) { bar(); } ",
                    new StatementBlock(
                        Factory.Code("try { baz(); } finally { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF("finally", '}', '{'),
                        new SourceLocation(15, 0, 15),
                        length: 1));
 }
Пример #11
0
 public void ParseBlockReportsErrorIfDoBlockUnterminatedAtEOF()
 {
     ParseBlockTest("do { var foo = bar; if(foo != null) { bar(); } ",
                    new StatementBlock(
                        Factory.Code("do { var foo = bar; if(foo != null) { bar(); } ").AsStatement()
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF("do", '}', '{'),
                        SourceLocation.Zero,
                        length: 1));
 }
Пример #12
0
        public void Execute_CombinesErrorsOnRewritingErrors()
        {
            // Arrange
            var engine = RazorEngine.Create(builder =>
            {
                builder.AddTagHelpers(new[]
                {
                    CreateTagHelperDescriptor(
                        tagName: "form",
                        typeName: "TestFormTagHelper",
                        assemblyName: "TestAssembly"),
                    CreateTagHelperDescriptor(
                        tagName: "input",
                        typeName: "TestInputTagHelper",
                        assemblyName: "TestAssembly"),
                });
            });

            var phase = new DefaultRazorTagHelperBinderPhase()
            {
                Engine = engine,
            };

            var content =
                @"
@addTagHelper *, TestAssembly
<form>
    <input value='Hello' type='text' />";
            var sourceDocument = TestRazorSourceDocument.Create(content, fileName: null);
            var codeDocument   = RazorCodeDocument.Create(sourceDocument);

            var originalTree = RazorSyntaxTree.Parse(sourceDocument);

            var initialError           = RazorDiagnostic.Create(new RazorError("Initial test error", SourceLocation.Zero, length: 1));
            var expectedRewritingError = RazorDiagnostic.Create(
                new RazorError(
                    LegacyResources.FormatTagHelpersParseTreeRewriter_FoundMalformedTagHelper("form"),
                    new SourceLocation(Environment.NewLine.Length * 2 + 30, 2, 1),
                    length: 4));

            var erroredOriginalTree = RazorSyntaxTree.Create(originalTree.Root, originalTree.Source, new[] { initialError }, originalTree.Options);

            codeDocument.SetSyntaxTree(erroredOriginalTree);

            // Act
            phase.Execute(codeDocument);

            // Assert
            var outputTree = codeDocument.GetSyntaxTree();

            Assert.Empty(originalTree.Diagnostics);
            Assert.NotSame(erroredOriginalTree, outputTree);
            Assert.Equal(new[] { initialError, expectedRewritingError }, outputTree.Diagnostics);
        }
 public void ReservedWords(string word)
 {
     ParseBlockTest(word,
                    new DirectiveBlock(
                        Factory.MetaCode(word).Accepts(AcceptedCharactersInternal.None)
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_ReservedWord(word),
                        SourceLocation.Zero,
                        word.Length));
 }
Пример #14
0
 public void ParseBlockIncludesUnexpectedCharacterInSingleStatementControlFlowStatementError()
 {
     ParseBlockTest("if(foo)) { var bar = foo; }",
                    new StatementBlock(
                        Factory.Code("if(foo)) { var bar = foo; }").AsStatement()
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_SingleLine_ControlFlowStatements_Not_Allowed("{", ")"),
                        new SourceLocation(7, 0, 7),
                        length: 1));
 }
Пример #15
0
 private void RunUnterminatedSimpleKeywordBlock(string keyword)
 {
     SingleSpanBlockTest(
         keyword + " (foo) { var foo = bar; if(foo != null) { bar(); } ",
         BlockKindInternal.Statement,
         SpanKindInternal.Code,
         new RazorError(
             LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(keyword, '}', '{'),
             SourceLocation.Zero,
             length: 1));
 }
Пример #16
0
 public void ParseBlockWithUnclosedTagAtEOFThrowsMissingEndTagException()
 {
     ParseBlockTest("<foo>blah blah blah blah blah",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup("<foo>").Accepts(AcceptedCharactersInternal.None)),
                        Factory.Markup("blah blah blah blah blah")),
                    new RazorError(
                        LegacyResources.FormatParseError_MissingEndTag("foo"),
                        new SourceLocation(1, 0, 1),
                        length: 3));
 }
Пример #17
0
 public void ParseBlockTerminatesNormalStringAtEndOfFile()
 {
     SingleSpanBlockTest("if(foo) { var foo = \"blah blah blah blah blah", BlockKindInternal.Statement, SpanKindInternal.Code,
                         new RazorError(
                             LegacyResources.ParseError_Unterminated_String_Literal,
                             new SourceLocation(20, 0, 20),
                             length: 1),
                         new RazorError(
                             LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF("if", '}', '{'),
                             SourceLocation.Zero,
                             length: 1));
 }
Пример #18
0
 public void ParseBlockStartingWithEndTagProducesRazorErrorThenOutputsMarkupSegmentAndEndsBlock()
 {
     ParseBlockTest("</foo> bar baz",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup("</foo>").Accepts(AcceptedCharactersInternal.None)),
                        Factory.Markup(" ").Accepts(AcceptedCharactersInternal.None)),
                    new RazorError(
                        LegacyResources.FormatParseError_UnexpectedEndTag("foo"),
                        new SourceLocation(2, 0, 2),
                        length: 3));
 }
Пример #19
0
 public void EmptyTagNestsLikeNormalTag()
 {
     ParseBlockTest("<p></> Bar",
                    new MarkupBlock(
                        BlockFactory.MarkupTagBlock("<p>", AcceptedCharactersInternal.None),
                        BlockFactory.MarkupTagBlock("</>", AcceptedCharactersInternal.None),
                        Factory.Markup(" ").Accepts(AcceptedCharactersInternal.None)),
                    new RazorError(
                        LegacyResources.FormatParseError_MissingEndTag("p"),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #20
0
 public void ParseBlockTerminatesUsingBlockAtEOLWhenRecoveringFromMissingCloseParen()
 {
     ParseBlockTest("using(foo bar" + Environment.NewLine
                    + "baz",
                    new StatementBlock(
                        Factory.Code("using(foo bar" + Environment.NewLine).AsStatement()
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
                        new SourceLocation(5, 0, 5),
                        length: 1));
 }
Пример #21
0
 public void ParseBlockShouldReportErrorAndTerminateAtEOFIfIfParenInExplicitExpressionUnclosed()
 {
     ParseBlockTest("(foo bar" + Environment.NewLine
                    + "baz",
                    new ExpressionBlock(
                        Factory.MetaCode("(").Accepts(AcceptedCharactersInternal.None),
                        Factory.Code($"foo bar{Environment.NewLine}baz").AsExpression()
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_ExplicitExpression, ')', '('),
                        SourceLocation.Zero,
                        length: 1));
 }
Пример #22
0
 public void ParseBlockWithHelperDirectiveProducesError()
 {
     ParseBlockTest("@helper fooHelper { }",
                    new ExpressionBlock(
                        Factory.CodeTransition(),
                        Factory.Code("helper")
                        .AsImplicitExpression(KeywordSet)
                        .Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
                    new RazorError(
                        LegacyResources.FormatParseError_HelperDirectiveNotAvailable(SyntaxConstants.CSharp.HelperKeyword),
                        new SourceLocation(1, 0, 1),
                        length: 6));
 }
Пример #23
0
 public void TagWithoutCloseAngleDoesNotTerminateBlock()
 {
     ParseBlockTest("<                      " + Environment.NewLine
                    + "   ",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup($"<                      {Environment.NewLine}   "))),
                    designTime: true,
                    expectedErrors: new RazorError(
                        LegacyResources.FormatParseError_UnfinishedTag(string.Empty),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #24
0
 public void ParseBlockIgnoresSingleSlashAtStart()
 {
     ParseBlockTest("@/ foo",
                    new ExpressionBlock(
                        Factory.CodeTransition(),
                        Factory.EmptyCSharp()
                        .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
                        .Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
                    new RazorError(
                        LegacyResources.FormatParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS("/"),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #25
0
 public void ParseBlockMethodParsesNothingIfFirstCharacterIsNotIdentifierStartOrParenOrBrace()
 {
     ParseBlockTest("@!!!",
                    new ExpressionBlock(
                        Factory.CodeTransition(),
                        Factory.EmptyCSharp()
                        .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
                        .Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
                    new RazorError(
                        LegacyResources.FormatParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS("!"),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #26
0
 public void ParseBlockOutputsZeroLengthCodeSpanIfInvalidCharacterFollowsTransition()
 {
     ParseBlockTest("@/",
                    new ExpressionBlock(
                        Factory.CodeTransition(),
                        Factory.EmptyCSharp()
                        .AsImplicitExpression(KeywordSet)
                        .Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
                    new RazorError(
                        LegacyResources.FormatParseError_Unexpected_Character_At_Start_Of_CodeBlock_CS("/"),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #27
0
 public void ParseBlockCorrectlyHandlesInCorrectTransitionsIfImplicitExpressionParensUnclosed()
 {
     ParseBlockTest("Href(" + Environment.NewLine
                    + "<h1>@Html.Foo(Bar);</h1>" + Environment.NewLine,
                    new ExpressionBlock(
                        Factory.Code("Href(" + Environment.NewLine)
                        .AsImplicitExpression(CSharpCodeParser.DefaultKeywords)
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_CloseBracket_Before_EOF("(", ")"),
                        new SourceLocation(4, 0, 4),
                        length: 1));
 }
Пример #28
0
 public void ParseBlockTreatsMalformedTagsAsContent()
 {
     ParseBlockTest("<foo></!-- bar --></foo>",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup("<foo>").Accepts(AcceptedCharactersInternal.None)),
                        new MarkupTagBlock(
                            Factory.Markup("</!-- bar -->").Accepts(AcceptedCharactersInternal.None))),
                    new RazorError(
                        LegacyResources.FormatParseError_MissingEndTag("foo"),
                        new SourceLocation(1, 0, 1),
                        length: 3));
 }
Пример #29
0
 public void ParseBlockWithUnfinishedTagAtEOFThrowsIncompleteTagException()
 {
     ParseBlockTest("<foo bar=baz",
                    new MarkupBlock(
                        new MarkupTagBlock(
                            Factory.Markup("<foo"),
                            new MarkupBlock(new AttributeBlockChunkGenerator("bar", new LocationTagged <string>(" bar=", 4, 0, 4), new LocationTagged <string>(string.Empty, 12, 0, 12)),
                                            Factory.Markup(" bar=").With(SpanChunkGenerator.Null),
                                            Factory.Markup("baz").With(new LiteralAttributeChunkGenerator(new LocationTagged <string>(string.Empty, 9, 0, 9), new LocationTagged <string>("baz", 9, 0, 9)))))),
                    new RazorError(
                        LegacyResources.FormatParseError_UnfinishedTag("foo"),
                        new SourceLocation(1, 0, 1),
                        length: 3));
 }
Пример #30
0
 public void ParseBlockReportsErrorIfExplicitCodeBlockUnterminatedAtEOF()
 {
     ParseBlockTest("{ var foo = bar; if(foo != null) { bar(); } ",
                    new StatementBlock(
                        Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                        Factory.Code(" var foo = bar; if(foo != null) { bar(); } ")
                        .AsStatement()
                        .AutoCompleteWith("}")),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
                            LegacyResources.BlockName_Code, '}', '{'),
                        SourceLocation.Zero,
                        length: 1));
 }