Skip to content

Commit

Permalink
Minor tests cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mkoertgen committed Mar 7, 2021
1 parent 2e952fe commit 7e8a8e9
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 5 deletions.
28 changes: 28 additions & 0 deletions FontAwesome.Sharp.Tests/WPF/TestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Windows.Media;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

namespace FontAwesome.Sharp.Tests.WPF
{
internal static class TestExtensions
{
public static ImageSourceAssertions Should(this ImageSource imageSource)
{
return new(imageSource);
}

internal class ImageSourceAssertions : ReferenceTypeAssertions<ImageSource, ImageSourceAssertions>
{
public ImageSourceAssertions(ImageSource imageSource) : base(imageSource)
{
}

protected override string Identifier => nameof(ImageSource);

public void NotBeEmpty()
{
Execute.Assertion.FailWith("Expected {context} not to be empty, but was.");
}
}
}
}
1 change: 0 additions & 1 deletion FontAwesome.Sharp.Tests/WindowsForms/IconCache_Should.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Drawing;
using FluentAssertions;
using Xunit;

namespace FontAwesome.Sharp.Tests.WindowsForms
Expand Down
52 changes: 52 additions & 0 deletions FontAwesome.Sharp.Tests/WindowsForms/TestExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System.Drawing;
using FluentAssertions.Execution;
using FluentAssertions.Primitives;

namespace FontAwesome.Sharp.Tests.WindowsForms
{
internal static class TestExtensions
{
public static BitmapAssertions Should(this Bitmap bitmap)
{
return new(bitmap);
}

//public static ColorAssertions Should(this Color color)
//{
// return new ColorAssertions(color);
//}

/*
internal class ColorAssertions : ObjectAssertions //ReferenceTypeAssertions<Color, ColorAssertions>
{
public ColorAssertions(Color color) : base(color)
{
}
protected override string Identifier => nameof(Color);
}*/

internal class BitmapAssertions : ReferenceTypeAssertions<Bitmap, BitmapAssertions>
{
public BitmapAssertions(Bitmap bitmap) : base(bitmap)
{
}

protected override string Identifier => nameof(Bitmap);

public void NotBeEmpty()
{
for (var y = 0; y < Subject.Height; y++)
for (var x = 0; x < Subject.Width; x++)
{
var color = Subject.GetPixel(x, y);
var isEmpty = color.IsEmpty || color.ToArgb() == 0;
if (!isEmpty)
return;
}

Execute.Assertion.FailWith("Expected {context} not to be empty, but was.");
}
}
}
}
8 changes: 4 additions & 4 deletions FontAwesome.Sharp/WinForms/FormsIconHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ private static Font GetAdjustedIconFont(this Graphics g, FontFamily fontFamily,

internal static FontFamily FontFamilyFor(this IconChar iconChar)
{
if (Fonts.Value == null) return Throw<FontFamily>("FontAwesome source font files not found!");
if (Fonts.Value == null) return Throw("FontAwesome source font files not found!");
var name = IconHelper.FontFor(iconChar)?.Source;
if (name == null) return FallbackFont.Value;
return Fonts.Value.Families.FirstOrDefault(f => name.EndsWith(f.Name, StringComparison.InvariantCultureIgnoreCase)) ?? FallbackFont.Value;
Expand All @@ -245,17 +245,17 @@ internal static FontFamily FontFamilyFor(this IconChar iconChar, IconFont iconFo
var key = (int)iconFont;
if (FontForStyle.TryGetValue(key, out var fontFamily)) return fontFamily;
if (!IconHelper.FontTitles.TryGetValue((int)iconFont, out var name))
return Throw<FontFamily>($"No font loaded for style: {iconFont}");
return Throw($"No font loaded for style: {iconFont}");

fontFamily = Fonts.Value.Families.FirstOrDefault(f => f.Name.Equals(name));
if (fontFamily == null)
return Throw<FontFamily>($"No font loaded for '{name}'");
return Throw($"No font loaded for '{name}'");

FontForStyle.Add(key, fontFamily);
return fontFamily;
}

internal static T Throw<T>(string message)
internal static FontFamily Throw(string message)
{
if (ThrowOnNullFonts) throw new InvalidOperationException(message);
return default;
Expand Down

0 comments on commit 7e8a8e9

Please sign in to comment.