Browse Source

Test false color parsing

af/octree-no-pixelmap
James Jackson-South 6 years ago
parent
commit
ad8525e6c1
  1. 7
      src/ImageSharp/Color/Color.cs
  2. 64
      tests/ImageSharp.Tests/Color/ColorTests.cs

7
src/ImageSharp/Color/Color.cs

@ -174,6 +174,13 @@ namespace SixLabors.ImageSharp
/// </returns>
public static bool TryParse(string input, out Color result)
{
result = default;
if (string.IsNullOrWhiteSpace(input))
{
return false;
}
if (NamedColorsLookupLazy.Value.TryGetValue(input, out result))
{
return true;

64
tests/ImageSharp.Tests/Color/ColorTests.cs

@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Tests
for (int i = 0; i < ReferencePalette.WebSafeColors.Length; i++)
{
Assert.Equal(ReferencePalette.WebSafeColors[i], actualPalette[i]);
Assert.Equal((Rgba32)ReferencePalette.WebSafeColors[i], actualPalette[i]);
}
}
@ -78,7 +78,7 @@ namespace SixLabors.ImageSharp.Tests
for (int i = 0; i < ReferencePalette.WernerColors.Length; i++)
{
Assert.Equal(ReferencePalette.WernerColors[i], actualPalette[i]);
Assert.Equal((Rgba32)ReferencePalette.WernerColors[i], actualPalette[i]);
}
}
@ -118,11 +118,35 @@ namespace SixLabors.ImageSharp.Tests
Assert.Throws<ArgumentException>(() => Color.ParseHex(string.Empty));
}
[Fact]
public void ThrowsOnInvalid()
{
Assert.Throws<ArgumentException>(() => Color.ParseHex("!"));
}
[Fact]
public void ThrowsOnNull()
{
Assert.Throws<ArgumentNullException>(() => Color.ParseHex(null));
}
[Fact]
public void FalseOnEmpty()
{
Assert.False(Color.TryParseHex(string.Empty, out Color _));
}
[Fact]
public void FalseOnInvalid()
{
Assert.False(Color.TryParseHex("!", out Color _));
}
[Fact]
public void FalseOnNull()
{
Assert.False(Color.TryParseHex(null, out Color _));
}
}
public class FromString
@ -156,6 +180,42 @@ namespace SixLabors.ImageSharp.Tests
Assert.Equal(expected, (Rgba32)actual);
}
}
[Fact]
public void ThrowsOnEmpty()
{
Assert.Throws<ArgumentException>(() => Color.Parse(string.Empty));
}
[Fact]
public void ThrowsOnInvalid()
{
Assert.Throws<ArgumentException>(() => Color.Parse("!"));
}
[Fact]
public void ThrowsOnNull()
{
Assert.Throws<ArgumentNullException>(() => Color.Parse(null));
}
[Fact]
public void FalseOnEmpty()
{
Assert.False(Color.TryParse(string.Empty, out Color _));
}
[Fact]
public void FalseOnInvalid()
{
Assert.False(Color.TryParse("!", out Color _));
}
[Fact]
public void FalseOnNull()
{
Assert.False(Color.TryParse(null, out Color _));
}
}
}
}

Loading…
Cancel
Save