예제 #1
0
        public async Task TestUserDefinedWhere()
        {
            var source = @"
using System;
using System.Linq;
using System.Collections.Generic;
namespace demo
{
    class Test
    {
        public class TestClass4
        {
            private string test;
            public TestClass4() => test = ""hello"";

            public TestClass4 Where(Func<string, bool> input)
            {
                return this;
            }

            public string Single()
            {
                return test;
            }
        }
        static void Main()
        {
            TestClass4 Test1 = new TestClass4();
            TestClass4 test = Test1.Where(y => true);
        }
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(source);
        }
예제 #2
0
        public async Task TestExpressionTreeInput()
        {
            var source = @"
using System;
using System.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;

class Test
{
    void Main()
    {
        string[] places = { ""Beach"", ""Pool"", ""Store"", ""House"",
                   ""Car"", ""Salon"", ""Mall"", ""Mountain""};

        IQueryable<String> queryableData = places.AsQueryable<string>();
        ParameterExpression pe = Expression.Parameter(typeof(string), ""place"");

        Expression left = Expression.Call(pe, typeof(string).GetMethod(""ToLower"", System.Type.EmptyTypes));
        Expression right = Expression.Constant(""coho winery"");
        Expression e1 = Expression.Equal(left, right);

        left = Expression.Property(pe, typeof(string).GetProperty(""Length""));
        right = Expression.Constant(16, typeof(int));
        Expression e2 = Expression.GreaterThan(left, right);

        Expression predicateBody = Expression.OrElse(e1, e2);
        Expression<Func<int, bool>> lambda1 = num => num < 5;

        string result = queryableData.Where(Expression.Lambda<Func<string, bool>>(predicateBody, new ParameterExpression[] { pe })).First();
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(source);
        }
        public async Task TestNullable_NoDiagnostics()
        {
            var code = @"
public class C
{
    public bool M1(bool? x)
    {
        return x == true;
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(code);
        }
        public async Task TestSimpleCaseForNotEqualsTrue_NoDiagnostics()
        {
            var code = @"
public class C
{
    public bool M1(bool x)
    {
        return x != true;
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(code);
        }
예제 #5
0
        public async Task TestNullableValueTypes_DoesntCrash()
        {
            var code = @"
public class C
{
    public bool M1(int? x)
    {
        return x == null;
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(code);
        }
        public async Task TestMissingWithOnlySetter()
        {
            await VerifyCS.VerifyAnalyzerAsync(@"
class C
{
    void Bar() { }

    int Goo
    {
        set => Bar();
    }
}");
        }
        public async Task TestWhenConstant_NoDiagnostics()
        {
            var code = @"
public class C
{
    public const bool MyTrueConstant = true;

    public bool M1(bool x)
    {
        return x == MyTrueConstant;
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(code);
        }
        public async Task TestOverloadedOperator_NoDiagnostics()
        {
            var code = @"
public class C
{
    public static bool operator ==(C a, bool b) => false;
    public static bool operator !=(C a, bool b) => true;

    public bool M1(C x)
    {
        return x == true;
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(code);
        }
예제 #9
0
        public async Task TestUnsupportedFunction()
        {
            var source = @"
using System;
using System.Linq;
using System.Collections.Generic;
namespace demo
{
    class Test
    {
        static List<int> test1 = new List<int> { 3, 12, 4, 6, 20 };
        int test2 = test1.Where(x => x > 0).Count();
    }
}";
            await VerifyCS.VerifyAnalyzerAsync(source);
        }
예제 #10
0
        public async Task TestArgumentsInSecondCall(string methodName)
        {
            var source = $@"
using System;
using System.Linq;
using System.Collections.Generic;

class Test
{{
    static void M()
    {{
        IEnumerable<string> test1 = new List<string>{{ ""hello"", ""world"", ""!"" }};
        var test2 = test1.Where(x => x == ""!"").{methodName}(x => x.Length == 1);
    }}
}}";
            await VerifyCS.VerifyAnalyzerAsync(source);
        }
예제 #11
0
        public async Task TestQueryableIsNotConsidered(string methodName)
        {
            var source = $@"
using System;
using System.Linq;
using System.Collections.Generic;
namespace demo
{{
    class Test
    {{
        void M()
        {{
            List<int> testvar1 = new List<int> {{ 1, 2, 3, 4, 5, 6, 7, 8 }};
            IQueryable<int> testvar2 = testvar1.AsQueryable().Where(x => x % 2 == 0);
            var output = testvar2.Where(x => x == 4).{methodName}();
        }}
    }}
}}";
            await VerifyCS.VerifyAnalyzerAsync(source);
        }
예제 #12
0
        public static async Task TestWhereWithIndexMethodTypes(
            [CombinatorialValues("(x, index) => x==index", "(x, index) => { return x==index; }")]
            string lambda,
            [CombinatorialValues(
                 "First",
                 "Last",
                 "Single",
                 "Any",
                 "Count",
                 "SingleOrDefault",
                 "FirstOrDefault",
                 "LastOrDefault"
                 )]
            string methodName
            )
        {
            var testCode =
                $@"
using System;
using System.Linq;
using System.Collections.Generic;
 
class Test
{{
    static void Main()
    {{
        static IEnumerable<int> Data()
        {{
            yield return 1;
            yield return 2;
        }}

        var test = Data().Where({lambda}).{methodName}();
    }}
}}";
            await VerifyCS.VerifyAnalyzerAsync(testCode);
        }