Exemplo n.º 1
0
        public async Task MapLineLocationsRelativeToViewport_Does_Nothing_Without_Viewport()
        {
            var(augmentation, locations, document, _, _) = await Setup(@"Console.WriteLine(""hello world"");");

            var(newAugmentation, newLocations) = await InstrumentationLineMapper.MapLineLocationsRelativeToViewportAsync(augmentation, locations, document);

            augmentation.Should().BeEquivalentTo(newAugmentation);
            locations.Should().BeEquivalentTo(newLocations);
        }
Exemplo n.º 2
0
        public async Task MapLineLocationsRelativeToViewport_Maps_Augmentation_FilePosition_Correctly()
        {
            var(augmentation, locations, document, viewport, spans) = await Setup(
                @"
{|a:int a = 0;|}
{|b:Console.WriteLine(""Entry Point"");|}
"
                );

            var(newAugmentation, newLocations) = await InstrumentationLineMapper.MapLineLocationsRelativeToViewportAsync(augmentation, locations, document, viewport);

            var linePositions         = newAugmentation.Data.Values.Select(state => state.CurrentFilePosition.Line);
            var expectedLinePositions = spans.Values.Select(span => span.First().Start.Line);

            linePositions.Should().Equal(expectedLinePositions);
        }
Exemplo n.º 3
0
        public async Task MapLineLocationsRelativeToViewport_Maps_Variable_Location_Correctly()
        {
            var(augmentation, locations, document, viewport, spans) = await Setup(
                @"
{|a:int a = 0;|}
{|b:Console.WriteLine(""Entry Point"");|}
");

            var(newAugmentation, newLocations) = await InstrumentationLineMapper.MapLineLocationsRelativeToViewportAsync(augmentation, locations, document, viewport);

            var variableLocationLines = newLocations.Data.Values
                                        .SelectMany(locs => locs)
                                        .Select(loc => loc.StartLine);
            var expectedLocations = spans["a"].First().Start.Line;

            variableLocationLines.Should().Equal(new[] { expectedLocations });
        }
Exemplo n.º 4
0
        public async Task MapLineLocationsRelativeToViewport_Maps_Multiple_Variables_On_Single_Line_Correctly()
        {
            var(augmentation, locations, document, viewport, spans) = await Setup(
                @"
{|variables:var (a, b) = (1, 2);|}
");

            var(newAugmentation, newLocations) = await InstrumentationLineMapper.MapLineLocationsRelativeToViewportAsync(augmentation, locations, document, viewport);

            var variableLocationLines = newLocations.Data.Values
                                        .SelectMany(locs => locs)
                                        .Select(loc => loc.StartLine)
                                        .Distinct();
            var expectedLocations = spans["variables"].First().Start.Line;

            variableLocationLines.Should().Equal(new[] { expectedLocations });
        }
Exemplo n.º 5
0
        private static async Task <Compilation> AugmentCompilationAsync(
            IEnumerable <Viewport> viewports,
            Compilation compilation,
            Document document,
            BufferId activeBufferId,
            Package build)
        {
            var regions = InstrumentationLineMapper.FilterActiveViewport(viewports, activeBufferId)
                          .Where(v => v.Destination?.Name != null)
                          .GroupBy(v => v.Destination.Name,
                                   v => v.Region,
                                   (name, region) => new InstrumentationMap(name, region))
                          .ToArray();

            var solution       = document.Project.Solution;
            var newCompilation = compilation;

            foreach (var tree in newCompilation.SyntaxTrees)
            {
                var replacementRegions = regions.FirstOrDefault(r => tree.FilePath.EndsWith(r.FileToInstrument))?.InstrumentationRegions;

                var subdocument = solution.GetDocument(tree);
                var visitor     = new InstrumentationSyntaxVisitor(subdocument, await subdocument.GetSemanticModelAsync(), replacementRegions);
                var linesWithInstrumentation = visitor.Augmentations.Data.Keys;

                var activeViewport = viewports.DefaultIfEmpty(null).First();

                var(augmentationMap, variableLocationMap) =
                    await InstrumentationLineMapper.MapLineLocationsRelativeToViewportAsync(
                        visitor.Augmentations,
                        visitor.VariableLocations,
                        document,
                        activeViewport);

                var rewrite = new InstrumentationSyntaxRewriter(
                    linesWithInstrumentation,
                    variableLocationMap,
                    augmentationMap);
                var newRoot = rewrite.Visit(tree.GetRoot());
                var newTree = tree.WithRootAndOptions(newRoot, tree.Options);

                newCompilation = newCompilation.ReplaceSyntaxTree(tree, newTree);
            }

            var instrumentationSyntaxTree = build.GetInstrumentationEmitterSyntaxTree();

            newCompilation = newCompilation.AddSyntaxTrees(instrumentationSyntaxTree);

            var augmentedDiagnostics = newCompilation.GetDiagnostics();

            if (augmentedDiagnostics.ContainsError())
            {
                throw new InvalidOperationException(
                          $@"Augmented source failed to compile

Diagnostics
-----------
{string.Join(NewLine, augmentedDiagnostics)}

Source
------
{newCompilation.SyntaxTrees.Select(s => $"// {s.FilePath ?? "(anonymous)"}{NewLine}//---------------------------------{NewLine}{NewLine}{s}").Join(NewLine + NewLine)}");
            }

            return(newCompilation);
        }