|
1 | 1 | import { CronFieldCollection } from '../src/CronFieldCollection';
|
2 |
| -import { CronSecond, CronMinute, CronHour, CronDayOfMonth, CronMonth, CronDayOfWeek } from '../src/fields'; |
| 2 | +import { |
| 3 | + CronSecond, |
| 4 | + CronMinute, |
| 5 | + CronHour, |
| 6 | + CronDayOfMonth, |
| 7 | + CronMonth, |
| 8 | + CronDayOfWeek, |
| 9 | + DayOfWeekRange, |
| 10 | + MonthRange, |
| 11 | + DayOfMonthRange, |
| 12 | + HourRange, |
| 13 | + SixtyRange, |
| 14 | +} from '../src/fields'; |
3 | 15 |
|
4 | 16 | describe('CronFieldCollection', () => {
|
| 17 | + describe('getters', () => { |
| 18 | + test('should access fields and assert correct types', () => { |
| 19 | + const cronFields = new CronFieldCollection({ |
| 20 | + second: new CronSecond([0]), |
| 21 | + minute: new CronMinute([0, 30]), |
| 22 | + hour: new CronHour([9]), |
| 23 | + dayOfMonth: new CronDayOfMonth([15]), |
| 24 | + month: new CronMonth([1]), |
| 25 | + dayOfWeek: new CronDayOfWeek([1, 2, 3, 4, 5]), |
| 26 | + }); |
| 27 | + |
| 28 | + expect(cronFields.second).toBeInstanceOf(CronSecond); |
| 29 | + expect(cronFields.minute).toBeInstanceOf(CronMinute); |
| 30 | + expect(cronFields.hour).toBeInstanceOf(CronHour); |
| 31 | + expect(cronFields.dayOfMonth).toBeInstanceOf(CronDayOfMonth); |
| 32 | + expect(cronFields.month).toBeInstanceOf(CronMonth); |
| 33 | + expect(cronFields.dayOfWeek).toBeInstanceOf(CronDayOfWeek); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('serialize', () => { |
| 38 | + test('should serialize collection', () => { |
| 39 | + const expected = { |
| 40 | + second: { |
| 41 | + wildcard: false, |
| 42 | + values: Array.from(Array(60).keys()), |
| 43 | + }, |
| 44 | + minute: { |
| 45 | + wildcard: false, |
| 46 | + values: [0, 30], |
| 47 | + }, |
| 48 | + hour: { |
| 49 | + wildcard: false, |
| 50 | + values: [9, 11, 13, 15, 17], |
| 51 | + }, |
| 52 | + dayOfMonth: { |
| 53 | + wildcard: false, |
| 54 | + values: [1, 15], |
| 55 | + }, |
| 56 | + month: { |
| 57 | + wildcard: false, |
| 58 | + values: [1, 3, 5, 7, 9, 11], |
| 59 | + }, |
| 60 | + dayOfWeek: { |
| 61 | + wildcard: false, |
| 62 | + values: [1, 2, 3, 4, 5], |
| 63 | + }, |
| 64 | + }; |
| 65 | + const cronFields = new CronFieldCollection({ |
| 66 | + second: new CronSecond(<SixtyRange[]>expected.second.values), |
| 67 | + minute: new CronMinute(<SixtyRange[]>expected.minute.values), |
| 68 | + hour: new CronHour(<HourRange[]>expected.hour.values), |
| 69 | + dayOfMonth: new CronDayOfMonth(<DayOfMonthRange[]>expected.dayOfMonth.values), |
| 70 | + month: new CronMonth(<MonthRange[]>expected.month.values), |
| 71 | + dayOfWeek: new CronDayOfWeek(<DayOfWeekRange[]>expected.dayOfWeek.values), |
| 72 | + }); |
| 73 | + |
| 74 | + expect(cronFields.stringify()).toEqual('0,30 9-17/2 1,15 */2 1-5'); |
| 75 | + expect(cronFields.stringify(true)).toEqual('* 0,30 9-17/2 1,15 */2 1-5'); |
| 76 | + expect(cronFields.serialize()).toEqual(expected); |
| 77 | + }); |
| 78 | + |
| 79 | + test('should serialize fields to an object', () => { |
| 80 | + const cronFields = new CronFieldCollection({ |
| 81 | + second: new CronSecond([0]), |
| 82 | + minute: new CronMinute([0, 30]), |
| 83 | + hour: new CronHour([9]), |
| 84 | + dayOfMonth: new CronDayOfMonth([15]), |
| 85 | + month: new CronMonth([1]), |
| 86 | + dayOfWeek: new CronDayOfWeek([1, 2, 3, 4, 5]), |
| 87 | + }); |
| 88 | + |
| 89 | + expect(cronFields.dayOfMonth.serialize()).toEqual({ |
| 90 | + values: [15], |
| 91 | + wildcard: false, |
| 92 | + }); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('constructor validation', () => { |
| 97 | + test('should throw error when second field is missing', () => { |
| 98 | + expect( |
| 99 | + () => |
| 100 | + new CronFieldCollection({ |
| 101 | + minute: new CronMinute([0]), |
| 102 | + hour: new CronHour([12]), |
| 103 | + dayOfMonth: new CronDayOfMonth([1]), |
| 104 | + month: new CronMonth([1]), |
| 105 | + dayOfWeek: new CronDayOfWeek([1]), |
| 106 | + } as any), |
| 107 | + ).toThrow('Validation error, Field second is missing'); |
| 108 | + }); |
| 109 | + |
| 110 | + test('should throw error when minute field is missing', () => { |
| 111 | + expect( |
| 112 | + () => |
| 113 | + new CronFieldCollection({ |
| 114 | + second: new CronSecond([0]), |
| 115 | + hour: new CronHour([12]), |
| 116 | + dayOfMonth: new CronDayOfMonth([1]), |
| 117 | + month: new CronMonth([1]), |
| 118 | + dayOfWeek: new CronDayOfWeek([1]), |
| 119 | + } as any), |
| 120 | + ).toThrow('Validation error, Field minute is missing'); |
| 121 | + }); |
| 122 | + |
| 123 | + test('should throw error when hour field is missing', () => { |
| 124 | + expect( |
| 125 | + () => |
| 126 | + new CronFieldCollection({ |
| 127 | + second: new CronSecond([0]), |
| 128 | + minute: new CronMinute([0]), |
| 129 | + dayOfMonth: new CronDayOfMonth([1]), |
| 130 | + month: new CronMonth([1]), |
| 131 | + dayOfWeek: new CronDayOfWeek([1]), |
| 132 | + } as any), |
| 133 | + ).toThrow('Validation error, Field hour is missing'); |
| 134 | + }); |
| 135 | + |
| 136 | + test('should throw error when dayOfMonth field is missing', () => { |
| 137 | + expect( |
| 138 | + () => |
| 139 | + new CronFieldCollection({ |
| 140 | + second: new CronSecond([0]), |
| 141 | + minute: new CronMinute([0]), |
| 142 | + hour: new CronHour([12]), |
| 143 | + month: new CronMonth([1]), |
| 144 | + dayOfWeek: new CronDayOfWeek([1]), |
| 145 | + } as any), |
| 146 | + ).toThrow('Validation error, Field dayOfMonth is missing'); |
| 147 | + }); |
| 148 | + |
| 149 | + test('should throw error when month field is missing', () => { |
| 150 | + expect( |
| 151 | + () => |
| 152 | + new CronFieldCollection({ |
| 153 | + second: new CronSecond([0]), |
| 154 | + minute: new CronMinute([0]), |
| 155 | + hour: new CronHour([12]), |
| 156 | + dayOfMonth: new CronDayOfMonth([1]), |
| 157 | + dayOfWeek: new CronDayOfWeek([1]), |
| 158 | + } as any), |
| 159 | + ).toThrow('Validation error, Field month is missing'); |
| 160 | + }); |
| 161 | + |
| 162 | + test('should throw error when dayOfWeek field is missing', () => { |
| 163 | + expect( |
| 164 | + () => |
| 165 | + new CronFieldCollection({ |
| 166 | + second: new CronSecond([0]), |
| 167 | + minute: new CronMinute([0]), |
| 168 | + hour: new CronHour([12]), |
| 169 | + dayOfMonth: new CronDayOfMonth([1]), |
| 170 | + month: new CronMonth([1]), |
| 171 | + } as any), |
| 172 | + ).toThrow('Validation error, Field dayOfWeek is missing'); |
| 173 | + }); |
| 174 | + |
| 175 | + test('should throw error for invalid day of month definition', () => { |
| 176 | + expect( |
| 177 | + () => |
| 178 | + new CronFieldCollection({ |
| 179 | + second: new CronSecond([0]), |
| 180 | + minute: new CronMinute([0]), |
| 181 | + hour: new CronHour([12]), |
| 182 | + dayOfMonth: new CronDayOfMonth([31]), // February only has 28/29 days |
| 183 | + month: new CronMonth([2]), // February |
| 184 | + dayOfWeek: new CronDayOfWeek([1]), |
| 185 | + }), |
| 186 | + ).toThrow('Invalid explicit day of month definition'); |
| 187 | + }); |
| 188 | + }); |
| 189 | + |
| 190 | + describe('CronFieldCollection.compactField', () => { |
| 191 | + test('empty array', () => { |
| 192 | + const result = CronFieldCollection.compactField([]); |
| 193 | + expect(result).toEqual([]); |
| 194 | + }); |
| 195 | + |
| 196 | + test('single element array', () => { |
| 197 | + const result = CronFieldCollection.compactField([1]); |
| 198 | + expect(result).toEqual([{ start: 1, count: 1 }]); |
| 199 | + }); |
| 200 | + |
| 201 | + test('2 elements array', () => { |
| 202 | + const result = CronFieldCollection.compactField([1, 2]); |
| 203 | + expect(result).toEqual([ |
| 204 | + { start: 1, count: 1 }, |
| 205 | + { start: 2, count: 1 }, |
| 206 | + ]); |
| 207 | + }); |
| 208 | + |
| 209 | + test('2 elements array big step', () => { |
| 210 | + const result = CronFieldCollection.compactField([1, 5]); |
| 211 | + expect(result).toEqual([ |
| 212 | + { start: 1, count: 1 }, |
| 213 | + { start: 5, count: 1 }, |
| 214 | + ]); |
| 215 | + }); |
| 216 | + |
| 217 | + test('3 elements array 1 step', () => { |
| 218 | + const result = CronFieldCollection.compactField([1, 2, 3]); |
| 219 | + expect(result).toEqual([{ start: 1, end: 3, count: 3, step: 1 }]); |
| 220 | + }); |
| 221 | + |
| 222 | + test('3 elements array 1 step, dangling extra at end', () => { |
| 223 | + const result = CronFieldCollection.compactField([1, 2, 3, 5]); |
| 224 | + expect(result).toEqual([ |
| 225 | + { start: 1, end: 3, count: 3, step: 1 }, |
| 226 | + { start: 5, count: 1 }, |
| 227 | + ]); |
| 228 | + }); |
| 229 | + |
| 230 | + test('3 elements array 1 step, dangling extra at end and beginning', () => { |
| 231 | + const result = CronFieldCollection.compactField([1, 4, 5, 6, 9]); |
| 232 | + expect(result).toEqual([ |
| 233 | + { start: 1, count: 1 }, |
| 234 | + { start: 4, end: 6, count: 3, step: 1 }, |
| 235 | + { start: 9, count: 1 }, |
| 236 | + ]); |
| 237 | + }); |
| 238 | + |
| 239 | + test('2 ranges with dangling in the middle', () => { |
| 240 | + const result = CronFieldCollection.compactField([1, 2, 3, 6, 9, 11, 13]); |
| 241 | + expect(result).toEqual([ |
| 242 | + { start: 1, end: 3, count: 3, step: 1 }, |
| 243 | + { start: 6, count: 1 }, |
| 244 | + { start: 9, end: 13, count: 3, step: 2 }, |
| 245 | + ]); |
| 246 | + }); |
| 247 | + |
| 248 | + test('with chars', () => { |
| 249 | + const result = CronFieldCollection.compactField(['L', 'W']); |
| 250 | + expect(result).toEqual([ |
| 251 | + { start: 'L', count: 1 }, |
| 252 | + { start: 'W', count: 1 }, |
| 253 | + ]); |
| 254 | + }); |
| 255 | + |
| 256 | + test('with chars and range', () => { |
| 257 | + const result = CronFieldCollection.compactField([1, 'L', 'W']); |
| 258 | + expect(result).toEqual([ |
| 259 | + { start: 1, count: 1 }, |
| 260 | + { start: 'L', count: 1 }, |
| 261 | + { start: 'W', count: 1 }, |
| 262 | + ]); |
| 263 | + }); |
| 264 | + |
| 265 | + test('with chars and range (v2)', () => { |
| 266 | + const result = CronFieldCollection.compactField([1, 2, 'L', 'W']); |
| 267 | + expect(result).toEqual([ |
| 268 | + { start: 1, count: 1 }, |
| 269 | + { start: 2, count: 1 }, |
| 270 | + { start: 'L', count: 1 }, |
| 271 | + { start: 'W', count: 1 }, |
| 272 | + ]); |
| 273 | + }); |
| 274 | + |
| 275 | + test('with chars and range (v3)', () => { |
| 276 | + const result = CronFieldCollection.compactField([1, 2, 3, 'L', 'W']); |
| 277 | + expect(result).toEqual([ |
| 278 | + { start: 1, end: 3, count: 3, step: 1 }, |
| 279 | + { start: 'L', count: 1 }, |
| 280 | + { start: 'W', count: 1 }, |
| 281 | + ]); |
| 282 | + }); |
| 283 | + }); |
| 284 | + |
5 | 285 | describe('from', () => {
|
6 | 286 | let base: CronFieldCollection;
|
7 | 287 |
|
|
0 commit comments