-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
testing equality of derived types #131
Comments
Hi @ride4sun 👋 I believe this is working as expected because a import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
abstract class Animal extends Equatable {
const Animal({required this.name});
final String name;
@override
List<Object> get props => [name];
}
mixin AnimalEqualityMixin on Animal {
static const _equality = DeepCollectionEquality();
@override
bool operator ==(Object other) {
return other is Animal && _equality.equals(props, other.props);
}
}
class Cat extends Animal with AnimalEqualityMixin {
const Cat({required String name}) : super(name: name);
}
class Dog extends Animal with AnimalEqualityMixin {
const Dog({required String name}) : super(name: name);
}
void main() {
final cat = Cat(name: 'Taco');
final dog = Dog(name: 'Taco');
print(cat == dog); // true
} I don't think this is within the scope of Hope that helps 👍 |
Here is a possible solution for this #133 |
Essentially Morgan and I created an optional property you can override called However, if you're creating a "derived" class, then you provide the type you want your class to be compared against. In your example @ride4sun, all you'd need to do is add an override to your class BleDevice<T> extends GenericDevice with Disposable {
BleDevice({@required this.device, @required String name, @required String id})
: super(name: name, id: id);
final T device;
@override
Set<Type> get derived => {GenericDevice};
} Then when you run your tests they will work as expected. You can give this a try by switching over to branch in your equatable:
git:
url: 'https://github.com/mrgnhnt96/equatable.git'
ref: derived if it makes it easier you can add it under your top level |
If you assume that all inheritance follows a hierarchical pattern similar to animals, then sure, checking equality between types makes little sense. But that's only a fraction of usages for inheritance. Personally, I'm creating test objects that inherit from the real objects, and not being able to compare them with object that are created by the app defeats the purpose. |
Can you provide a link to a DartPad/Gist/Repository that illustrates what you’re trying to achieve? Thanks! |
@SupposedlySam, could you try and help me? I've created #147 for a better explanation for my case, but basically, my question is: When your derived class is an |
@FMorschel , maybe you could create an extension method on your enum that takes in the second enum type and compares them to return MyEnum.equals(MyOtherEnum); // true It wouldn't be generic but maybe you can work with that? |
As I've said on #147
This is done in my original class that my This, I believe is how this package should solve the equality of instances, using This is fair because my enum constants can be considered as an instance of my class, but not the other way around because my class is not even an So even if I tried using your suggestion above, my problem wouldn't be solved. I know creating a new method like the |
I think this suggestion can work!
we can have both options, but for me i want this to be a global rule because all of the derived classes are the same as their parents Editalthough this will do what we need, but it will also create other equality issues (e.g generated code with |
Going to close this for now since it's quite old and it seems to be out of scope for this package imo. Happy to revisit the conversation if others feel strongly though. |
Describe the bug
I have two types:
and
I Wrote a test like this:
I expected the test to pass but because because Equality is looking into the runtime type its failed
Any suggestions ideas - is that really how it should be?
Makes me wonder if that could not be enough:
Is the dart analyzer not catching that I compare not comparable types?
The text was updated successfully, but these errors were encountered: