Пример #1
0
        public static void HasLocalSwitch(string location, string name, bool expected)
        {
            var code       = @"
namespace N
{
    class C
    {
        int M(object o)
        {
            var x = 1;
            switch (o)
            {
                case int y:
                    return 2;
            }

            return 3;
        }
    }
}";
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(location);

            Assert.AreEqual(expected, Scope.HasLocal(node, name));
        }
Пример #2
0
        public static void HasLocalLocalOut(string location, string name, string text, bool expected)
        {
            var code       = @"
namespace N
{
    class C
    {
        int M(string text)
        {
            var x = 1;

            if (int.TryParse(out var y))
            {
                return 2;
            }

            return 3;
        }
    }
}".AssertReplace("out var y", text);
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(location);

            Assert.AreEqual(expected, Scope.HasLocal(node, name));
        }
Пример #3
0
        public static void HasLocalLocalNested(string location, string name, bool expected)
        {
            var code       = @"
namespace N
{
    class C
    {
        int M(bool b)
        {
            var x = b;
            return 1;

            if (x)
            {
                var y = b;
                return 2;
            }
            else
            {
                var z = b;
                return 3;
            }
        }
    }
}";
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(location);

            Assert.AreEqual(expected, Scope.HasLocal(node, name));
        }
Пример #4
0
        public static void HasParameter(string expression, string name, bool expected)
        {
            var code       = @"
namespace N
{
    using System;

    [Obsolete(""0"")]
    class C
    {
        private readonly int f = 1;

        public C()
            :this(2)
        {
        }

        public C(int x)
        {
            this.f = 3;
        }

        public event Action E
        {
            add { _ = 4; }
            remove { _ = 5; }
        }

        public int P
        {
            get => 6;
            set => _ = 7;
        }

        int M(int x) => 8;

        int M(double x)
        {
            return 9;
        }

        static int M(string x)
        {
            return 10;
        }
    }
}";
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(expression);

            Assert.AreEqual(expected, Scope.HasParameter(node, name));
        }
Пример #5
0
        public static void HasParameterLambda(string location, string name, string lambda, bool expected)
        {
            var code       = @"
namespace N
{
    class C
    {
        int M(string x)
        {
            x = 1;
            Func<int, int> f = y => 2;
            return 3;
        }
    }
}".AssertReplace("Func<int, int> f = y => 2", lambda);
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(location);

            Assert.AreEqual(expected, Scope.HasParameter(node, name));
        }
Пример #6
0
        public static void HasParameterLocalFunction(string location, string name, bool expected)
        {
            var code       = @"
namespace N
{
    class C
    {
        int M(string x)
        {
            return 1;

            int M(long y)
            {
                return 2;
            }
        }
    }
}";
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(location);

            Assert.AreEqual(expected, Scope.HasParameter(node, name));
        }
Пример #7
0
        public static void HasLocal(string expression, string name, bool expected)
        {
            var code       = @"
namespace N
{
    using System;

    [Obsolete(""0"")]
    public class C
    {
        private readonly int f = 1;

        public C()
            :this(2)
        {
        }

        public C()
        {
            var x = 'a';
            this.f = 3;
        }

        public event Action E
        {
            add { _ = 4; }
            remove { _ = 5; }
        }

        public int P
        {
            get => 6;
            set => _ = 7;
        }

        int M1() => 8;

        int M2()
        {
            var x = 'a';
            return 9;
        }

        static int M3(bool b)
        {
            var x = 'a';
            return 10;

            if (text is null)
            {
                var y = 'a';
                return 11;
            }

            int M(long _)
            {
                var z = 'a';
                return 12;
            }
        }
    }
}";
            var syntaxTree = CSharpSyntaxTree.ParseText(code);
            var node       = syntaxTree.Find <SyntaxNode>(expression);

            Assert.AreEqual(expected, Scope.HasLocal(node, name));
        }