Exemplo n.º 1
0
        private ParserContext SetupTestContext(string document)
        {
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();

            return(SetupTestContext(document, b => { }, codeParser, markupParser, codeParser));
        }
Exemplo n.º 2
0
        private ParserContext SetupTestRun(string document, Action <TextReader> positioningAction)
        {
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();

            return(SetupTestRun(document, positioningAction, codeParser, markupParser, codeParser, new Mock <ParserVisitor>().Object));
        }
Exemplo n.º 3
0
        private ParserContext SetupTestContext(string document, Action <TextReader> positioningAction)
        {
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();

            return(SetupTestContext(document, positioningAction, codeParser, markupParser, codeParser));
        }
        internal virtual RazorSyntaxTree ParseDocument(RazorLanguageVersion version, string document, IEnumerable <DirectiveDescriptor> directives, bool designTime = false, RazorParserFeatureFlags featureFlags = null, string fileKind = null)
        {
            directives ??= Array.Empty <DirectiveDescriptor>();

            var source = TestRazorSourceDocument.Create(document, filePath: null, relativePath: null, normalizeNewLines: true);

            var options = CreateParserOptions(version, directives, designTime, featureFlags, fileKind);
            var context = new ParserContext(source, options);

            var codeParser   = new CSharpCodeParser(directives, context);
            var markupParser = new HtmlMarkupParser(context);

            codeParser.HtmlParser   = markupParser;
            markupParser.CodeParser = codeParser;

            var root = markupParser.ParseDocument().CreateRed();

            var diagnostics = context.ErrorSink.Errors;

            var codeDocument = RazorCodeDocument.Create(source);

            var syntaxTree = RazorSyntaxTree.Create(root, source, diagnostics, options);

            codeDocument.SetSyntaxTree(syntaxTree);

            var defaultDirectivePass = new DefaultDirectiveSyntaxTreePass();

            syntaxTree = defaultDirectivePass.Execute(codeDocument, syntaxTree);

            return(syntaxTree);
        }
Exemplo n.º 5
0
        private ParserContext SetupTestRun(string document, ParserVisitor listener)
        {
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();

            return(SetupTestRun(document, b => { }, codeParser, markupParser, codeParser, listener));
        }
Exemplo n.º 6
0
        internal virtual RazorSyntaxTree ParseDocument(string document, IEnumerable <DirectiveDescriptor> directives, bool designTime = false)
        {
            directives = directives ?? Array.Empty <DirectiveDescriptor>();

            var source = TestRazorSourceDocument.Create(document, fileName: null);

            var options = CreateParserOptions(directives, designTime);
            var context = new ParserContext(source, options);

            var codeParser   = new CSharpCodeParser(directives, context);
            var markupParser = new HtmlMarkupParser(context);

            codeParser.HtmlParser   = markupParser;
            markupParser.CodeParser = codeParser;

            markupParser.ParseDocument();

            var root        = context.Builder.Build();
            var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));

            var codeDocument = RazorCodeDocument.Create(source);

            var syntaxTree = RazorSyntaxTree.Create(root, source, diagnostics, options);

            codeDocument.SetSyntaxTree(syntaxTree);

            var defaultDirectivePass = new DefaultDirectiveSyntaxTreePass();

            syntaxTree = defaultDirectivePass.Execute(codeDocument, syntaxTree);

            return(syntaxTree);
        }
Exemplo n.º 7
0
        internal virtual RazorSyntaxTree ParseCodeBlock(
            RazorLanguageVersion version,
            string document,
            IEnumerable <DirectiveDescriptor> directives,
            bool designTime)
        {
            directives = directives ?? Array.Empty <DirectiveDescriptor>();

            var source = TestRazorSourceDocument.Create(document, filePath: null, relativePath: null, normalizeNewLines: true);

            var options = CreateParserOptions(version, directives, designTime);
            var context = new ParserContext(source, options);

            var codeParser   = new CSharpCodeParser(directives, context);
            var markupParser = new HtmlMarkupParser(context);

            codeParser.HtmlParser   = markupParser;
            markupParser.CodeParser = codeParser;

            var root = codeParser.ParseBlock().CreateRed();

            var diagnostics = context.ErrorSink.Errors;

            var syntaxTree = RazorSyntaxTree.Create(root, source, diagnostics, options);

            return(syntaxTree);
        }
Exemplo n.º 8
0
        internal virtual RazorSyntaxTree ParseCodeBlock(
            string document,
            IEnumerable <DirectiveDescriptor> directives,
            bool designTime)
        {
            directives = directives ?? Array.Empty <DirectiveDescriptor>();

            var source  = TestRazorSourceDocument.Create(document, fileName: null);
            var options = CreateParserOptions(directives, designTime);
            var context = new ParserContext(source, options);

            var parser = new CSharpCodeParser(directives, context);

            parser.HtmlParser = new HtmlMarkupParser(context)
            {
                CodeParser = parser,
            };

            parser.ParseBlock();

            var root        = context.Builder.Build();
            var diagnostics = context.ErrorSink.Errors?.Select(error => RazorDiagnostic.Create(error));

            return(RazorSyntaxTree.Create(root, source, diagnostics, options));
        }
Exemplo n.º 9
0
        public void ParseAddOrRemoveDirective_CalculatesAssemblyLocationInLookupText(string text, int assemblyLocation)
        {
            // Arrange
            var source  = TestRazorSourceDocument.Create();
            var options = RazorParserOptions.CreateDefault();
            var context = new ParserContext(source, options);

            var parser = new CSharpCodeParser(context);

            var directive = new CSharpCodeParser.ParsedDirective()
            {
                DirectiveText = text,
            };

            var diagnostics = new List <RazorDiagnostic>();
            var expected    = new SourceLocation(assemblyLocation, 0, assemblyLocation);

            // Act
            var result = parser.ParseAddOrRemoveDirective(directive, SourceLocation.Zero, diagnostics);

            // Assert
            Assert.Empty(diagnostics);
            Assert.Equal("foo", result.TypePattern);
            Assert.Equal("assemblyName", result.AssemblyName);
        }
Exemplo n.º 10
0
        public void ParseAddOrRemoveDirective_CreatesErrorIfInvalidLookupText_DoesNotThrow(string directiveText, int errorLength)
        {
            // Arrange
            var source  = TestRazorSourceDocument.Create();
            var options = RazorParserOptions.CreateDefault();
            var context = new ParserContext(source, options);

            var parser = new CSharpCodeParser(context);

            var directive = new CSharpCodeParser.ParsedDirective()
            {
                DirectiveText = directiveText
            };

            var diagnostics   = new List <RazorDiagnostic>();
            var expectedError = RazorDiagnosticFactory.CreateParsing_InvalidTagHelperLookupText(
                new SourceSpan(new SourceLocation(1, 2, 3), errorLength), directiveText);

            // Act
            var result = parser.ParseAddOrRemoveDirective(directive, new SourceLocation(1, 2, 3), diagnostics);

            // Assert
            Assert.Same(directive, result);

            var error = Assert.Single(diagnostics);

            Assert.Equal(expectedError, error);
        }
        public void AddingKeywordToRazorKeywordsListBeforeAccessingTopLevelKeywordsAddsItToTopLevelKeywords()
        {
            CSharpCodeParser parser = new CSharpCodeParser();

            parser.RazorKeywords.Add(TestExtraKeyword, _ => { Assert.Fail("Should never be called!"); return(true); });
            Assert.IsTrue(parser.TopLevelKeywords.Contains(TestExtraKeyword));
        }
Exemplo n.º 12
0
        public void ConstructorAcceptsActiveParserIfIsSameAsEitherCodeOrMarkupParser()
        {
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();

            new ParserContext(new BufferingTextReader(TextReader.Null), codeParser, markupParser, codeParser, new Mock <ParserVisitor>().Object);
            new ParserContext(new BufferingTextReader(TextReader.Null), codeParser, markupParser, markupParser, new Mock <ParserVisitor>().Object);
        }
Exemplo n.º 13
0
        public void ConstructorAcceptsActiveParserIfIsSameAsEitherCodeOrMarkupParser()
        {
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();

            new ParserContext(new SeekableTextReader(TextReader.Null), codeParser, markupParser, codeParser);
            new ParserContext(new SeekableTextReader(TextReader.Null), codeParser, markupParser, markupParser);
        }
Exemplo n.º 14
0
        public void ParseBlockMethodThrowsArgNullExceptionOnNullContext()
        {
            // Arrange
            CSharpCodeParser parser = new CSharpCodeParser();

            // Act and Assert
            Assert.Throws <InvalidOperationException>(() => parser.ParseBlock(), RazorResources.Parser_Context_Not_Set);
        }
Exemplo n.º 15
0
        public TextLine(string text)
        {
            // Set the text
            this.Text = text;

            // Parse the words
            this.Words = CSharpCodeParser.ParseWords(text);
        }
        public override ParserBase CreateCodeParser()
        {
            CSharpCodeParser parser = new CSharpCodeParser();

            parser.RazorKeywords.Add(TestExtraKeyword, _ => { Assert.Fail("Should never be called!"); return(true); });
            KeywordSet = parser.TopLevelKeywords;
            return(parser);
        }
Exemplo n.º 17
0
        public void ConstructorRequiresNonNullSource()
        {
            var codeParser = new CSharpCodeParser();

            Assert.ThrowsArgumentNull(
                () => new ParserContext(null, codeParser, new HtmlMarkupParser(), codeParser),
                "source"
                );
        }
