Commit 9b55e06

derdilla <82763757+NobodyForNothing@users.noreply.github.com>
2024-03-17 14:41:03
implement DateRange assertions
Signed-off-by: derdilla <82763757+NobodyForNothing@users.noreply.github.com>
1 parent 8b921bd
Changed files (2)
health_data_store
lib
src
test
health_data_store/lib/src/types/date_range.dart
@@ -13,7 +13,8 @@ class DateRange with _$DateRange {
   const DateRange._();
 
   /// Creates a date range for the given start and end [DateTime].
-  const factory DateRange({
+  @Assert('end.isAfter(start)')
+  factory DateRange({
     /// The start of the range of dates.
     required DateTime start,
     /// The end of the range of dates.
health_data_store/test/src/types/date_range_test.dart
@@ -4,16 +4,23 @@ import 'package:test/test.dart';
 void main() {
   test('should initialize', () {
     final timeA = DateTime.now();
-    final timeB = timeA.subtract(Duration(hours: 20));
+    final timeB = timeA.add(Duration(hours: 20));
     final range = DateRange(start: timeA, end: timeB);
     expect(range.start, equals(timeA));
     expect(range.end, equals(timeB));
   });
+  test('should throw assertion error when used incorrectly', () {
+    final start = DateTime.now();
+    final end = start.subtract(Duration(hours: 20));
+    expect(end.isBefore(start), true);
+    expect(() => DateRange(start: start,end: end),
+        throwsA(isA<AssertionError>()));
+  });
   test('should calculate difference', () {
     final timeA = DateTime.now();
-    final timeB = timeA.subtract(Duration(hours: 21, seconds: 42));
+    final timeB = timeA.add(Duration(hours: 21, seconds: 42));
     final range = DateRange(start: timeA, end: timeB);
-    expect(range.duration, equals(Duration(hours: -21, seconds: -42)));
+    expect(range.duration, equals(Duration(hours: 21, seconds: 42)));
   });
   test('should determine start and end time in seconds since epoch', () {
     final timeA = DateTime.fromMillisecondsSinceEpoch(0);