ComparerTests.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using Avalonia.Collections;
  6. using Avalonia.Logging;
  7. using Xunit;
  8. namespace Avalonia.Controls.DataGridTests.Collections;
  9. public class NoStringTypeComparerTests
  10. {
  11. public static IEnumerable<object[]> GetComparerForNotStringTypeParameters()
  12. {
  13. yield return
  14. [
  15. nameof(Item.IntProp1),
  16. (object item, object value) =>
  17. {
  18. (item as Item)!.IntProp1 = (int)value;
  19. },
  20. (object item) => (object)(item as Item)!.IntProp1,
  21. new object[] { 2, 3, 1 },
  22. new object[] { 1, 2, 3 }
  23. ];
  24. yield return
  25. [
  26. nameof(Item.IntProp2),
  27. (object item, object value) =>
  28. {
  29. (item as Item)!.IntProp2 = (int?)value;
  30. },
  31. (object item) => (object)(item as Item)!.IntProp2,
  32. new object[] { 2, 3, null, 1 },
  33. new object[] { null, 1, 2, 3 }
  34. ];
  35. yield return
  36. [
  37. nameof(Item.DoubleProp1),
  38. (object item, object value) =>
  39. {
  40. (item as Item)!.DoubleProp1 = (double)value;
  41. },
  42. (object item) => (object)(item as Item)!.DoubleProp1,
  43. new object[] { 2.1, 3.1, 1.1 },
  44. new object[] { 1.1, 2.1, 3.1 }
  45. ];
  46. yield return
  47. [
  48. nameof(Item.DoubleProp2),
  49. (object item, object value) =>
  50. {
  51. (item as Item)!.DoubleProp2 = (double?)value;
  52. },
  53. (object item) => (object)(item as Item)!.DoubleProp2,
  54. new object[] { 2.1, 3.1, null, 1.1 },
  55. new object[] { null, 1.1, 2.1, 3.1 }
  56. ];
  57. yield return
  58. [
  59. nameof(Item.DecimalProp1),
  60. (object item, object value) =>
  61. {
  62. (item as Item)!.DecimalProp1 = (decimal)value;
  63. },
  64. (object item) => (object)(item as Item)!.DecimalProp1,
  65. new object[] { 2.1M, 3.1M, 1.1M },
  66. new object[] { 1.1M, 2.1M, 3.1M }
  67. ];
  68. yield return
  69. [
  70. nameof(Item.DecimalProp2),
  71. (object item, object value) =>
  72. {
  73. (item as Item)!.DecimalProp2 = (decimal?)value;
  74. },
  75. (object item) => (object)(item as Item)!.DecimalProp2,
  76. new object[] { 2.1M, 3.1M, null, 1.1M },
  77. new object[] { null, 1.1M, 2.1M, 3.1M }
  78. ];
  79. yield return
  80. [
  81. nameof(Item.EnumProp1),
  82. (object item, object value) =>
  83. {
  84. (item as Item)!.EnumProp1 = (LogEventLevel)value;
  85. },
  86. (object item) => (object)(item as Item)!.EnumProp1,
  87. new object[] { LogEventLevel.Information, LogEventLevel.Debug, LogEventLevel.Error },
  88. new object[] { LogEventLevel.Debug, LogEventLevel.Information, LogEventLevel.Error }
  89. ];
  90. yield return
  91. [
  92. nameof(Item.EnumProp2),
  93. (object item, object value) =>
  94. {
  95. (item as Item)!.EnumProp2 = (LogEventLevel?)value;
  96. },
  97. (object item) => (object)(item as Item)!.EnumProp2,
  98. new object[]
  99. {
  100. LogEventLevel.Information,
  101. LogEventLevel.Debug,
  102. null,
  103. LogEventLevel.Error
  104. },
  105. new object[]
  106. {
  107. null,
  108. LogEventLevel.Debug,
  109. LogEventLevel.Information,
  110. LogEventLevel.Error
  111. }
  112. ];
  113. yield return
  114. [
  115. nameof(Item.CustomProp2),
  116. (object item, object value) =>
  117. {
  118. (item as Item)!.CustomProp2 = (CustomType?)value;
  119. },
  120. (object item) => (object)(item as Item)!.CustomProp2,
  121. new object[]
  122. {
  123. new CustomType() { Prop = 2 },
  124. new CustomType() { Prop = 3 },
  125. null,
  126. new CustomType() { Prop = 1 }
  127. },
  128. new object[]
  129. {
  130. null,
  131. new CustomType() { Prop = 1 },
  132. new CustomType() { Prop = 2 },
  133. new CustomType() { Prop = 3 }
  134. }
  135. ];
  136. }
  137. [Theory]
  138. [MemberData(nameof(GetComparerForNotStringTypeParameters))]
  139. public void GetComparerForNotStringType_Correctly_WhenSorting(
  140. string pathName,
  141. Action<object, object> setAction,
  142. Func<object, object> getAction,
  143. object[] orignal,
  144. object[] ordered
  145. )
  146. {
  147. List<Item> items = new();
  148. for (int i = 0; i < orignal.Length; i++)
  149. {
  150. var item = new Item();
  151. setAction(item, orignal[i]);
  152. items.Add(item);
  153. }
  154. //Ascending
  155. var sortDescription = DataGridSortDescription.FromPath(
  156. pathName,
  157. ListSortDirection.Ascending
  158. );
  159. sortDescription.Initialize(typeof(Item));
  160. var result = sortDescription.OrderBy(items).ToList();
  161. for (int i = 0; i < ordered.Length; i++)
  162. {
  163. Assert.Equal(ordered[i], getAction(result[i]));
  164. }
  165. //Descending
  166. sortDescription = DataGridSortDescription.FromPath(pathName, ListSortDirection.Descending);
  167. sortDescription.Initialize(typeof(Item));
  168. result = sortDescription.OrderBy(items).ToList();
  169. ordered = ordered.Reverse().ToArray();
  170. for (int i = 0; i < ordered.Length; i++)
  171. {
  172. Assert.Equal(ordered[i], getAction(result[i]));
  173. }
  174. }
  175. private class Item
  176. {
  177. public int IntProp1 { get; set; }
  178. public int? IntProp2 { get; set; }
  179. public double DoubleProp1 { get; set; }
  180. public double? DoubleProp2 { get; set; }
  181. public decimal DecimalProp1 { get; set; }
  182. public decimal? DecimalProp2 { get; set; }
  183. public LogEventLevel EnumProp1 { get; set; }
  184. public LogEventLevel? EnumProp2 { get; set; }
  185. public CustomType? CustomProp2 { get; set; }
  186. }
  187. public struct CustomType : IComparable
  188. {
  189. public int Prop { get; set; }
  190. public int CompareTo(object obj)
  191. {
  192. if (obj is CustomType other)
  193. {
  194. return Prop.CompareTo(other.Prop);
  195. }
  196. else
  197. {
  198. return 1;
  199. }
  200. }
  201. public override bool Equals(object obj)
  202. {
  203. if (obj is CustomType other)
  204. {
  205. return Prop == other.Prop;
  206. }
  207. return false;
  208. }
  209. public override int GetHashCode()
  210. => Prop;
  211. }
  212. }