public ITimeSlot Build(string descriptor) { descriptor = Descriptor.Translate(descriptor, _culture, CultureInfo.InvariantCulture); if (descriptor == Descriptor.Keywords.Always) { return(new AlwaysTimeSlot()); } if (descriptor == Descriptor.Keywords.Never) { return(new NeverTimeSlot()); } var words = descriptor.Split(' ').ToList(); if (words.Contains(Descriptor.Keywords.Intersecting) || words.Contains(Descriptor.Keywords.Or) || words.Contains(Descriptor.Keywords.But) || descriptor.Contains("(") || descriptor.Contains(")")) { return(ComplexTimeSlot.FromDescriptor(descriptor, _aliasesProvider)); } if (descriptor.StartsWith(Descriptor.Keywords.Between)) { return(AbsoluteTimeSlot.FromDescriptor(descriptor)); } if (descriptor.StartsWith(":")) { return(AliasTimeSlot.FromDescriptor(descriptor, _aliasesProvider)); } if (!descriptor.StartsWith(Descriptor.Keywords.Every)) { throw new InvalidDescriptorException(descriptor); } if (words.Contains(Descriptor.Keywords.Day)) { return(DailyRecurrentTimeSlot.FromDescriptor(descriptor)); } if (words.Contains(Descriptor.Keywords.Month)) { return(MonthlyRecurrentTimeSlot.FromDescriptor(descriptor)); } if (words.Contains(Descriptor.Keywords.Week)) { return(WeeklyRecurrentTimeSlot.FromDescriptor(descriptor)); } if (words.Contains(Descriptor.Keywords.Year)) { return(YearlyRecurrentTimeSlot.FromDescriptor(descriptor)); } throw new InvalidDescriptorException(descriptor); }
public void TestToString() { { var proof = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Thursday); const string str = "EVERY WEEK BETWEEN MONDAY 00:00 AND THURSDAY 23:59"; Assert.AreEqual(str, proof.ToString()); } { var proof = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Thursday, new TimeSpan(8, 30, 00), new TimeSpan(9, 15, 00)); const string str = "EVERY WEEK BETWEEN MONDAY 08:30 AND THURSDAY 09:15"; Assert.AreEqual(str, proof.ToString()); } }
public void TestBuilder() { { var proof = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Thursday); const string str = "EVERY WEEK BETWEEN MONDAY 00:00 AND THURSDAY 23:59"; var builded = new TimeSlotBuilder(NoAliasProvider).Build(str); Assert.AreEqual(proof, builded); } { var proof = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Thursday, new TimeSpan(8, 30, 00), new TimeSpan(9, 15, 00)); const string str = "EVERY WEEK BETWEEN MONDAY 08:30 AND THURSDAY 09:15"; var builded = new TimeSlotBuilder(NoAliasProvider).Build(str); Assert.AreEqual(proof, builded); } }
public void TestContains() { { var rule = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Sunday); Assert.IsTrue(rule.Contains(System.DateTime.Now)); } { var rule = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Sunday, new TimeSpan(12, 36, 59), new TimeSpan(23, 59, 59)); Assert.IsFalse(rule.Contains(new System.DateTime(2014, 8, 4, 12, 35, 45))); } { var rule = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Sunday, new TimeSpan(12, 34, 59), new TimeSpan(23, 59, 59)); Assert.IsTrue(rule.Contains(new System.DateTime(1934, 2, 6, 12, 35, 45))); } { var rule = new WeeklyRecurrentTimeSlot(DayOfWeek.Monday, DayOfWeek.Sunday); Assert.IsTrue(rule.Contains(new System.DateTime(2015, 4, 26, 23, 59, 59))); } }