invio / Invio.Immutable

C# Library used to ease immutable class creation and data management
MIT License
2 stars 0 forks source link

Unwrap `IEnumerable` objects in `ToString()` implementation #13

Closed carusology closed 6 years ago

carusology commented 7 years ago

Background

As a result of #1, the default comparison behavior for non-string IEnumerable properties is to compare the items in the enumerable rather than use referential equality. However, this behavior does not carry over to the default ToString() implementations. If we unwrap an enumerable to for checks of equality and hash code generation, we should certainly unwrap that enumerable in the default ToString() implementation.

Task

Update the ToString() implementation on ImmutableBase<TImmutable> to create array-looking string representations of properties treated as enumerables, like so:


public class MyImmutable : ImmutableBase<MyImmutable> {

    public int Number { get; }
    public IEnumerable<String> EnumerableProperty { get; }

    public MyImmutable(int number, IEnumerable<String> enumerableProperty) {
        this.Number = number;
        this.EnumerableProperty = enumerableProperty;
    }
}

public class MyImmutableTests {

    [Test]
    public void ToStringUnwraps() {

        // Arrange

        var immutable = new MyImmutable(5, new [] { "biz", "buzz" });

        // Act

        var output = immutable.ToString();

        // Assert

        Assert.Equal("{ Number: 5, EnumerableProperty: [ biz, buzz ] }", output);
    }

}