Example #1
0
        public void UncolonedDefaultFailTests(Units units, string input)
        {
            var options = new TimeSpanParserOptions();

            options.UncolonedDefault = units;

            Assert.ThrowsException <ArgumentException>(() => TimeSpanParser.Parse(input, options));
        }
Example #2
0
        public void NoAllowDotSeparatedDayHoursExample(string parseThis)
        {
            var options = new TimeSpanParserOptions();

            options.AllowDotSeparatedDayHours = false;
            var expected = new TimeSpan(3, 18, 0);

            Assert.AreEqual(expected, TimeSpanParser.Parse(parseThis, options));
        }
Example #3
0
        public void GuideBasicsTests(string input, double seconds)
        {
            var options = new TimeSpanParserOptions();

            options.UncolonedDefault = Units.Seconds;

            var timeSpanParser = TimeSpanParser.Parse(input, options);
            var builtInParser  = TimeSpan.FromSeconds(seconds);

            Assert.AreEqual(builtInParser, timeSpanParser);
        }
        public void TooManyColonsTests3(string parseThis)
        {
            var options = new TimeSpanParserOptions();

            options.AllowDotSeparatedDayHours = false;
            options.AutoUnitsIfTooManyColons  = false;

            Console.WriteLine(parseThis);
            Console.WriteLine("number of colons: " + parseThis.Count(ch => ch == ':'));
            var actual = TimeSpanParser.Parse(parseThis); // should all pass
        }
        //[DataRow("32:18:10:00:00:00")] // ok
        //[DataRow("32:18:00:1")]
        public void TooManyColonsTests2(string parseThis)
        {
            var options = new TimeSpanParserOptions();

            options.AllowDotSeparatedDayHours = false;
            options.AutoUnitsIfTooManyColons  = false;

            Console.WriteLine(parseThis);
            Console.WriteLine("number of colons: " + parseThis.Count(ch => ch == ':'));
            Assert.ThrowsException <FormatException>(() => TimeSpanParser.Parse(parseThis, options));
        }
Example #6
0
        public void UncolonedDefaultTests(Units units, string input, string oldschool)
        {
            var options = new TimeSpanParserOptions();

            options.UncolonedDefault = units;

            var timeSpanParser = TimeSpanParser.Parse(input, options);
            var builtInParser  = TimeSpan.Parse(oldschool);

            Assert.AreEqual(builtInParser, timeSpanParser);
        }
Example #7
0
        public void AntiSplitTest3()
        {
            var options = new TimeSpanParserOptions();

            options.StrictBigToSmall = false;

            var success = TimeSpanParser.TryParse("10 days 15 seconds 20:00:00 10:00 minutes 00:20:00", out TimeSpan[] timeSpans, options);

            Assert.IsTrue(success);
            Assert.AreEqual(timeSpans.Length, 1);
            Assert.AreEqual(timeSpans[0], TimeSpan.Parse("10.20:30:15"));
        }
Example #8
0
        [DataRow("0:18:30:00,0000000", 0, 18, 30, 0, 0)] // G fr-FR
        public void ReversedFormatStringFR(string parseThis, int days, int hours, int minutes, int seconds, int milliseconds)
        {
            Console.WriteLine(parseThis);
            var expected = new TimeSpan(days, hours, minutes, seconds, milliseconds);

            var options = new TimeSpanParserOptions();

            options.FormatProvider = new CultureInfo("fr-FR");
            TimeSpan actual;
            bool     success = TimeSpanParser.TryParse(parseThis, options, out actual);

            Assert.IsTrue(success);
            Assert.AreEqual(expected, actual);
        }
Example #9
0
        public void RequireDaysAllowingDotSeperatorTests(string parseThis, string regularExpected, string withoutAllowing)
        {
            var optionsWithMinutes = new TimeSpanParserOptions();

            optionsWithMinutes.AllowDotSeparatedDayHours = false;
            optionsWithMinutes.AutoUnitsIfTooManyColons  = false; // so require days
            optionsWithMinutes.ColonedDefault            = Units.Minutes;

            var optionsWithHours = new TimeSpanParserOptions();

            optionsWithHours.AllowDotSeparatedDayHours = false;
            optionsWithHours.AutoUnitsIfTooManyColons  = false;
            optionsWithHours.ColonedDefault            = Units.Hours; // (default)

            var optionsWithDays = new TimeSpanParserOptions();

            optionsWithDays.AllowDotSeparatedDayHours = false;
            optionsWithDays.AutoUnitsIfTooManyColons  = false;
            optionsWithDays.ColonedDefault            = Units.Days;

            var optionsWithNone = new TimeSpanParserOptions();

            optionsWithNone.AllowDotSeparatedDayHours = false;
            optionsWithNone.AutoUnitsIfTooManyColons  = false;
            optionsWithNone.ColonedDefault            = Units.None;

            string withDaysText = $"{parseThis} days";

            var expectRegular         = TimeSpan.Parse(regularExpected);
            var expectWithoutAllowing = TimeSpan.Parse(withoutAllowing);

            var actualWithMinutes  = TimeSpanParser.Parse(parseThis, optionsWithMinutes);
            var actualWithMinutes2 = TimeSpanParser.Parse(withDaysText, optionsWithMinutes);
            var actualWithHours    = TimeSpanParser.Parse(parseThis, optionsWithHours);
            var actualWithHours2   = TimeSpanParser.Parse(withDaysText, optionsWithHours);
            var actualWithDays     = TimeSpanParser.Parse(parseThis, optionsWithDays);
            var actualWithDays2    = TimeSpanParser.Parse(withDaysText, optionsWithDays);
            var actualWithNone     = TimeSpanParser.Parse(parseThis, optionsWithNone);
            var actualWithNone2    = TimeSpanParser.Parse(withDaysText, optionsWithNone);

            //TODO


            Console.WriteLine("number of colons: " + parseThis.Count(ch => ch == ':'));


            //Assert.AreEqual(expectRegular, actualRegular);
            //Assert.AreEqual(expectWithoutAllowing, actualWithoutAllowing);
        }
