Skip to content

Commit

Permalink
fix(date_ranger): address issues and test each case
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 committed Feb 26, 2023
1 parent 3352cc5 commit 8f0ae79
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
15 changes: 12 additions & 3 deletions lib/src/model/date/date_ranger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,21 @@ mixin DateRanger {
/// ```
Duration overlappingDurationWith(DateRanger other) {
if (hasInfiniteStart && hasInfiniteEnd ||
other.hasInfiniteStart && other.hasInfiniteEnd) return Duration.zero;
other.hasInfiniteStart && other.hasInfiniteEnd) {
if (isFinite) return duration;
if (other.isFinite) return other.duration;

return Duration.zero;
}
if (hasInfiniteStart && !other.hasInfiniteStart) {
return endDate!.difference(other.startDate!);
final difference = endDate!.difference(other.startDate!);

return difference.isNegative ? Duration.zero : difference;
}
if (hasInfiniteEnd && !other.hasInfiniteEnd) {
return startDate!.difference(other.endDate!);
final difference = other.endDate!.difference(startDate!);

return difference.isNegative ? Duration.zero : difference;
}
if (startDate!.isBefore(other.endDate!) &&
endDate!.isAfter(other.startDate!)) {
Expand Down
77 changes: 72 additions & 5 deletions test/model/date/date_ranger_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,24 +226,91 @@ void main() {

group('.overlappingDurationWith()', () {
test(
'should return the overlapping duration with another DateRanger',
'should return the inner overlapping Duration with another DateRanger',
() {
final dateRange1 = DateRange(
final DateRanger dateRanger1 = DateRange(
startDate: DateTime(2022, 12, 4, 9, 15),
endDate: DateTime(2022, 12, 4, 12, 15),
);
final DateRanger dateRanger2 = DateRange(
startDate: DateTime(2022, 12, 4, 10),
endDate: DateTime(2022, 12, 4, 11),
);
expect(
dateRanger1.overlappingDurationWith(dateRanger2),
const Duration(hours: 1),
);
expect(
dateRanger2.overlappingDurationWith(dateRanger1),
const Duration(hours: 1),
);
},
);

test(
'should return the overlapping Duration between infinite DateRangers',
() {
final dateRange1 = DateRange(
endDate: DateTime(2022, 12, 4, 12, 15),
);
final dateRange2 = DateRange(
startDate: DateTime(2022, 12, 4, 10, 15),
endDate: DateTime(2022, 12, 4, 11, 15),
);
expect(
dateRange1.overlappingDurationWith(dateRange2),
const Duration(hours: 1),
const Duration(hours: 2),
);
expect(
dateRange1.overlappingDurationWith(dateRange2),
dateRange2.overlappingDurationWith(dateRange1),
const Duration(hours: 2),
);
expect(
DateRange.infinite.overlappingDurationWith(DateRange.infinite),
Duration.zero,
);
expect(
DateRange.infinite.overlappingDurationWith(dateRange1),
Duration.zero,
);
expect(
dateRange1.overlappingDurationWith(DateRange.infinite),
Duration.zero,
);
final dateRange3 = DateRange.fromDate(DateTime(2022, 12, 4));
expect(
DateRange.infinite.overlappingDurationWith(dateRange3),
const Duration(days: 1),
);
expect(
dateRange3.overlappingDurationWith(DateRange.infinite),
const Duration(days: 1),
);
},
);

test(
'should return a Duration of zero if this finite DateRanger does not '
'overlap with another finite DateRanger',
() {
final dateRange1 = DateRange.fromDate(DateTime(2022, 12, 4));
final dateRange2 = DateRange.fromDate(DateTime(2022, 12, 5));
expect(dateRange1.overlappingDurationWith(dateRange2), Duration.zero);
expect(dateRange2.overlappingDurationWith(dateRange1), Duration.zero);
},
);

test(
'should return a Duration of zero if this infinite DateRanger does not '
'overlap with another infinite DateRanger',
() {
final dateRange1 = DateRange(
startDate: DateTime(2022, 12, 4, 12, 15),
);
final dateRange2 = DateRange(
endDate: DateTime(2022, 12, 4, 10, 15),
);
expect(dateRange1.overlappingDurationWith(dateRange2), Duration.zero);
expect(dateRange2.overlappingDurationWith(dateRange1), Duration.zero);
},
);
});
Expand Down

0 comments on commit 8f0ae79

Please sign in to comment.