public async Task AddContractNamespaceIfNeededDoesNotAddsNamespaceIfAlreadyExists()
        {
            string src =
                @"using System;
using System.Diagnostics.Contracts;

internal class SampleClass
{
    private SampleClass(string s{caret}tr)
    {
    }
}";
            var doc = await ClassTemplate.FromFullSource(src);

            var newRoot = RequiresUtils.AddContractNamespaceIfNeeded(doc.Root);

            Console.WriteLine(newRoot.GetText().ToString());

            string expected =
                @"using System;
using System.Diagnostics.Contracts;

internal class SampleClass
{
    private SampleClass(string str)
    {
    }
}";

            Assert.AreEqual(expected, newRoot.GetText().ToString());
        }
        public async Task AddContractNamespaceIfNeededAddsNamespaceOnlyOnce()
        {
            string src =
                @"internal class SampleClass
{
    private SampleClass(string s{caret}tr)
    {
    }
}";
            var doc = await ClassTemplate.FromFullSource(src);

            var newRoot = RequiresUtils.AddContractNamespaceIfNeeded(doc.Root);

            Console.WriteLine(newRoot.GetText().ToString());

            string expected =
                @"using System.Diagnostics.Contracts;
internal class SampleClass
{
    private SampleClass(string str)
    {
    }
}";

            Assert.AreEqual(expected, newRoot.GetText().ToString());

            // Calling the same method once more time
            newRoot = RequiresUtils.AddContractNamespaceIfNeeded(newRoot);
            Assert.AreEqual(expected, newRoot.GetText().ToString(), "Second call to AddContractNamespacedIfNeeded should have no effect");
        }
示例#3
0
        public async Task <Document> ApplyRefactoringAsync(Document document, CancellationToken token)
        {
            MethodDeclarationSyntax method = _selectedNode.AncestorsAndSelf().OfType <MethodDeclarationSyntax>().First();

            Option <ExpressionStatementSyntax> lastRequires = await GetLastRequiresStatement(document, method, token);

            SyntaxNode root = await document.GetSyntaxRootAsync(token);

            SyntaxNode rootWithRequires = root.ReplaceNode(method, RequiresUtils.AddEnsures(method, _semanticModel, lastRequires));
            SyntaxNode rootWithUsings   = RequiresUtils.AddContractNamespaceIfNeeded(rootWithRequires);

            return(document.WithSyntaxRoot(rootWithUsings));
        }
        public async Task <Document> ApplyRefactoringAsync(Document document, CancellationToken token)
        {
            Contract.Assert(_parameter.HasValue);

            SyntaxNode root = await document.GetSyntaxRootAsync(token);

            SemanticModel semanticModel = await document.GetSemanticModelAsync(token);

            var rootWithRequires = root.ReplaceNode(_enclosingMethod.Value.CurrentMethod,
                                                    _enclosingMethod.Value.AddRequires(_parameter.Value, semanticModel));

            var rootWithUsings = RequiresUtils.AddContractNamespaceIfNeeded(rootWithRequires);

            return(document.WithSyntaxRoot(rootWithUsings));
        }