public static void FiguresOutFromOtherDocument(string declaration, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(new[] { @" namespace N { class C1 { private int _f; } }".AssertReplace("private int _f", declaration), @" namespace N { class C2 { } }", }); foreach (var document in sln.Projects.Single().Documents) { var editor = Microsoft.CodeAnalysis.Editing.DocumentEditor.CreateAsync(document).Result; Assert.AreEqual(expected, CodeStyle.UnderscoreFields(editor)); } }
public static void FiguresOutFromOtherTree(string declaration, CodeStyleResult expected) { var c1 = CSharpSyntaxTree.ParseText(@" namespace N { class C1 { private int _f; } }".AssertReplace("private int _f", declaration)); var c2 = CSharpSyntaxTree.ParseText(@" namespace N { class C2 { } }"); var compilation = CSharpCompilation.Create("test", new[] { c1, c2 }, MetadataReferences.FromAttributes()); Assert.AreEqual(2, compilation.SyntaxTrees.Length); foreach (var tree in compilation.SyntaxTrees) { var semanticModel = compilation.GetSemanticModel(tree); Assert.AreEqual(expected, CodeStyle.UnderscoreFields(semanticModel)); } }
public static async Task FiguresOutFromOtherClass(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(new[] { @" namespace N { class C1 { private int f; C1() { this.f = 1; } } }".AssertReplace("this.f = 1", expression), @" namespace N { class C2 { } }", }); foreach (var document in sln.Projects.Single().Documents) { Assert.AreEqual(expected, await document.QualifyFieldAccessAsync(CancellationToken.None).ConfigureAwait(false)); } }
public static async Task FiguresOutFromOtherClass(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(new[] { @" namespace N { class C1 { C1() { this.P = 1; } public int P { get; } } }".AssertReplace("this.P = 1", expression), @" namespace N { class C2 { } }", }); foreach (var document in sln.Projects.Single().Documents) { var editor = await Microsoft.CodeAnalysis.Editing.DocumentEditor.CreateAsync(document).ConfigureAwait(false); Assert.AreEqual(expected, await editor.QualifyPropertyAccessAsync(CancellationToken.None).ConfigureAwait(false)); } }
public static async Task FiguresOutFromOtherDocument(string declaration, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution( new[] { @" namespace N { class C1 { private int _f; } }".AssertReplace("private int _f", declaration), @" namespace N { class C2 { } }", }); foreach (var document in sln.Projects.Single().Documents) { Assert.AreEqual(expected, await CodeStyle.UnderscoreFieldsAsync(document, CancellationToken.None).ConfigureAwait(false)); } }
public static async Task CallInSetter(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { private string f; public string Name { get => this.f; set { this.f = value; ↓this.M1(); } } public void M1() { } public static void M2() { } } }".AssertReplace("this.M1()", expression)); Assert.AreEqual(expected, await editor.QualifyMethodAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task CallInSetter(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(@" namespace N { class C { private string f; public string Name { get => this.f; set { this.f = value; ↓this.M1(); } } public void M1() { } public static void M2() { } } }".AssertReplace("this.M1()", expression)); var document = sln.Projects.Single().Documents.Single(); Assert.AreEqual(expected, await document.QualifyMethodAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task UsedInNameof(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { public string M() => nameof(this.M); } }".AssertReplace("this.M", expression)); Assert.AreEqual(expected, await editor.QualifyMethodAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task ReturningInMethodExpressionBody(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { private int f; public int M() => this.f; } }".AssertReplace("this.f", expression)); Assert.AreEqual(expected, await editor.QualifyFieldAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task Arrow(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { public int P { get; } public int M() => this.P; } }".AssertReplace("this.P", expression)); Assert.AreEqual(expected, await editor.QualifyPropertyAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static void WhenField(string declaration, CodeStyleResult expected) { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { class C { private int _f; } }".AssertReplace("private int _f", declaration)); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.AreEqual(expected, CodeStyle.UnderscoreFields(semanticModel)); }
public static async Task ExpressionBody(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { public int P => this.M1(); public int M1() => 1; public static int M2() => 2; } }".AssertReplace("this.M1()", expression)); Assert.AreEqual(expected, await editor.QualifyMethodAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task Arrow(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(@" namespace N { class C { public int P { get; } public int M() => this.P; } }".AssertReplace("this.P", expression)); var document = sln.Projects.Single().Documents.Single(); Assert.AreEqual(expected, await document.QualifyPropertyAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task ReturningInMethodExpressionBody(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(@" namespace N { class C { private int f; public int M() => this.f; } }".AssertReplace("this.f", expression)); var document = sln.Projects.Single().Documents.Single(); Assert.AreEqual(expected, await document.QualifyFieldAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task UsedInNameofShadowedInProperty(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { private readonly int value; public string P { set => _ = nameof(this.value); } } }".AssertReplace("this.value", expression)); Assert.AreEqual(expected, await editor.QualifyFieldAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task AssigningInCtor(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { class C { private int _f1; private int f2; public int F3; C() { _f1 = 1; } } }".AssertReplace("_f1 = 1", expression)); Assert.AreEqual(expected, await editor.QualifyFieldAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task RaisingInMethod(string expression, CodeStyleResult expected) { var editor = CreateDocumentEditor(@" namespace N { using System; class C { public event Action E; void M() { E?.Invoke(); } } }".AssertReplace("E?.Invoke();", expression)); Assert.AreEqual(expected, await editor.QualifyEventAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task Assignment(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(@" namespace N { class C { C() { _ = this.M1(); } public int M1() => 1; public static int M2() => 2; } }".AssertReplace("this.M1()", expression)); var document = sln.Projects.Single().Documents.Single(); Assert.AreEqual(expected, await document.QualifyMethodAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task RaisingInMethod(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(@" namespace N { using System; class C { public event Action E; void M() { E?.Invoke(); } } }".AssertReplace("E?.Invoke();", expression)); var document = sln.Projects.Single().Documents.Single(); Assert.AreEqual(expected, await document.QualifyEventAccessAsync(CancellationToken.None).ConfigureAwait(false)); }
public static async Task AssigningInCtor(string expression, CodeStyleResult expected) { var sln = CodeFactory.CreateSolution(@" namespace N { class C { private int _f1; private int f2; public int F3; C() { _f1 = 1; } } }".AssertReplace("_f1 = 1", expression)); var document = sln.Projects.Single().Documents.Single(); Assert.AreEqual(expected, await document.QualifyFieldAccessAsync(CancellationToken.None).ConfigureAwait(false)); }