|
7 | 7 | #include <iostream> |
8 | 8 |
|
9 | 9 | using namespace libcron; |
| 10 | +const auto EXPECT_FAILURE = true; |
10 | 11 |
|
11 | | -void test(const char* const random_schedule) |
| 12 | +void test(const char* const random_schedule, bool expect_failure = false) |
12 | 13 | { |
13 | 14 | libcron::CronRandomization cr; |
14 | | - std::unordered_map<int, std::unordered_map<int, int>> results{}; |
15 | 15 |
|
16 | 16 | for (int i = 0; i < 5000; ++i) |
17 | 17 | { |
18 | 18 | auto res = cr.parse(random_schedule); |
19 | | - REQUIRE(std::get<0>(res)); |
20 | 19 | auto schedule = std::get<1>(res); |
21 | 20 |
|
22 | | - INFO("schedule:" << schedule); |
23 | 21 | Cron<> cron; |
24 | | - REQUIRE(cron.add_schedule("validate schedule", schedule, []() {})); |
| 22 | + |
| 23 | + if(expect_failure) |
| 24 | + { |
| 25 | + // Parsing of random might succeed, but it yields an invalid schedule. |
| 26 | + auto r = std::get<0>(res) && cron.add_schedule("validate schedule", schedule, []() {}); |
| 27 | + REQUIRE_FALSE(r); |
| 28 | + } |
| 29 | + else |
| 30 | + { |
| 31 | + REQUIRE(std::get<0>(res)); |
| 32 | + REQUIRE(cron.add_schedule("validate schedule", schedule, []() {})); |
| 33 | + |
| 34 | + } |
25 | 35 | } |
26 | 36 | } |
27 | 37 |
|
@@ -103,3 +113,68 @@ SCENARIO("Test readme examples") |
103 | 113 | } |
104 | 114 | } |
105 | 115 | } |
| 116 | + |
| 117 | +SCENARIO("Randomization using text versions of days and months") |
| 118 | +{ |
| 119 | + GIVEN("0 0 0 ? * R(TUE-FRI)") |
| 120 | + { |
| 121 | + THEN("Valid schedule generated") |
| 122 | + { |
| 123 | + test("0 0 0 ? * R(TUE-FRI)"); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + GIVEN("Valid schedule") |
| 128 | + { |
| 129 | + THEN("Valid schedule generated") |
| 130 | + { |
| 131 | + test("0 0 0 ? R(JAN-DEC) R(MON-FRI)"); |
| 132 | + } |
| 133 | + AND_WHEN("Given 0 0 0 ? R(DEC-MAR) R(SAT-SUN)") |
| 134 | + { |
| 135 | + THEN("Valid schedule generated") |
| 136 | + { |
| 137 | + test("0 0 0 ? R(DEC-MAR) R(SAT-SUN)"); |
| 138 | + } |
| 139 | + } |
| 140 | + AND_THEN("Given 0 0 0 ? R(JAN-FEB) *") |
| 141 | + { |
| 142 | + THEN("Valid schedule generated") |
| 143 | + { |
| 144 | + test("0 0 0 ? R(JAN-FEB) *"); |
| 145 | + } |
| 146 | + } |
| 147 | + AND_THEN("Given 0 0 0 ? R(OCT-OCT) *") |
| 148 | + { |
| 149 | + THEN("Valid schedule generated") |
| 150 | + { |
| 151 | + test("0 0 0 ? R(OCT-OCT) *"); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + GIVEN("Invalid schedule") |
| 157 | + { |
| 158 | + THEN("No schedule generated") |
| 159 | + { |
| 160 | + // Day of month specified - not allowed with day of week |
| 161 | + test("0 0 0 1 R(JAN-DEC) R(MON-SUN)", EXPECT_FAILURE); |
| 162 | + } |
| 163 | + AND_THEN("No schedule generated") |
| 164 | + { |
| 165 | + // Invalid range |
| 166 | + test("0 0 0 ? R(JAN) *", EXPECT_FAILURE); |
| 167 | + } |
| 168 | + AND_THEN("No schedule generated") |
| 169 | + { |
| 170 | + // Days in month field |
| 171 | + test("0 0 0 ? R(MON-TUE) *", EXPECT_FAILURE); |
| 172 | + } |
| 173 | + AND_THEN("No schedule generated") |
| 174 | + { |
| 175 | + // Month in day field |
| 176 | + test("0 0 0 ? * R(JAN-JUN)", EXPECT_FAILURE); |
| 177 | + } |
| 178 | + |
| 179 | + } |
| 180 | +} |
0 commit comments