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 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/lib/src/cronparse.dart b/lib/src/cronparse.dart index f3d62ef..26b5887 100644 --- a/lib/src/cronparse.dart +++ b/lib/src/cronparse.dart @@ -80,12 +80,12 @@ class Cron { } final String expr; - String _parsedExpr; - String _minuteField; - String _hourField; - String _dayOfMonthField; - String _monthField; - String _dayOfWeekField; + late String _parsedExpr; + 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) { diff --git a/pubspec.yaml b/pubspec.yaml index ad6b92e..e6811c4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,10 +1,10 @@ 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: 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..9e4366f 100644 --- a/test/cronparse_test.dart +++ b/test/cronparse_test.dart @@ -9,11 +9,11 @@ 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]); + final res = () => Cron(t[0] as String); expect( res, throwsArgumentError, - reason: "input: ${t[0]}, expected: ArgumentError" + reason: "input: ${t[0]}, expected: ArgumentError", ); } }); @@ -22,9 +22,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 +36,14 @@ 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 +54,15 @@ 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 +71,15 @@ 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 +94,15 @@ 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 +113,22 @@ 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 +141,15 @@ 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 +158,15 @@ 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 +174,16 @@ 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 +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", () { @@ -180,11 +225,15 @@ 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 +244,66 @@ 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/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 766291d..8c88eae 100644 --- a/test/validators_test.dart +++ b/test/validators_test.dart @@ -8,15 +8,23 @@ 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]); - expect(res, equals(t[1]), reason: "input: ${t[0]}, expected: ${t[1]}, got: $res"); + 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]); - expect(res, equals(t[1]), reason: "input: ${t[0]}, expected: false, got: $res"); + final res = isValid(t[0] as String); + expect( + res, + equals(t[1]), + reason: "input: ${t[0]}, expected: false, got: $res", + ); } }); }); -} \ No newline at end of file +}