Example #10
0
        public void TimeSpanTicks(string parseThis, long ticks)
        {
            // 10,000 ticks is one millisecond
            //     10 ticks is one microsecond
            //    0.01 tick is one nanosecond
            //       100 ns is one tick

            var expected = new TimeSpan(ticks);

            var options = new TimeSpanParserOptions();

            options.FormatProvider = new CultureInfo("en-US");

            TimeSpan actual = TimeSpanParser.Parse(parseThis, options);

            Assert.AreEqual(expected, actual);
        }
Example #11
0
        [DataRow("-1.5:00:00:00", "-1:12:00:00", "-1:12:00:00")] // 1.5 days (even with allowing, because too many colons)
        public void WithAndWithoutAllowingDotSeperatorTests(string parseThis, string regularExpected, string withoutAllowing)
        {
            var options = new TimeSpanParserOptions();

            options.AllowDotSeparatedDayHours = false;

            Console.WriteLine(parseThis);
            Console.WriteLine("number of colons: " + parseThis.Count(ch => ch == ':'));

            var actualRegular = TimeSpanParser.Parse(parseThis);
            var expectRegular = TimeSpan.Parse(regularExpected);

            var actualWithoutAllowing = TimeSpanParser.Parse(parseThis, options);
            var expectWithoutAllowing = TimeSpan.Parse(withoutAllowing);

            Assert.AreEqual(expectRegular, actualRegular);
            Assert.AreEqual(expectWithoutAllowing, actualWithoutAllowing);
        }
        //TODO: test with DefaultColoned too
        public void TooManyColonsNoAutoUnitsTests(string parseThis, bool expectSuccess)
        {
            var options = new TimeSpanParserOptions();

            options.AllowDotSeparatedDayHours = true;
            options.AutoUnitsIfTooManyColons  = false;

            if (expectSuccess)
            {
                Console.WriteLine(parseThis);
                Console.WriteLine("number of colons: " + parseThis.Count(ch => ch == ':'));
                var actual = TimeSpanParser.Parse(parseThis); // should all pass
                //TODO: expected value too
            }
            else
            {
                Assert.ThrowsException <ArgumentException>(() => TimeSpanParser.Parse(parseThis));
                //Assert.ThrowsException<FormatException>(() => TimeSpanParser.Parse(parseThis));
            }
        }
Example #13
0
        [DataRow("1:12:24:02.04", 1, 12, 23, 62, 40)] // US format
        public void FutureParseUSFormatWithFR(string parseThis)
        {
            //TODO: make this pass by fixing parser to try French format first, then fallback to invariant. Perhaps allowing a mix if unambiguous

            Console.WriteLine(parseThis);

            var fr      = new CultureInfo("fr-FR");
            var options = new TimeSpanParserOptions();

            options.FormatProvider = fr;

            // TimeSpan can parse it with fr (so why can't we?)
            var expected = TimeSpan.Parse(parseThis, fr);

            TimeSpan actual;
            bool     success = TimeSpanParser.TryParse(parseThis, options, out actual);

            Assert.IsTrue(success);
            Assert.AreEqual(expected, actual);
        }
Example #14
0
        void DoParseAndCompare(Dictionary <string, TimeSpan?> expected, string parseThis, TimeSpanParserOptions options)
        {
            Console.WriteLine(parseThis);

            Dictionary <string, TimeSpan?> matches;
            bool success = TimeSpanParser.TryParsePrefixed(parseThis, prefixes, options, out matches);

            if (expected == null)   // use expected = null to expect failure
            {
                if (success)
                {
                    Console.WriteLine(PrettyPrintTimeDict(matches, "actual"));
                }
                Assert.IsFalse(success);
                return;
            }

            Assert.IsTrue(success);

            Console.WriteLine(PrettyPrintTimeDict(expected, "expected"));
            Console.WriteLine(PrettyPrintTimeDict(matches, "actual"));

            bool itsAMatch = expected.OrderBy(kvp => kvp.Key)
                             .SequenceEqual(matches.OrderBy(kvp => kvp.Key));

            Assert.IsTrue(itsAMatch);
        }