public async void ForValueTypeNullAssert_RemovesAssertion() { const string original = @" using Xunit; public class Tests { [Fact] public void TestMethod() { int i = 1; Assert.NotNull(i); } }"; const string expected = @" using Xunit; public class Tests { [Fact] public void TestMethod() { int i = 1; } }"; var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, original); Assert.Equal(expected, actual); }
public async void ForPartialClassDeclarations_MakesSingleDeclarationPublic() { var source = @" partial class TestClass { [Xunit.Fact] public void TestMethod1() {} } partial class TestClass { [Xunit.Fact] public void TestMethod2() {} }"; var expected = @" public partial class TestClass { [Xunit.Fact] public void TestMethod1() {} } partial class TestClass { [Xunit.Fact] public void TestMethod2() {} }"; var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
public async void WithNoParameterlessConstructor_AddsConstructor_WithUsing() { var code = @"using System; using Xunit.Abstractions; public class MyTestCase : IXunitSerializable { public void Foo() { } }"; var expected = @"using System; using Xunit.Abstractions; public class MyTestCase : IXunitSerializable { [Obsolete(""Called by the de-serializer; should only be called by deriving classes for de-serialization purposes"")] public MyTestCase() { } public void Foo() { } }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, CompilationReporting.IgnoreErrors, code); Assert.Equal(expected, result, ignoreLineEndingDifferences: true); }
public async void ForPartialClassDeclarations_MakesSingleDeclarationPublic() { var source = @" [Xunit.CollectionDefinition(""MyCollection"")] partial class CollectionDefinitionClass { } partial class CollectionDefinitionClass { }"; var expected = @" [Xunit.CollectionDefinition(""MyCollection"")] public partial class CollectionDefinitionClass { } partial class CollectionDefinitionClass { }"; var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
public async void WithNoBaseClass_WithoutUsing_AddsBaseClass() { var code = "public class MyTestCase : Xunit.Abstractions.ITestCase { }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, code, CompilationReporting.IgnoreErrors, XunitReferences.PkgExecutionExtensibility); Assert.Equal("public class MyTestCase : Xunit.LongLivedMarshalByRefObject, Xunit.Abstractions.ITestCase { }", result); }
public async void WithBadBaseClass_WithUsing_ReplacesBaseClass() { var code = "using Xunit; using Xunit.Abstractions; public class Foo { } public class MyTestCase : Foo, ITestCase { }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, code, CompilationReporting.IgnoreErrors, XunitReferences.PkgExecutionExtensibility); Assert.Equal("using Xunit; using Xunit.Abstractions; public class Foo { } public class MyTestCase : LongLivedMarshalByRefObject, ITestCase { }", result); }
public async void WithNoBaseClass_WithUsing_AddsBaseClass() { var code = "using Xunit; using Xunit.Abstractions; public class MyTestCase : ITestCase { }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, CompilationReporting.IgnoreErrors, code); Assert.Equal("using Xunit; using Xunit.Abstractions; public class MyTestCase : LongLivedMarshalByRefObject, ITestCase { }", result); }
public async void WithBadBaseClass_ReplacesBaseClass() { var code = "public class Foo { } public class MyTestCase : Foo, Xunit.Abstractions.ITestCase { }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, CompilationReporting.IgnoreErrors, code); Assert.Equal("public class Foo { } public class MyTestCase : Xunit.LongLivedMarshalByRefObject, Xunit.Abstractions.ITestCase { }", result); }
public async void NamedArgumentsInCorrectPositionOnlySwapsArgumentValues() { var source = string.Format(Template, "Assert.Equal(expected: i, actual: 0)"); var expected = string.Format(Template, "Assert.Equal(expected: 0, actual: i)"); var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
public async void NamedArgumentsTakePossibleThirdParameterIntoAccount() { var source = string.Format(Template, "Assert.Equal(comparer: null, actual: 0, expected: i)"); var expected = string.Format(Template, "Assert.Equal(comparer: null, actual: i, expected: 0)"); var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
public async void SwapArguments() { var source = string.Format(Template, "Assert.Equal(i, 0)"); var expected = string.Format(Template, "Assert.Equal(0, i)"); var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
public async void MakesClassPublic(string nonPublicAccessModifier) { var source = $"{nonPublicAccessModifier} class TestClass {{ [Xunit.Fact] public void TestMethod() {{ }} }}"; var expected = "public class TestClass { [Xunit.Fact] public void TestMethod() { } }"; var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
public async void PartiallyNamedArgumentsInCorrectPositionOnlySwapsArgumentValues() { // C# 7.2 supports this new supported "non-trailing named arguments" var source = string.Format(Template, "Assert.Equal(expected: i, 0)"); var expected = string.Format(Template, "Assert.Equal(expected: 0, i)"); var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source, CompilationReporting.IgnoreErrors); Assert.Equal(expected, actual); }
public async void MakesClassPublic(string nonPublicAccessModifier) { var source = $@" [Xunit.CollectionDefinition(""MyCollection"")] { nonPublicAccessModifier} class CollectionDefinitionClass {{ }}"; var expected = @" [Xunit.CollectionDefinition(""MyCollection"")] public class CollectionDefinitionClass { }"; var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, source); Assert.Equal(expected, actual); }
// https://github.com/xunit/xunit/issues/1753 public async void ForAssertionWithTrivia_RemovesAssertionAndLeavesLeadingTriviaInPlace() { const string original = @" using System; using Xunit; namespace XUnitTestProject1 { public class UnitTest1 { [Fact] public void Test1() { int i = 1; // I am a comment which gets deleted by the quick fix // Assert Assert.NotNull(i); Assert.Null(null); } } }"; const string expected = @" using System; using Xunit; namespace XUnitTestProject1 { public class UnitTest1 { [Fact] public void Test1() { int i = 1; // I am a comment which gets deleted by the quick fix // Assert Assert.Null(null); } } }"; var actual = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, original); Assert.Equal(expected, actual); }
public async void WithNonPublicParameterlessConstructor_ChangesVisibility_WithoutUsing() { var code = @"public class MyTestCase : Xunit.Abstractions.IXunitSerializable { protected MyTestCase() { throw new System.DivideByZeroException(); } }"; var expected = @"public class MyTestCase : Xunit.Abstractions.IXunitSerializable { [System.Obsolete(""Called by the de-serializer; should only be called by deriving classes for de-serialization purposes"")] public MyTestCase() { throw new System.DivideByZeroException(); } }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, CompilationReporting.IgnoreErrors, code); Assert.Equal(expected, result, ignoreLineEndingDifferences: true); }
public async void PreservesExistingObsoleteAttribute() { var code = @"using obo = System.ObsoleteAttribute; public class MyTestCase : Xunit.Abstractions.IXunitSerializable { [obo(""This is my custom obsolete message"")] protected MyTestCase() { throw new System.DivideByZeroException(); } }"; var expected = @"using obo = System.ObsoleteAttribute; public class MyTestCase : Xunit.Abstractions.IXunitSerializable { [obo(""This is my custom obsolete message"")] public MyTestCase() { throw new System.DivideByZeroException(); } }"; var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, CompilationReporting.IgnoreErrors, code); Assert.Equal(expected, result, ignoreLineEndingDifferences: true); }
public async void MakesClassPublic(string visibility) { var result = await CodeAnalyzerHelper.GetFixedCodeAsync(analyzer, fixer, $"{visibility}class TestClass {{ [Xunit.Fact] public void TestMethod() {{ }} }}"); Assert.Equal("public class TestClass { [Xunit.Fact] public void TestMethod() { } }", result); }