File: src\Analyzers\CSharp\Tests\SimplifyLinqExpression\CSharpSimplifyLinqTypeCheckAndCastTests.cs
Web Access
Project: src\src\CodeStyle\CSharp\Tests\Microsoft.CodeAnalysis.CSharp.CodeStyle.UnitTests.csproj (Microsoft.CodeAnalysis.CSharp.CodeStyle.UnitTests)
// 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.
 
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CSharp.SimplifyLinqExpression;
using Microsoft.CodeAnalysis.Editor.UnitTests.CodeActions;
using Microsoft.CodeAnalysis.SimplifyLinqExpression;
using Microsoft.CodeAnalysis.Test.Utilities;
using Roslyn.Test.Utilities;
using Xunit;
 
namespace Microsoft.CodeAnalysis.CSharp.Analyzers.UnitTests.SimplifyLinqExpression;
 
using VerifyCS = CSharpCodeFixVerifier<
    CSharpSimplifyLinqTypeCheckAndCastDiagnosticAnalyzer,
    CSharpSimplifyLinqTypeCheckAndCastCodeFixProvider>;
 
[Trait(Traits.Feature, Traits.Features.CodeActionsSimplifyLinqTypeCheckAndCast)]
public sealed partial class CSharpSimplifyLinqTypeCheckAndCast
{
    [Fact]
    public async Task TestCastMethod()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).[|Cast|]<int>();
                    }
                }
                """,
            FixedCode = """
                using System.Linq;
                
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.[|OfType|]<int>();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect1()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.{|CS1061:Where1|}(a => a is int).Cast<int>();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect2()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => {|CS0103:b|} is int).Cast<int>();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect3()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is string).Cast<int>();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect4()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).{|CS1061:Cast1<int>|}();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect5()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).{|CS1501:Cast<int>|}(0);
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect6()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).{|CS0411:Cast|}();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestCastMethod_Incorrect7()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where({|CS0103:s|}).Cast<int>();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).[|Select|](a => (int)a);
                    }
                }
                """,
            FixedCode = """
                using System.Linq;
                
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.[|OfType|]<int>();
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect1()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.{|CS1061:Where1|}(a => a is int).Select(a => (int)a);
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect2()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => {|CS0103:b|} is int).Select(a => (int)a);
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect3()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is string).Select(a => (int)a);
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect4()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).{|CS1061:Select1|}(a => (int)a);
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect5()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).Select(a => (int){|CS0103:b|});
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect6()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).Select({|CS0103:a|});
                    }
                }
                """,
        }.RunAsync();
    }
 
    [Fact]
    public async Task TestSelectWithCast_Incorrect7()
    {
        await new VerifyCS.Test
        {
            TestCode = """
                using System.Linq;
 
                class C
                {
                    void M(object[] objs)
                    {
                        var y = objs.Where(a => a is int).Select<object, int>(a => (int)a);
                    }
                }
                """,
        }.RunAsync();
    }
}