diff --git a/lib/solve/utils/duration_formatter.dart b/lib/solve/utils/duration_formatter.dart index 34d1aed..437b6da 100644 --- a/lib/solve/utils/duration_formatter.dart +++ b/lib/solve/utils/duration_formatter.dart @@ -1,16 +1,24 @@ class DurationFormatter { static String format(Duration duration) { - final minutesStr = duration.inMinutes.toString(); + final roundedDuration = _roundDuration(duration); + final minutesStr = roundedDuration.inMinutes.toString(); final secondsStr = - duration.inSeconds.remainder(60).toString().padLeft(2, '0'); - final millisecondsStr = (duration.inMilliseconds.remainder(1000) / 10) - .round() - .toString() - .padLeft(2, '0'); + roundedDuration.inSeconds.remainder(60).toString().padLeft(2, '0'); + final millisecondsStr = + (roundedDuration.inMilliseconds.remainder(1000) / 10) + .round() + .toString() + .padLeft(2, '0'); var textStr = '$secondsStr.$millisecondsStr'; if (duration >= const Duration(minutes: 1)) { textStr = '$minutesStr:$textStr'; } return textStr; } + + static Duration _roundDuration(Duration duration) { + final milliseconds = duration.inMilliseconds; + final roundedMilliseconds = (milliseconds / 10).round() * 10; + return Duration(milliseconds: roundedMilliseconds); + } } diff --git a/test/solve/model/solve_test.dart b/test/solve/model/solve_test.dart index 894dc72..02bb6f5 100644 --- a/test/solve/model/solve_test.dart +++ b/test/solve/model/solve_test.dart @@ -9,6 +9,20 @@ void main() { scramble: '', ); + final roundedDownSolve = Solve( + uid: '', + timestamp: DateTime(2000), + time: const Duration(milliseconds: 10004), + scramble: '', + ); + + final roundedUpSolve = Solve( + uid: '', + timestamp: DateTime(2000), + time: const Duration(milliseconds: 9995), + scramble: '', + ); + group('Solve', () { test('returns correct effective time without +2', () { expect(solve.effectiveTime, const Duration(seconds: 10)); @@ -19,6 +33,14 @@ void main() { expect(plusTwoSolve.effectiveTime, const Duration(seconds: 12)); }); + test('returns correct time to display rounded down', () { + expect(roundedDownSolve.timeToDisplay, '10.00'); + }); + + test('returns correct time to display rounded up', () { + expect(roundedUpSolve.timeToDisplay, '10.00'); + }); + test('returns correct time to display without +2', () { expect(solve.timeToDisplay, '10.00'); }); diff --git a/test/solve/utils/duration_formatter_test.dart b/test/solve/utils/duration_formatter_test.dart index 35ad5b4..150eab7 100644 --- a/test/solve/utils/duration_formatter_test.dart +++ b/test/solve/utils/duration_formatter_test.dart @@ -37,5 +37,15 @@ void main() { const duration = Duration(hours: 1); expect(DurationFormatter.format(duration), equals('60:00.00')); }); + + test('formats duration rounded down correctly', () { + const duration = Duration(milliseconds: 10004); + expect(DurationFormatter.format(duration), equals('10.00')); + }); + + test('formats duration rounded up correctly', () { + const duration = Duration(milliseconds: 9995); + expect(DurationFormatter.format(duration), equals('10.00')); + }); }); } diff --git a/test/stats/model/average_stat_test.dart b/test/stats/model/average_stat_test.dart new file mode 100644 index 0000000..b58e8df --- /dev/null +++ b/test/stats/model/average_stat_test.dart @@ -0,0 +1,26 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:kubrs_app/stats/model/average_stat.dart'; + +void main() { + group('AverageStat', () { + test('displays empty average value correctly', () { + final emptyAverage = AverageStat.empty(5); + expect(emptyAverage.displayedValue, '-'); + }); + + test('displays DNF average value correctly', () { + final dnfAverage = AverageStat.dnf(5); + expect(dnfAverage.displayedValue, 'DNF'); + }); + + test('displays average value rounded down correctly', () { + final average = AverageStat(10004, 5); + expect(average.displayedValue, '10.00'); + }); + + test('displays average value rounded up correctly', () { + final average = AverageStat(9995, 5); + expect(average.displayedValue, '10.00'); + }); + }); +} diff --git a/test/stats/model/best_stat_test.dart b/test/stats/model/best_stat_test.dart new file mode 100644 index 0000000..c50f680 --- /dev/null +++ b/test/stats/model/best_stat_test.dart @@ -0,0 +1,26 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:kubrs_app/stats/model/best_stat.dart'; + +void main() { + group('BestStat', () { + test('displays empty best value correctly', () { + final emptyBest = BestStat.empty(); + expect(emptyBest.displayedValue, '-'); + }); + + test('displays DNF best value correctly', () { + final dnfBest = BestStat.dnf(); + expect(dnfBest.displayedValue, 'DNF'); + }); + + test('displays best value rounded down correctly', () { + final best = BestStat(10004); + expect(best.displayedValue, '10.00'); + }); + + test('displays best value rounded up correctly', () { + final best = BestStat(9995); + expect(best.displayedValue, '10.00'); + }); + }); +} diff --git a/test/stats/model/mean_stat_test.dart b/test/stats/model/mean_stat_test.dart new file mode 100644 index 0000000..b0100cb --- /dev/null +++ b/test/stats/model/mean_stat_test.dart @@ -0,0 +1,26 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:kubrs_app/stats/model/mean_stat.dart'; + +void main() { + group('MeanStat', () { + test('displays empty mean value correctly', () { + final emptyMean = MeanStat.empty(5); + expect(emptyMean.displayedValue, '-'); + }); + + test('displays DNF mean value correctly', () { + final dnfMean = MeanStat.dnf(5); + expect(dnfMean.displayedValue, 'DNF'); + }); + + test('displays mean value rounded down correctly', () { + final mean = MeanStat(10004, 5); + expect(mean.displayedValue, '10.00'); + }); + + test('displays mean value rounded up correctly', () { + final mean = MeanStat(9995, 5); + expect(mean.displayedValue, '10.00'); + }); + }); +} diff --git a/test/stats/model/worst_stat_test.dart b/test/stats/model/worst_stat_test.dart new file mode 100644 index 0000000..9a283b3 --- /dev/null +++ b/test/stats/model/worst_stat_test.dart @@ -0,0 +1,26 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:kubrs_app/stats/model/worst_stat.dart'; + +void main() { + group('WorstStat', () { + test('displays empty worst value correctly', () { + final emptyWorst = WorstStat.empty(); + expect(emptyWorst.displayedValue, '-'); + }); + + test('displays DNF worst value correctly', () { + final dnfWorst = WorstStat.dnf(); + expect(dnfWorst.displayedValue, 'DNF'); + }); + + test('displays worst value rounded down correctly', () { + final worst = WorstStat(10004); + expect(worst.displayedValue, '10.00'); + }); + + test('displays worst value rounded up correctly', () { + final worst = WorstStat(9995); + expect(worst.displayedValue, '10.00'); + }); + }); +}