From 9b4e59cb0e8a4ce9c5803a874561ac8a23306685 Mon Sep 17 00:00:00 2001 From: Gustl22 Date: Sun, 6 Oct 2024 11:09:40 +0200 Subject: [PATCH] fix: Calculation of persons age --- .../test/data/person_test.dart | 22 ---------- .../lib/src/data/person.dart | 2 +- .../test/data/person_test.dart | 43 +++++++++++++++++++ 3 files changed, 44 insertions(+), 23 deletions(-) delete mode 100644 wrestling_scoreboard_client/test/data/person_test.dart create mode 100644 wrestling_scoreboard_common/test/data/person_test.dart diff --git a/wrestling_scoreboard_client/test/data/person_test.dart b/wrestling_scoreboard_client/test/data/person_test.dart deleted file mode 100644 index aa02a333..00000000 --- a/wrestling_scoreboard_client/test/data/person_test.dart +++ /dev/null @@ -1,22 +0,0 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility that Flutter provides. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:wrestling_scoreboard_common/common.dart'; -import 'package:flutter_test/flutter_test.dart'; - -void main() { - test('Check functionality of person', () { - MockableDateTime.isMocked = true; - MockableDateTime.mockedDateTime = DateTime(2021, 8, 1); - - final personA = Person(prename: 'John', surname: 'Doe', birthDate: DateTime(2015, 8, 1)); - final personB = Person(prename: 'Max', surname: 'Muster', birthDate: DateTime(2015, 7, 31)); - - expect(personA.age, 6); - expect(personB.age, 5); - }); -} diff --git a/wrestling_scoreboard_common/lib/src/data/person.dart b/wrestling_scoreboard_common/lib/src/data/person.dart index d3d07b3c..33116a71 100644 --- a/wrestling_scoreboard_common/lib/src/data/person.dart +++ b/wrestling_scoreboard_common/lib/src/data/person.dart @@ -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; } diff --git a/wrestling_scoreboard_common/test/data/person_test.dart b/wrestling_scoreboard_common/test/data/person_test.dart new file mode 100644 index 00000000..d9101573 --- /dev/null +++ b/wrestling_scoreboard_common/test/data/person_test.dart @@ -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); + }); +}