public void CheckForStructureChangesStartsFullReparseIfChangeOverlapsMultipleSpans() {
            // Arrange
            RazorEditorParser parser = new RazorEditorParser(CreateHost(), TestLinePragmaFileName);
            ITextBuffer original = new StringTextBuffer("Foo @bar Baz");
            ITextBuffer changed = new StringTextBuffer("Foo @bap Daz");
            TextChange change = new TextChange(7, 3, original, 3, changed);

            ManualResetEventSlim parseComplete = new ManualResetEventSlim();
            int parseCount = 0;
            parser.DocumentParseComplete += (sender, args) => {
                Interlocked.Increment(ref parseCount);
                parseComplete.Set();
            };

            Assert.AreEqual(PartialParseResult.Rejected, parser.CheckForStructureChanges(new TextChange(0, 0, new StringTextBuffer(String.Empty), 12, original)));
            MiscUtils.DoWithTimeoutIfNotDebugging(parseComplete.Wait); // Wait for the parse to finish
            parseComplete.Reset();

            // Act
            PartialParseResult result = parser.CheckForStructureChanges(change);
            
            // Assert
            Assert.AreEqual(PartialParseResult.Rejected, result);
            MiscUtils.DoWithTimeoutIfNotDebugging(parseComplete.Wait);
            Assert.AreEqual(2, parseCount);
        }
 public void TreesAreDifferentReturnsFalseIfTreeStructureIsSame()
 {
     var factory = SpanFactory.CreateCsHtml();
     var original = new MarkupBlock(
         factory.Markup("<p>"),
         new ExpressionBlock(
             factory.CodeTransition(),
             factory.Code("f")
                    .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: false)),
         factory.Markup("</p>"));
     factory.Reset();
     var modified = new MarkupBlock(
         factory.Markup("<p>"),
         new ExpressionBlock(
             factory.CodeTransition(),
             factory.Code("foo")
                    .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: false)),
         factory.Markup("</p>"));
     original.LinkNodes();
     modified.LinkNodes();
     var oldBuffer = new StringTextBuffer("<p>@f</p>");
     var newBuffer = new StringTextBuffer("<p>@foo</p>");
     Assert.False(BackgroundParser.TreesAreDifferent(
         original, modified, new[] {
             new TextChange(position: 5, oldLength: 0, oldBuffer: oldBuffer, newLength: 2, newBuffer: newBuffer)
         }));
 }
        internal virtual void RunParseTest(string document, Func<ParserBase, Action> parserActionSelector, Block expectedRoot, IList<RazorError> expectedErrors, bool designTimeParser) {
            // Create the source
            using (StringTextBuffer reader = new StringTextBuffer(document)) {
                ParserResults results = null;
                try {
                    ParserBase codeParser = CreateCodeParser();
                    MarkupParser markupParser = CreateMarkupParser();
                    SyntaxTreeBuilderVisitor listener = new SyntaxTreeBuilderVisitor();
                    ParserContext context = CreateParserRun(new TextBufferReader(reader), codeParser, markupParser, listener);
                    context.DesignTimeMode = designTimeParser;

                    codeParser.Context = context;
                    markupParser.Context = context;

                    // Run the parser
                    parserActionSelector(context.ActiveParser)();
                    context.OnComplete();

                    // Collect the results
                    results = listener.Results;
                    EvaluateResults(TestContext, results, expectedRoot, expectedErrors);
                }
                finally {
                    if (TestContext != null && results != null && results.Document != null) {
                        TestContext.WriteLine(String.Empty);
                        TestContext.WriteLine("Actual Parse Tree:");
                        WriteNode(0, TestContext, results.Document);
                    }
                }
            }
        }
        public void TreesAreDifferentReturnsFalseForAddedContent(string content)
        {
            // Arrange
            var factory = SpanFactory.CreateCsHtml();
            var blockFactory = new BlockFactory(factory);
            var original = new MarkupBlock(
                blockFactory.MarkupTagBlock("<p>"),
                blockFactory.TagHelperBlock(
                    tagName: "div",
                    tagMode: TagMode.StartTagAndEndTag,
                    start: new SourceLocation(3, 0, 3),
                    startTag: blockFactory.MarkupTagBlock("<div>"),
                    children: new SyntaxTreeNode[]
                    {
                        factory.Markup($"{Environment.NewLine}{Environment.NewLine}")
                    },
                    endTag: blockFactory.MarkupTagBlock("</div>")),
                blockFactory.MarkupTagBlock("</p>"));

            factory.Reset();

            var modified = new MarkupBlock(
                blockFactory.MarkupTagBlock("<p>"),
                blockFactory.TagHelperBlock(
                    tagName: "div",
                    tagMode: TagMode.StartTagAndEndTag,
                    start: new SourceLocation(3, 0, 3),
                    startTag: blockFactory.MarkupTagBlock("<div>"),
                    children: new SyntaxTreeNode[]
                    {
                        factory.Markup($"{Environment.NewLine}{content}{Environment.NewLine}")
                    },
                    endTag: blockFactory.MarkupTagBlock("</div>")),
                blockFactory.MarkupTagBlock("</p>"));
            original.LinkNodes();
            modified.LinkNodes();

            var oldBuffer = new StringTextBuffer($"<p><div>{Environment.NewLine}{Environment.NewLine}</div></p>");
            var newBuffer = new StringTextBuffer(
                $"<p><div>{Environment.NewLine}{content}{Environment.NewLine}</div></p>");

            // Act
            var treesAreDifferent = BackgroundParser.TreesAreDifferent(
                original,
                modified,
                new[]
                {
                    new TextChange(
                        position: 8 + Environment.NewLine.Length,
                        oldLength: 0,
                        oldBuffer: oldBuffer,
                        newLength: content.Length,
                        newBuffer: newBuffer)
                });

            // Assert
            Assert.False(treesAreDifferent);
        }
        public void SpanWithAcceptTrailingDotOffProvisionallyAcceptsEndReplacementWithTrailingDot() {
            // Arrange
            var span = new ImplicitExpressionSpan("abcd.", CSharpCodeParser.DefaultKeywords, acceptTrailingDot: false, acceptedCharacters: AcceptedCharacters.None);
            var newBuffer = new StringTextBuffer("abcdef.");
            var oldBuffer = new StringTextBuffer("abcd.");
            var textChange = new TextChange(0, 5, oldBuffer, 7, newBuffer);

            // Act
            PartialParseResult result = span.ApplyChange(textChange);            

            // Assert
            Assert.AreEqual(PartialParseResult.Accepted | PartialParseResult.Provisional, result);
        }
        public void SpanWithAcceptTrailingDotOnAcceptsIntelliSenseReplaceWhichActuallyInsertsDot() {
            // Arrange
            var span = new ImplicitExpressionSpan("abcd", CSharpCodeParser.DefaultKeywords, acceptTrailingDot: true, acceptedCharacters: AcceptedCharacters.None);
            var newBuffer = new StringTextBuffer("abcd.");
            var oldBuffer = new StringTextBuffer("abcd");
            var textChange = new TextChange(0, 4, oldBuffer, 5, newBuffer);

            // Act
            PartialParseResult result = span.ApplyChange(textChange);

            // Assert
            Assert.AreEqual(PartialParseResult.Accepted, result);
        }
