Browse Source

Ensure that TryParse won't throw.

pull/4323/head
Dariusz Komosinski 6 years ago
parent
commit
c414e26cf6
  1. 13
      src/Avalonia.Visuals/Media/Color.cs
  2. 20
      tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs

13
src/Avalonia.Visuals/Media/Color.cs

@ -89,6 +89,11 @@ namespace Avalonia.Media
/// <returns>The <see cref="Color"/>.</returns>
public static Color Parse(string s)
{
if (s is null)
{
throw new ArgumentNullException(nameof(s));
}
if (TryParse(s, out Color color))
{
return color;
@ -120,14 +125,16 @@ namespace Avalonia.Media
/// <returns>The status of the operation.</returns>
public static bool TryParse(string s, out Color color)
{
color = default;
if (s == null)
{
throw new ArgumentNullException(nameof(s));
return false;
}
if (s.Length == 0)
{
throw new FormatException();
return false;
}
if (s[0] == '#' && TryParseInternal(s.AsSpan(), out color))
@ -144,8 +151,6 @@ namespace Avalonia.Media
return true;
}
color = default;
return false;
}

20
tests/Avalonia.Visuals.UnitTests/Media/ColorTests.cs

@ -179,5 +179,25 @@ namespace Avalonia.Visuals.UnitTests.Media
{
Assert.False(Color.TryParse("#ff808g80", out _));
}
[Fact]
public void Parse_Throws_ArgumentNullException_For_Null_Input()
{
Assert.Throws<ArgumentNullException>(() => Color.Parse((string)null));
}
[Fact]
public void Parse_Throws_FormatException_For_Invalid_Input()
{
Assert.Throws<FormatException>(() => Color.Parse(string.Empty));
}
[Theory]
[InlineData("")]
[InlineData(null)]
public void TryParse_Returns_False_For_Invalid_Input(string input)
{
Assert.False(Color.TryParse(input, out _));
}
}
}

Loading…
Cancel
Save