Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Test false color parsing
  • Loading branch information
JimBobSquarePants committed Feb 4, 2020
commit 343d75adef1ef55d01af891ccf19482f0366cee6
7 changes: 7 additions & 0 deletions src/ImageSharp/Color/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ public static Color Parse(string input)
/// </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;
Expand Down
64 changes: 62 additions & 2 deletions tests/ImageSharp.Tests/Color/ColorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void WebSafePalette_IsCorrect()

for (int i = 0; i < ReferencePalette.WebSafeColors.Length; i++)
{
Assert.Equal(ReferencePalette.WebSafeColors[i], actualPalette[i]);
Assert.Equal((Rgba32)ReferencePalette.WebSafeColors[i], actualPalette[i]);
}
}

Expand All @@ -78,7 +78,7 @@ public void WernerPalette_IsCorrect()

for (int i = 0; i < ReferencePalette.WernerColors.Length; i++)
{
Assert.Equal(ReferencePalette.WernerColors[i], actualPalette[i]);
Assert.Equal((Rgba32)ReferencePalette.WernerColors[i], actualPalette[i]);
}
}

Expand Down Expand Up @@ -118,11 +118,35 @@ public void ThrowsOnEmpty()
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
Expand Down Expand Up @@ -156,6 +180,42 @@ public void TryColorNames()
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 _));
}
}
}
}