Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
24 changes: 12 additions & 12 deletions example/main.dart
Original file line number Diff line number Diff line change
@@ -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
}
cron = Cron("*/15 * * * *");
print(cron.previousRelativeTo(time)); // "2019-11-23 15:45:00.000"
print(cron.sincePreviousRelativeTo(time) == Duration(minutes: -15)); // true
}
12 changes: 6 additions & 6 deletions lib/src/cronparse.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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"
sdk: '>=2.12.0 <3.0.0'
170 changes: 124 additions & 46 deletions test/cronparse_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
);
}
});
Expand All @@ -22,9 +22,10 @@ void main() {
for (final t in tests) {
print(t[0]);
expect(
Cron(t[0]),
Cron(t[0] as String),
isA<Cron>(),
reason: "input: ${t[0]}, expected: Cron, got: ${Cron(t[0])}",
reason:
"input: ${t[0]}, expected: Cron, got: ${Cron(t[0] as String)}",
);
}
});
Expand All @@ -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);
Expand All @@ -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", () {
Expand All @@ -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", () {
Expand All @@ -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", () {
Expand All @@ -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);
Expand All @@ -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", () {
Expand All @@ -127,22 +158,32 @@ 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'", () {
// https://crontab.guru/cron-bug.html
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
Expand All @@ -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", () {
Expand All @@ -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
Expand All @@ -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",
);
}
});
});
});
}

3 changes: 0 additions & 3 deletions test/fixtures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,3 @@ const regexTests = [
['@asdf', false],
['@ ', false],
];



Loading