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);
        }
Пример #2
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);
            }
        }
        public async Task Syntax_Tree_Has_Parameters_When_Augmentation_Has_Parameters()
        {
            // 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 parameters    = (await document.GetSemanticModelAsync()).LookupSymbols(310).Where(s => s.Kind == SymbolKind.Parameter);
            var augmentations = new[] { new Augmentation(statement, null, null, parameters, 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\\\":\\\"args\\\"", treeString);
        }