From 7667bb8fef4b33aa0b7863a55f92e6c29a56b30c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20Su=C3=A1rez?= Date: Tue, 8 Aug 2023 18:59:37 +0200 Subject: [PATCH] Fix Graphics Font comparison method (#15985) * Fix the issue * Added more tests * Updated Impl --- src/Graphics/src/Graphics/Font.cs | 7 ++-- .../tests/Graphics.Tests/FontUnitTests.cs | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) create mode 100644 src/Graphics/tests/Graphics.Tests/FontUnitTests.cs diff --git a/src/Graphics/src/Graphics/Font.cs b/src/Graphics/src/Graphics/Font.cs index d5ec5dabe798..7811b4611c05 100644 --- a/src/Graphics/src/Graphics/Font.cs +++ b/src/Graphics/src/Graphics/Font.cs @@ -45,8 +45,11 @@ public Font(string name, int weight = FontWeights.Normal, FontStyleType styleTyp public FontStyleType StyleType { get; private set; } public bool Equals(IFont other) - => StyleType == other.StyleType && Weight == other.Weight && Name.Equals(other.Name, StringComparison.Ordinal); - + => + StyleType == other.StyleType && + Weight == other.Weight && + ((Name is null && other.Name is null) || Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase)); + public override bool Equals(object obj) => obj is IFont font && Equals(font); diff --git a/src/Graphics/tests/Graphics.Tests/FontUnitTests.cs b/src/Graphics/tests/Graphics.Tests/FontUnitTests.cs new file mode 100644 index 000000000000..7a770531e39f --- /dev/null +++ b/src/Graphics/tests/Graphics.Tests/FontUnitTests.cs @@ -0,0 +1,33 @@ +using Xunit; + +namespace Microsoft.Maui.Graphics.Tests +{ + public class FontUnitTests + { + [Fact] + public void TestEqualsDefault() + { + Assert.True(Font.Default.Equals(Font.Default)); + } + + [Fact] + public void TestEqualsDefaultBold() + { + Assert.True(Font.DefaultBold.Equals(Font.DefaultBold)); + } + + [Fact] + public void TestEqualsDefaultFonts() + { + Assert.False(Font.Default.Equals(Font.DefaultBold)); + } + + [Fact] + public void TestEqualsArialFont() + { + var font1 = new Font("Arial"); + var font2 = new Font("Arial"); + Assert.True(font1.Equals(font2)); + } + } +}