Exemplo n.º 18
0
        public static ParseResult Parse(Input input)
        {
            var document = CreateSourceDocument(input.Content);
            var options  = RazorParserOptions.Create(builder => {
                foreach (var directive in GetDirectives())
                {
                    builder.Directives.Add(directive);
                }

                builder.SetDesignTime(input.DesignTime);
            });

            // Syntax tree
            var context      = new ParserContext(document, options);
            var codeParser   = new CSharpCodeParser(GetDirectives(), context);
            var markupParser = new HtmlMarkupParser(context);

            codeParser.HtmlParser   = markupParser;
            markupParser.CodeParser = codeParser;
            var root = markupParser.ParseDocument().CreateRed();

            // IR tree
            RazorCodeDocument codeDocument;
            var engine = RazorProjectEngine.Create(configure: null);

            if (input.DesignTime)
            {
                codeDocument = engine.ProcessDesignTime(document, null, null, TestTagHelpers.GetDescriptors());
            }
            else
            {
                codeDocument = engine.Process(document, null, null, TestTagHelpers.GetDescriptors());
            }
            if (input.TagHelperPhase)
            {
                root = codeDocument.GetSyntaxTree().Root;
            }

            var syntaxTreeRoot = TreeSerializer.Serialize(root);

            var intermediateNode = codeDocument.GetDocumentIntermediateNode();
            var intermediateRoot = IRSerializer.Serialize(intermediateNode);

            // Generated code
            var cSharpDocument = codeDocument.GetCSharpDocument();
            var generatedCode  = new GeneratedCodeResult {
                Code = cSharpDocument.GeneratedCode
            };

            return(new ParseResult
            {
                SyntaxTreeRoot = syntaxTreeRoot,
                IntermediateRoot = intermediateRoot,
                GeneratedCode = generatedCode
            });
        }
Exemplo n.º 19
0
        public void ParseBlockMethodThrowsArgNullExceptionOnNullContext()
        {
            // Arrange
            var parser = new CSharpCodeParser();

            // Act and Assert
            var exception = Assert.Throws <InvalidOperationException>(() => parser.ParseBlock());

            Assert.Equal(RazorResources.Parser_Context_Not_Set, exception.Message);
        }
Exemplo n.º 20
0
        public void DecorateCodeParserDoesNotModifyIncomingParser()
        {
            // Arrange
            var expected = new CSharpCodeParser();

            // Act
            var actual = CreateHost().DecorateCodeParser(expected);

            // Assert
            Assert.Same(expected, actual);
        }
Exemplo n.º 21
0
        public TextLine(string text, int lineNumber)
        {
            // Set the text
            this.Text = text;

            // Set the LineNumber
            this.LineNumber = lineNumber;

            // Parse the words
            this.Words = CSharpCodeParser.ParseWords(text);
        }
Exemplo n.º 22
0
        public void DecorateCodeParser_ReplacesCSharpCodeParserWithMvcSpecificOne()
        {
            // Arrange
            MvcWebPageRazorHost host = new MvcWebPageRazorHost("foo.cshtml", "bar");
            var parser = new CSharpCodeParser();

            // Act
            var result = host.DecorateCodeParser(parser);

            // Assert
            Assert.IsType <MvcCSharpRazorCodeParser>(result);
        }
Exemplo n.º 23
0
        public void MapDirectives_HandlesDuplicates()
        {
            // Arrange
            var source  = TestRazorSourceDocument.Create();
            var options = RazorParserOptions.CreateDefault();
            var context = new ParserContext(source, options);
            var parser  = new CSharpCodeParser(context);

            // Act & Assert (Does not throw)
            parser.MapDirectives((b, t) => { }, "test");
            parser.MapDirectives((b, t) => { }, "test");
        }
Exemplo n.º 24
0
        public void SwitchActiveParserSetsCodeParserAsActiveIfMarkupParserCurrentlyActive()
        {
            // Arrange
            var           codeParser   = new CSharpCodeParser();
            var           markupParser = new HtmlMarkupParser();
            ParserContext context      = SetupTestRun("barbazbiz", b => b.Read(), codeParser, markupParser, markupParser, new Mock <ParserVisitor>().Object);

            Assert.AreSame(markupParser, context.ActiveParser);

            // Act
            context.SwitchActiveParser();

            // Assert
            Assert.AreSame(codeParser, context.ActiveParser);
        }
Exemplo n.º 25
0
        public void SwitchActiveParserSetsCodeParserAsActiveIfMarkupParserCurrentlyActive()
        {
            // Arrange
            var codeParser   = new CSharpCodeParser();
            var markupParser = new HtmlMarkupParser();
            var context      = SetupTestContext("barbazbiz", b => b.Read(), codeParser, markupParser, markupParser);

            Assert.Same(markupParser, context.ActiveParser);

            // Act
            context.SwitchActiveParser();

            // Assert
            Assert.Same(codeParser, context.ActiveParser);
        }
Exemplo n.º 26
0
        public void ConstructorRequiresNonNullMarkupParser()
        {
            var codeParser = new CSharpCodeParser();

            Assert.ThrowsArgumentNull(
                () =>
                new ParserContext(
                    new SeekableTextReader(TextReader.Null),
                    codeParser,
                    null,
                    codeParser
                    ),
                "markupParser"
                );
        }
Exemplo n.º 27
0
        /// <summary>
        /// This method gets the Generic Event Description.
        /// </summary>
        /// <param name="eventName"></param>
        /// <returns></returns>
        private static string GetGenericEventDescritpion(string eventName)
        {
            // initial value
            string description = "";

            // Create  string builder
            StringBuilder sb = new StringBuilder("");

            // locals
            bool   isVerb    = false;
            string firstWord = "";
            string lastWord  = "";

            // Get the words
            List <Word> words = CSharpCodeParser.ParseWordsByCapitalLetters(eventName);

            // if there are one or more words
            if ((words != null) && (words.Count > 0))
            {
                // we have to test if the first word is a verb
                isVerb    = false;
                firstWord = words[0].Text;
                lastWord  = words[words.Count - 1].Text;

                // we have to determine if the first word is a verb
                isVerb = IsVerb(firstWord);

                // if the first word is a verb
                if (isVerb)
                {
                    // remove the first word
                    words.RemoveAt(0);
                }

                // now iterate the words
                foreach (Word word in words)
                {
                    // append the text of this word
                    sb.Append(" " + word.Text);
                }

                // Trim
                description = sb.ToString().Trim();
            }

            // return value
            return(description);
        }
Exemplo n.º 28
0
        public void ConstructorInitializesProperties()
        {
            // Arrange
            var expectedBuffer       = new SeekableTextReader(TextReader.Null);
            var expectedCodeParser   = new CSharpCodeParser();
            var expectedMarkupParser = new HtmlMarkupParser();

            // Act
            var context = new ParserContext(expectedBuffer, expectedCodeParser, expectedMarkupParser, expectedCodeParser);

            // Assert
            Assert.NotNull(context.Source);
            Assert.Same(expectedCodeParser, context.CodeParser);
            Assert.Same(expectedMarkupParser, context.MarkupParser);
            Assert.Same(expectedCodeParser, context.ActiveParser);
        }
Exemplo n.º 29
0
        public void ConstructorInitializesProperties()
        {
            // Arrange
            LookaheadTextReader expectedBuffer       = new BufferingTextReader(TextReader.Null);
            CSharpCodeParser    expectedCodeParser   = new CSharpCodeParser();
            HtmlMarkupParser    expectedMarkupParser = new HtmlMarkupParser();
            ParserVisitor       expectedListener     = new Mock <ParserVisitor>().Object;

            // Act
            ParserContext context = new ParserContext(expectedBuffer, expectedCodeParser, expectedMarkupParser, expectedCodeParser, expectedListener);

            // Assert
            Assert.IsInstanceOfType(context.Source, typeof(BufferingTextReader));
            Assert.AreSame(expectedBuffer, context.Source);
            Assert.AreSame(expectedCodeParser, context.CodeParser);
            Assert.AreSame(expectedMarkupParser, context.MarkupParser);
            Assert.AreSame(expectedCodeParser, context.ActiveParser);
            Assert.AreSame(expectedListener, context.Visitor);
        }
Exemplo n.º 30
0
        private static void RunParseWithListenerTest(Action <RazorParser, TextReader> parserAction)
        {
            // Arrange
            var markupParser   = new MockMarkupParser();
            var codeParser     = new CSharpCodeParser();
            var parser         = new RazorParser(codeParser, markupParser, tagHelperDescriptorResolver: null);
            var expectedReader = new StringReader("foo");

            // Act
            parserAction(parser, expectedReader);

            // Assert
            var actualContext = markupParser.Context;

            Assert.NotNull(actualContext);
            Assert.Same(markupParser, actualContext.MarkupParser);
            Assert.Same(markupParser, actualContext.ActiveParser);
            Assert.Same(codeParser, actualContext.CodeParser);
        }
Exemplo n.º 31
0
 public void ClassSetup() {
     codeParser = new CSharpCodeParser();
     fileSetup = new SrcMLFileUnitSetup(Language.CSharp);
 }
 public void ClassSetup() {
     FileUnitSetup = new SrcMLFileUnitSetup(Language.CPlusPlus);
     CodeParser = new CSharpCodeParser();
 }