Beispiel #1
0
        public static void LocalDeclarationWithCastExpressionBody(SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class C
    {
        public C()
        {
            var value = (double)this.Value;
            value = 2;
        }

        public int Value => 1;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, Settings.Default.MetadataReferences);
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindConstructorDeclaration("C");

            using var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None);
            Assert.AreEqual(expected, string.Join(", ", walker.Literals));
        }
Beispiel #2
0
        public static void Binary(string expression, SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class C
    {
        public object Value1 => 1;

        public object Value2 => 2;

        public object M() => Value1 ?? Value2;
    }
}".AssertReplace("Value1 ?? Value2", expression));
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindMethodDeclaration("M");

            using (var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None))
            {
                Assert.AreEqual(expected, string.Join(", ", walker.Literals));
            }
        }
Beispiel #3
0
        public static void Static(SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class C
    {
        public C()
        {
            Equals(Value, 2);
            int j = 3;
        }

        public static int Value => 1;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindConstructorDeclaration("C");

            using var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None);
            Assert.AreEqual(expected, string.Join(", ", walker.Literals));
        }
Beispiel #4
0
        public static void PropertyInitializerBeforeDefaultCtorObjectInitializer(SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public sealed class C
    {
        public static readonly C Default = new C { Value2 = 2 };

        public int Value1 { get; set; } = 1;

        public int Value2 { get; set; }
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindExpression("new C { Value2 = 2 }");

            using (var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None))
            {
                Assert.AreEqual(expected, string.Join(", ", walker.Literals));
            }
        }
Beispiel #5
0
        public static void IgnoreNameOfMethod(SearchScope scope)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class C
    {
        public C()
        {
            var text = nameof(this.Value());
        }

        public int Value() => 1;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindConstructorDeclaration("C");

            using (var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None))
            {
                Assert.AreEqual(string.Empty, string.Join(", ", walker.Literals));
            }
        }
Beispiel #6
0
        public static void GetOverrideExplicitBase(SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class BaseClass
    {
        protected virtual int P => 2;
    }

    public class C : BaseClass
    {
        public int Start() => base.P;

        protected override int P => 1 + base.P;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindMethodDeclaration("Start");

            using var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None);
            Assert.AreEqual(expected, string.Join(", ", walker.Literals));
        }
Beispiel #7
0
        public static void FieldInitializerBeforeConstructorWhenNotDocumentOrder(SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class C
    {
        public C()
        {
            this.value = 2;
        }

        private readonly int value = 1;
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindClassDeclaration("C");

            using (var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None))
            {
                Assert.AreEqual(expected, string.Join(", ", walker.Literals));
            }
        }
Beispiel #8
0
        public static void WalkOverridden(SearchScope scope, string expected)
        {
            var syntaxTree    = CSharpSyntaxTree.ParseText(@"
namespace N
{
    public class CBase
    {
        protected virtual int M() => 2;
    }

    public sealed class C : CBase
    {
        protected override int M() => 1 * base.M();
    }
}");
            var compilation   = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes());
            var semanticModel = compilation.GetSemanticModel(syntaxTree);
            var node          = syntaxTree.FindMethodDeclaration("protected override int M()");

            using (var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None))
            {
                Assert.AreEqual(expected, string.Join(", ", walker.Literals));
            }
        }