TypeExtensionsTests.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using Xunit;
  3. namespace Jellyfin.Extensions.Tests
  4. {
  5. public class TypeExtensionsTests
  6. {
  7. [Theory]
  8. [InlineData(typeof(byte), byte.MaxValue, false)]
  9. [InlineData(typeof(short), short.MinValue, false)]
  10. [InlineData(typeof(ushort), ushort.MaxValue, false)]
  11. [InlineData(typeof(int), int.MinValue, false)]
  12. [InlineData(typeof(uint), uint.MaxValue, false)]
  13. [InlineData(typeof(long), long.MinValue, false)]
  14. [InlineData(typeof(ulong), ulong.MaxValue, false)]
  15. [InlineData(typeof(decimal), -1.0, false)]
  16. [InlineData(typeof(bool), true, false)]
  17. [InlineData(typeof(char), 'a', false)]
  18. [InlineData(typeof(string), "", false)]
  19. [InlineData(typeof(object), 1, false)]
  20. [InlineData(typeof(byte), 0, true)]
  21. [InlineData(typeof(short), 0, true)]
  22. [InlineData(typeof(ushort), 0, true)]
  23. [InlineData(typeof(int), 0, true)]
  24. [InlineData(typeof(uint), 0, true)]
  25. [InlineData(typeof(long), 0, true)]
  26. [InlineData(typeof(ulong), 0, true)]
  27. [InlineData(typeof(decimal), 0, true)]
  28. [InlineData(typeof(bool), false, true)]
  29. [InlineData(typeof(char), '\x0000', true)]
  30. [InlineData(typeof(string), null, true)]
  31. [InlineData(typeof(object), null, true)]
  32. [InlineData(typeof(PhonyClass), null, true)]
  33. [InlineData(typeof(DateTime), null, true)] // Special case handled within the test.
  34. [InlineData(typeof(DateTime), null, false)] // Special case handled within the test.
  35. [InlineData(typeof(byte?), null, true)]
  36. [InlineData(typeof(short?), null, true)]
  37. [InlineData(typeof(ushort?), null, true)]
  38. [InlineData(typeof(int?), null, true)]
  39. [InlineData(typeof(uint?), null, true)]
  40. [InlineData(typeof(long?), null, true)]
  41. [InlineData(typeof(ulong?), null, true)]
  42. [InlineData(typeof(decimal?), null, true)]
  43. [InlineData(typeof(bool?), null, true)]
  44. [InlineData(typeof(char?), null, true)]
  45. public void IsNullOrDefault_Matches_Expected(Type type, object? value, bool expectedResult)
  46. {
  47. if (type == typeof(DateTime))
  48. {
  49. if (expectedResult)
  50. {
  51. value = default(DateTime);
  52. }
  53. else
  54. {
  55. value = DateTime.Now;
  56. }
  57. }
  58. Assert.Equal(expectedResult, type.IsNullOrDefault(value));
  59. Assert.Equal(expectedResult, value.IsNullOrDefault());
  60. }
  61. private class PhonyClass
  62. {
  63. }
  64. }
  65. }