58 lines
1.6 KiB
Dart
58 lines
1.6 KiB
Dart
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:marianum_mobile/api/connect/rmv/iso_duration.dart';
|
|
|
|
void main() {
|
|
group('IsoDuration.fromJson', () {
|
|
test('null and empty return null', () {
|
|
expect(IsoDuration.fromJson(null), isNull);
|
|
expect(IsoDuration.fromJson(''), isNull);
|
|
});
|
|
|
|
test('hours-minutes-seconds parse correctly', () {
|
|
expect(
|
|
IsoDuration.fromJson('PT1H30M15S'),
|
|
const Duration(hours: 1, minutes: 30, seconds: 15),
|
|
);
|
|
});
|
|
|
|
test('hours only', () {
|
|
expect(IsoDuration.fromJson('PT2H'), const Duration(hours: 2));
|
|
});
|
|
|
|
test('minutes only', () {
|
|
expect(IsoDuration.fromJson('PT45M'), const Duration(minutes: 45));
|
|
});
|
|
|
|
test('seconds with fraction', () {
|
|
expect(
|
|
IsoDuration.fromJson('PT30.5S'),
|
|
const Duration(milliseconds: 30500),
|
|
);
|
|
});
|
|
|
|
test('invalid string returns null', () {
|
|
expect(IsoDuration.fromJson('not a duration'), isNull);
|
|
});
|
|
});
|
|
|
|
group('IsoDuration.toJson', () {
|
|
test('null returns null', () => expect(IsoDuration.toJson(null), isNull));
|
|
|
|
test('zero duration formats as PT0S', () {
|
|
expect(IsoDuration.toJson(Duration.zero), 'PT0S');
|
|
});
|
|
|
|
test('hours and minutes only formats without seconds', () {
|
|
expect(
|
|
IsoDuration.toJson(const Duration(hours: 1, minutes: 30)),
|
|
'PT1H30M',
|
|
);
|
|
});
|
|
|
|
test('full duration roundtrips through parse', () {
|
|
const original = Duration(hours: 2, minutes: 15, seconds: 7);
|
|
expect(IsoDuration.fromJson(IsoDuration.toJson(original)), original);
|
|
});
|
|
});
|
|
}
|