Browse Source

feat: PixelSize TryParse (#14979)

* feat: PixelSize TryParse

* test: Add PixelSize Parse and TryParse

* fix: Address review

* fix: Address Review

* fix: Address review
pull/15250/head
workgroupengineering 2 years ago
committed by GitHub
parent
commit
f7bcb5f3ab
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 34
      src/Avalonia.Base/PixelSize.cs
  2. 77
      tests/Avalonia.Base.UnitTests/PixelSizeTests.cs

34
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
/// </summary>
/// <param name="s">The string.</param>
/// <returns>The <see cref="PixelSize"/>.</returns>
/// <exception cref="FormatException"/>
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.");
}
/// <summary>
/// Try parsing <paramref name="source"/> as <see cref="PixelSize"/>.
/// </summary>
/// <param name="source">The <see cref="string"/> to parse.</param>
/// <param name="result">The result of parsing. if <paramref name="source"/> is not valid <paramref name="result"/> is <see cref="PixelSize.Empty"/> </param>
/// <returns><c>true</c> if <paramref name="source"/> is valid <see cref="PixelSize"/>, otherwise <c>false</c>.</returns>
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;
}
}

77
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<object[]> 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<object[]> TryParseArguments()
{
yield return new object[]
{
"1024,768",
new PixelSize(1024, 768),
null,
};
yield return new object[]
{
"1024x768",
PixelSize.Empty,
null,
};
}
}
Loading…
Cancel
Save