protected static Tuple <Document[], bool, TextSpan?[]> GetDocumentsAndSpans(string[] sources, string language, bool addLanguageSpecificCodeAnalysisReference = true)
        {
            Assert.True(language == LanguageNames.CSharp || language == LanguageNames.VisualBasic, "Unsupported language");

            var  spans    = new TextSpan?[sources.Length];
            bool useSpans = false;

            for (int i = 0; i < sources.Length; i++)
            {
                string fileName = language == LanguageNames.CSharp ? "Test" + i + ".cs" : "Test" + i + ".vb";

                string   source;
                int?     pos;
                TextSpan?span;
                MarkupTestFile.GetPositionAndSpan(sources[i], out source, out pos, out span);

                sources[i] = source;
                spans[i]   = span;

                if (span != null)
                {
                    useSpans = true;
                }
            }

            var project   = CreateProject(sources, language, addLanguageSpecificCodeAnalysisReference);
            var documents = project.Documents.ToArray();

            Assert.Equal(sources.Length, documents.Length);

            return(Tuple.Create(documents, useSpans, spans));
        }
Esempio n. 2
0
        public static void TestSequencePoints(
            string markup,
            CSharpCompilationOptions compilationOptions,
            CSharpParseOptions parseOptions = null,
            string methodName = ""
            )
        {
            int?     position;
            TextSpan?expectedSpan;
            string   source;

            MarkupTestFile.GetPositionAndSpan(markup, out source, out position, out expectedSpan);

            var compilation = CreateCompilationWithMscorlib40AndSystemCore(
                source,
                options: compilationOptions,
                parseOptions: parseOptions
                );

            compilation
            .GetDiagnostics()
            .Where(d => d.Severity == DiagnosticSeverity.Error)
            .Verify();

            var  pdb           = PdbValidation.GetPdbXml(compilation, qualifiedMethodName: methodName);
            bool hasBreakpoint = CheckIfSpanWithinSequencePoints(
                expectedSpan.GetValueOrDefault(),
                source,
                pdb
                );

            Assert.True(hasBreakpoint);
        }
        private Task AssertSmartTokenFormatterCloseBraceWithBaseIndentation(string markup, int baseIndentation, int expectedIndentation)
        {
            MarkupTestFile.GetPositionAndSpan(markup,
                                              out var code, out var position, out TextSpan span);

            return(AssertSmartTokenFormatterCloseBraceAsync(
                       code,
                       SourceText.From(code).Lines.IndexOf(position),
                       expectedIndentation,
                       baseIndentation,
                       span));
        }
Esempio n. 4
0
        public static void TestSequencePoints(string markup, CSharpCompilationOptions compilationOptions, CSharpParseOptions parseOptions = null, string methodName = "")
        {
            int?     position;
            TextSpan?expectedSpan;
            string   source;

            MarkupTestFile.GetPositionAndSpan(markup, out source, out position, out expectedSpan);
            var  pdb           = GetPdbXml(source, compilationOptions, methodName, parseOptions);
            bool hasBreakpoint = CheckIfSpanWithinSequencePoints(expectedSpan.GetValueOrDefault(), source, pdb);

            Assert.True(hasBreakpoint);
        }
        private void AssertSmartTokenFormatterCloseBraceWithBaseIndentation(string markup, int baseIndentation, int expectedIndentation)
        {
            string   code;
            int      position;
            TextSpan span;

            MarkupTestFile.GetPositionAndSpan(markup, out code, out position, out span);

            AssertSmartTokenFormatterCloseBrace(
                code,
                SourceText.From(code).Lines.IndexOf(position),
                expectedIndentation,
                baseIndentation,
                span);
        }
        private void TestLambdaBody(string markedExpression, bool isLambdaBody, bool isReducedLambdaBody = false)
        {
            string   markedSource = @"
using System;
using System.Linq;

class C 
{ 
    void M() 
    { 
        var expr = " + markedExpression + @"; 
    }
 
    static T F<T>(T x) => x;
}";
            string   source;
            int?     position;
            TextSpan?span;

            MarkupTestFile.GetPositionAndSpan(markedSource, out source, out position, out span);

            Assert.Null(position);
            Assert.NotNull(span);

            var tree        = SyntaxFactory.ParseSyntaxTree(source);
            var compilation = CreateCompilationWithMscorlib45(new[] { tree }, new[] { SystemCoreRef });

            compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).Verify();
            var model = compilation.GetSemanticModel(tree, ignoreAccessibility: false);

            var  enclosingMethod = (IMethodSymbol)model.GetEnclosingSymbol(span.Value.Start);
            var  enclosingSyntax = enclosingMethod.DeclaringSyntaxReferences.Single().GetSyntax();
            bool expected        = enclosingMethod.MethodKind == MethodKind.LambdaMethod && enclosingSyntax.Span.Contains(span.Value);

            var node = tree.GetRoot().FindNode(span.Value);

            Assert.False(isLambdaBody && isReducedLambdaBody);
            Assert.Equal(expected, LambdaUtilities.IsLambdaBody(node, allowReducedLambdas: true));
            Assert.Equal(isLambdaBody || isReducedLambdaBody, expected);
            Assert.Equal(isLambdaBody, LambdaUtilities.IsLambdaBody(node));

            var methodDef = tree.GetRoot().DescendantNodes().OfType <MethodDeclarationSyntax>().Where(d => d.Identifier.ValueText == "M").Single();

            Assert.Equal("C", model.GetEnclosingSymbol(methodDef.SpanStart).ToTestDisplayString());
            Assert.Equal("C", model.GetEnclosingSymbol(methodDef.ParameterList.CloseParenToken.SpanStart).ToTestDisplayString());
            Assert.Equal("void C.M()", model.GetEnclosingSymbol(methodDef.Body.SpanStart).ToTestDisplayString());
        }
