public static void ExplicitBaseParameterLess(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class CBase { public CBase() { var i = 1; } } public class C : CBase { public C() : base() { var j = 2; } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var node = syntaxTree.FindConstructorDeclaration("public C()"); using (var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None)) { Assert.AreEqual(expected, string.Join(", ", walker.Literals)); } }
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() { return 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)); }
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 }, 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)); } }
public static void AssignmentSetterWithGetterThis(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { private int value; public C() { this.Value = this.Value; } public int Value { get => 1; set => this.value = 2; } } }"); 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)); }
public static void StaticOtherType(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public static class C1 { public static int Value => 1; } public class C2 { public C2() { Equals(C1.Value, 2); int j = 3; } } }"); 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)); } }
public static void StaticBeforeExplicitParameterlessWhenNotDocumentOrder(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public C() { var i = 2; } static C() { var i = 1; } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var node = syntaxTree.FindTypeDeclaration("C"); using var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None); Assert.AreEqual(expected, string.Join(", ", walker.Literals)); }
public static void PropertyUnary(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { private int value; public C() { Value++; } public int Value { get => 1; set => value = 2; } } }"); 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)); } }
public static void ChainedThis(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public C() : this(1) { var j = 3; } public C(int _) { var i = 2; } } }"); 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)); }
public static void OverrideCallingBaseStartingFromBase(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class BaseClass { public void M1() => this.M2(); protected virtual void M2() { _ = 2; } } public class C : BaseClass { public void Start() => this.M1(); protected override void M2() { _ = 1; base.M2(); } } }"); 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)); }
public static void PropertyInitializerBeforeConstructor(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public sealed class C { public static readonly C Default = new C() { Value2 = 3 }; public C() { this.Value1 = 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 = 3 }"); using var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None); Assert.AreEqual(expected, string.Join(", ", walker.Literals)); }
public static void ArgumentBeforeInvocationStaticAndInstance(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public C() { var value = Meh(this.Value()); value = 3; } public int Value() => 1; private static int Meh(int i) { return 2 * i; } } }"); 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)); }
public static void InvocationVirtual(SearchScope scope, string expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { using System; public class C : IDisposable { public void Dispose() { var i = 1; Dispose(true); i = 3; } protected virtual void Dispose(bool disposing) { if (disposing) { var j = 2; } } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); var node = syntaxTree.FindMethodDeclaration("public void Dispose()"); using var walker = LiteralWalker.Borrow(node, scope, semanticModel, CancellationToken.None); Assert.AreEqual(expected, string.Join(", ", walker.Literals)); }
public static void ExplicitParameterless(SearchScope scope) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { public class C { public C() { var i = 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("1", walker.Literals.Single().ToString()); }
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)); }
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)); }
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)); }
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)); }
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 }, Settings.Default.MetadataReferences); 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)); }