GithubHelp home page GithubHelp logo

Comments (10)

jnyrup avatar jnyrup commented on June 8, 2024 1

I agree about not going down the TwoDimensionalArrayAssertions path.

Since Array.GetValue() has overloads for up to three indices before deferring to params int[] indices or params long[] indices, Let's as well go with three Shoulds for T[,], T[,,] and T[,,,].
If users needs more, they can always add those overloads themselves as they just resolve ambiguity and forward to MultiDimensionArrayAssertions.

Since we haven't had requests for multi-dimensional arrays, I don't think we should duplicate all functionality from GenericCollectionAssertions

I suggest starting out soft with these:

  • BeEmpty()
  • NotBeEmpty()
  • HaveCount(int expected)
  • NotHaveCount(int unexpected)

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 8, 2024

You can already use BeEquivalenTo for that. It has support for multi-dimensional arrays.

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 8, 2024

But what kind of assertion would you suggest?

from fluentassertions.

Meir017 avatar Meir017 commented on June 8, 2024

for asserting the Length and GetLength functionality

maybe something like this

var array = new string[2,3] { { "a", "b", "c" }, { "d", "e", "f" } };
array.Should().BeEmpty();
array.Should().NotBeEmpty();

array.Should().HaveElementAt(0, 0, "a");
array.Should().HaveElementAt(1, 2, "f");

array.Should().HaveTotalLength(6);
array.Should().HaveDimensionLength(0, 2);
array.Should().HaveDimensionLength(1, 3);

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 8, 2024

I like that. What do you think @jnyrup ?

from fluentassertions.

Meir017 avatar Meir017 commented on June 8, 2024

I'm wondering if for each different array dimension there should be a dedicated assertions class, this will allow to have strongly-typed assertions and not just use the System.Array type.

this would mean that instead of HaveDimensionLength there could be a dedicated method for each dimension length

something like this:

public static class Extensions
{
    public static TwoDimensionalArrayAssertions<T> Should<T>(this T[,] actual) => new(actual);
    public static ThreeDimensionalArrayAssertions<T> Should<T>(this T[,,] actual) => new(actual);
    public static FourDimensionalArrayAssertions<T> Should<T>(this T[,,,] actual) => new(actual);
}

public class TwoDimensionalArrayAssertions<T>
{
    public TwoDimensionalArrayAssertions(T[,] array) { }
}
public class ThreeDimensionalArrayAssertions<T>
{
    public ThreeDimensionalArrayAssertions(T[,,] array) { }
}
public class FourDimensionalArrayAssertions<T>
{
    public FourDimensionalArrayAssertions(T[,,,] array) { }
}

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 8, 2024

That's possible, but this is the first time in 13 years that somebody suggests this feature, so your original proposal should be good enough.

from fluentassertions.

Meir017 avatar Meir017 commented on June 8, 2024

I think that a generic assertion extension will not work here

public static ArrayAssertions Should(this Array array) => new ArrayAssertions(array);

I think that the assertion extensions need to be for each dimension separately, otherwise when using a single-dimension array the extension method used will be the new one instead of the Collections.GenericCollectionAssertions<T>

so we would need something like this -

public static class Ex
{
    public static MultiDimensionArrayAssertions Should<T>(this T[,] array) => new MultiDimensionArrayAssertions(array);
    public static MultiDimensionArrayAssertions Should<T>(this T[,,] array) => new MultiDimensionArrayAssertions(array);
    public static MultiDimensionArrayAssertions Should<T>(this T[,,,] array) => new MultiDimensionArrayAssertions(array);
    // more dimensions ...
}

public class MultiDimensionArrayAssertions // what should this extend from?
{
    public MultiDimensionArrayAssertions(Array array) { }
}

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 8, 2024

Yeah, you're right. You can define an extension method on Array, but for single-dimension arrays, it'll result in ambiguous calls.

from fluentassertions.

dennisdoomen avatar dennisdoomen commented on June 8, 2024

Let's as well go with three Shoulds for T[,], T[,,] and T[,,,].

That settles it then

from fluentassertions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.