From f6c76277159ebd1f9510cab3dced8be5041b5d2c Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Sun, 11 Jul 2021 18:32:09 +0200 Subject: [PATCH 1/6] migrate to null safety --- lib/src/cronparse.dart | 68 +++++++++---------- pubspec.yaml | 2 +- test/cronparse_test.dart | 139 ++++++++++++++++++++++++-------------- test/validators_test.dart | 4 +- 4 files changed, 126 insertions(+), 87 deletions(-) diff --git a/lib/src/cronparse.dart b/lib/src/cronparse.dart index f3d62ef..57ba6b4 100644 --- a/lib/src/cronparse.dart +++ b/lib/src/cronparse.dart @@ -51,7 +51,7 @@ class Cron { _monthField = field[3].contains("/") ? field[3].replaceFirst("*", "1-12") : field[3]; - _monthField = _monthField + _monthField = _monthField! .toLowerCase() .replaceAll('jan', '1') .replaceAll('feb', '2') @@ -68,7 +68,7 @@ class Cron { _dayOfWeekField = field[4].contains("/") ? field[4].replaceFirst("*", "0-7") : field[4]; - _dayOfWeekField = _dayOfWeekField + _dayOfWeekField = _dayOfWeekField! .toLowerCase() .replaceAll('mon', '1') .replaceAll('tue', '2') @@ -80,12 +80,12 @@ class Cron { } final String expr; - String _parsedExpr; - String _minuteField; - String _hourField; - String _dayOfMonthField; - String _monthField; - String _dayOfWeekField; + late String _parsedExpr; + String? _minuteField; + String? _hourField; + String? _dayOfMonthField; + String? _monthField; + String? _dayOfWeekField; /// `matches` returns true if the full expression matches the given time; bool matches(DateTime time) { @@ -100,8 +100,8 @@ class Cron { bool minuteMatches(DateTime time) { if (_minuteField == '*') return true; - if (_minuteField.contains("/")) { - final s = _minuteField.split("/"); + if (_minuteField!.contains("/")) { + final s = _minuteField!.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -109,9 +109,9 @@ class Cron { } return false; } - assert(!_minuteField.contains("/")); + assert(!_minuteField!.contains("/")); - final values = _minuteField.split(','); + final values = _minuteField!.split(','); for (final value in values) { if (value.contains("-")) { final bounds = value.split("-").map((v) => int.parse(v)).toList(); @@ -131,8 +131,8 @@ class Cron { bool hourMatches(DateTime time) { if (_hourField == '*') return true; - if (_hourField.contains("/")) { - final s = _hourField.split("/"); + if (_hourField!.contains("/")) { + final s = _hourField!.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -140,12 +140,12 @@ class Cron { } return false; } - assert(!_hourField.contains("/")); + assert(!_hourField!.contains("/")); - final values = _hourField.split(','); + final values = _hourField!.split(','); for (final value in values) { if (value.contains("-")) { - final bounds = _hourField.split("-").map((v) => int.parse(v)).toList(); + final bounds = _hourField!.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.hour) return true; } @@ -160,8 +160,8 @@ class Cron { bool dayOfMonthMatches(DateTime time) { if (_dayOfMonthField == '*') return true; - if (_dayOfMonthField.contains("/")) { - final s = _dayOfWeekField.split("/"); + if (_dayOfMonthField!.contains("/")) { + final s = _dayOfWeekField!.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -169,13 +169,13 @@ class Cron { } return false; } - assert(!_dayOfMonthField.contains("/")); + assert(!_dayOfMonthField!.contains("/")); - final values = _dayOfMonthField.split(','); + final values = _dayOfMonthField!.split(','); for (final value in values) { if (value.contains("-")) { final bounds = - _dayOfMonthField.split("-").map((v) => int.parse(v)).toList(); + _dayOfMonthField!.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.day) return true; } @@ -188,12 +188,12 @@ class Cron { /// `monthMatches` returns true if the month field of the expression /// matches the month of the given time bool monthMatches(DateTime time) { - assert(!_dayOfWeekField.contains(RegExp(r'a-zA-Z'))); + assert(!_dayOfWeekField!.contains(RegExp(r'a-zA-Z'))); if (_monthField == '*') return true; - if (_monthField.contains("/")) { - final s = _minuteField.split("/"); + if (_monthField!.contains("/")) { + final s = _minuteField!.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -201,12 +201,12 @@ class Cron { } return false; } - assert(!_monthField.contains("/")); + assert(!_monthField!.contains("/")); - final values = _monthField.split(','); + final values = _monthField!.split(','); for (final value in values) { if (value.contains("-")) { - final bounds = _monthField.split("-").map((v) => int.parse(v)).toList(); + final bounds = _monthField!.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.month) return true; } @@ -219,12 +219,12 @@ class Cron { /// `dayOfWeekMatches` returns true if the day of week field of the expression /// matches the day of week of the given time bool dayOfWeekMatches(DateTime time) { - assert(!_dayOfWeekField.contains(RegExp(r'a-zA-Z'))); + assert(!_dayOfWeekField!.contains(RegExp(r'a-zA-Z'))); if (_dayOfWeekField == '*') return true; - if (_dayOfWeekField.contains("/")) { - final s = _dayOfWeekField.split("/"); + if (_dayOfWeekField!.contains("/")) { + final s = _dayOfWeekField!.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -232,13 +232,13 @@ class Cron { } return false; } - assert(!_dayOfWeekField.contains("/")); + assert(!_dayOfWeekField!.contains("/")); - final values = _dayOfWeekField.split(','); + final values = _dayOfWeekField!.split(','); for (final value in values) { if (value.contains("-")) { final bounds = - _dayOfWeekField.split("-").map((v) => int.parse(v)).toList(); + _dayOfWeekField!.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.weekday || (i == 0 && time.weekday == 7)) return true; } diff --git a/pubspec.yaml b/pubspec.yaml index ad6b92e..eededa5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,4 +7,4 @@ dev_dependencies: test: ^1.3.4 environment: - sdk: ">=2.7.0 <3.0.0" \ No newline at end of file + sdk: '>=2.12.0 <3.0.0' \ No newline at end of file diff --git a/test/cronparse_test.dart b/test/cronparse_test.dart index 3d5fbca..836bc52 100644 --- a/test/cronparse_test.dart +++ b/test/cronparse_test.dart @@ -9,12 +9,9 @@ void main() { test('throws on invalid expressions', () { final tests = regexTests.where((t) => t[1] == false); for (final t in tests) { - final res = () => Cron(t[0]); - expect( - res, - throwsArgumentError, - reason: "input: ${t[0]}, expected: ArgumentError" - ); + final res = () => Cron(t[0] as String); + expect(res, throwsArgumentError, + reason: "input: ${t[0]}, expected: ArgumentError"); } }); test('returns a Cron from valid expressions', () { @@ -22,9 +19,10 @@ void main() { for (final t in tests) { print(t[0]); expect( - Cron(t[0]), + Cron(t[0] as String), isA(), - reason: "input: ${t[0]}, expected: Cron, got: ${Cron(t[0])}", + reason: + "input: ${t[0]}, expected: Cron, got: ${Cron(t[0] as String)}", ); } }); @@ -35,9 +33,11 @@ void main() { final cron = Cron(expression); final time = DateTime.now(); final result = cron.matches(time); - expect(result, isTrue, reason: "expression: $expression, time: $time, got: $result, expected: true"); + expect(result, isTrue, + reason: + "expression: $expression, time: $time, got: $result, expected: true"); }); - group("minute", (){ + group("minute", () { test('always matches *', () { final cron = Cron("* 1 1 1 1"); expect(cron.minuteMatches(DateTime.now()), isTrue); @@ -48,10 +48,12 @@ void main() { ['0 * * * *', "2020-02-03 08:01:49", isFalse] ]; for (final t in tests) { - final cron = Cron(t[0]); - final time = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], reason: "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect(result, t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); } }); test("matches range values", () { @@ -60,10 +62,12 @@ void main() { ['5-9 * * * *', '2020-02-03 08:15:49', isFalse], ]; for (final t in tests) { - final cron = Cron(t[0]); - final time = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], reason: "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect(result, t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); } }); test("matches within a set of values", () { @@ -78,10 +82,12 @@ void main() { ['1,2,3,50-59 * * * *', '2020-02-03 08:12:49', isFalse], ]; for (final t in tests) { - final cron = Cron(t[0]); - final time = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], reason: "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect(result, t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); } }); test("matches skip values", () { @@ -92,17 +98,19 @@ void main() { ['10-30/2 * * * *', '2020-02-03 08:22:49', isTrue], ]; for (final t in tests) { - final cron = Cron(t[0]); - final time = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], reason: "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect(result, t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); } }); }); - group("hour", (){}); - group("day of month", (){}); - group("month", (){}); - group("day of week", (){ + group("hour", () {}); + group("day of month", () {}); + group("month", () {}); + group("day of week", () { test('always matches *', () { final cron = Cron("1 1 1 1 *"); expect(cron.dayOfWeekMatches(DateTime.now()), isTrue); @@ -115,10 +123,12 @@ void main() { ['* * * * 4', '2019-11-24 05:00:00', isFalse], ]; for (final t in tests) { - final cron = Cron(t[0]); - final time = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final time = DateTime.parse(t[1] as String); final result = cron.dayOfWeekMatches(time); - expect(result, t[2], reason: "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect(result, t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); } }); test("matches range values", () { @@ -127,10 +137,12 @@ void main() { ['* * * * 1-3', '2019-11-23 05:00:00', isFalse], ]; for (final t in tests) { - final cron = Cron(t[0]); - final time = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final time = DateTime.parse(t[1] as String); final result = cron.dayOfWeekMatches(time); - expect(result, t[2], reason: "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect(result, t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); } }); test("we pass a test for 'the bug'", () { @@ -138,11 +150,13 @@ void main() { final cron = Cron('* * *,* * SUN'); final time = DateTime.parse('2019-11-23 05:00:00'); final result = cron.matches(time); - expect(result, true, reason: "expression: ${'* * *,* * SUN'}, time: ${'2019-11-23 05:00:00'}, got: $result, expected: ${true}"); + expect(result, true, + reason: + "expression: ${'* * *,* * SUN'}, time: ${'2019-11-23 05:00:00'}, got: $result, expected: ${true}"); }); }); }); - group("DateTime calculations", (){ + group("DateTime calculations", () { test("nextRelativeTo returns the next matching time", () { final tests = [ // simple tests @@ -160,7 +174,8 @@ void main() { final input = DateTime.parse(t[1]); final expected = DateTime.parse(t[2]); final result = cron.nextRelativeTo(input); - expect(result, expected, reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect(result, expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected"); } }); test("previousRelativeTo returns the previous matching time", () { @@ -180,11 +195,12 @@ void main() { final input = DateTime.parse(t[1]); final expected = DateTime.parse(t[2]); final result = cron.previousRelativeTo(input); - expect(result, expected, reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect(result, expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected"); } }); }); - group("Duration calculations", (){ + group("Duration calculations", () { test("untilNextRelativeTo returns the duration to the next match", () { final tests = [ // simple tests @@ -195,37 +211,60 @@ void main() { // TODO: range tests // TODO: set tests // TODO: skip tests - ["*/5 * * * *", "2019-11-23 12:00:01", Duration(minutes: 4, seconds: 59)], + [ + "*/5 * * * *", + "2019-11-23 12:00:01", + Duration(minutes: 4, seconds: 59) + ], ]; for (final t in tests) { - final cron = Cron(t[0]); - final input = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final input = DateTime.parse(t[1] as String); final expected = t[2]; final result = cron.untilNextRelativeTo(input); - expect(result, expected, reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect(result, expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected"); } }); - test("sincePreviousRelativeTo returns the duration since the previous match", () { + test( + "sincePreviousRelativeTo returns the duration since the previous match", + () { final tests = [ // simple tests ["* * * * *", "2019-11-23 12:00:00", -Duration(minutes: 1)], - ["* * * * *", "2019-11-23 12:00:01", -Duration(minutes: 1, seconds: 1)], - ["5 * * * *", "2019-11-23 12:00:01", -Duration(minutes: 55, seconds: 1)], - ["0 13 * * *", "2019-11-23 12:00:01", -Duration(hours: 23, seconds: 1)], + [ + "* * * * *", + "2019-11-23 12:00:01", + -Duration(minutes: 1, seconds: 1) + ], + [ + "5 * * * *", + "2019-11-23 12:00:01", + -Duration(minutes: 55, seconds: 1) + ], + [ + "0 13 * * *", + "2019-11-23 12:00:01", + -Duration(hours: 23, seconds: 1) + ], // TODO: range tests // TODO: set tests // TODO: skip tests - ["*/5 * * * *", "2019-11-23 12:00:01", -Duration(minutes: 5, seconds: 1)], + [ + "*/5 * * * *", + "2019-11-23 12:00:01", + -Duration(minutes: 5, seconds: 1) + ], ]; for (final t in tests) { - final cron = Cron(t[0]); - final input = DateTime.parse(t[1]); + final cron = Cron(t[0] as String); + final input = DateTime.parse(t[1] as String); final expected = t[2]; final result = cron.sincePreviousRelativeTo(input); - expect(result, expected, reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect(result, expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected"); } }); }); }); } - diff --git a/test/validators_test.dart b/test/validators_test.dart index 766291d..c2b6d9f 100644 --- a/test/validators_test.dart +++ b/test/validators_test.dart @@ -8,13 +8,13 @@ void main() { group("`isValid`", () { test('returns true for valid cron expressions', () { for (final t in regexTests.where((t) => t[1] == true)) { - final res = isValid(t[0]); + final res = isValid(t[0] as String); expect(res, equals(t[1]), reason: "input: ${t[0]}, expected: ${t[1]}, got: $res"); } }); test('returns false for invalid cron expressions', () { for (final t in regexTests.where((t) => t[1] == false)) { - final res = isValid(t[0]); + final res = isValid(t[0] as String); expect(res, equals(t[1]), reason: "input: ${t[0]}, expected: false, got: $res"); } }); From d0941b0b98a0a5341561a7b377051b2707a66938 Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Sun, 11 Jul 2021 18:42:09 +0200 Subject: [PATCH 2/6] imrpove null safety migration --- lib/src/cronparse.dart | 66 +++++++++++++++++++++--------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/lib/src/cronparse.dart b/lib/src/cronparse.dart index 57ba6b4..26b5887 100644 --- a/lib/src/cronparse.dart +++ b/lib/src/cronparse.dart @@ -51,7 +51,7 @@ class Cron { _monthField = field[3].contains("/") ? field[3].replaceFirst("*", "1-12") : field[3]; - _monthField = _monthField! + _monthField = _monthField .toLowerCase() .replaceAll('jan', '1') .replaceAll('feb', '2') @@ -68,7 +68,7 @@ class Cron { _dayOfWeekField = field[4].contains("/") ? field[4].replaceFirst("*", "0-7") : field[4]; - _dayOfWeekField = _dayOfWeekField! + _dayOfWeekField = _dayOfWeekField .toLowerCase() .replaceAll('mon', '1') .replaceAll('tue', '2') @@ -81,11 +81,11 @@ class Cron { final String expr; late String _parsedExpr; - String? _minuteField; - String? _hourField; - String? _dayOfMonthField; - String? _monthField; - String? _dayOfWeekField; + late String _minuteField; + late String _hourField; + late String _dayOfMonthField; + late String _monthField; + late String _dayOfWeekField; /// `matches` returns true if the full expression matches the given time; bool matches(DateTime time) { @@ -100,8 +100,8 @@ class Cron { bool minuteMatches(DateTime time) { if (_minuteField == '*') return true; - if (_minuteField!.contains("/")) { - final s = _minuteField!.split("/"); + if (_minuteField.contains("/")) { + final s = _minuteField.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -109,9 +109,9 @@ class Cron { } return false; } - assert(!_minuteField!.contains("/")); + assert(!_minuteField.contains("/")); - final values = _minuteField!.split(','); + final values = _minuteField.split(','); for (final value in values) { if (value.contains("-")) { final bounds = value.split("-").map((v) => int.parse(v)).toList(); @@ -131,8 +131,8 @@ class Cron { bool hourMatches(DateTime time) { if (_hourField == '*') return true; - if (_hourField!.contains("/")) { - final s = _hourField!.split("/"); + if (_hourField.contains("/")) { + final s = _hourField.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -140,12 +140,12 @@ class Cron { } return false; } - assert(!_hourField!.contains("/")); + assert(!_hourField.contains("/")); - final values = _hourField!.split(','); + final values = _hourField.split(','); for (final value in values) { if (value.contains("-")) { - final bounds = _hourField!.split("-").map((v) => int.parse(v)).toList(); + final bounds = _hourField.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.hour) return true; } @@ -160,8 +160,8 @@ class Cron { bool dayOfMonthMatches(DateTime time) { if (_dayOfMonthField == '*') return true; - if (_dayOfMonthField!.contains("/")) { - final s = _dayOfWeekField!.split("/"); + if (_dayOfMonthField.contains("/")) { + final s = _dayOfWeekField.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -169,13 +169,13 @@ class Cron { } return false; } - assert(!_dayOfMonthField!.contains("/")); + assert(!_dayOfMonthField.contains("/")); - final values = _dayOfMonthField!.split(','); + final values = _dayOfMonthField.split(','); for (final value in values) { if (value.contains("-")) { final bounds = - _dayOfMonthField!.split("-").map((v) => int.parse(v)).toList(); + _dayOfMonthField.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.day) return true; } @@ -188,12 +188,12 @@ class Cron { /// `monthMatches` returns true if the month field of the expression /// matches the month of the given time bool monthMatches(DateTime time) { - assert(!_dayOfWeekField!.contains(RegExp(r'a-zA-Z'))); + assert(!_dayOfWeekField.contains(RegExp(r'a-zA-Z'))); if (_monthField == '*') return true; - if (_monthField!.contains("/")) { - final s = _minuteField!.split("/"); + if (_monthField.contains("/")) { + final s = _minuteField.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -201,12 +201,12 @@ class Cron { } return false; } - assert(!_monthField!.contains("/")); + assert(!_monthField.contains("/")); - final values = _monthField!.split(','); + final values = _monthField.split(','); for (final value in values) { if (value.contains("-")) { - final bounds = _monthField!.split("-").map((v) => int.parse(v)).toList(); + final bounds = _monthField.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.month) return true; } @@ -219,12 +219,12 @@ class Cron { /// `dayOfWeekMatches` returns true if the day of week field of the expression /// matches the day of week of the given time bool dayOfWeekMatches(DateTime time) { - assert(!_dayOfWeekField!.contains(RegExp(r'a-zA-Z'))); + assert(!_dayOfWeekField.contains(RegExp(r'a-zA-Z'))); if (_dayOfWeekField == '*') return true; - if (_dayOfWeekField!.contains("/")) { - final s = _dayOfWeekField!.split("/"); + if (_dayOfWeekField.contains("/")) { + final s = _dayOfWeekField.split("/"); var skips = int.parse(s[1]); final bounds = s[0].split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i += skips) { @@ -232,13 +232,13 @@ class Cron { } return false; } - assert(!_dayOfWeekField!.contains("/")); + assert(!_dayOfWeekField.contains("/")); - final values = _dayOfWeekField!.split(','); + final values = _dayOfWeekField.split(','); for (final value in values) { if (value.contains("-")) { final bounds = - _dayOfWeekField!.split("-").map((v) => int.parse(v)).toList(); + _dayOfWeekField.split("-").map((v) => int.parse(v)).toList(); for (var i = bounds[0]; i <= bounds[1]; i++) { if (i == time.weekday || (i == 0 && time.weekday == 7)) return true; } From 8d9d96bd2c8b05f9cb53b9a6f777b86c02672454 Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Sun, 11 Jul 2021 18:42:14 +0200 Subject: [PATCH 3/6] format tests --- test/cronparse_test.dart | 107 ++++++++++++++++++++++++++------------- 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/test/cronparse_test.dart b/test/cronparse_test.dart index 836bc52..9e4366f 100644 --- a/test/cronparse_test.dart +++ b/test/cronparse_test.dart @@ -10,8 +10,11 @@ void main() { final tests = regexTests.where((t) => t[1] == false); for (final t in tests) { final res = () => Cron(t[0] as String); - expect(res, throwsArgumentError, - reason: "input: ${t[0]}, expected: ArgumentError"); + expect( + res, + throwsArgumentError, + reason: "input: ${t[0]}, expected: ArgumentError", + ); } }); test('returns a Cron from valid expressions', () { @@ -33,9 +36,12 @@ void main() { final cron = Cron(expression); final time = DateTime.now(); final result = cron.matches(time); - expect(result, isTrue, - reason: - "expression: $expression, time: $time, got: $result, expected: true"); + expect( + result, + isTrue, + reason: + "expression: $expression, time: $time, got: $result, expected: true", + ); }); group("minute", () { test('always matches *', () { @@ -51,9 +57,12 @@ void main() { final cron = Cron(t[0] as String); final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], - reason: - "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect( + result, + t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}", + ); } }); test("matches range values", () { @@ -65,9 +74,12 @@ void main() { final cron = Cron(t[0] as String); final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], - reason: - "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect( + result, + t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}", + ); } }); test("matches within a set of values", () { @@ -85,9 +97,12 @@ void main() { final cron = Cron(t[0] as String); final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], - reason: - "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect( + result, + t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}", + ); } }); test("matches skip values", () { @@ -101,9 +116,12 @@ void main() { final cron = Cron(t[0] as String); final time = DateTime.parse(t[1] as String); final result = cron.minuteMatches(time); - expect(result, t[2], - reason: - "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect( + result, + t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}", + ); } }); }); @@ -126,9 +144,12 @@ void main() { final cron = Cron(t[0] as String); final time = DateTime.parse(t[1] as String); final result = cron.dayOfWeekMatches(time); - expect(result, t[2], - reason: - "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect( + result, + t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}", + ); } }); test("matches range values", () { @@ -140,9 +161,12 @@ void main() { final cron = Cron(t[0] as String); final time = DateTime.parse(t[1] as String); final result = cron.dayOfWeekMatches(time); - expect(result, t[2], - reason: - "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}"); + expect( + result, + t[2], + reason: + "expression: ${t[0]}, time: ${t[1]}, got: $result, expected: ${t[2] == isTrue ? true : false}", + ); } }); test("we pass a test for 'the bug'", () { @@ -150,9 +174,12 @@ void main() { final cron = Cron('* * *,* * SUN'); final time = DateTime.parse('2019-11-23 05:00:00'); final result = cron.matches(time); - expect(result, true, - reason: - "expression: ${'* * *,* * SUN'}, time: ${'2019-11-23 05:00:00'}, got: $result, expected: ${true}"); + expect( + result, + true, + reason: + "expression: ${'* * *,* * SUN'}, time: ${'2019-11-23 05:00:00'}, got: $result, expected: ${true}", + ); }); }); }); @@ -174,8 +201,11 @@ void main() { final input = DateTime.parse(t[1]); final expected = DateTime.parse(t[2]); final result = cron.nextRelativeTo(input); - expect(result, expected, - reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect( + result, + expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected", + ); } }); test("previousRelativeTo returns the previous matching time", () { @@ -195,8 +225,11 @@ void main() { final input = DateTime.parse(t[1]); final expected = DateTime.parse(t[2]); final result = cron.previousRelativeTo(input); - expect(result, expected, - reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect( + result, + expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected", + ); } }); }); @@ -222,8 +255,11 @@ void main() { final input = DateTime.parse(t[1] as String); final expected = t[2]; final result = cron.untilNextRelativeTo(input); - expect(result, expected, - reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect( + result, + expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected", + ); } }); test( @@ -261,8 +297,11 @@ void main() { final input = DateTime.parse(t[1] as String); final expected = t[2]; final result = cron.sincePreviousRelativeTo(input); - expect(result, expected, - reason: "expression: ${t[0]}, got: $result, expected: $expected"); + expect( + result, + expected, + reason: "expression: ${t[0]}, got: $result, expected: $expected", + ); } }); }); From 9e33016304ed2b3264871f2a8fd18f58600bff01 Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Sun, 11 Jul 2021 18:42:46 +0200 Subject: [PATCH 4/6] run `dartfmt -w .` --- example/main.dart | 24 ++++++++++++------------ test/fixtures.dart | 3 --- test/validators_test.dart | 14 +++++++++++--- 3 files changed, 23 insertions(+), 18 deletions(-) diff --git a/example/main.dart b/example/main.dart index 438fa9a..edb846f 100644 --- a/example/main.dart +++ b/example/main.dart @@ -1,18 +1,18 @@ import 'package:cronparse/cronparse.dart'; void main() { - // validate a cron expression - final valid = isValid("54 2-3,4-9 */3 FEB MON-FRI"); - print(valid); // true + // validate a cron expression + final valid = isValid("54 2-3,4-9 */3 FEB MON-FRI"); + print(valid); // true - // calculate times based on a cron expression - final time = DateTime.parse("2019-11-23 16:00:00"); + // calculate times based on a cron expression + final time = DateTime.parse("2019-11-23 16:00:00"); - var cron = Cron("0 22 * * *"); - print(cron.nextRelativeTo(time)); // "2019-11-23 22:00:00.000" - print(cron.untilNextRelativeTo(time) == Duration(hours: 6)); // true + var cron = Cron("0 22 * * *"); + print(cron.nextRelativeTo(time)); // "2019-11-23 22:00:00.000" + print(cron.untilNextRelativeTo(time) == Duration(hours: 6)); // true - cron = Cron("*/15 * * * *"); - print(cron.previousRelativeTo(time)); // "2019-11-23 15:45:00.000" - print(cron.sincePreviousRelativeTo(time) == Duration(minutes: -15)); // true -} \ No newline at end of file + cron = Cron("*/15 * * * *"); + print(cron.previousRelativeTo(time)); // "2019-11-23 15:45:00.000" + print(cron.sincePreviousRelativeTo(time) == Duration(minutes: -15)); // true +} diff --git a/test/fixtures.dart b/test/fixtures.dart index 1c66ee0..8888be2 100644 --- a/test/fixtures.dart +++ b/test/fixtures.dart @@ -145,6 +145,3 @@ const regexTests = [ ['@asdf', false], ['@ ', false], ]; - - - diff --git a/test/validators_test.dart b/test/validators_test.dart index c2b6d9f..8c88eae 100644 --- a/test/validators_test.dart +++ b/test/validators_test.dart @@ -9,14 +9,22 @@ void main() { test('returns true for valid cron expressions', () { for (final t in regexTests.where((t) => t[1] == true)) { final res = isValid(t[0] as String); - expect(res, equals(t[1]), reason: "input: ${t[0]}, expected: ${t[1]}, got: $res"); + expect( + res, + equals(t[1]), + reason: "input: ${t[0]}, expected: ${t[1]}, got: $res", + ); } }); test('returns false for invalid cron expressions', () { for (final t in regexTests.where((t) => t[1] == false)) { final res = isValid(t[0] as String); - expect(res, equals(t[1]), reason: "input: ${t[0]}, expected: false, got: $res"); + expect( + res, + equals(t[1]), + reason: "input: ${t[0]}, expected: false, got: $res", + ); } }); }); -} \ No newline at end of file +} From 6f999915e63c6e4837d5f6629bc0da7aababe14a Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Sun, 11 Jul 2021 18:44:01 +0200 Subject: [PATCH 5/6] upgrade version --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index eededa5..e6811c4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: cronparse description: Parse and calculate different times related to Unix cron expressions -version: 0.1.1 +version: 0.2.0 homepage: https://github.com/justintout/cronparse dev_dependencies: From d06a0b1a710b18a5fd0c843fb3a419148084d0ff Mon Sep 17 00:00:00 2001 From: nilsreichardt Date: Sun, 11 Jul 2021 18:44:09 +0200 Subject: [PATCH 6/6] add changes to chnagelog --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4229165..2104c82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [0.2.0] - 2021-07-11 +* Migrate to null safety +* Fix pub.dev health suggestions (Formatting) ## [0.1.1] - 2020-06-12 * Fix pub.dev health suggestions