Exemple #1
0
        public void PropertyOfIndexerShouldBe_ShouldNotThrowException()
        {
            const string assertion = "actual[0].Message.Should().Be(\"test\");";
            var          source    = GenerateCode.EnumerableCodeBlockAssertion(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #2
0
        public void DictionaryShouldContainPair_WhenPropertiesOfDifferentVariables_ShouldNotTrigger()
        {
            const string assertion = "actual.Should().ContainValue(pair.Value).And.ContainKey(otherPair.Key);";
            var          source    = GenerateCode.DictionaryAssertion(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
        public void AssignVoidLambdaToAction_TestAnalyzer()
        {
            const string statement = "Action action = () => {};";
            var          source    = GenerateCode.AsyncFunctionStatement(statement);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #4
0
        public void CountWithPredicate()
        {
            const string assertion = "actual.Count(d => d.Message.Contains(\"a\")).Should().Be(2);";
            var          source    = GenerateCode.EnumerableCodeBlockAssertion(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #5
0
        public void AssertionCallMultipleMethodWithTheSameNameAndArguments()
        {
            const string assertion = "actual.Should().Contain(d => d.Message.Contains(\"a\")).And.Contain(d => d.Message.Contains(\"c\"));";
            var          source    = GenerateCode.EnumerableCodeBlockAssertion(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #6
0
        public void CustomClass_ShouldNotTrigger_DictionaryAnalyzers()
        {
            const string source = @"
using System.Linq;
using FluentAssertions;
using FluentAssertions.Extensions;

namespace TestNamespace
{
    class MyDict<TKey, TValue>
    {
        public bool ContainsKey(TKey key) => false;
    }
    
    public class Program
    {
        public static void Main()
        {
            var dict = new MyDict<int, string>();
            dict.ContainsKey(0).Should().BeTrue();
        }
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #7
0
        public void StringShouldNotBeEmptyAndShouldNotBeNull_ShouldNotTrigger()
        {
            const string assertion = "actual.Should().NotBeEmpty().And.Should().NotBeNull();";
            var          source    = GenerateCode.StringAssertion(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #8
0
        public void Collection_SelectWhereShouldOnlyHaveUniqueItems_ShouldNotTrigger()
        {
            const string source = @"
using System.Linq;
using FluentAssertions;
using FluentAssertions.Extensions;

namespace TestNamespace
{
    public class Program
    {
        public static void Main()
        {
            var list = new[] { 1, 2, 3 };
    
            list.Select(e => e.ToString())
                .Where(e => e != string.Empty)
                .Should()
                .OnlyHaveUniqueItems();
        }
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #9
0
        public void PropertyOfElementAtShouldBe_ShouldNotTriggerDiagnostic()
        {
            const string assertion = "actual.ElementAt(0).Message.Should().Be(\"test\");";
            var          source    = GenerateCode.EnumerableCodeBlockAssertion(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #10
0
        public void NestedAssertions_ShouldNotTrigger()
        {
            const string declaration = "var nestedList = new List<List<int>>();";
            const string assertion   = "nestedList.Should().NotBeNull().And.ContainSingle().Which.Should().NotBeEmpty();";
            var          source      = GenerateCode.EnumerableCodeBlockAssertion(declaration + assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #11
0
        public void WritingToConsole_ShouldNotThrow()
        {
            const string source = @"
public class TestClass
{
    public static void Main()
    {
        System.Console.WriteLine();
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #12
0
        public void StaticWithNameof_ShouldNotThrow()
        {
            const string source = @"public class TestClass
{
    private static string StaticResult { get; set; }

    public static void Main()
    {
        StaticResult = nameof(Main);
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #13
0
        public void ExpressionBasedFunction_ShouldNotThrow()
        {
            const string source = @"
public class TestClass
{
    private SomeClass CreateSomeClass() => new SomeClass();
 
    public class SomeClass
    { }
    public static void Main() { }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
        public void AssignAsyncVoidLambdaToAction_TestAnalyzer(string assertion)
        {
            var source = GenerateCode.AsyncFunctionStatement(assertion);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult
            {
                Id        = AsyncVoidAnalyzer.DiagnosticId,
                Message   = AsyncVoidAnalyzer.Message,
                Locations = new DiagnosticResultLocation[]
                {
                    new DiagnosticResultLocation("Test0.cs", 10, 13)
                },
                Severity = DiagnosticSeverity.Warning
            });
        }
Exemple #15
0
        public void CollectionShouldNotContainProperty_WhenAssertionIsIdiomatic_ShouldNotTrigger()
        {
            const string source = @"
using FluentAssertions;
using FluentAssertions.Extensions;

public class TestClass
{
    public static void Main()
    {
        var list = new[] { string.Empty };
        list.Should().OnlyContain(e => e.Contains(string.Empty));
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
        private void VerifyCSharpDiagnostic <TDiagnosticAnalyzer>(string sourceAssertion) where TDiagnosticAnalyzer : Microsoft.CodeAnalysis.Diagnostics.DiagnosticAnalyzer, new()
        {
            var source = GenerateCode.NumericAssertion(sourceAssertion);

            var type         = typeof(TDiagnosticAnalyzer);
            var diagnosticId = (string)type.GetField("DiagnosticId").GetValue(null);
            var message      = (string)type.GetField("Message").GetValue(null);

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source, new DiagnosticResult
            {
                Id        = diagnosticId,
                Message   = message,
                Locations = new DiagnosticResultLocation[]
                {
                    new DiagnosticResultLocation("Test0.cs", 10, 13)
                },
                Severity = DiagnosticSeverity.Info
            });
        }
Exemple #17
0
        public void CollectionShouldHaveElementAt_ShouldNotThrow()
        {
            const string source = @"
using System.Linq;
using FluentAssertions;
using FluentAssertions.Extensions;

namespace TestNamespace
{
    public class Program
    {
        public static void Main()
        {
            var list = new[] { "" FOO "" };
            list[0].Trim().Should().Be(""FOO"");
        }
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #18
0
        public void DictionaryShouldHaveCount1_ShouldNotReport()
        {
            const string source = @"
using System.Linq;
using System.Collections.Generic;
using FluentAssertions;
using FluentAssertions.Extensions;

namespace TestNamespace
{
    public class Program
    {
        public static void Main()
        {
            var dict = new Dictionary<string, object>();
            dict.Should().HaveCount(1);
        }
    }
}";

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }
Exemple #19
0
 private void VerifyCSharpNoDiagnosticsCodeBlock(string assertion)
 {
     var source = GenerateCode.EnumerableCodeBlockAssertion(assertion);
     DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
 }
Exemple #20
0
        public void CollectionShouldHaveElementAt_ShouldIgnoreDictionaryTypes()
        {
            string source = GenerateCode.DictionaryAssertion("actual[\"key\"].Should().Be(expectedValue);");

            DiagnosticVerifier.VerifyCSharpDiagnosticUsingAllAnalyzers(source);
        }