Пример #1
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));
 }
Пример #2
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())));
        }
Пример #3
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));
 }
Пример #4
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));
 }
Пример #5
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));
 }
Пример #6
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));
 }
Пример #7
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));
 }
Пример #8
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));
 }
 public void ParseBlockShouldOutputZeroLengthCodeSpanIfEOFOccursAfterStartOfExplicitExpression()
 {
     ParseBlockTest("@(",
                    new ExpressionBlock(
                        Factory.CodeTransition(),
                        Factory.MetaCode("(").Accepts(AcceptedCharactersInternal.None),
                        Factory.EmptyCSharp().AsExpression()
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
                            LegacyResources.BlockName_ExplicitExpression, ")", "("),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #10
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));
 }
Пример #11
0
 public void RazorCommentInVerbatimBlock()
 {
     ParseDocumentTest("@{" + Environment.NewLine
                       + "    <text" + Environment.NewLine
                       + "    @**@" + Environment.NewLine
                       + "}",
                       new MarkupBlock(
                           Factory.EmptyHtml(),
                           new StatementBlock(
                               Factory.CodeTransition(),
                               Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                               Factory.Code($"{Environment.NewLine}    ")
                               .AsStatement()
                               .AutoCompleteWith("}"),
                               new MarkupBlock(
                                   new MarkupTagBlock(
                                       Factory.MarkupTransition("<text").Accepts(AcceptedCharactersInternal.Any)),
                                   Factory.Markup(Environment.NewLine).Accepts(AcceptedCharactersInternal.None),
                                   Factory.Markup("    ").With(SpanChunkGenerator.Null),
                                   new CommentBlock(
                                       Factory.MarkupTransition(HtmlSymbolType.RazorCommentTransition)
                                       .Accepts(AcceptedCharactersInternal.None),
                                       Factory.MetaMarkup("*", HtmlSymbolType.RazorCommentStar)
                                       .Accepts(AcceptedCharactersInternal.None),
                                       Factory.Span(SpanKindInternal.Comment, new HtmlSymbol(
                                                        string.Empty,
                                                        HtmlSymbolType.Unknown))
                                       .Accepts(AcceptedCharactersInternal.Any),
                                       Factory.MetaMarkup("*", HtmlSymbolType.RazorCommentStar)
                                       .Accepts(AcceptedCharactersInternal.None),
                                       Factory.MarkupTransition(HtmlSymbolType.RazorCommentTransition)
                                       .Accepts(AcceptedCharactersInternal.None)),
                                   Factory.Markup(Environment.NewLine).With(SpanChunkGenerator.Null),
                                   Factory.Markup("}")))),
                       new RazorError(
                           LegacyResources.ParseError_TextTagCannotContainAttributes,
                           new SourceLocation(7 + Environment.NewLine.Length, 1, 5),
                           length: 4),
                       new RazorError(
                           LegacyResources.FormatParseError_MissingEndTag("text"),
                           new SourceLocation(7 + Environment.NewLine.Length, 1, 5),
                           length: 4),
                       new RazorError(
                           LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"),
                           new SourceLocation(1, 0, 1),
                           length: 1));
 }
Пример #12
0
 public void ParseBlockMethodOutputsOpenCurlyAsCodeSpanIfEofFoundAfterOpenCurlyBrace()
 {
     ParseBlockTest("{",
                    new StatementBlock(
                        Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                        Factory.EmptyCSharp()
                        .AsStatement()
                        .With(new AutoCompleteEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString)
     {
         AutoCompleteString = "}"
     })
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"),
                        SourceLocation.Zero,
                        length: 1));
 }
Пример #13
0
 public void VerbatimBlockAutoCompleteAtEOF()
 {
     ParseBlockTest("@{",
                    new StatementBlock(
                        Factory.CodeTransition(),
                        Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                        Factory.EmptyCSharp()
                        .AsStatement()
                        .With(new AutoCompleteEditHandler(CSharpLanguageCharacteristics.Instance.TokenizeString)
     {
         AutoCompleteString = "}"
     })
                        ),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
                            LegacyResources.BlockName_Code, "}", "{"),
                        new SourceLocation(1, 0, 1),
                        length: 1));
 }
Пример #14
0
 public void ParseBlockHandlesOpenAngleAtEof()
 {
     ParseDocumentTest("@{" + Environment.NewLine
                       + "<",
                       new MarkupBlock(
                           Factory.EmptyHtml(),
                           new StatementBlock(
                               Factory.CodeTransition(),
                               Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                               Factory.Code(Environment.NewLine)
                               .AsStatement()
                               .AutoCompleteWith("}"),
                               new MarkupBlock(
                                   new MarkupTagBlock(
                                       Factory.Markup("<"))))),
                       new RazorError(
                           LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
                               LegacyResources.BlockName_Code, "}", "{"),
                           new SourceLocation(1, 0, 1),
                           length: 1));
 }