Esempio n. 7
0
        protected async Task VerifyRefactoringAsync(string sourceWithMarkup, string expected, CancellationToken cancellationToken = default)
        {
            MarkupTestFile.GetPositionAndSpan(sourceWithMarkup, out string source, cursorPosition: out _, out TextSpan? textSpan);
            Assert.True(textSpan.HasValue, "Input source is missing span markup '[||]'");

            var document = CreateDocument(source, LanguageNames.CSharp);
            var actions  = new List <CodeAction>();
            var context  = new CodeRefactoringContext(document, textSpan.Value, (a) => actions.Add(a), cancellationToken);
            var codeRefactoringProvider = GetCodeRefactoringProvider();
            await codeRefactoringProvider.ComputeRefactoringsAsync(context);

            var codeAction = actions.First();
            var operations = await codeAction.GetOperationsAsync(cancellationToken);

            var solution = operations.OfType <ApplyChangesOperation>().Single().ChangedSolution;

            document = solution.GetDocument(document.Id);

            var newDocumentString = getStringFromDocument(document);

            Assert.Equal(expected, newDocumentString);
Esempio n. 8
0
        private void TestLambdaBody(string markedExpression, bool isLambdaBody)
        {
            string   markedSource = @"
using System;
using System.Linq;

class C 
{ 
    void M() 
    { 
        var expr = " + markedExpression + @"; 
    }
 
    static T F<T>(T x) => x;
}";
            string   source;
            int?     position;
            TextSpan?span;

            MarkupTestFile.GetPositionAndSpan(markedSource, out source, out position, out span);

            Assert.Null(position);
            Assert.NotNull(span);

            var tree        = SyntaxFactory.ParseSyntaxTree(source);
            var compilation = CreateCompilationWithMscorlib45(new[] { tree }, new[] { SystemCoreRef });

            compilation.GetDiagnostics().Where(d => d.Severity == DiagnosticSeverity.Error).Verify();
            var model = compilation.GetSemanticModel(tree, ignoreAccessibility: false);

            var  enclosingMethod = (IMethodSymbol)model.GetEnclosingSymbol(span.Value.Start);
            var  enclosingSyntax = enclosingMethod.DeclaringSyntaxReferences.Single().GetSyntax();
            bool expected        = enclosingMethod.MethodKind == MethodKind.LambdaMethod && enclosingSyntax.Span.Contains(span.Value);

            var node = tree.GetRoot().FindNode(span.Value);

            Assert.Equal(expected, LambdaUtilities.IsLambdaBody(node));
            Assert.Equal(isLambdaBody, expected);
        }