fluentassertions.analyzers icon indicating copy to clipboard operation
fluentassertions.analyzers copied to clipboard

xunit: define codefixes for CollectionAsserts

Open Meir017 opened this issue 2 years ago • 2 comments

The asserts defined in https://github.com/xunit/assert.xunit/blob/main/CollectionAsserts.cs

  • [ ] All
  • [ ] AllAsync?
  • [ ] Collection
  • [ ] CollectionAsync?
  • [ ] Contains
  • [ ] Distinct
  • [ ] DoesNotContain
  • [ ] Empty
  • [ ] NotEmpty
  • [ ] Equal
  • [ ] NotEqual
  • [ ] Single

Meir017 avatar Jan 11 '22 05:01 Meir017

All:

public void All<T>(IEnumerable<T> collection, T expected)
{
    Assert.All(collection, x => Assert.Equal(expected, x));
    collection.Should().OnlyContain(x => x.Should().Be(expected));

    Assert.All(collection, x => Assert.NotEqual(expected, x));
    collection.Should().NotContain(x => x.Should().NotBe(expected));

    Assert.All(collection, x => Assert.Same(expected, x));
    collection.Should().OnlyContain(x => x.Should().BeSameAs(expected));

    Assert.All(collection, x => Assert.NotSame(expected, x));
    collection.Should().NotContain(x => x.Should().NotBeSameAs(expected));

    Assert.All(collection, i => Assert.IsType<int>(expected));
    collection.Should().AllBeOfType<int>();

    Assert.All(collection, i => Assert.IsAssignableFrom<int>(expected));
    collection.Should().AllBeAssignableTo<int>();
}

Meir017 avatar Jan 11 '22 06:01 Meir017

Collection:

public void Collection<T>(IEnumerable<T> collection, params Action<T>[] elementInspectors)
{
    T expected = default;
    Assert.Collection(collection,
        x => Assert.Equal(x, expected),
        x => Assert.NotEqual(x, expected),
        x => Assert.Same(x, expected),
        x => Assert.NotSame(x, expected),
        x => Assert.IsType<int>(expected),
        x => Assert.IsAssignableFrom<int>(x)
    );
    collection.Should().Satisfy(
        x => x.Equals(expected),
        x => !x.Equals(expected),
        x => ReferenceEquals(expected, x),
        x => !ReferenceEquals(expected, x),
        x => x.GetType() == typeof(int),
        x => x.GetType().IsAssignableFrom(typeof(int))
    );
}

Meir017 avatar Jan 11 '22 06:01 Meir017