Esempio n. 1
0
        public async Task RemoveParameters_ReferenceInUnchangeableDocument()
        {
            var workspaceXml = @"
<Workspace>
    <Project Language=""C#"" AssemblyName=""CSharpAssembly"" CommonReferences=""true"">
        <Document FilePath = ""C1.cs"">
public class C1
{
    public static bool $$M(int x, int y)
    {
        return x > y;
    }
}</Document>
        <Document FilePath = ""C2.cs"">
public class C2
{
    bool _x = C1.M(1, 2); 
}</Document>
        <Document FilePath = ""C3.cs"" CanApplyChange=""false"">
public class C3
{
    bool _x = C1.M(1, 2); 
}</Document>
    </Project>
</Workspace>";

            var updatedSignature = new[] { 1, 0 };

            using (var testState = ChangeSignatureTestState.Create(XElement.Parse(workspaceXml)))
            {
                testState.TestChangeSignatureOptionsService.IsCancelled      = false;
                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 == "C1.cs")
                    {
                        // declaration should be changed
                        Assert.Contains("public static bool M(int y, int x)", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                    }
                    else if (updatedDocument.Name == "C2.cs")
                    {
                        // changeable document should be changed
                        Assert.Contains("bool _x = C1.M(2, 1);", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                    }
                    else if (updatedDocument.Name == "C3.cs")
                    {
                        // shouldn't change unchangeable document
                        Assert.Contains("bool _x = C1.M(1, 2);", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                    }
                }
            }
        }
        public async Task RemoveParameters_ExtensionMethodInAnotherFile()
        {
            var workspaceXml = @"
<Workspace>
    <Project Language=""C#"" AssemblyName=""CSharpAssembly"" CommonReferences=""true"">";

            for (int 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 (int 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>";

            // Ext(this F f, int i, string s) --> Ext(this F f, string s)
            // If a reference does not bind correctly, it will believe Ext is not an extension
            // method and remove the string argument instead of the int argument.

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

            using (var testState = await ChangeSignatureTestState.CreateAsync(XElement.Parse(workspaceXml)))
            {
                testState.TestChangeSignatureOptionsService.IsCancelled      = false;
                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());
                    }
                }
            }
        }
Esempio n. 3
0
        public async Task AddRemoveParameters_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),
                new AddedParameterOrExistingIndex(new AddedParameter(null, "int", "newIntegerParameter", CallSiteKind.Value, callSiteValue: "123"), "int")
            };

            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, int newIntegerParameter)", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                }
                else
                {
                    Assert.Contains(@"c.Ext(""two"", 123);", (await updatedDocument.GetTextAsync(CancellationToken.None)).ToString());
                }
            }
        }