|
| 1 | +using System.Xml; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace AltaSoft.DomainPrimitives.UnitTests; |
| 5 | + |
| 6 | +public class ParseTimeOnlyTests |
| 7 | +{ |
| 8 | + // ─── Valid cases ─────────────────────────────────────────────────────────── |
| 9 | + |
| 10 | + [Theory] |
| 11 | + [InlineData("15:00:00", 15, 0, 0)] |
| 12 | + [InlineData("00:00:00", 0, 0, 0)] |
| 13 | + [InlineData("23:59:59", 23, 59, 59)] |
| 14 | + [InlineData("01:02:03", 1, 2, 3)] |
| 15 | + // With positive offset |
| 16 | + [InlineData("15:00:00+04:00", 15, 0, 0)] |
| 17 | + [InlineData("15:00:00+00:00", 15, 0, 0)] |
| 18 | + [InlineData("00:00:00+05:30", 0, 0, 0)] |
| 19 | + [InlineData("23:59:59+14:00", 23, 59, 59)] |
| 20 | + // With negative offset |
| 21 | + [InlineData("15:00:00-04:00", 15, 0, 0)] |
| 22 | + [InlineData("15:00:00-00:00", 15, 0, 0)] |
| 23 | + [InlineData("00:00:00-05:30", 0, 0, 0)] |
| 24 | + |
| 25 | + // With bare plus |
| 26 | + [InlineData("15:00:00+", 15, 0, 0)] |
| 27 | + [InlineData("00:00:00+", 0, 0, 0)] |
| 28 | + public void Parse_ValidInput_ReturnsExpectedTimeOnly(string input, int hour, int minute, int second) |
| 29 | + { |
| 30 | + var result = ParseTimeOnly(input); |
| 31 | + |
| 32 | + Assert.Equal(new TimeOnly(hour, minute, second), result); |
| 33 | + } |
| 34 | + |
| 35 | + // ─── Invalid cases: has date part ───────────────────────────────────────── |
| 36 | + |
| 37 | + [Theory] |
| 38 | + [InlineData("2025/01/11")] |
| 39 | + [InlineData("11/01/2025")] |
| 40 | + public void Parse_InputWithDatePart_Throws(string input) |
| 41 | + { |
| 42 | + Assert.Throws<FormatException>(() => ParseTimeOnly(input)); |
| 43 | + } |
| 44 | + |
| 45 | + // ─── Invalid cases: malformed time ──────────────────────────────────────── |
| 46 | + |
| 47 | + [Theory] |
| 48 | + [InlineData("99:00:00")] // invalid hour |
| 49 | + [InlineData("15:60:00")] // invalid minute |
| 50 | + [InlineData("15:00:60")] // invalid second |
| 51 | + [InlineData("abc")] // garbage |
| 52 | + [InlineData("1500:00")] // malformed |
| 53 | + [InlineData("15:00:00++04:00")] // double operator |
| 54 | + [InlineData("15:00:00+25:00")] // invalid offset hour |
| 55 | + [InlineData("")] // empty |
| 56 | + [InlineData(" ")] // whitespace |
| 57 | + public void Parse_MalformedInput_Throws(string input) |
| 58 | + { |
| 59 | + Assert.Throws<FormatException>(() => ParseTimeOnly(input)); |
| 60 | + } |
| 61 | + private static TimeOnly ParseTimeOnly(string content) |
| 62 | + { |
| 63 | + var xml = $"<root>{content}</root>"; |
| 64 | + var reader = XmlReader.Create(new StringReader(xml)); |
| 65 | + reader.ReadToFollowing("root"); |
| 66 | + return reader.ReadElementContentAsTimeOnly(); |
| 67 | + } |
| 68 | +} |
0 commit comments