Beispiel #7
0
 public void TreesAreDifferentReturnsTrueIfTreeStructureIsDifferent()
 {
     var factory = SpanFactory.CreateCsHtml();
     Block original = new MarkupBlock(
         factory.Markup("<p>"),
         new ExpressionBlock(
             factory.CodeTransition()),
         factory.Markup("</p>"));
     Block modified = new MarkupBlock(
         factory.Markup("<p>"),
         new ExpressionBlock(
             factory.CodeTransition("@"),
             factory.Code("f")
                    .AsImplicitExpression(CSharpCodeParser.DefaultKeywords, acceptTrailingDot: false)),
         factory.Markup("</p>"));
     ITextBuffer oldBuffer = new StringTextBuffer("<p>@</p>");
     ITextBuffer newBuffer = new StringTextBuffer("<p>@f</p>");
     Assert.True(BackgroundParser.TreesAreDifferent(
         original, modified, new[] {
             new TextChange(position: 4, oldLength: 0, oldBuffer: oldBuffer, newLength: 1, newBuffer: newBuffer)
         }));
 }
        public void CheckForStructureChangesStartsReparseAndFiresDocumentParseCompletedEventIfNoAdditionalChangesQueued() {
            // Arrange
            using (RazorEditorParser parser = CreateClientParser()) {
                StringTextBuffer input = new StringTextBuffer(SimpleCSHTMLDocument.ReadAllText());

                DocumentParseCompleteEventArgs capturedArgs = null;
                ManualResetEventSlim parseComplete = new ManualResetEventSlim(false);

                parser.DocumentParseComplete += (sender, args) => {
                    capturedArgs = args;
                    parseComplete.Set();
                };

                // Act
                parser.CheckForStructureChanges(new TextChange(0, 0, new StringTextBuffer(String.Empty), input.Length, input));

                // Assert
                MiscUtils.DoWithTimeoutIfNotDebugging(parseComplete.Wait);
                Assert.AreEqual(SimpleCSHTMLDocumentGenerated.ReadAllText(),
                                MiscUtils.StripRuntimeVersion(
                                    capturedArgs.GeneratorResults.GeneratedCode.GenerateCode<CSharpCodeProvider>()
                                ));
            }
        }
