forked from fluentscheduler/FluentScheduler
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26ee40a
commit e9bb639
Showing
6 changed files
with
237 additions
and
19 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
FluentScheduler.UnitTests/Fluent/3 - Duration/Day/ExceptionUnitTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
namespace FluentScheduler.UnitTests | ||
{ | ||
using System; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class ExceptionUnitTests | ||
{ | ||
[TestMethod] | ||
public void Except() | ||
{ | ||
// Arrange | ||
var now = new DateTime(2019, 2, 25); | ||
var expected = new DateTime(2019, 2, 27); | ||
var exceptionalDays = new DayOfWeek[2] | ||
{ | ||
DayOfWeek.Monday, | ||
DayOfWeek.Tuesday, | ||
}; | ||
|
||
var fluentCalculator = new FluentTimeCalculator(); | ||
var calculator = (ITimeCalculator)fluentCalculator; | ||
var run = new ExceptionUnit(fluentCalculator); | ||
|
||
// Act | ||
run.Except(exceptionalDays); | ||
var calculated = calculator.Calculate(now); | ||
|
||
// Assert | ||
Assert.AreEqual(expected, calculated.Value); | ||
} | ||
|
||
[TestMethod] | ||
public void ExceptEmpty() | ||
{ | ||
// Arrange | ||
var now = new DateTime(2019, 2, 25); | ||
var expected = new DateTime(2019, 2, 25); | ||
var exceptionalDays = new DayOfWeek[2]; | ||
|
||
var fluentCalculator = new FluentTimeCalculator(); | ||
var calculator = (ITimeCalculator)fluentCalculator; | ||
var run = new ExceptionUnit(fluentCalculator); | ||
|
||
// Act | ||
run.Except(exceptionalDays); | ||
var calculated = calculator.Calculate(now); | ||
|
||
// Assert | ||
Assert.AreEqual(expected, calculated.Value); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(ArgumentException))] | ||
public void ExceptEveryDay() | ||
{ | ||
// Arrange | ||
var now = new DateTime(2019, 2, 25); | ||
var allDays = new DayOfWeek[7] | ||
{ | ||
DayOfWeek.Sunday, | ||
DayOfWeek.Monday, | ||
DayOfWeek.Tuesday, | ||
DayOfWeek.Wednesday, | ||
DayOfWeek.Thursday, | ||
DayOfWeek.Friday, | ||
DayOfWeek.Saturday, | ||
}; | ||
|
||
var fluentCalculator = new FluentTimeCalculator(); | ||
var calculator = (ITimeCalculator)fluentCalculator; | ||
var run = new ExceptionUnit(fluentCalculator); | ||
|
||
// Act / Assert | ||
run.Except(allDays); | ||
calculator.Calculate(now); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ | |
[TestClass] | ||
public class PeriodDurationSetTests | ||
{ | ||
|
||
[TestMethod] | ||
public void Seconds() | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
namespace FluentScheduler | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
public class ExceptionUnit | ||
{ | ||
private readonly FluentTimeCalculator _calculator; | ||
|
||
internal ExceptionUnit(FluentTimeCalculator calculator) => _calculator = calculator; | ||
|
||
/// <summary> | ||
/// Excludes given days from job scheduling. | ||
/// </summary> | ||
/// <param name="exceptionalDays">Days to exclude</param> | ||
public void Except(params DayOfWeek[] exceptionalDays) | ||
{ | ||
var allDays = (IEnumerable<DayOfWeek>)Enum.GetValues(typeof(DayOfWeek)); | ||
|
||
if (allDays.All(day => exceptionalDays.Contains(day))) | ||
throw new ArgumentException($"\"{nameof(exceptionalDays)}\" cannot contain all days of week."); | ||
|
||
_calculator.PeriodCalculations.Add(last => | ||
{ | ||
while (exceptionalDays.Contains(last.DayOfWeek)) | ||
last = last.AddDays(1); | ||
return last; | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters