|
| 1 | +// This file is part of YamlDotNet - A .NET library for YAML. |
| 2 | +// Copyright (c) Antoine Aubry and contributors |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | +// this software and associated documentation files (the "Software"), to deal in |
| 6 | +// the Software without restriction, including without limitation the rights to |
| 7 | +// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 | +// of the Software, and to permit persons to whom the Software is furnished to do |
| 9 | +// so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in all |
| 12 | +// copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +// SOFTWARE. |
| 21 | + |
| 22 | +using System; |
| 23 | +using FluentAssertions; |
| 24 | +using Xunit; |
| 25 | +using YamlDotNet.Core; |
| 26 | +using YamlDotNet.Serialization; |
| 27 | + |
| 28 | +namespace YamlDotNet.Test.Serialization |
| 29 | +{ |
| 30 | + public class ScalarNodeDeserializerTests |
| 31 | + { |
| 32 | + private readonly IDeserializer deserializer = new DeserializerBuilder().Build(); |
| 33 | + |
| 34 | + [Theory] |
| 35 | + [InlineData("2024-01-15")] |
| 36 | + [InlineData("2024-12-31T23:59:59Z")] |
| 37 | + [InlineData("2024-06-15T10:30:00")] |
| 38 | + public void DateTime_ParsesValidValues(string yamlValue) |
| 39 | + { |
| 40 | + var result = deserializer.Deserialize<DateTime>(yamlValue); |
| 41 | + result.Should().BeAfter(DateTime.MinValue); |
| 42 | + } |
| 43 | + |
| 44 | + [Theory] |
| 45 | + [InlineData("not-a-date")] |
| 46 | + [InlineData("hello world")] |
| 47 | + public void DateTime_ThrowsOnInvalidValues(string yamlValue) |
| 48 | + { |
| 49 | + Action act = () => deserializer.Deserialize<DateTime>(yamlValue); |
| 50 | + act.Should().Throw<YamlException>(); |
| 51 | + } |
| 52 | + |
| 53 | + [Theory] |
| 54 | + [InlineData("1:00:00", 3600)] // 1 hour = 3600 seconds |
| 55 | + [InlineData("1:30", 90)] // 1*60 + 30 = 90 |
| 56 | + [InlineData("1:00", 60)] // 1*60 + 0 = 60 |
| 57 | + public void Base60Integer_ParsesValidValues(string yamlValue, int expected) |
| 58 | + { |
| 59 | + var result = deserializer.Deserialize<int>(yamlValue); |
| 60 | + result.Should().Be(expected); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public void Base60Integer_RejectsInvalidSexagesimalDigit() |
| 65 | + { |
| 66 | + // 1:99 is invalid because 99 >= 60 |
| 67 | + Action act = () => deserializer.Deserialize<int>("1:99"); |
| 68 | + act.Should().Throw<YamlException>() |
| 69 | + .WithInnerException<FormatException>() |
| 70 | + .WithMessage("*sexagesimal*less than 60*"); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public void Base60Integer_AllowsFirstChunkAbove59() |
| 75 | + { |
| 76 | + // The first chunk can be any value (it's the most significant digit) |
| 77 | + // 100:30 = 100*60 + 30 = 6030 |
| 78 | + var result = deserializer.Deserialize<int>("100:30"); |
| 79 | + result.Should().Be(6030); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void Base60Integer_RejectsSecondChunkOf60() |
| 84 | + { |
| 85 | + // 1:60 is invalid because 60 >= 60 |
| 86 | + Action act = () => deserializer.Deserialize<int>("1:60"); |
| 87 | + act.Should().Throw<YamlException>() |
| 88 | + .WithInnerException<FormatException>() |
| 89 | + .WithMessage("*sexagesimal*less than 60*"); |
| 90 | + } |
| 91 | + |
| 92 | + [Theory] |
| 93 | + [InlineData("0b1010", 10)] // binary |
| 94 | + [InlineData("0b11111111", 255)] // binary 255 |
| 95 | + [InlineData("010", 8)] // octal |
| 96 | + [InlineData("0x1F", 31)] // hex |
| 97 | + [InlineData("0xFF", 255)] // hex |
| 98 | + public void IntegerBases_ParseCorrectly(string yamlValue, int expected) |
| 99 | + { |
| 100 | + var result = deserializer.Deserialize<int>(yamlValue); |
| 101 | + result.Should().Be(expected); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments