public void ImmutableList(string call) { var testCode = @" namespace RoslynSandbox { using System.Collections.Immutable; class Foo { public Foo(ImmutableList<int> values) { ↓values.Add(1); } } }".AssertReplace("Add(1)", call); AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void TranslationGetOrCreateNameofPropertyFullyQualified() { var testCode = @" namespace RoslynSandbox { using Gu.Localization; public class Foo { public Foo() { var translation = Translation.GetOrCreate(Properties.Resources.ResourceManager, ↓nameof(Foo)); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, ResourcesCode, testCode); }
public void ResourceManagerGetObjectMissingNameof() { var testCode = @" namespace RoslynSandbox { using RoslynSandbox.Properties; public class Foo { public Foo() { var translate = Properties.Resources.ResourceManager.GetObject(↓Resources.Key); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, ResourcesCode, TranslateCode, testCode); }
public void TestAttributeAndParameter_Multiple() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [Test] public void Test↓(string text, int index, bool value) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void TranslateTranslationForStringLiteralFullyQualified() { var testCode = @" namespace RoslynSandbox { using Gu.Localization; public class Foo { public Foo() { var translate = Properties.Translate.TranslationFor(↓""Missing""); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, ResourcesCode, TranslateCode, testCode); }
public void DoubleToInt() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(↓1.0)] public void Test(int i) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void ArgumentIsNullAndParameterIsInt() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(↓null)] public void Test(int obj) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void ParamsConstructorSecondParameter(string call) { var code = @" namespace RoslynSandbox { using System; public class C { public C(int i, params int[] ints) { var foo = Activator.CreateInstance(typeof(C), 1, null); } } }".AssertReplace("Activator.CreateInstance(typeof(C), 1, null)", call); AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code); }
public void OneConstructorOneStringParameters(string call) { var code = @" namespace RoslynSandbox { using System; public class C { public C(string text1) { var foo = Activator.CreateInstance(typeof(C), ↓null); } } }".AssertReplace("Activator.CreateInstance(typeof(C), ↓null)", call); AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code); }
public void SetPropertyToSelfWithThis() { var testCode = @" namespace RoslynSandbox { public class Foo { public int A { get; private set; } private void Bar() { ↓this.A = this.A; } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void AnalyzeWhenTestMethodHasTaskReturnTypeAndExpectedResult() { var expectedDiagnostic = ExpectedDiagnostic.Create( AnalyzerIdentifiers.TestMethodAsyncExpectedResultAndNonGenricTaskReturnTypeUsage); var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@" public sealed class AnalyzeWhenTestMethodHasTaskReturnTypeAndExpectedResult { [↓TestCase(ExpectedResult = 1)] public Task TaskTestCaseWithExpectedResult() { return Task.Run(() => 1); } }"); var message = "Async test method must have Task<T> return type when a result is expected, but the return type was 'System.Threading.Tasks.Task'."; AnalyzerAssert.Diagnostics(analyzer, expectedDiagnostic.WithMessage(message), testCode); }
public void SetPropertyToSelf(string before, string after) { var testCode = @" namespace RoslynSandbox { public class Foo { public int A { get; private set; } private void Bar() { ↓this.A = this.A; } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Assigning made to same, did you mean to assign something else?"), testCode); }
public void UselessDefaultGuid() { var testCode = @" namespace RoslynSandbox { using System; public class A { public void F() { var g = ↓new System.Guid(); } } }"; AnalyzerAssert.Diagnostics <Analyzers.GU0070DefaultConstructedValueTypeWithNoUsefulDefault>(testCode); }
public void SimpleLambdaExpression() { var testCode = @" namespace RoslynSandbox { using System; internal static class Foo { internal static void Bar() { Func<int,object> f = x => ↓System.IO.File.OpenRead(null); } } }"; AnalyzerAssert.Diagnostics <IDISP005ReturntypeShouldIndicateIDisposable>(testCode); }
public void AnalyzeWhenIncompatibleTypesProvided_WithLambdaActualValue() { var testCode = TestUtility.WrapClassInNamespaceAndAddUsing(@" class A { } class B { } public class Tests { [Test] public void AnalyzeWhenIncompatibleTypesProvided_WithNegatedAssert() { var expected = new B(); Assert.That(() => new A(), Is.Not.EqualTo(↓expected)); } }"); AnalyzerAssert.Diagnostics(analyzer, expectedDiagnostic, testCode); }
public void DisposingParameter() { var testCode = @" namespace RoslynSandbox { using System; public class Foo { public void Bar(IDisposable meh) { ↓meh.Dispose(); } } }"; AnalyzerAssert.Diagnostics <IDISP007DontDisposeInjected>(testCode); }
public void SingleArgument() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(↓1)] public void Test(string str) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void PassingNullAsObj(string call) { var code = @" namespace RoslynSandbox { public class C { public C(int value) { var foo = typeof(C).GetConstructor(new[] { typeof(int) }).Invoke(null, new object[] { 1 }); } } }".AssertReplace("GetConstructor(new[] { typeof(int) }).Invoke(null, new object[] { 1 })", call); var message = "Use overload of Invoke without obj parameter."; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage(message), code); }
public void TestCaseAttribute_IfMultipleParametersAreWrong() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(1, ↓2)] public void Test(int i, string str) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void WhenNoAttribute() { var testCode = @" namespace RoslynSandbox { using System; public class ↓FooException : Exception { public FooException() : base(string.Empty) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void WrongArrayType() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; public class FooTests { [TestCase(↓new double[] {3, 5})] public void Test(int[] array) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void MissingPropertyGetAccessor() { var code = @" namespace RoslynSandbox { public sealed class C { public C() { var methodInfo = typeof(C).GetMethod(↓""get_Bar""); } public int Bar { set; } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code); }
public void TestCaseParams() { var testCode = @" namespace RoslynSandbox { using NUnit.Framework; class Foo { [TestCase(1)] public void Test↓(int i, int j, params int[] ints) { } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void UselessDefaultDateTime() { var testCode = @" namespace RoslynSandbox { using System; public class A { public void F() { var g = ↓new System.DateTime(); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, testCode); }
public void TranslateKeyNameofPropertyFullyQualified() { var testCode = @" namespace RoslynSandbox { using Gu.Localization; public class Foo { public Foo() { var translate = Properties.Translate.Key(↓nameof(Foo)); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, ResourcesCode, TranslateCode, testCode); }
public void OneErrorWithExpectedDiagnosticsWithMessageAndErrorIndicatedInCode() { var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓_value1; } }"; var expectedDiagnostic = ExpectedDiagnostic.CreateFromCodeWithErrorsIndicated("SA1309", "Field '_value1' must not begin with an underscore", code, out code); var analyzer = new FieldNameMustNotBeginWithUnderscore(); AnalyzerAssert.Diagnostics <FieldNameMustNotBeginWithUnderscore>(new[] { expectedDiagnostic }, code); AnalyzerAssert.Diagnostics(analyzer.GetType(), new[] { expectedDiagnostic }, code); AnalyzerAssert.Diagnostics(analyzer, new[] { expectedDiagnostic }, code); }
public void ResourceManagerGetObjectStringLiteralWithUsing() { var testCode = @" namespace RoslynSandbox { using RoslynSandbox.Properties; public class Foo { public Foo() { var translate = Resources.ResourceManager.GetObject(↓""Missing""); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, ResourcesCode, TranslateCode, testCode); }
public void WithExpectedDiagnosticWhenOneReportsError() { var code = @" namespace RoslynSandbox { class Foo { private readonly int ↓wrongName; public int WrongName { get; set; } } }"; var expectedDiagnostic = ExpectedDiagnostic.Create(FieldAndPropertyMustBeNamedFooAnalyzer.FieldDiagnosticId); AnalyzerAssert.Diagnostics <FieldAndPropertyMustBeNamedFooAnalyzer>(expectedDiagnostic, code); AnalyzerAssert.Diagnostics(new FieldAndPropertyMustBeNamedFooAnalyzer(), expectedDiagnostic, code); }
public void ResourceManagerGetObjectWithCultureNameofPropertyWithUsing() { var testCode = @" namespace RoslynSandbox { using RoslynSandbox.Properties; public class Foo { public Foo() { var translate = Resources.ResourceManager.GetObject(↓nameof(Resources), System.Globalization.CultureInfo.GetCultureInfo(""sv-SE"")); } } }"; AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, ResourcesCode, TranslateCode, testCode); }
public void Linq(string linq) { var testCode = @" namespace RoslynSandbox { using System.Linq; class Foo { void Bar() { var ints = new[] { 1, 2, 3 }; ↓ints.Select(x => x); } } }".AssertReplace("ints.Select(x => x);", linq); AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage("Don't ignore the return value."), testCode); }