Ejemplo n.º 1
0
        public async Task AddOptionalParameter_ToAttribute()
        {
            var markup           = @"
[My(1)]
class MyAttribute : System.Attribute
{
    public MyAttribute($$int a) { }
}";
            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "x", CallSiteKind.Value, callSiteValue: "100", isRequired: false, defaultValue: "10"),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "y", CallSiteKind.Omitted, isRequired: false, defaultValue: "11"),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "z", CallSiteKind.Value, callSiteValue: "102", isRequired: false, defaultValue: "12")
            };

            // TODO: The = in the attribute is a bug. You cannot specify that the attribute should use : instead in the SyntaxGenerator
            // https://github.com/dotnet/roslyn/issues/43354
            var updatedCode = @"
[My(1, 100, z = 102)]
class MyAttribute : System.Attribute
{
    public MyAttribute(int a, int x = 10, int y = 11, int z = 12) { }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 2
0
        public async Task AddOptionalParameter_CallsiteInferred_MultipleLocals()
        {
            var markup           = @"
class C
{
    void M$$()
    {
        int x = 7;
        int y = 8;
        M();
    }
}";
            var updatedSignature = new[] {
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "a", CallSiteKind.Inferred)
            };
            var updatedCode = @"
class C
{
    void M(int a)
    {
        int x = 7;
        int y = 8;
        M(y);
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 3
0
        public async Task AddOptionalParameter_ToIndexer()
        {
            var markup           = @"
class B
{
    public int this$$[int a] { get { return 5; } }

    public void M()
    {
        var d = this[1];
    }
}";
            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "x", CallSiteKind.Value, callSiteValue: "100", isRequired: false, defaultValue: "10"),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "y", CallSiteKind.Omitted, isRequired: false, defaultValue: "11"),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "z", CallSiteKind.Value, callSiteValue: "102", isRequired: false, defaultValue: "12")
            };
            var updatedCode = @"
class B
{
    public int this[int a, int x = 10, int y = 11, int z = 12] { get { return 5; } }

