public async Task Syntax_Tree_Has_Extra_Statements_When_Everything_Is_Augmented()
        {
            // arrange
            var document   = Sources.GetDocument(Sources.withMultipleMethodsAndComplexLayout);
            var syntaxTree = await document.GetSyntaxTreeAsync();

            var augmentations = syntaxTree.GetRoot()
                                .DescendantNodes()
                                .Where(n => n is StatementSyntax)
                                .Select(n => new Augmentation((StatementSyntax)n, null, null, null, null));
            var augMap = new AugmentationMap(augmentations.ToArray());

            var rewriter = new InstrumentationSyntaxRewriter(
                augMap.Data.Keys,
                new VariableLocationMap(),
                augMap
                );

            // act
            var newTree           = rewriter.ApplyToTree(syntaxTree);
            var newStatementCount = newTree.GetRoot().DescendantNodes().Count(n => n is StatementSyntax);

            // assert
            Assert.Equal(24, newStatementCount);
        }
        public async Task Syntax_Tree_Has_Locals_When_Augmentation_Has_Locals()
        {
            // arrange
            var document   = Sources.GetDocument(Sources.withMultipleMethodsAndComplexLayout);
            var syntaxTree = await document.GetSyntaxTreeAsync();

            var statement = (StatementSyntax)syntaxTree.GetRoot().DescendantNodes().Single(n => n.ToString() == @"Console.WriteLine(""Entry Point"");");

            var locals        = (await document.GetSemanticModelAsync()).LookupSymbols(310).Where(s => s.Kind == SymbolKind.Local);
            var augmentations = new[] { new Augmentation(statement, locals, null, null, null) };

            var augMap   = new AugmentationMap(augmentations.ToArray());
            var rewriter = new InstrumentationSyntaxRewriter(
                augMap.Data.Keys,
                new VariableLocationMap(),
                augMap
                );

            // act
            var newTree    = rewriter.ApplyToTree(syntaxTree);
            var treeString = newTree.ToString();

            // assert
            Assert.Contains("\\\"name\\\":\\\"j\\\"", treeString);
            Assert.Contains("\\\"name\\\":\\\"k\\\"", treeString);
            Assert.Contains("\\\"name\\\":\\\"p\\\"", treeString);
        }
        public async Task Syntax_Tree_Has_A_Single_Extra_Statement_When_There_Is_One_Augmentation()
        {
            // arrange
            var document   = Sources.GetDocument(Sources.simple);
            var syntaxTree = await document.GetSyntaxTreeAsync();

            var statementCount = syntaxTree.GetRoot().DescendantNodes().Count(n => n is StatementSyntax);
            var statement      = (StatementSyntax)syntaxTree.GetRoot().DescendantNodes().Single(n => n.ToString() == @"Console.WriteLine(""Entry Point"");");

            var augmentation = new Augmentation(statement, null, null, null, null);
            var augMap       = new AugmentationMap(augmentation);

            var rewriter = new InstrumentationSyntaxRewriter(
                augMap.Data.Keys,
                new VariableLocationMap(),
                augMap
                );

            // act
            var newTree           = rewriter.ApplyToTree(syntaxTree);
            var newStatementCount = newTree.GetRoot().DescendantNodes().Count(n => n is StatementSyntax);

            // assert
            Assert.Equal(statementCount + 1, newStatementCount);
        }
Esempio n. 4
0
                             )> Setup(string viewportCodeMarkup)
        {
            MarkupTestFile.GetNamedSpans(viewportCodeMarkup, out var viewportCode, out var textSpans);
            var code = $@"
using System;
using System.Linq;
namespace RoslynRecorder
{{
    class Program
    {{
        static void Main(string[] args)
        {{
        int thisShouldBeIgnored = 1;
#region test
        {viewportCode}
#endregion
        }}
    }}
}}
";
            var linePositionSpans = textSpans.ToDictionary(
                kv => kv.Key,
                kv => kv.Value.Select(span => span.ToLinePositionSpan(SourceText.From(viewportCode))
                                      )
                );

            var withLF    = code.EnforceLF();
            var document  = Sources.GetDocument(withLF);
            var workspace = new Workspace(files: new[] { new File("test.cs", withLF) });
            var visitor   = new InstrumentationSyntaxVisitor(document, await document.GetSemanticModelAsync());
            var viewport  = workspace.ExtractViewPorts().DefaultIfEmpty(null).First();

            return(visitor.Augmentations, visitor.VariableLocations, document, viewport, linePositionSpans);
        }
Esempio n. 5
0
        private async Task <string> RewriteCodeWithInstrumentation(string text)
        {
            var document  = Sources.GetDocument(text, true);
            var visitor   = new InstrumentationSyntaxVisitor(document, await document.GetSemanticModelAsync());
            var rewritten = new InstrumentationSyntaxRewriter(
                visitor.Augmentations.Data.Keys,
                visitor.VariableLocations,
                visitor.Augmentations
                );

            return(rewritten.ApplyToTree(document.GetSyntaxTreeAsync().Result).GetText().ToString().EnforceLF());
        }
        public async Task Static_Fields_Are_Captured_In_Static_Methods()
        {
            //arrange
            var document = Sources.GetDocument(Sources.withStaticAndNonStaticField, true);
            InstrumentationSyntaxVisitor visitor = new InstrumentationSyntaxVisitor(document, await document.GetSemanticModelAsync());

            //act
            var augmentations = visitor.Augmentations.Data.Values.ToList();

            //assert
            Assert.Single(augmentations[0].Fields);
            Assert.Contains(augmentations[0].Fields, f => f.Name == "a");
        }
        private async Task <AugmentationMap> GetAugmentationMapAsync(string source, IEnumerable <TextSpan> regions = null)
        {
            var document = Sources.GetDocument(source, true);

            var semanticModel = await document.GetSemanticModelAsync();

            if (regions == null)
            {
                return(new InstrumentationSyntaxVisitor(document, semanticModel).Augmentations);
            }
            else
            {
                return(new InstrumentationSyntaxVisitor(document, semanticModel, regions).Augmentations);
            }
        }
        public async Task Syntax_Tree_Is_Unchanged_When_Given_No_Augmentations()
        {
            // arrange
            var document   = Sources.GetDocument(Sources.simple);
            var syntaxTree = await document.GetSyntaxTreeAsync();

            var rewriter = new InstrumentationSyntaxRewriter
                           (
                Enumerable.Empty <SyntaxNode>(),
                new VariableLocationMap(),
                new AugmentationMap()
                           );

            // act
            var newTree = rewriter.ApplyToTree(syntaxTree);

            // assert
            Assert.True(syntaxTree.IsEquivalentTo(newTree));
        }
        public RewriterVariableLocationTests()
        {
            MarkupTestFile.GetSpans(Sources.withNonAssignedLocals, out var code, out ImmutableArray <TextSpan> _);
            var document          = Sources.GetDocument(code, true);
            var syntaxTree        = document.GetSyntaxTreeAsync().Result;
            var instrumentedNodes = syntaxTree.GetRoot()
                                    .DescendantNodes()
                                    .Where(n => n is StatementSyntax);

            var symbols = document.GetSemanticModelAsync().Result.LookupSymbols(250)
                          .Where(symbol => symbol.Kind == SymbolKind.Local);

            var a          = symbols.First(symbol => symbol.Name == "a");
            var aLocations = new[]
            {
                new VariableLocation(a, 12, 12, 12, 13),
                new VariableLocation(a, 9, 9, 16, 21)
            };

            var s          = symbols.First(symbol => symbol.Name == "s");
            var sLocations = new[]
            {
                new VariableLocation(s, 8, 8, 19, 20),
                new VariableLocation(s, 11, 11, 12, 13)
            };
            var locationMap = new VariableLocationMap();

            locationMap.AddLocations(a, aLocations);
            locationMap.AddLocations(s, sLocations);

            var rewriter = new InstrumentationSyntaxRewriter(
                instrumentedNodes,
                locationMap,
                new AugmentationMap()
                );

            var rewrittenProgramWithWhitespace = rewriter.ApplyToTree(syntaxTree).ToString();

            rewrittenProgram = TestUtils.RemoveWhitespace(rewrittenProgramWithWhitespace);
        }
Esempio n. 10
0
        private async Task FindAndValidateVariablesAsync(string markup)
        {
            MarkupTestFile.GetNamedSpans(
                markup,
                out var text,
                out IDictionary <string, ImmutableArray <TextSpan> > spans);

            var document = Sources.GetDocument(text);
            var fileLineLocationSpans = ConvertSpans(spans, await document.GetTextAsync());

            var visitor   = new InstrumentationSyntaxVisitor(document, await document.GetSemanticModelAsync());
            var locations = visitor.VariableLocations.Data.ToDictionary(
                key => key.Key.Name,
                values => values.Value.Select(location => location.ToLinePositionSpan()));

            foreach (var kv in locations)
            {
                var expected = new HashSet <LinePositionSpan>(fileLineLocationSpans[kv.Key]);
                var actual   = new HashSet <LinePositionSpan>(kv.Value);
                Assert.Equal(expected, actual);
            }
        }