From f7bcb5f3ab55fa26231f7e2dfa6f2ace451960e2 Mon Sep 17 00:00:00 2001 From: workgroupengineering Date: Fri, 5 Apr 2024 13:35:41 +0200 Subject: [PATCH] feat: PixelSize TryParse (#14979) * feat: PixelSize TryParse * test: Add PixelSize Parse and TryParse * fix: Address review * fix: Address Review * fix: Address review --- src/Avalonia.Base/PixelSize.cs | 34 +++++++- .../Avalonia.Base.UnitTests/PixelSizeTests.cs | 77 +++++++++++++++++++ 2 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 tests/Avalonia.Base.UnitTests/PixelSizeTests.cs diff --git a/src/Avalonia.Base/PixelSize.cs b/src/Avalonia.Base/PixelSize.cs index 251fe6ff85..3d4896a45d 100644 --- a/src/Avalonia.Base/PixelSize.cs +++ b/src/Avalonia.Base/PixelSize.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.Globalization; using Avalonia.Utilities; @@ -67,13 +68,38 @@ namespace Avalonia /// /// The string. /// The . + /// public static PixelSize Parse(string s) { - using (var tokenizer = new StringTokenizer(s, CultureInfo.InvariantCulture, exceptionMessage: "Invalid PixelSize.")) + if (TryParse(s, out var result)) { - return new PixelSize( - tokenizer.ReadInt32(), - tokenizer.ReadInt32()); + return result; + } + throw new FormatException("Invalid PixelSize."); + } + + /// + /// Try parsing as . + /// + /// The to parse. + /// The result of parsing. if is not valid is + /// true if is valid , otherwise false. + public static bool TryParse([NotNullWhen(true)] string? source, + out PixelSize result) + { + result = Empty; + if(string.IsNullOrEmpty(source)) + { + return false; + } + using (var tokenizer = new StringTokenizer(source, exceptionMessage: "Invalid PixelSize.")) + { + if (tokenizer.TryReadInt32(out var w) && tokenizer.TryReadInt32(out var h)) + { + result = new(w, h); + return true; + } + return false; } } diff --git a/tests/Avalonia.Base.UnitTests/PixelSizeTests.cs b/tests/Avalonia.Base.UnitTests/PixelSizeTests.cs new file mode 100644 index 0000000000..d04237d53d --- /dev/null +++ b/tests/Avalonia.Base.UnitTests/PixelSizeTests.cs @@ -0,0 +1,77 @@ +using System; +using System.Collections.Generic; +using Xunit; + +namespace Avalonia.Base.UnitTests; + +public class PixelSizeTests +{ + [Theory] + [MemberData(nameof(ParseArguments))] + public void Parse(string source, PixelSize expected, Exception exception) + { + Exception error = null; + PixelSize result = default; + try + { + result = PixelSize.Parse(source); + } + catch (Exception ex) + { + error = ex; + } + Assert.Equal(exception?.Message, error?.Message); + Assert.Equal(expected, result); + } + + [Theory] + [MemberData(nameof(TryParseArguments))] + public void TryParse(string source, PixelSize? expected, Exception exception) + { + Exception error = null; + PixelSize result = PixelSize.Empty; + try + { + + PixelSize.TryParse(source, out result); + } + catch (Exception ex) + { + error = ex; + } + Assert.Equal(exception?.Message, error?.Message); + Assert.Equal(expected, result); + } + + public static IEnumerable ParseArguments() + { + yield return new object[] + { + "1024,768", + new PixelSize(1024, 768), + null, + }; + yield return new object[] + { + "1024x768", + default(PixelSize), + new FormatException("Invalid PixelSize."), + }; + } + + public static IEnumerable TryParseArguments() + { + yield return new object[] + { + "1024,768", + new PixelSize(1024, 768), + null, + }; + yield return new object[] + { + "1024x768", + PixelSize.Empty, + null, + }; + } +}