    public void M()
    {
        var d = this[1, 100, z: 102];
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 4
0
        public async Task AddOptionalParameter_CallsiteInferred_SingleParameter()
        {
            var markup           = @"
class C
{
    void M$$(int x)
    {
        M(1);
    }
}";
            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "a", CallSiteKind.Inferred)
            };
            var updatedCode = @"
class C
{
    void M(int x, int a)
    {
        M(1, x);
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 5
0
        public async Task AddOptionalParameter_CallsiteInferred_ImplicitlyConvertable()
        {
            var markup           = @"
class B { }
class D : B { }

class C
{
    void M$$()
    {
        D d = null;
        M();
    }
}";
            var updatedSignature = new[] {
                AddedParameterOrExistingIndex.CreateAdded("B", "b", CallSiteKind.Inferred)
            };
            var updatedCode = @"
class B { }
class D : B { }

class C
{
    void M(B b)
    {
        D d = null;
        M(d);
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
        public async Task AddOptionalParameterWithOmittedCallsiteToAttributeConstructor()
        {
            var markup      = @"
[Some(1, 2, 4)]
class SomeAttribute : System.Attribute
{
    public SomeAttribute$$(int a, int b, int y = 4)
    {
    }
}";
            var permutation = new[] {
                new AddedParameterOrExistingIndex(0),
                new AddedParameterOrExistingIndex(1),
                AddedParameterOrExistingIndex.CreateAdded("int", "x", CallSiteKind.Omitted, isRequired: false, defaultValue: "3"),
                new AddedParameterOrExistingIndex(2)
            };
            var updatedCode = @"
[Some(1, 2, y: 4)]
class SomeAttribute : System.Attribute
{
    public SomeAttribute(int a, int b, int x = 3, int y = 4)
    {
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : permutation, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
        public async Task AddOptionalParameter_BeforeOptionalParameter_CallsiteOmitted()
        {
            var markup           = @"
class C
{
    void M$$(int x = 2)
    {
        M()
        M(2);
        M(x: 2);
    }
}";
            var updatedSignature = new[] {
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "a", CallSiteKind.Omitted, isRequired: false, defaultValue: "1"),
                new AddedParameterOrExistingIndex(0)
            };
            var updatedCode = @"
class C
{
    void M(int a = 1, int x = 2)
    {
        M()
        M(x: 2);
        M(x: 2);
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 8
0
        public async Task AddAndReorderConstructorParametersAndArguments()
        {
            var markup =
                @"
using System;

class MyClass2 : MyClass
{
    public MyClass2() : base(5, ""test2"")
    {
    }
}

class MyClass
{
    public MyClass() : this(2, ""test"")
    {
    }

    public $$MyClass(int x, string y)
    {
        var t = new MyClass(x, y);
    }
}";
            var permutation = new[]
            {
                new AddedParameterOrExistingIndex(1),
                AddedParameterOrExistingIndex.CreateAdded("byte", "b", CallSiteKind.Value, "34"),
                new AddedParameterOrExistingIndex(0)
            };
            var updatedCode =
                @"
using System;

class MyClass2 : MyClass
{
    public MyClass2() : base(""test2"", 34, 5)
    {
    }
}

class MyClass
{
    public MyClass() : this(""test"", 34, 2)
    {
    }

    public MyClass(string y, byte b, int x)
    {
        var t = new MyClass(y, 34, x);
    }
}";

            await TestChangeSignatureViaCommandAsync(
                LanguageNames.CSharp,
                markup,
                updatedSignature : permutation,
                expectedUpdatedInvocationDocumentCode : updatedCode
                );
        }
        public async Task AddOptionalParameter_AfterRequiredParameter_CallsiteOmitted()
        {
            var markup           = @"
class C
{
    void M$$(int x)
    {
        M(1);
    }
}";
            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "a", isRequired: false, defaultValue: "1", isCallsiteOmitted: true)
            };
            var updatedCode = @"
class C
{
    void M(int x, int a = 1)
    {
        M(1);
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
        public async Task AddOptionalParameter_BeforeExpandedParamsArray_CallsiteOmitted()
        {
            var markup           = @"
class C
{
    void M$$(params int[] p)
    {
        M();
        M(1);
        M(1, 2);
        M(1, 2, 3);
    }
}";
            var updatedSignature = new[] {
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "a", isRequired: false, defaultValue: "1", isCallsiteOmitted: true),
                new AddedParameterOrExistingIndex(0)
            };
            var updatedCode = @"
class C
{
    void M(int a = 1, params int[] p)
    {
        M();
        M(p: new int[] { 1 });
        M(p: new int[] { 1, 2 });
        M(p: new int[] { 1, 2, 3 });
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 11
0
        public async Task AddParameterWithOmittedArgument_ParamsAsArray()
        {
            var markup      = @"
public class C
{
    void $$M(int x, int y, params int[] p)
    {
        M(x, y, p: p);
    }
}";
            var permutation = new[] {
                new AddedParameterOrExistingIndex(0),
                new AddedParameterOrExistingIndex(1),
                AddedParameterOrExistingIndex.CreateAdded("int", "z", isRequired: false, defaultValue: "3", isCallsiteOmitted: true),
                new AddedParameterOrExistingIndex(2)
            };
            var updatedCode = @"
public class C
{
    void M(int x, int y, int z = 3, params int[] p)
    {
        M(x, y, p: p);
    }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : permutation, expectedUpdatedInvocationDocumentCode : updatedCode);
        }
Ejemplo n.º 12
0
        public async Task AddParameterOnUnparenthesizedLambda()
        {
            var markup =
                @"
using System.Linq;

namespace ConsoleApp426
{
    class Program
    {
        static void M(string[] args)
        {
            if (args.All(b$$ => Test()))
            {

            }
        }

        static bool Test() { return true; }
    }
}";
            var permutation = new[]
            {
                new AddedParameterOrExistingIndex(0),
                AddedParameterOrExistingIndex.CreateAdded(
                    "byte",
                    "bb",
                    CallSiteKind.Value,
                    callSiteValue: "34"
                    )
            };

            var updatedCode =
                @"
using System.Linq;

namespace ConsoleApp426
{
    class Program
    {
        static void M(string[] args)
        {
            if (args.All((b, byte bb) => Test()))
            {

            }
        }

        static bool Test() { return true; }
    }
}";

            await TestChangeSignatureViaCommandAsync(
                LanguageNames.CSharp,
                markup,
                updatedSignature : permutation,
                expectedUpdatedInvocationDocumentCode : updatedCode
                );
        }
Ejemplo n.º 13
0
        public async Task AddOptionalParameter_ToConstructor_TargetTypedNew()
        {
            var markup           = @"
class B
{
    public B() : this(1) { }
    public B$$(int a)
    {
        B q = new(1);
    }
}

class D : B
{
    public D() : base(1) { }
}";
            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "x", CallSiteKind.Value, callSiteValue: "100", isRequired: false, defaultValue: "10"),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "y", CallSiteKind.Omitted, isRequired: false, defaultValue: "11"),
                AddedParameterOrExistingIndex.CreateAdded("System.Int32", "z", CallSiteKind.Value, callSiteValue: "102", isRequired: false, defaultValue: "12")
            };
            var updatedCode = @"
class B
{
    public B() : this(1, 100, z: 102) { }
    public B(int a, int x = 10, int y = 11, int z = 12)
    {
        B q = new(1, 100, z: 102);
    }
}

class D : B
{
    public D() : base(1, 100, z: 102) { }
}";

            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature : updatedSignature, expectedUpdatedInvocationDocumentCode : updatedCode);
        }