public void Format_NoValidPattern()
        {
            var pattern = new CompositePatternBuilder <LocalDate>
            {
                { LocalDatePattern.Iso, _ => false },
                { LocalDatePattern.CreateWithInvariantCulture("yyyy"), _ => false },
            }.Build();

            Assert.Throws <FormatException>(() => pattern.Format(new LocalDate(2017, 1, 1)));
        }
        public void Parse()
        {
            var pattern = new CompositePatternBuilder <LocalDate>
            {
                { LocalDatePattern.Iso, _ => true },
                { LocalDatePattern.CreateWithInvariantCulture("yyyy"), _ => false },
            }.Build();

            Assert.IsTrue(pattern.Parse("2017-03-20").Success);
            Assert.IsFalse(pattern.Parse("2017-03").Success);
            Assert.IsTrue(pattern.Parse("2017").Success);
        }
        public void Enumerators()
        {
            var pattern1 = LocalDatePattern.Iso;
            var pattern2 = LocalDatePattern.CreateWithInvariantCulture("yyyy");

            var builder = new CompositePatternBuilder <LocalDate>
            {
                { pattern1, _ => true },
                { pattern2, _ => false },
            };

            CollectionAssert.AreEqual(new[] { pattern1, pattern2 }, builder.ToList());
            CollectionAssert.AreEqual(new[] { pattern1, pattern2 }, builder.OfType <LocalDatePattern>().ToList());
        }
        public void IsoPattern(string text)
        {
            // We assert that the text round-trips. If it does, it's
            // reasonable to assume it parsed correctly...
            var shortPattern = LocalDateTimePattern.CreateWithInvariantCulture("uuuu'-'MM'-'dd'T'HH':'mm");
            var pattern      = new CompositePatternBuilder <LocalDateTime>
            {
                { LocalDateTimePattern.ExtendedIso, _ => true },
                { shortPattern, ldt => ldt.Second == 0 && ldt.NanosecondOfSecond == 0 }
            }.Build();
            var    value     = pattern.Parse(text).Value;
            string formatted = pattern.Format(value);

            Assert.AreEqual(text, formatted);
        }
Exemplo n.º 5
0
        public Options()
        {
            string[] formats =
            {
                "H'h'm'm's's'", "H'h'm'm'", "M'm's's'", "H'h'", "M'm'", "S's'"
            };
            var patterns = formats.Select(DurationPattern.CreateWithInvariantCulture);
            var builder  = new CompositePatternBuilder <Duration>();

            foreach (var pattern in patterns)
            {
                builder.Add(pattern, _ => true);
            }
            _composite = builder.Build();
        }
        public void Build_Empty()
        {
            var pattern = new CompositePatternBuilder <LocalDate>();

            Assert.Throws <InvalidOperationException>(() => pattern.Build());
        }