RemoveParametersTests.cs 11.3 KB
Newer Older
J
Jonathon Marolf 已提交
1 2 3
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
4

5
using System;
6 7
using System.Linq;
using System.Threading;
C
Cyrus Najmabadi 已提交
8
using System.Threading.Tasks;
9
using System.Xml.Linq;
I
Ivan Basov 已提交
10
using Microsoft.CodeAnalysis.ChangeSignature;
11 12
using Microsoft.CodeAnalysis.Editor.CSharp.ChangeSignature;
using Microsoft.CodeAnalysis.Editor.Implementation.Interactive;
13
using Microsoft.CodeAnalysis.Editor.Shared.Utilities;
14
using Microsoft.CodeAnalysis.Editor.UnitTests;
15
using Microsoft.CodeAnalysis.Editor.UnitTests.ChangeSignature;
16
using Microsoft.CodeAnalysis.Editor.UnitTests.Workspaces;
17
using Microsoft.CodeAnalysis.Test.Utilities;
18
using Microsoft.CodeAnalysis.Test.Utilities.ChangeSignature;
19
using Microsoft.VisualStudio.Text.Editor.Commanding.Commands;
20 21 22 23 24 25 26
using Roslyn.Test.Utilities;
using Xunit;

namespace Microsoft.CodeAnalysis.Editor.CSharp.UnitTests.ChangeSignature
{
    public partial class ChangeSignatureTests : AbstractChangeSignatureTests
    {
27
        [WpfFact, Trait(Traits.Feature, Traits.Features.ChangeSignature)]
C
Cyrus Najmabadi 已提交
28
        public async Task RemoveParameters1()
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
        {
            var markup = @"
static class Ext
{
    /// <summary>
    /// This is a summary of <see cref=""M(object, int, string, bool, int, string, int[])""/>
    /// </summary>
    /// <param name=""o""></param>
    /// <param name=""a""></param>
    /// <param name=""b""></param>
    /// <param name=""c""></param>
    /// <param name=""x""></param>
    /// <param name=""y""></param>
    /// <param name=""p""></param>
    static void $$M(this object o, int a, string b, bool c, int x = 0, string y = ""Zero"", params int[] p)
    {
        object t = new object();

        M(t, 1, ""two"", true, 3, ""four"", new[] { 5, 6 });
        M(t, 1, ""two"", true, 3, ""four"", 5, 6);
        t.M(1, ""two"", true, 3, ""four"", new[] { 5, 6 });
        t.M(1, ""two"", true, 3, ""four"", 5, 6);

        M(t, 1, ""two"", true, 3, ""four"");
        M(t, 1, ""two"", true, 3);
        M(t, 1, ""two"", true);

        M(t, 1, ""two"", c: true);
        M(t, 1, ""two"", true, 3, y: ""four"");

        M(t, 1, ""two"", true, 3, p: new[] { 5 });
        M(t, 1, ""two"", true, p: new[] { 5 });
        M(t, 1, ""two"", true, y: ""four"");
        M(t, 1, ""two"", true, x: 3);

        M(t, 1, ""two"", true, y: ""four"", x: 3);
        M(t, 1, y: ""four"", x: 3, b: ""two"", c: true);
        M(t, y: ""four"", x: 3, c: true, b: ""two"", a: 1);
        M(t, p: new[] { 5 }, y: ""four"", x: 3, c: true, b: ""two"", a: 1);
        M(p: new[] { 5 }, y: ""four"", x: 3, c: true, b: ""two"", a: 1, o: t);
    }
}";
            var updatedSignature = new[] { 0, 2, 5 };
            var updatedCode = @"
static class Ext
{
    /// <summary>
    /// This is a summary of <see cref=""M(object, string, string)""/>
    /// </summary>
    /// <param name=""o""></param>
    /// <param name=""b""></param>
    /// <param name=""y""></param>
    /// 
    /// 
    /// 
    /// 
    static void M(this object o, string b, string y = ""Zero"")
    {
        object t = new object();

        M(t, ""two"", ""four"");
        M(t, ""two"", ""four"");
        t.M(""two"", ""four"");
        t.M(""two"", ""four"");

        M(t, ""two"", ""four"");
        M(t, ""two"");
        M(t, ""two"");

        M(t, ""two"");
        M(t, ""two"", y: ""four"");

        M(t, ""two"");
        M(t, ""two"");
        M(t, ""two"", y: ""four"");
        M(t, ""two"");

        M(t, ""two"", y: ""four"");
        M(t, y: ""four"", b: ""two"");
        M(t, y: ""four"", b: ""two"");
        M(t, y: ""four"", b: ""two"");
        M(y: ""four"", b: ""two"", o: t);
    }
}";

C
Cyrus Najmabadi 已提交
114
            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature: updatedSignature, expectedUpdatedInvocationDocumentCode: updatedCode);
115 116
        }

117
        [WpfFact, Trait(Traits.Feature, Traits.Features.ChangeSignature)]
C
Cyrus Najmabadi 已提交
118
        public async Task RemoveParameters_GenericParameterType()
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        {
            var markup = @"
class DA
{
    void M(params int[] i) { }

    void B()
    {
        DP20<int>.D d = new DP20<int>.D(M);

        /*DA19*/$$d(0);
        d();
        d(0, 1);
    }
}
public class DP20<T>
{
    public delegate void /*DP20*/D(params T[] t);
    public event D E1;
    public event D E2;

    public void M1(params T[] t) { }
    public void M2(params T[] t) { }
    public void M3(params T[] t) { }

    void B()
    {
        D d = new D(M1);
        E1 += new D(M2);
        E2 -= new D(M3);
    }
}";
151
            var updatedSignature = Array.Empty<int>();
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
            var updatedCode = @"
class DA
{
    void M() { }

    void B()
    {
        DP20<int>.D d = new DP20<int>.D(M);

        /*DA19*/d();
        d();
        d();
    }
}
public class DP20<T>
{
    public delegate void /*DP20*/D();
    public event D E1;
    public event D E2;

    public void M1() { }
    public void M2() { }
    public void M3() { }

    void B()
    {
        D d = new D(M1);
        E1 += new D(M2);
        E2 -= new D(M3);
    }
}";

C
Cyrus Najmabadi 已提交
184
            await TestChangeSignatureViaCommandAsync(LanguageNames.CSharp, markup, updatedSignature: updatedSignature, expectedUpdatedInvocationDocumentCode: updatedCode);
185
        }
186

D
David Poeschl 已提交
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
        [WpfFact, Trait(Traits.Feature, Traits.Features.ChangeSignature)]
        [WorkItem(1102830, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1102830")]
        [WorkItem(784, "https://github.com/dotnet/roslyn/issues/784")]
        public async Task RemoveParameters_ExtensionMethodInAnotherFile()
        {
            var workspaceXml = @"
<Workspace>
    <Project Language=""C#"" AssemblyName=""CSharpAssembly"" CommonReferences=""true"">";

            for (var i = 0; i <= 4; i++)
            {
                workspaceXml += $@"
<Document FilePath = ""C{i}.cs"">
class C{i}
{{
    void M()
    {{
        C5 c = new C5();
        c.Ext(1, ""two"");
    }}
}}
</Document>";
            }

            workspaceXml += @"
<Document FilePath = ""C5.cs"">
public class C5
{
}

public static class C5Ext
{
    public void $$Ext(this C5 c, int i, string s)
    {
    }
}
</Document>";

            for (var i = 6; i <= 9; i++)
            {
                workspaceXml += $@"
<Document FilePath = ""C{i}.cs"">
class C{i}
{{
    void M()
    {{
        C5 c = new C5();
        c.Ext(1, ""two"");
    }}
}}
</Document>";
            }

            workspaceXml += @"
    </Project>
</Workspace>";

            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                new AddedParameterOrExistingIndex(2) };

            using var testState = ChangeSignatureTestState.Create(XElement.Parse(workspaceXml));
            testState.TestChangeSignatureOptionsService.UpdatedSignature = updatedSignature;
            var result = testState.ChangeSignature();

            Assert.True(result.Succeeded);
            Assert.Null(testState.ErrorMessage);

            foreach (var updatedDocument in testState.Workspace.Documents.Select(d => result.UpdatedSolution.GetDocument(d.Id)))
            {
                if (updatedDocument.Name == "C5.cs")
                {
                    Assert.Contains("void Ext(this C5 c, string s)", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                }
                else
                {
                    Assert.Contains(@"c.Ext(""two"");", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                }
            }
        }

268
        [WpfFact, Trait(Traits.Feature, Traits.Features.ChangeSignature)]
J
Jared Parsons 已提交
269
        [WorkItem(1102830, "http://vstfdevdiv:8080/DevDiv2/DevDiv/_workitems/edit/1102830")]
270
        [WorkItem(784, "https://github.com/dotnet/roslyn/issues/784")]
I
Ivan Basov 已提交
271
        public async Task AddRemoveParameters_ExtensionMethodInAnotherFile()
272 273 274 275
        {
            var workspaceXml = @"
<Workspace>
    <Project Language=""C#"" AssemblyName=""CSharpAssembly"" CommonReferences=""true"">";
J
Jared Parsons 已提交
276

C
Use var  
Cyrus Najmabadi 已提交
277
            for (var i = 0; i <= 4; i++)
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
            {
                workspaceXml += $@"
<Document FilePath = ""C{i}.cs"">
class C{i}
{{
    void M()
    {{
        C5 c = new C5();
        c.Ext(1, ""two"");
    }}
}}
</Document>";
            }

            workspaceXml += @"
<Document FilePath = ""C5.cs"">
public class C5
{
}

public static class C5Ext
{
    public void $$Ext(this C5 c, int i, string s)
    {
    }
}
</Document>";

C
Use var  
Cyrus Najmabadi 已提交
306
            for (var i = 6; i <= 9; i++)
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            {
                workspaceXml += $@"
<Document FilePath = ""C{i}.cs"">
class C{i}
{{
    void M()
    {{
        C5 c = new C5();
        c.Ext(1, ""two"");
    }}
}}
</Document>";
            }

            workspaceXml += @"
    </Project>
</Workspace>";

I
Ivan Basov 已提交
325 326 327
            var updatedSignature = new[] {
                new AddedParameterOrExistingIndex(0),
                new AddedParameterOrExistingIndex(2),
328
                new AddedParameterOrExistingIndex(new AddedParameter(null, "int", "newIntegerParameter", CallSiteKind.Value, callSiteValue:"123"), "int") };
329

C
Cyrus Najmabadi 已提交
330 331 332
            using var testState = ChangeSignatureTestState.Create(XElement.Parse(workspaceXml));
            testState.TestChangeSignatureOptionsService.UpdatedSignature = updatedSignature;
            var result = testState.ChangeSignature();
333

C
Cyrus Najmabadi 已提交
334 335
            Assert.True(result.Succeeded);
            Assert.Null(testState.ErrorMessage);
336

C
Cyrus Najmabadi 已提交
337 338 339 340
            foreach (var updatedDocument in testState.Workspace.Documents.Select(d => result.UpdatedSolution.GetDocument(d.Id)))
            {
                if (updatedDocument.Name == "C5.cs")
                {
I
Ivan Basov 已提交
341
                    Assert.Contains("void Ext(this C5 c, string s, int newIntegerParameter)", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
C
Cyrus Najmabadi 已提交
342 343
                }
                else
344
                {
I
Ivan Basov 已提交
345
                    Assert.Contains(@"c.Ext(""two"", 123);", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
346 347 348
                }
            }
        }
349

K
Kevin Pilch-Bisson 已提交
350
        [WpfFact]
351 352
        [Trait(Traits.Feature, Traits.Features.ChangeSignature)]
        [Trait(Traits.Feature, Traits.Features.Interactive)]
C
CyrusNajmabadi 已提交
353
        public void ChangeSignatureCommandDisabledInSubmission()
354
        {
C
Cyrus Najmabadi 已提交
355
            using var workspace = TestWorkspace.Create(XElement.Parse(@"
356 357 358 359 360 361 362 363 364 365 366
                <Workspace>
                    <Submission Language=""C#"" CommonReferences=""true"">  
                        class C
                        {
                            void M$$(int x)
                            {
                            }
                        }
                    </Submission>
                </Workspace> "),
                workspaceKind: WorkspaceKind.Interactive,
T
Tomáš Matoušek 已提交
367
                composition: EditorTestCompositions.EditorFeaturesWpf);
C
Cyrus Najmabadi 已提交
368 369
            // Force initialization.
            workspace.GetOpenDocumentIds().Select(id => workspace.GetTestDocument(id).GetTextView()).ToList();
370

C
Cyrus Najmabadi 已提交
371
            var textView = workspace.Documents.Single().GetTextView();
372

373
            var handler = new CSharpChangeSignatureCommandHandler(workspace.GetService<IThreadingContext>());
374

C
Cyrus Najmabadi 已提交
375 376
            var state = handler.GetCommandState(new RemoveParametersCommandArgs(textView, textView.TextBuffer));
            Assert.True(state.IsUnspecified);
377

C
Cyrus Najmabadi 已提交
378 379
            state = handler.GetCommandState(new ReorderParametersCommandArgs(textView, textView.TextBuffer));
            Assert.True(state.IsUnspecified);
380
        }
381
    }
S
Sam Harwell 已提交
382
}