From 0c8db17b489885a2d2d120698392cf6fe8171914 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 12 Jul 2024 13:11:34 +0200 Subject: [PATCH] Include X and Y in ToString when it is specified in one of the constructors (#1674). --- src/Magick.NET/Types/MagickGeometry.cs | 55 ++++++++++++++----- .../MagickGeometryTests/TheToStringMethod.cs | 36 ++++++++++++ 2 files changed, 78 insertions(+), 13 deletions(-) diff --git a/src/Magick.NET/Types/MagickGeometry.cs b/src/Magick.NET/Types/MagickGeometry.cs index 6344254a36..87616f012f 100644 --- a/src/Magick.NET/Types/MagickGeometry.cs +++ b/src/Magick.NET/Types/MagickGeometry.cs @@ -11,18 +11,26 @@ namespace ImageMagick; /// public sealed partial class MagickGeometry : IMagickGeometry { + private readonly bool _includeXyInToString; + /// /// Initializes a new instance of the class. /// public MagickGeometry() - => Initialize(0, 0, 0, 0); + { + Initialize(0, 0, 0, 0); + _includeXyInToString = false; + } /// /// Initializes a new instance of the class using the specified width and height. /// /// The width and height. public MagickGeometry(int widthAndHeight) - => Initialize(0, 0, widthAndHeight, widthAndHeight); + { + Initialize(0, 0, widthAndHeight, widthAndHeight); + _includeXyInToString = false; + } /// /// Initializes a new instance of the class using the specified width and height. @@ -30,7 +38,10 @@ public MagickGeometry(int widthAndHeight) /// The width. /// The height. public MagickGeometry(int width, int height) - => Initialize(0, 0, width, height); + { + Initialize(0, 0, width, height); + _includeXyInToString = false; + } /// /// Initializes a new instance of the class using the specified offsets, width and height. @@ -40,7 +51,10 @@ public MagickGeometry(int width, int height) /// The width. /// The height. public MagickGeometry(int x, int y, int width, int height) - => Initialize(x, y, width, height); + { + Initialize(x, y, width, height); + _includeXyInToString = true; + } /// /// Initializes a new instance of the class using the specified width and height. @@ -48,7 +62,10 @@ public MagickGeometry(int x, int y, int width, int height) /// The percentage of the width. /// The percentage of the height. public MagickGeometry(Percentage percentageWidth, Percentage percentageHeight) - => InitializeFromPercentage(0, 0, percentageWidth, percentageHeight); + { + InitializeFromPercentage(0, 0, percentageWidth, percentageHeight); + _includeXyInToString = false; + } /// /// Initializes a new instance of the class using the specified offsets, width and height. @@ -58,7 +75,10 @@ public MagickGeometry(Percentage percentageWidth, Percentage percentageHeight) /// The percentage of the width. /// The percentage of the height. public MagickGeometry(int x, int y, Percentage percentageWidth, Percentage percentageHeight) - => InitializeFromPercentage(x, y, percentageWidth, percentageHeight); + { + InitializeFromPercentage(x, y, percentageWidth, percentageHeight); + _includeXyInToString = true; + } /// /// Initializes a new instance of the class using the specified geometry. @@ -76,6 +96,8 @@ public MagickGeometry(string value) Initialize(instance, flags); else InitializeFromAspectRation(instance, value); + + _includeXyInToString = value.IndexOf("+") >= 0 || value.IndexOf("-") >= 0; } /// @@ -325,15 +347,22 @@ public override string ToString() var result = string.Empty; - if (Width > 0) - result += Width; + if (Width == 0 && Height == 0) + { + result = "0x0"; + } + else + { + if (Width > 0) + result += Width; - if (Height > 0) - result += "x" + Height; - else if (!IsPercentage) - result += "x"; + if (Height > 0) + result += "x" + Height; + else if (!IsPercentage) + result += "x"; + } - if (X != 0 || Y != 0) + if (X != 0 || Y != 0 || _includeXyInToString) { if (X >= 0) result += '+'; diff --git a/tests/Magick.NET.Tests/Types/MagickGeometryTests/TheToStringMethod.cs b/tests/Magick.NET.Tests/Types/MagickGeometryTests/TheToStringMethod.cs index f07d975489..14d29d4a05 100644 --- a/tests/Magick.NET.Tests/Types/MagickGeometryTests/TheToStringMethod.cs +++ b/tests/Magick.NET.Tests/Types/MagickGeometryTests/TheToStringMethod.cs @@ -110,5 +110,41 @@ public void ShouldSetGreaterAndIsPercentage() Assert.Equal("50%>", geometry.ToString()); } + + [Fact] + public void ShouldIncludeZeroXYWhenSpecified() + { + var geometry = new MagickGeometry(0, 0, 5, 10); + + Assert.Equal("5x10+0+0", geometry.ToString()); + } + + [Fact] + public void ShouldReturnTheCorrectStringWhenBothWidthAndHeightAreZero() + { + var geometry = new MagickGeometry(0, 0); + + Assert.Equal("0x0", geometry.ToString()); + } + + [Fact] + public void ShouldCreateTheCorrectStringWhenAllValuesAreZero() + { + var geometry = new MagickGeometry(0, 0, 0, 0); + + Assert.Equal("0x0+0+0", geometry.ToString()); + } + + [Theory] + [InlineData("0x0+0+0")] + [InlineData("0x0-0+0")] + [InlineData("0x0+0-0")] + [InlineData("0x0-0-0")] + public void ShouldIncludeZeroXYWhenSpecifiedInStringConstructor(string value) + { + var geometry = new MagickGeometry(value); + + Assert.Equal("0x0+0+0", geometry.ToString()); + } } }