Giter VIP home page Giter VIP logo

Comments (4)

knaeckeKami avatar knaeckeKami commented on August 28, 2024 3

expect does not use ==, but has a more sophisticated matching method.
See https://github.com/dart-lang/matcher/blob/3a80b989c5c1071d32af11ea9d3fae55807a14c2/lib/src/equals_matcher.dart#L18 ,
and https://github.com/dart-lang/matcher/blob/3a80b989c5c1071d32af11ea9d3fae55807a14c2/lib/src/equals_matcher.dart#L107 in particular.

So, Iterables are compared deeply by expect, even if == would return false on them.

But you don't give an Iterable to expect, you give a Right([1, 2, 3, 4, 5]) to expect.

So expect eventually compares the Either instances by ==.
And Either compares the wrapped value by ==, which will return false, because List don't implement ==, and since these are two instances of Lists, == will return false.

@SandroMaglione suggested using Lists that do implement ==, for example fast_immutable_collections.

Your would use it like this: Right(IList([1, 2, 3, 4, 5]));
I would recommend doing this anyway since mutable collections don't play well with functional programming.

from fpdart.

SandroMaglione avatar SandroMaglione commented on August 28, 2024

Hi @Babwenbiber

The reason the test fails is that natively dart compares lists by identity. This means that dart will not compare the elements inside the list but it will check that the two lists are the same instance (which is not the case).

There are many solutions to this problem. Generally, you should override the == and hashCode methods (like fpdart does for Either).

Another solution would be to use Equatable package.

For lists specifically, you must either implement a custom equality method (that compares by equality and not by identity) or relay on some package that does this already:

Does this answer solve your doubts?

from fpdart.

Babwenbiber avatar Babwenbiber commented on August 28, 2024

Thanks a lot for the detailed answer.

I am still quite unsure about some points.

I have read many times, that == does compare identity, but expect([1, 2, 3, 4, 5], [1, 2, 3, 4, 5]); yields true. So how does this fit into the story?

Furthermore, I checked out the fast_.. package and couldn't understand how to apply this to either. Can you give me a mini example or best practice on how you write your tests?

Thanks

from fpdart.

Babwenbiber avatar Babwenbiber commented on August 28, 2024

Thanks.
I didn't know people use IList instead of List :)

from fpdart.

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.