Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change option IncludeSeconds to RequireSeconds #88

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
73 changes: 40 additions & 33 deletions NCrontab.Tests/CrontabScheduleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,20 @@ public void AllTimeString()
[Test]
public void SixPartAllTimeString()
{
Assert.AreEqual("* * * * * *", CrontabSchedule.Parse("* * * * * *", new ParseOptions { IncludingSeconds = true }).ToString());
Assert.AreEqual("* * * * * *", CrontabSchedule.Parse("* * * * * *", new ParseOptions { RequireSeconds = true }).ToString());
}

[Test]
public void CannotParseWhenSecondsRequired()
{
Assert.Throws<CrontabException>(() => CrontabSchedule.Parse("* * * * *", new ParseOptions { IncludingSeconds = true }));
Assert.Throws<CrontabException>(() => CrontabSchedule.Parse("* * * * *", new ParseOptions { RequireSeconds = true }));
}

[TestCase("* * * * *")]
[TestCase("* * * * * *")]
public void CanParseEitherFormatWhenSecondsAreNotRequired(string expression)
{
Assert.DoesNotThrow(() => CrontabSchedule.Parse(expression, new ParseOptions { RequireSeconds = false }));
}

[TestCase("* 1-3 * * *" , "* 1-2,3 * * *" , false)]
Expand All @@ -86,9 +93,9 @@ public void CannotParseWhenSecondsRequired()
[TestCase("22 * * * 1,3,5,7,9,11 *", "22 * * * */2 *" , true )]
[TestCase("33 10,25,40 * * * *" , "33 10-40/15 * * * *" , true )]
[TestCase("55 * * * 1,3,8 1-2,5" , "55 * * * Mar,Jan,Aug Fri,Mon-Tue", true )]
public void Formatting(string format, string expression, bool includingSeconds)
public void Formatting(string format, string expression, bool requireSeconds)
{
var options = new ParseOptions { IncludingSeconds = includingSeconds };
var options = new ParseOptions { RequireSeconds = requireSeconds };
Assert.AreEqual(format, CrontabSchedule.Parse(expression, options).ToString());
}

Expand Down Expand Up @@ -274,11 +281,11 @@ public void Formatting(string format, string expression, bool includingSeconds)
[TestCase("01/01/2000 12:00:00", "40 14/1 * * *", "01/01/2000 14:40:00", false)]
[TestCase("01/01/2000 14:40:00", "40 14/1 * * *", "01/01/2000 15:40:00", false)]

public void Evaluations(string startTimeString, string cronExpression, string nextTimeString, bool includingSeconds)
public void Evaluations(string startTimeString, string cronExpression, string nextTimeString, bool requireSeconds)
{
CronCall(startTimeString, cronExpression, nextTimeString, new ParseOptions
{
IncludingSeconds = includingSeconds
RequireSeconds = requireSeconds
});
}

Expand All @@ -296,11 +303,11 @@ public void Evaluations(string startTimeString, string cronExpression, string ne
[TestCase(" * * * * * Mon", "01/01/2003 00:00:00", "02/01/2003 12:00:00", true )]
[TestCase("10 30 12 * * Mon", "01/01/2003 00:00:00", "06/01/2003 12:00:10", true )]

public void FiniteOccurrences(string cronExpression, string startTimeString, string endTimeString, bool includingSeconds)
public void FiniteOccurrences(string cronExpression, string startTimeString, string endTimeString, bool requireSeconds)
{
CronFinite(cronExpression, startTimeString, endTimeString, new ParseOptions
{
IncludingSeconds = includingSeconds
RequireSeconds = requireSeconds
});
}

Expand All @@ -317,74 +324,74 @@ public void FiniteOccurrences(string cronExpression, string startTimeString, str
#endif
[TestCase("* * 31 Feb *", false)]
[TestCase("* * * 31 Feb *", true)]
public void DontLoopIndefinitely(string expression, bool includingSeconds)
public void DontLoopIndefinitely(string expression, bool requireSeconds)
{
CronFinite(expression, "01/01/2001 00:00:00", "01/01/2010 00:00:00", new ParseOptions
{
IncludingSeconds = includingSeconds
RequireSeconds = requireSeconds
});
}

static void BadField(string expression, bool includingSeconds)
static void BadField(string expression, bool requireSeconds)
{
Assert.Throws<CrontabException>(() => CrontabSchedule.Parse(expression, new ParseOptions
{
IncludingSeconds = includingSeconds
RequireSeconds = requireSeconds
}));
Assert.That(CrontabSchedule.TryParse(expression, new ParseOptions
{
IncludingSeconds = includingSeconds
RequireSeconds = requireSeconds
}), Is.Null);
}

[TestCase("bad * * * * *", false)]
public void BadSecondsField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void BadSecondsField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("bad * * * *", false)]
[TestCase("* bad * * * *", true)]
public void BadMinutesField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void BadMinutesField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* bad * * *", false)]
[TestCase("* * bad * * *", true)]
public void BadHoursField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void BadHoursField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* * bad * *", false)]
[TestCase("* * * bad * *", true)]
public void BadDayField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void BadDayField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* * * bad *", false)]
[TestCase("* * * * bad *", true)]
public void BadMonthField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void BadMonthField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* * * * mon,bad,wed", false)]
[TestCase("* * * * * mon,bad,wed", true)]
public void BadDayOfWeekField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void BadDayOfWeekField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* 1,2,3,456,7,8,9 * * *", false)]
[TestCase("* * 1,2,3,456,7,8,9 * * *", true)]
public void OutOfRangeField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void OutOfRangeField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* 1,Z,3,4 * * *", false)]
[TestCase("* * 1,Z,3,4 * * *", true)]
public void NonNumberValueInNumericOnlyField(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void NonNumberValueInNumericOnlyField(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* 1/Z * * *", false)]
[TestCase("* * 1/Z * * *", true)]
public void NonNumericFieldInterval(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void NonNumericFieldInterval(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

[TestCase("* 3-l2 * * *", false)]
[TestCase("* * 3-l2 * * *", true)]
public void NonNumericFieldRangeComponent(string expression, bool includingSeconds) =>
BadField(expression, includingSeconds);
public void NonNumericFieldRangeComponent(string expression, bool requireSeconds) =>
BadField(expression, requireSeconds);

/// <summary>
/// Test case for
Expand Down
26 changes: 13 additions & 13 deletions NCrontab/CrontabSchedule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public sealed partial class CrontabSchedule

public sealed partial class ParseOptions
{
public bool IncludingSeconds { get; set; }
public bool RequireSeconds { get; set; }
}

//
Expand Down Expand Up @@ -113,23 +113,23 @@ public static T TryParse<T>(string expression, ParseOptions? options, Func<Cront

var tokens = expression.Split(StringSeparatorStock.Space, StringSplitOptions.RemoveEmptyEntries);

var includingSeconds = options != null && options.IncludingSeconds;
var expectedTokenCount = includingSeconds ? 6 : 5;
if (tokens.Length < expectedTokenCount || tokens.Length > expectedTokenCount)
var secondsRequired = options != null && options.RequireSeconds;
var minimumExpectedTokenCount = secondsRequired ? 6 : 5;
const int maximumExpectedTokenCount = 6;
if (tokens.Length < minimumExpectedTokenCount || tokens.Length > maximumExpectedTokenCount)
{
return errorSelector(() =>
{
var components =
includingSeconds
? "6 components of a schedule in the sequence of seconds, minutes, hours, days, months, and days of week"
: "5 components of a schedule in the sequence of minutes, hours, days, months, and days of week";
return new CrontabException($"'{expression}' is an invalid crontab expression. It must contain {components}.");
});
const string fiveComponents =
"5 components of a schedule in the sequence of minutes, hours, days, months, and days of week";
const string sixComponents =
"6 components of a schedule in the sequence of seconds, minutes, hours, days, months, and days of week";

return errorSelector(() => new CrontabException(
$"'{expression}' is an invalid crontab expression. It must contain either {fiveComponents} or {sixComponents}."));
}

var fields = new CrontabField[6];

var offset = includingSeconds ? 0 : 1;
var offset = secondsRequired ? 0 : tokens.Length == 6 ? 0 : 1;
for (var i = 0; i < tokens.Length; i++)
{
var kind = (CrontabFieldKind)i + offset;
Expand Down
4 changes: 2 additions & 2 deletions NCrontab/PublicAPI/net35/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ NCrontab.CrontabSchedule.GetNextOccurrence(System.DateTime baseTime) -> System.D
NCrontab.CrontabSchedule.GetNextOccurrence(System.DateTime baseTime, System.DateTime endTime) -> System.DateTime
NCrontab.CrontabSchedule.GetNextOccurrences(System.DateTime baseTime, System.DateTime endTime) -> System.Collections.Generic.IEnumerable<System.DateTime>!
NCrontab.CrontabSchedule.ParseOptions
NCrontab.CrontabSchedule.ParseOptions.IncludingSeconds.get -> bool
NCrontab.CrontabSchedule.ParseOptions.IncludingSeconds.set -> void
NCrontab.CrontabSchedule.ParseOptions.RequireSeconds.get -> bool
NCrontab.CrontabSchedule.ParseOptions.RequireSeconds.set -> void
NCrontab.CrontabSchedule.ParseOptions.ParseOptions() -> void
NCrontab.ExceptionProvider
NCrontab.ICrontabField
Expand Down
4 changes: 2 additions & 2 deletions NCrontab/PublicAPI/netstandard1.0/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ NCrontab.CrontabSchedule.GetNextOccurrence(System.DateTime baseTime) -> System.D
NCrontab.CrontabSchedule.GetNextOccurrence(System.DateTime baseTime, System.DateTime endTime) -> System.DateTime
NCrontab.CrontabSchedule.GetNextOccurrences(System.DateTime baseTime, System.DateTime endTime) -> System.Collections.Generic.IEnumerable<System.DateTime>!
NCrontab.CrontabSchedule.ParseOptions
NCrontab.CrontabSchedule.ParseOptions.IncludingSeconds.get -> bool
NCrontab.CrontabSchedule.ParseOptions.IncludingSeconds.set -> void
NCrontab.CrontabSchedule.ParseOptions.RequireSeconds.get -> bool
NCrontab.CrontabSchedule.ParseOptions.RequireSeconds.set -> void
NCrontab.CrontabSchedule.ParseOptions.ParseOptions() -> void
NCrontab.ExceptionProvider
NCrontab.ICrontabField
Expand Down
4 changes: 2 additions & 2 deletions NCrontab/PublicAPI/netstandard2.0/PublicAPI.Shipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ NCrontab.CrontabSchedule.GetNextOccurrence(System.DateTime baseTime) -> System.D
NCrontab.CrontabSchedule.GetNextOccurrence(System.DateTime baseTime, System.DateTime endTime) -> System.DateTime
NCrontab.CrontabSchedule.GetNextOccurrences(System.DateTime baseTime, System.DateTime endTime) -> System.Collections.Generic.IEnumerable<System.DateTime>!
NCrontab.CrontabSchedule.ParseOptions
NCrontab.CrontabSchedule.ParseOptions.IncludingSeconds.get -> bool
NCrontab.CrontabSchedule.ParseOptions.IncludingSeconds.set -> void
NCrontab.CrontabSchedule.ParseOptions.RequireSeconds.get -> bool
NCrontab.CrontabSchedule.ParseOptions.RequireSeconds.set -> void
NCrontab.CrontabSchedule.ParseOptions.ParseOptions() -> void
NCrontab.ExceptionProvider
NCrontab.ICrontabField
Expand Down
5 changes: 2 additions & 3 deletions NCrontabConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@
expression = expression.Trim();
var options = new CrontabSchedule.ParseOptions
{
IncludingSeconds = expression.Split(' ').Length > 5,
RequireSeconds = expression.Split(' ').Length > 5,
};

var start = ParseDateArgument(startTimeString, "start");
var end = ParseDateArgument(endTimeString, "end");
format = format ?? (options.IncludingSeconds ? "ddd, dd MMM yyyy HH:mm:ss"
: "ddd, dd MMM yyyy HH:mm");
format ??= options.RequireSeconds ? "ddd, dd MMM yyyy HH:mm:ss" : "ddd, dd MMM yyyy HH:mm";

var schedule = CrontabSchedule.Parse(expression, options);

Expand Down
2 changes: 1 addition & 1 deletion NCrontabViewer/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ void DoCrontabbing()
return;

_isSixPart = expression.Split(Separators, StringSplitOptions.RemoveEmptyEntries).Length == 6;
_crontab = CrontabSchedule.Parse(expression, new CrontabSchedule.ParseOptions { IncludingSeconds = _isSixPart });
_crontab = CrontabSchedule.Parse(expression, new CrontabSchedule.ParseOptions { RequireSeconds = _isSixPart });

_totalOccurrenceCount = 0;

Expand Down