PixelSizeTests.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Collections.Generic;
  3. using Xunit;
  4. namespace Avalonia.Base.UnitTests;
  5. public class PixelSizeTests
  6. {
  7. [Theory]
  8. [MemberData(nameof(ParseArguments))]
  9. public void Parse(string source, PixelSize expected, Exception exception)
  10. {
  11. Exception error = null;
  12. PixelSize result = default;
  13. try
  14. {
  15. result = PixelSize.Parse(source);
  16. }
  17. catch (Exception ex)
  18. {
  19. error = ex;
  20. }
  21. Assert.Equal(exception?.Message, error?.Message);
  22. Assert.Equal(expected, result);
  23. }
  24. [Theory]
  25. [MemberData(nameof(TryParseArguments))]
  26. public void TryParse(string source, PixelSize? expected, Exception exception)
  27. {
  28. Exception error = null;
  29. PixelSize result = PixelSize.Empty;
  30. try
  31. {
  32. PixelSize.TryParse(source, out result);
  33. }
  34. catch (Exception ex)
  35. {
  36. error = ex;
  37. }
  38. Assert.Equal(exception?.Message, error?.Message);
  39. Assert.Equal(expected, result);
  40. }
  41. public static IEnumerable<object[]> ParseArguments()
  42. {
  43. yield return new object[]
  44. {
  45. "1024,768",
  46. new PixelSize(1024, 768),
  47. null,
  48. };
  49. yield return new object[]
  50. {
  51. "1024x768",
  52. default(PixelSize),
  53. new FormatException("Invalid PixelSize."),
  54. };
  55. }
  56. public static IEnumerable<object[]> TryParseArguments()
  57. {
  58. yield return new object[]
  59. {
  60. "1024,768",
  61. new PixelSize(1024, 768),
  62. null,
  63. };
  64. yield return new object[]
  65. {
  66. "1024x768",
  67. PixelSize.Empty,
  68. null,
  69. };
  70. }
  71. }