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 void ChecksContainingClassFirst() { var c1 = CSharpSyntaxTree.ParseText(@" namespace N { class C1 { private int _f; } }"); var c2 = CSharpSyntaxTree.ParseText(@" namespace N { class C1 { private int value; } }"); var compilation = CSharpCompilation.Create("test", new[] { c1, c2 }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(compilation.SyntaxTrees[0]); Assert.AreEqual(CodeStyleResult.Yes, CodeStyle.UnderscoreFields(semanticModel)); semanticModel = compilation.GetSemanticModel(compilation.SyntaxTrees[1]); Assert.AreEqual(CodeStyleResult.No, CodeStyle.UnderscoreFields(semanticModel)); }
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 void FiguresOutFromOtherClass() { var fooCode = CSharpSyntaxTree.ParseText(@" namespace RoslynSandbox { class Foo { private int _value; public int Bar() => _value = 1; } }"); var barCode = CSharpSyntaxTree.ParseText(@" namespace RoslynSandbox { class Bar { } }"); var compilation = CSharpCompilation.Create("test", new[] { fooCode, barCode }, MetadataReferences.FromAttributes()); Assert.AreEqual(2, compilation.SyntaxTrees.Length); foreach (var tree in compilation.SyntaxTrees) { var semanticModel = compilation.GetSemanticModel(tree); Assert.AreEqual(true, CodeStyle.UnderscoreFields(semanticModel)); } }
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 void WhenFieldIsNotNamedWithUnderscore() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace RoslynSandbox { class Foo { int value; public int Bar() => value = 1; } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.AreEqual(false, CodeStyle.UnderscoreFields(semanticModel)); }
public void WhenUsingThis() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace RoslynSandbox { class Foo { public int Value { get; private set; } public int Bar() => this.value = 1; } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.AreEqual(false, CodeStyle.UnderscoreFields(semanticModel)); }
public static void Repro() { var editor = CreateDocumentEditor(@" namespace Vanguard_MVVM.ViewModels { using System; using System.Collections.Generic; using System.ComponentModel; public sealed class MainWindowViewModel : INotifyPropertyChanged { IChildDataContext _childDataContext; readonly string _title; MainWindowViewModel() { _title = ""MVVM Attempt""; } public IChildDataContext ChildDataContext { get { return _childDataContext; } private set { if (Equals(value, _childDataContext)) return; ↓_childDataContext = value; NotifyPropertyChanged(nameof(ChildDataContext)); } } public static MainWindowViewModel Instance { get; } = new MainWindowViewModel(); public string Title => ChildDataContext?.Title == null ? _title : string.Concat(_title, "" - "", ChildDataContext?.Title); public event PropertyChangedEventHandler PropertyChanged; void NotifyPropertyChanged(string propertyName) => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } }"); Assert.AreEqual(CodeStyleResult.Yes, CodeStyle.UnderscoreFields(editor)); }
public static void WhenUnknown() { var sln = CodeFactory.CreateSolution(@" namespace N { internal class C { internal C(int i, double d) { } internal void M() { } } }"); var editor = Microsoft.CodeAnalysis.Editing.DocumentEditor.CreateAsync(sln.Projects.Single().Documents.Single()).Result; Assert.AreEqual(CodeStyleResult.NotFound, CodeStyle.UnderscoreFields(editor)); }
public void WhenPropertyIsAssignedWithoutThis() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace RoslynSandbox { class Foo { public Foo(int bar) { Bar = bar; } public int Bar { get; set; } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.AreEqual(true, CodeStyle.UnderscoreFields(semanticModel)); }
public void DefaultsToStyleCop() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace RoslynSandbox { internal class Foo { internal Foo() { } internal Foo(string text) : this() { } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }, MetadataReferences.FromAttributes()); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.AreEqual(false, CodeStyle.UnderscoreFields(semanticModel)); }
public static void WhenUnknown() { var syntaxTree = CSharpSyntaxTree.ParseText(@" namespace N { class C { C(int p) { this.P = i; } void M() { } public int P { get; } } }"); var compilation = CSharpCompilation.Create("test", new[] { syntaxTree }); var semanticModel = compilation.GetSemanticModel(syntaxTree); Assert.AreEqual(CodeStyleResult.NotFound, CodeStyle.UnderscoreFields(semanticModel)); }