Skip to content

Commit

Permalink
fix: fix duration formatter rounding
Browse files Browse the repository at this point in the history
  • Loading branch information
GabinL21 committed Aug 11, 2023
1 parent c319b07 commit 59ff8a7
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 6 deletions.
20 changes: 14 additions & 6 deletions lib/solve/utils/duration_formatter.dart
Original file line number Diff line number Diff line change
@@ -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);
}
}
22 changes: 22 additions & 0 deletions test/solve/model/solve_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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');
});
Expand Down
10 changes: 10 additions & 0 deletions test/solve/utils/duration_formatter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
});
});
}
26 changes: 26 additions & 0 deletions test/stats/model/average_stat_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
});
}
26 changes: 26 additions & 0 deletions test/stats/model/best_stat_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
});
}
26 changes: 26 additions & 0 deletions test/stats/model/mean_stat_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
});
}
26 changes: 26 additions & 0 deletions test/stats/model/worst_stat_test.dart
Original file line number Diff line number Diff line change
@@ -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');
});
});
}

0 comments on commit 59ff8a7

Please sign in to comment.