westy92 / const-date-time

A drop-in replacement for Dart's DateTime class with a const constructor.
https://pub.dev/packages/const_date_time
MIT License
4 stars 2 forks source link

[minor] Overrides runtimeType #10

Closed ohitsdaniel closed 1 year ago

ohitsdaniel commented 1 year ago

Once again, hi! 👋 👻

We ran into some issues in our snapshot tests relying on Equatable types. As we needed to inject a const DateTime in a immutable struct, I stumbled upon this library, allowing me to properly test our data objects. We rely on the Equatable package to synthesise equality checks for these objects. Unfortunately, Equatable relies on runtimeType equality of all properties in the checks and was therefore failing the test assertion.

It's quite easy to reproduce:

import 'package:equatable/equatable.dart';
import 'package:const_date_time/const_date_time.dart';

class SomeType extends Equatable {
  final DateTime date;

  SomeType({
    required this.date,
  });

  @override
  List<Object?> get props => [date];
}

void main() {
  print(SomeType(date: DateTime.fromMicrosecondsSinceEpoch(0)) == SomeType(date: ConstDateTime.fromMicrosecondsSinceEpoch(0)));

  print(DateTime.fromMicrosecondsSinceEpoch(0) == ConstDateTime.fromMicrosecondsSinceEpoch(0))
}

This example will print false even though the equality check implemented in ConstDateTime class prints true.

As it requires a bigger pull request on Equatable and some more in-depth thinking how derived types can allow equatability with parent types, I would suggest we merge this semi-hacky solution that allows our tests to pass. :)

westy92 commented 1 year ago

Published as 1.1.0. Thank you!