Skip to content

Commit

Permalink
Added tests (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoboss committed Feb 13, 2024
1 parent 8790852 commit 9038704
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
12 changes: 10 additions & 2 deletions lib/src/extensions/time_only_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ extension TimeOnlyExtensions on TimeOnly {
);
}

/// Converts the [TimeOnly] to a string in the format `HH:mm:ss.SSS`.
/// Converts the [TimeOnly] to a string in the format `HH:mm:ss` or
/// `HH:mm:ss.SSS` if milliseconds are present.
String toRfc3339String() {
return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}.${milliseconds.toString().padLeft(3, '0')}';
final String fractionString;
if (milliseconds > 0) {
fractionString = '.${milliseconds.toString().padLeft(3, '0')}';
} else {
fractionString = '';
}

return '${hours.toString().padLeft(2, '0')}:${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}$fractionString';
}
}
2 changes: 1 addition & 1 deletion lib/src/time_only.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ abstract class TimeOnly {
/// This factory uses the [DateTime.parse] method to create a new
/// [_TimeOnlyImpl] instance from a string.
factory TimeOnly.fromDateTimeString(String dateTimeString) {
final dateTime = DateTime.parse(dateTimeString);
final dateTime = DateTime.parse('2024-01-01 $dateTimeString');

return TimeOnly.fromDateTime(dateTime);
}
Expand Down
35 changes: 35 additions & 0 deletions test/time_only_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:kiota_abstractions/kiota_abstractions.dart';
import 'package:test/test.dart';

void main() {
group('TimeOnly', () {
test('fromDateTimeString and toRfc3339String', () {
expect(
TimeOnly.fromDateTimeString('12:34:56').toRfc3339String(),
'12:34:56',
);
expect(
TimeOnly.fromDateTimeString('12:34').toRfc3339String(),
'12:34:00',
);
expect(TimeOnly.fromDateTimeString('12').toRfc3339String(), '12:00:00');
});

test('fromDateTime', () {
expect(
TimeOnly.fromDateTime(DateTime(2024, 1, 1, 12, 34, 56))
.toRfc3339String(),
'12:34:56',
);
});

test('fromComponents', () {
expect(
TimeOnly.fromComponents(12, 34, 56, 789).toRfc3339String(),
'12:34:56.789',
);
expect(TimeOnly.fromComponents(12, 34, 56).toRfc3339String(), '12:34:56');
expect(TimeOnly.fromComponents(12, 34).toRfc3339String(), '12:34:00');
});
});
}

0 comments on commit 9038704

Please sign in to comment.