Beispiel #9
0
        public void NormalizeDoesntAffectShrinkingReplacements()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("D");
            var oldBuffer = new StringTextBuffer("DateTime");
            var original = new TextChange(0, 8, oldBuffer, 1, newBuffer);

            // Act
            TextChange normalized = original.Normalize();

            // Assert
            Assert.Equal(original, normalized);
        }
Beispiel #10
0
        public void NormalizeDoesntAffectChangesWithoutCommonPrefixes()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("DateTime.");
            var oldBuffer = new StringTextBuffer("Date.");
            var original = new TextChange(0, 5, oldBuffer, 9, newBuffer);

            // Act
            TextChange normalized = original.Normalize();

            // Assert
            Assert.Equal(original, normalized);
        }
Beispiel #11
0
        public void NormalizeFixesUpIntelliSenseStyleReplacements()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("Date.");
            var oldBuffer = new StringTextBuffer("Date");
            var original = new TextChange(0, 4, oldBuffer, 5, newBuffer);

            // Act
            TextChange normalized = original.Normalize();

            // Assert
            Assert.Equal(new TextChange(4, 0, oldBuffer, 1, newBuffer), normalized);
        }
Beispiel #12
0
        public void ApplyChangeWithReplacedTextReturnsNewContentWithChangeApplied()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("abcdefg");
            var oldBuffer = new StringTextBuffer("");
            var textChange = new TextChange(1, 1, oldBuffer, 2, newBuffer);

            // Act
            string text = textChange.ApplyChange("abcdefg", 1);

            // Assert
            Assert.Equal("bcbcdefg", text);
        }
Beispiel #13
0
        public void NewTextWithReplaceReturnsChangedTextFromBuffer()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("test");
            var oldBuffer = new StringTextBuffer("tebb");
            var textChange = new TextChange(2, 2, oldBuffer, 1, newBuffer);

            // Act
            string newText = textChange.NewText;
            string oldText = textChange.OldText;

            // Assert
            Assert.Equal("s", newText);
            Assert.Equal("bb", oldText);
        }
Beispiel #14
0
        public void NewTextWithDeleteReturnsEmptyString()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("test");
            var oldBuffer = new StringTextBuffer("");
            var textChange = new TextChange(1, 1, oldBuffer, 0, newBuffer);

            // Act
            string text = textChange.NewText;

            // Assert
            Assert.Equal(String.Empty, text);
        }
Beispiel #15
0
        public void NewTextWithInsertReturnsChangedTextFromBuffer()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("test");
            var oldBuffer = new StringTextBuffer("");
            var textChange = new TextChange(0, 0, oldBuffer, 3, newBuffer);

            // Act
            string text = textChange.NewText;
            string oldText = textChange.OldText;

            // Assert
            Assert.Equal("tes", text);
            Assert.Equal("", oldText);
        }
Beispiel #16
0
        public void OldTextReturnsOldSpanFromOldBuffer2()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("test");
            var oldBuffer = new StringTextBuffer("text");
            var textChange = new TextChange(2, 2, oldBuffer, 1, newBuffer);

            // Act
            string text = textChange.OldText;

            // Assert
            Assert.Equal("xt", text);
        }
        private static void RunDisposeTest(Action<LookaheadTextReader> triggerAction)
        {
            // Arrange
            var source = new StringTextBuffer("abcdefg");
            var reader = new TextBufferReader(source);

            // Act
            triggerAction(reader);

            // Assert
            Assert.True(source.Disposed);
        }
Beispiel #18
0
        public void ApplyChangeWithInsertedTextReturnsNewContentWithChangeApplied()
        {
            // Arrange
            var newBuffer = new StringTextBuffer("test");
            var oldBuffer = new StringTextBuffer("");
            var textChange = new TextChange(0, 0, oldBuffer, 3, newBuffer);

            // Act
            string text = textChange.ApplyChange("abcd", 0);

            // Assert
            Assert.Equal("tesabcd", text);
        }