public async Task TestNoDiagnostic_NoNewLineAtEndOfFile() { await VerifyNoDiagnosticAsync(@" class C { }", options : Options.AddConfigOption(ConfigOptionKeys.NewLineAtEndOfFile, false)); }
public async Task Test_RemoveNewLine() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { bool x = false; do { M(); }[||] while (x); } } ", @" class C { void M() { bool x = false; do { M(); } while (x); } } ", options : Options.AddConfigOption(ConfigOptionKeys.NewLineBeforeWhileInDoStatement, false)); }
public async Task Test_PartialClass2() { await VerifyDiagnosticAndFixAsync(@" public class Foo { partial class [|C|] { } protected internal partial class C { } partial interface [|I|] { } protected internal partial interface I { } partial interface [|S|] { } protected internal partial interface S { } } ", @" public class Foo { protected internal partial class C { } protected internal partial class C { } protected internal partial interface I { } protected internal partial interface I { } protected internal partial interface S { } protected internal partial interface S { } } ", options : Options.AddConfigOption(ConfigOptionKeys.AccessibilityModifiers, ConfigOptionValues.AccessibilityModifiers_Explicit)); }
public async Task Test_Destructor() { await VerifyDiagnosticAndFixAsync(@" class C { ~C() [|{|] M(); } public void M() { } } ", @" class C { ~C() { M(); } public void M() { } } ", options : Options.AddConfigOption(ConfigOptionKeys.BlockBracesStyle, ConfigOptionValues.BlockBracesStyle_MultiLine)); }
public async Task TestNoDiagnostic_ExpressionTree() { await VerifyNoDiagnosticAsync(@" using System.Collections.Generic; using System.Linq; class C { void M() { var items = default(IQueryable<C>); var items2 = items .Where(f => f.P == null) .ToList(); var items3 = items .Where(f => f.P != null) .ToList(); } public object P { get; } } ", options : Options.AddConfigOption(ConfigOptionKeys.NullCheckStyle, ConfigOptionValues.NullCheckStyle_PatternMatching)); }
public async Task Test_AfterInsteadOfBefore() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { bool x = false, y = false, z = false; x = (x) [||]? y : z; } } ", @" class C { void M() { bool x = false, y = false, z = false; x = (x) ? y : z; } } ", options : Options.AddConfigOption(ConfigOptionKeys.ConditionalOperatorNewLine, "after")); }
public async Task Test_AnonymousType_AfterInsteadOfBefore() { await VerifyDiagnosticAndFixAsync(@" using System.Linq; using System.Collections.Generic; class C { void M() { List<string> list = null; var x = list.Select(f => new { X [|=|] """" }); } } ", @" using System.Linq; using System.Collections.Generic; class C { void M() { List<string> list = null; var x = list.Select(f => new { X = """" }); } } ", options : Options.AddConfigOption(ConfigOptionKeys.EqualsTokenNewLine, "after")); }
public async Task Test_LocalFunction_MultilineExpression() { await VerifyDiagnosticAndFixAsync(@" class C { string M(object x, object y) { return null; string LF() [|{ return M( x, y); }|] } } ", @" class C { string M(object x, object y) { return null; string LF() => M( x, y); } } ", options : Options.AddConfigOption(ConfigOptionKeys.BodyStyle, ConfigOptionValues.BodyStyle_Expression)); }
public async Task Test_PropertyWithGetter_MultilineExpression() { await VerifyDiagnosticAndFixAsync(@" class C { string P [|{ get { return M( null, null); } }|] string M(string x, string y) => null; } ", @" class C { string P => M( null, null); string M(string x, string y) => null; } ", options : Options.AddConfigOption(ConfigOptionKeys.BodyStyle, ConfigOptionValues.BodyStyle_Expression)); }
public async Task Test_PreferImplicitWhenTypeIsObvious_CollectionInitializer_Local() { await VerifyDiagnosticAndFixAsync(@" using System.Collections.Generic; class C { void M() { var items = new List<string>() { new [|string|](' ', 1) }; } } ", @" using System.Collections.Generic; class C { void M() { var items = new List<string>() { new(' ', 1) }; } } ", options : Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious)); }
public async Task Test_ConvertImplicitToExplicit_DoNotPreferVar_UsingStatement2() { await VerifyDiagnosticAndFixAsync(@" using System.IO; class C { void M() { using (StringReader s = [|new("""")|]) { } } } ", @" using System.IO; class C { void M() { using (StringReader s = new StringReader("""")) { } } } ", options : Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_Explicit) .AddConfigOption(ConfigOptionKeys.UseVarInsteadOfImplicitObjectCreation, false)); }
public async Task Test_PreferImplicitWhenTypeIsObvious_DoNotPreferVar_UsingStatement2() { await VerifyDiagnosticAndFixAsync(@" using System.IO; class C { void M() { using (StringReader s = new [|StringReader|]("""")) { } } } ", @" using System.IO; class C { void M() { using (StringReader s = new("""")) { } } } ", options : Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_ImplicitWhenTypeIsObvious) .AddConfigOption(ConfigOptionKeys.UseVarInsteadOfImplicitObjectCreation, false)); }
public async Task Test_PreferImplicit_CollectionInitializer_Field() { await VerifyDiagnosticAndFixAsync(@" using System.Collections.Generic; class C { private List<string> _items = new() { new [|string|](' ', 1) }; void M() { } } ", @" using System.Collections.Generic; class C { private List<string> _items = new() { new(' ', 1) }; void M() { } } ", options : Options.AddConfigOption(ConfigOptionKeys.ObjectCreationTypeStyle, ConfigOptionValues.ObjectCreationTypeStyle_Implicit)); }
public async Task Test_PreferVarInsteadOfImplicitObjectCreation_DoNotPreferVar_UsingStatement() { await VerifyDiagnosticAndFixAsync(@" using System.IO; class C { void M() { using ([|StringReader s = new("""")|]) { } } } ", @" using System.IO; class C { void M() { using (var s = new StringReader("""")) { } } } ", options : Options.AddConfigOption(ConfigOptionKeys.UseVarInsteadOfImplicitObjectCreation, true)); }
public async Task TestNoDiagnostic_NoEmptyLineBetweenClosingBraceAndSwitchSection2() { await VerifyNoDiagnosticAsync(@" class C { string M() { string s = null; switch (s) { case ""a"": { return """"; } case ""b"": return """"; } return null; } } ", options : Options.AddConfigOption(ConfigOptionKeys.BlankLineBetweenClosingBraceAndSwitchSection, true)); }
public async Task Test_IndexerWithGetterAndSetter() { await VerifyDiagnosticAndFixAsync(@" class C { string _f; string this[int index] { get [|{ return _f; }|] set [|{ _f = value; }|] } } ", @" class C { string _f; string this[int index] { get => _f; set => _f = value; } } ", options : Options.AddConfigOption(ConfigOptionKeys.BodyStyle, ConfigOptionValues.BodyStyle_Expression)); }
public async Task Test_Invocation_WrapAndIndent_NullConditionalOperator_NewLineAfter() { await VerifyDiagnosticAndFixAsync(@" class C { C M() { var x = new C(); return [|x.M() .M()?.M()|]; } } ", @" class C { C M() { var x = new C(); return x.M() .M()? .M(); } } ", options : Options.AddConfigOption(ConfigOptionKeys.NullConditionalOperatorNewLine, "after")); }
public async Task Test_IndexerWithGetterAndSetter_Throw() { await VerifyDiagnosticAndFixAsync(@" class C { string _f; string this[int index] { get [|{ throw new System.NotImplementedException(); }|] set [|{ throw new System.NotImplementedException(); }|] } } ", @" class C { string _f; string this[int index] { get => throw new System.NotImplementedException(); set => throw new System.NotImplementedException(); } } ", options : Options.AddConfigOption(ConfigOptionKeys.BodyStyle, ConfigOptionValues.BodyStyle_Expression)); }
public async Task Test_BeforeInsteadOfAfter_ColonToken() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { bool x = false, y = false, z = false; x = (x) ? y [||]: z; } } ", @" class C { void M() { bool x = false, y = false, z = false; x = (x) ? y : z; } } ", options : Options.AddConfigOption(ConfigOptionKeys.ConditionalOperatorNewLine, "before")); }
public async Task Test_StringEmpty() { await VerifyDiagnosticAndFixAsync(@" using System; class C { void M() { string s = null; s = [|string.Empty|]; s = [|String.Empty|]; s = [|System.String.Empty|]; s = [|global::System.String.Empty|]; } } ", @" using System; class C { void M() { string s = null; s = """"; s = """"; s = """"; s = """"; } } ", options : Options.AddConfigOption(ConfigOptionKeys.EmptyStringStyle, ConfigOptionValues.EmptyStringStyle_Literal)); }
public async Task Test_Assignment_AfterInsteadOfBefore() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { string s = null; s [|=|] null; } } ", @" class C { void M() { string s = null; s = null; } } ", options : Options.AddConfigOption(ConfigOptionKeys.EqualsTokenNewLine, "after")); }
public async Task Test_EmptyString() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { string s = null; s = [|""""|]; s = [|@""""|]; s = [|$""""|]; s = [|@$""""|]; s = [|$@""""|]; } } ", @" class C { void M() { string s = null; s = string.Empty; s = string.Empty; s = string.Empty; s = string.Empty; s = string.Empty; } } ", options : Options.AddConfigOption(ConfigOptionKeys.EmptyStringStyle, ConfigOptionValues.EmptyStringStyle_Field)); }
public async Task Test_NotIsNull() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { string s = null; if (!([|s is null|])) { } } } ", @" class C { void M() { string s = null; if (s != null) { } } } ", options : Options.AddConfigOption(ConfigOptionKeys.NullCheckStyle, ConfigOptionValues.NullCheckStyle_EqualityOperator)); }
public async Task Test_AfterInsteadOfBefore() { await VerifyDiagnosticAndFixAsync(@" using System.Linq; class C { void M() { string s = """" .Select(f => f.ToString()) .FirstOrDefault() [|?|].ToString(); } } ", @" using System.Linq; class C { void M() { string s = """" .Select(f => f.ToString()) .FirstOrDefault()? .ToString(); } } ", options : Options.AddConfigOption(ConfigOptionKeys.NullConditionalOperatorNewLine, "after")); }
public async Task Test_NotEqualsToNull() { await VerifyDiagnosticAndFixAsync(@" class C { void M() { string s = null; if ([|s != null|]) { } } } ", @" class C { void M() { string s = null; if (s is not null) { } } } ", options : Options.AddConfigOption(ConfigOptionKeys.NullCheckStyle, ConfigOptionValues.NullCheckStyle_PatternMatching)); }
public async Task Test_AfterInsteadOfBefore2() { await VerifyDiagnosticAndFixAsync(@" using System.Linq; class C { void M() { var x = ToString() .ToString() [|?|].Length; } } ", @" using System.Linq; class C { void M() { var x = ToString() .ToString()? .Length; } } ", options : Options.AddConfigOption(ConfigOptionKeys.NullConditionalOperatorNewLine, "after")); }
public async Task Test_PartialClass() { await VerifyDiagnosticAndFixAsync(@" namespace N { partial class [|C|] { partial void M(); } partial class [|C|] { partial void M() { } } } ", @" namespace N { internal partial class C { partial void M(); } internal partial class C { partial void M() { } } } ", options : Options.AddConfigOption(ConfigOptionKeys.AccessibilityModifiers, ConfigOptionValues.AccessibilityModifiers_Explicit)); }
public async Task Test_NoIndentation2() { await VerifyDiagnosticAndFixAsync(@" class C { string M(string x) { return M( [|"""" + """" + """"|]); } } ", @" class C { string M(string x) { return M( """" + """" + """"); } } ", options : Options.AddConfigOption(ConfigOptionKeys.BinaryOperatorNewLine, "after")); }
public async Task Test_PartialClass8() { await VerifyDiagnosticAndFixAsync(@" public class C { protected class C2 { } } public class C3 : C { new class [|C2|] { } } ", @" public class C { protected class C2 { } } public class C3 : C { new private class C2 { } } ", options : Options.AddConfigOption(ConfigOptionKeys.AccessibilityModifiers, ConfigOptionValues.AccessibilityModifiers_Explicit)); }
public async Task Test_RemoveEmptyLines_Property() { await VerifyDiagnosticAndFixAsync(@" class C { string _p; string P { get { return _p; } [||] set { _p = value; } } } ", @" class C { string _p; string P { get { return _p; } set { _p = value; } } } ", options : Options.AddConfigOption(ConfigOptionKeys.BlankLineBetweenSingleLineAccessors, false)); }