Пример #15
0
        public void CSharpDocument_DesignTime_PreservesParserErrors()
        {
            // Arrange
            var engine = RazorEngine.CreateDesignTime();

            var document = RazorCodeDocument.Create(TestRazorSourceDocument.Create("@{", fileName: "test.cshtml"));

            var expected = RazorDiagnostic.Create(new RazorError(
                                                      LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"),
                                                      new SourceLocation("test.cshtml", 1, 0, 1),
                                                      length: 1));

            // Act
            engine.Process(document);

            // Assert
            var csharpDocument = document.GetCSharpDocument();
            var error          = Assert.Single(csharpDocument.Diagnostics);

            Assert.Equal(expected, error);
        }
Пример #16
0
        public void FunctionsDirectiveAutoCompleteAtEOF()
        {
            // Arrange
            var chunkGenerator = new DirectiveChunkGenerator(FunctionsDirective.Directive);

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

            // Act & Assert
            ParseBlockTest(
                "@functions{",
                new[] { FunctionsDirective.Directive },
                new DirectiveBlock(chunkGenerator,
                                   Factory.CodeTransition(),
                                   Factory.MetaCode("functions").Accepts(AcceptedCharactersInternal.None),
                                   Factory.MetaCode("{").AutoCompleteWith("}", atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None)));
        }
Пример #17
0
        public void ParseBlockDoesNoErrorRecoveryForFunctionsBlock()
        {
            // Arrange
            var chunkGenerator = new DirectiveChunkGenerator(FunctionsDirective.Directive);

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

            // Act & Assert
            ParseBlockTest(
                "functions { { { { { } zoop",
                new[] { FunctionsDirective.Directive },
                new DirectiveBlock(chunkGenerator,
                                   Factory.MetaCode("functions").Accepts(AcceptedCharactersInternal.None),
                                   Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace),
                                   Factory.MetaCode("{").AutoCompleteWith("}", atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None),
                                   Factory.Code(" { { { { } zoop").AsStatement()));
        }
Пример #18
0
        public void ParseBlockReportsErrorIfClassBlockUnterminatedAtEOF()
        {
            // Arrange
            var chunkGenerator = new DirectiveChunkGenerator(FunctionsDirective.Directive);

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

            // Act & Assert
            ParseBlockTest(
                "functions { var foo = bar; if(foo != null) { bar(); } ",
                new[] { FunctionsDirective.Directive },
                new DirectiveBlock(chunkGenerator,
                                   Factory.MetaCode("functions").Accepts(AcceptedCharactersInternal.None),
                                   Factory.Span(SpanKindInternal.Markup, " ", CSharpSymbolType.WhiteSpace).Accepts(AcceptedCharactersInternal.AllWhiteSpace),
                                   Factory.MetaCode("{").AutoCompleteWith("}", atEndOfSpan: true).Accepts(AcceptedCharactersInternal.None),
                                   Factory.Code(" var foo = bar; if(foo != null) { bar(); } ").AsStatement()));
        }
Пример #19
0
 public void ParseBlockHandlesOpenAngleWithProperTagFollowingIt()
 {
     ParseDocumentTest("@{" + Environment.NewLine
                       + "<" + Environment.NewLine
                       + "</html>",
                       new MarkupBlock(
                           Factory.EmptyHtml(),
                           new StatementBlock(
                               Factory.CodeTransition(),
                               Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                               Factory.Code(Environment.NewLine)
                               .AsStatement()
                               .AutoCompleteWith("}"),
                               new MarkupBlock(
                                   new MarkupTagBlock(
                                       Factory.Markup("<" + Environment.NewLine))
                                   ),
                               new MarkupBlock(
                                   new MarkupTagBlock(
                                       Factory.Markup("</html>").Accepts(AcceptedCharactersInternal.None))
                                   ),
                               Factory.EmptyCSharp().AsStatement()
                               )
                           ),
                       designTime: true,
                       expectedErrors: new[]
     {
         new RazorError(
             LegacyResources.FormatParseError_UnexpectedEndTag("html"),
             new SourceLocation(5 + Environment.NewLine.Length * 2, 2, 2),
             length: 4),
         new RazorError(
             LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF("code", "}", "{"),
             new SourceLocation(1, 0, 1),
             length: 1)
     });
 }
Пример #20
0
 public void ParseBlockMethodProducesErrorIfEOFAfterTransitionInEmbeddedExpression()
 {
     ParseBlockTest("{" + Environment.NewLine
                    + "    @",
                    new StatementBlock(
                        Factory.MetaCode("{").Accepts(AcceptedCharactersInternal.None),
                        Factory.Code(Environment.NewLine + "    ")
                        .AsStatement()
                        .AutoCompleteWith("}"),
                        new ExpressionBlock(
                            Factory.CodeTransition(),
                            Factory.EmptyCSharp()
                            .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: true)
                            .Accepts(AcceptedCharactersInternal.NonWhiteSpace)),
                        Factory.EmptyCSharp().AsStatement()
                        ),
                    new RazorError(
                        LegacyResources.ParseError_Unexpected_EndOfFile_At_Start_Of_CodeBlock,
                        6 + Environment.NewLine.Length, 1, 5, length: 1),
                    new RazorError(
                        LegacyResources.FormatParseError_Expected_EndOfBlock_Before_EOF(LegacyResources.BlockName_Code, "}", "{"),
                        SourceLocation.Zero,
                        length: 1));
 }