Added tests for parsing repeat after back to functions

This commit is contained in:
konrad 2019-03-17 16:29:55 +01:00
parent 94d5691e1e
commit 2437270031
Signed by untrusted user: konrad
GPG Key ID: F40E70337AB24C9B
3 changed files with 53 additions and 23 deletions

View File

@ -115,7 +115,7 @@ class _TaskEditPageState extends State<TaskEditPage> {
? null
: _repeatAfterValue.toString(),
onSaved: (repeatAfter) => _repeatAfter =
_makeDurationFromType(
getDurationFromType(
repeatAfter, _repeatAfterType),
decoration: new InputDecoration(
labelText: 'Repeat after',
@ -208,26 +208,4 @@ class _TaskEditPageState extends State<TaskEditPage> {
);
});
}
_makeDurationFromType(String value, String type) {
// Return an empty duration if either of the values is not set
if (value == null || value == '' || type == null || type == '') {
return Duration();
}
int val = int.parse(value);
switch (type) {
case 'Hours':
return Duration(seconds: val * 60);
case 'Days':
return Duration(seconds: val * 60 * 24);
case 'Weeks':
return Duration(seconds: val * 60 * 24 * 7);
case 'Months':
return Duration(seconds: val * 60 * 24 * 7 * 30);
case 'Years':
return Duration(seconds: val * 60 * 24 * 7 * 365);
}
}
}

View File

@ -42,3 +42,25 @@ getRepeatAfterValueFromDuration(Duration repeatAfter) {
// Otherwise Hours
return repeatAfter.inHours;
}
getDurationFromType(String value, String type) {
// Return an empty duration if either of the values is not set
if (value == null || value == '' || type == null || type == '') {
return Duration();
}
int val = int.parse(value);
switch (type) {
case 'Hours':
return Duration(hours: val);
case 'Days':
return Duration(days: val);
case 'Weeks':
return Duration(days: val * 7);
case 'Months':
return Duration(days: val * 30);
case 'Years':
return Duration(days: val * 365);
}
}

View File

@ -43,4 +43,34 @@ void main() {
expect(getRepeatAfterTypeFromDuration(testDuration), null);
expect(getRepeatAfterValueFromDuration(testDuration), null);
});
test('Hours to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Hours');
expect(parsedDuration, Duration(hours: 6));
});
test('Days to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Days');
expect(parsedDuration, Duration(days: 6));
});
test('Weeks to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Weeks');
expect(parsedDuration, Duration(days: 6 * 7));
});
test('Months to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Months');
expect(parsedDuration, Duration(days: 6 * 30));
});
test('Years to duration', () {
Duration parsedDuration = getDurationFromType('6', 'Years');
expect(parsedDuration, Duration(days: 6 * 365));
});
test('null to duration', () {
Duration parsedDuration = getDurationFromType(null, null);
expect(parsedDuration, Duration());
});
}