Skip to content

Commit

Permalink
fix: Calculation of persons age
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustl22 committed Oct 6, 2024
1 parent 34228e5 commit 9b4e59c
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 23 deletions.
22 changes: 0 additions & 22 deletions wrestling_scoreboard_client/test/data/person_test.dart

This file was deleted.

2 changes: 1 addition & 1 deletion wrestling_scoreboard_common/lib/src/data/person.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Person with _$Person implements DataObject, Organizational {
final monthDiff = today.month - birthDate!.month;
final dayDiff = today.day - birthDate!.day;

return monthDiff >= 0 && dayDiff >= 0 ? yearDiff : yearDiff - 1;
return monthDiff > 0 || (monthDiff == 0 && dayDiff >= 0) ? yearDiff : yearDiff - 1;
}
return null;
}
Expand Down
43 changes: 43 additions & 0 deletions wrestling_scoreboard_common/test/data/person_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:test/test.dart';
import 'package:wrestling_scoreboard_common/common.dart';

void main() {
test('Persons age', () {
MockableDateTime.isMocked = true;

final personA = Person(prename: 'John', surname: 'Doe', birthDate: DateTime(2015, 8, 1));
final personB = Person(prename: 'Max', surname: 'Muster', birthDate: DateTime(2015, 7, 31));
final personC = Person(prename: 'Ashok', surname: 'Kumar', birthDate: DateTime(2015, 1, 1));
final personD = Person(prename: 'Fred', surname: 'Nurk', birthDate: DateTime(2014, 12, 31));

MockableDateTime.mockedDateTime = DateTime(2020, 12, 30);
expect(personA.age, 5);
expect(personB.age, 5);
expect(personC.age, 5);
expect(personD.age, 5);

MockableDateTime.mockedDateTime = DateTime(2020, 12, 31);
expect(personA.age, 5);
expect(personB.age, 5);
expect(personC.age, 5);
expect(personD.age, 6);

MockableDateTime.mockedDateTime = DateTime(2021, 1, 1);
expect(personA.age, 5);
expect(personB.age, 5);
expect(personC.age, 6);
expect(personD.age, 6);

MockableDateTime.mockedDateTime = DateTime(2021, 7, 31);
expect(personA.age, 5);
expect(personB.age, 6);
expect(personC.age, 6);
expect(personD.age, 6);

MockableDateTime.mockedDateTime = DateTime(2021, 8, 1);
expect(personA.age, 6);
expect(personB.age, 6);
expect(personC.age, 6);
expect(personD.age, 6);
});
}

0 comments on commit 9b4e59c

Please sign in to comment.