AvaloniaPropertyDictionaryTests.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using Avalonia.Utilities;
  4. using Xunit;
  5. namespace Avalonia.Base.UnitTests.Utilities
  6. {
  7. public class AvaloniaPropertyDictionaryTests
  8. {
  9. private static AvaloniaProperty[] TestProperties;
  10. static AvaloniaPropertyDictionaryTests()
  11. {
  12. TestProperties = new AvaloniaProperty[100];
  13. for (var i = 0; i < 100; ++i)
  14. {
  15. TestProperties[i] = new StyledProperty<string>(
  16. $"Test{i}",
  17. typeof(AvaloniaPropertyDictionaryTests),
  18. new StyledPropertyMetadata<string>());
  19. }
  20. Shuffle(TestProperties, 42);
  21. }
  22. [Theory]
  23. [MemberData(nameof(Counts))]
  24. public void Property_Indexer_Finds_Value(int count)
  25. {
  26. if (count == 0)
  27. return;
  28. var target = CreateTarget(count);
  29. var index = count / 2;
  30. var property = TestProperties[index];
  31. var result = target[property];
  32. Assert.Equal($"Value{index}", result);
  33. }
  34. [Theory]
  35. [MemberData(nameof(Counts))]
  36. public void Property_Indexer_Throws_If_Value_Not_Found(int count)
  37. {
  38. var target = CreateTarget(count);
  39. var index = count;
  40. var property = TestProperties[index];
  41. Assert.Throws<KeyNotFoundException>(() => target[property]);
  42. }
  43. [Theory]
  44. [MemberData(nameof(Counts))]
  45. public void Property_Indexer_Adds_New_Value(int count)
  46. {
  47. var target = CreateTarget(count);
  48. var index = count;
  49. var property = TestProperties[index];
  50. target[property] = "new";
  51. Assert.Equal("new", target[property]);
  52. }
  53. [Theory]
  54. [MemberData(nameof(Counts))]
  55. public void Property_Indexer_Sets_Existing_Value(int count)
  56. {
  57. if (count == 0)
  58. return;
  59. var target = CreateTarget(count);
  60. var index = count / 2;
  61. var property = TestProperties[index];
  62. Assert.Equal($"Value{index}", target[property]);
  63. target[property] = "new";
  64. Assert.Equal("new", target[property]);
  65. }
  66. [Theory]
  67. [MemberData(nameof(Counts))]
  68. public void Int_Indexer_Finds_Value(int count)
  69. {
  70. if (count == 0)
  71. return;
  72. var target = CreateTarget(count);
  73. var index = count / 2;
  74. var result = target[index];
  75. Assert.NotNull(result);
  76. }
  77. [Theory]
  78. [MemberData(nameof(Counts))]
  79. public void Int_Indexer_Throws_If_Index_Out_Of_Range(int count)
  80. {
  81. var target = CreateTarget(count);
  82. var index = count;
  83. Assert.Throws<IndexOutOfRangeException>(() => target[index]);
  84. }
  85. [Theory]
  86. [MemberData(nameof(Counts))]
  87. public void Add_Adds_New_Value(int count)
  88. {
  89. var target = CreateTarget(count);
  90. var index = count;
  91. var property = TestProperties[index];
  92. target.Add(property, "new");
  93. Assert.Equal("new", target[property]);
  94. }
  95. [Theory]
  96. [MemberData(nameof(Counts))]
  97. public void Add_Throws_If_Key_Exists(int count)
  98. {
  99. if (count == 0)
  100. return;
  101. var target = CreateTarget(count);
  102. var index = count / 2;
  103. var property = TestProperties[index];
  104. Assert.Throws<ArgumentException>(() => target.Add(property, "new"));
  105. }
  106. [Theory]
  107. [MemberData(nameof(Counts))]
  108. public void ContainsKey_Returns_True_If_Value_Exists(int count)
  109. {
  110. if (count == 0)
  111. return;
  112. var target = CreateTarget(count);
  113. var index = count / 2;
  114. var property = TestProperties[index];
  115. Assert.True(target.ContainsKey(property));
  116. }
  117. [Theory]
  118. [MemberData(nameof(Counts))]
  119. public void ContainsKey_Returns_False_If_Value_Does_Not_Exist(int count)
  120. {
  121. var target = CreateTarget(count);
  122. var index = count;
  123. var property = TestProperties[index];
  124. Assert.False(target.ContainsKey(property));
  125. }
  126. [Theory]
  127. [MemberData(nameof(Counts))]
  128. public void GetKeyValue_Finds_Value(int count)
  129. {
  130. if (count == 0)
  131. return;
  132. var target = CreateTarget(count);
  133. var index = count / 2;
  134. target.GetKeyValue(index, out var property, out var value);
  135. Assert.NotNull(property);
  136. Assert.NotNull(value);
  137. }
  138. [Theory]
  139. [MemberData(nameof(Counts))]
  140. public void GetKeyValue_Throws_If_Index_Out_Of_Range(int count)
  141. {
  142. var target = CreateTarget(count);
  143. var index = count;
  144. Assert.Throws<IndexOutOfRangeException>(() => target.GetKeyValue(index, out var _, out var _));
  145. }
  146. [Theory]
  147. [MemberData(nameof(Counts))]
  148. public void Remove_Removes_Value(int count)
  149. {
  150. if (count == 0)
  151. return;
  152. var target = CreateTarget(count);
  153. var index = count / 2;
  154. var property = TestProperties[index];
  155. Assert.True(target.Remove(property));
  156. Assert.False(target.ContainsKey(property));
  157. }
  158. [Theory]
  159. [MemberData(nameof(Counts))]
  160. public void Remove_Returns_False_If_Value_Not_Present(int count)
  161. {
  162. var target = CreateTarget(count);
  163. var index = count;
  164. var property = TestProperties[index];
  165. Assert.False(target.Remove(property));
  166. }
  167. [Theory]
  168. [MemberData(nameof(Counts))]
  169. public void Remove_Returns_Existing_Value(int count)
  170. {
  171. if (count == 0)
  172. return;
  173. var target = CreateTarget(count);
  174. var index = count / 2;
  175. var property = TestProperties[index];
  176. Assert.True(target.Remove(property, out var value));
  177. Assert.Equal($"Value{index}", value);
  178. }
  179. [Theory]
  180. [MemberData(nameof(Counts))]
  181. public void TryAdd_Adds_New_Value(int count)
  182. {
  183. var target = CreateTarget(count);
  184. var index = count;
  185. var property = TestProperties[index];
  186. Assert.True(target.TryAdd(property, "new"));
  187. Assert.Equal("new", target[property]);
  188. }
  189. [Theory]
  190. [MemberData(nameof(Counts))]
  191. public void TryAdd_Returns_False_If_Key_Exists(int count)
  192. {
  193. if (count == 0)
  194. return;
  195. var target = CreateTarget(count);
  196. var index = count / 2;
  197. var property = TestProperties[index];
  198. Assert.False(target.TryAdd(property, "new"));
  199. }
  200. [Theory]
  201. [MemberData(nameof(Counts))]
  202. public void TryGetValue_Finds_Value(int count)
  203. {
  204. if (count == 0)
  205. return;
  206. var target = CreateTarget(count);
  207. var index = count / 2;
  208. var property = TestProperties[index];
  209. Assert.True(target.TryGetValue(property, out var value));
  210. Assert.Equal($"Value{index}", value);
  211. }
  212. [Theory]
  213. [MemberData(nameof(Counts))]
  214. public void TryGetValue_Returns_False_If_Key_Does_Not_Exist(int count)
  215. {
  216. if (count == 0)
  217. return;
  218. var target = CreateTarget(count);
  219. var index = count;
  220. var property = TestProperties[index];
  221. Assert.False(target.TryGetValue(property, out var value));
  222. Assert.Null(value);
  223. }
  224. public static TheoryData<int> Counts()
  225. {
  226. var result = new TheoryData<int>();
  227. result.Add(0);
  228. result.Add(1);
  229. result.Add(10);
  230. result.Add(13);
  231. result.Add(50);
  232. result.Add(72);
  233. return result;
  234. }
  235. private static AvaloniaPropertyDictionary<string> CreateTarget(int items)
  236. {
  237. var result = new AvaloniaPropertyDictionary<string>();
  238. for (var i = 0; i < items; ++i)
  239. result.Add(TestProperties[i], $"Value{i}");
  240. return result;
  241. }
  242. private static void Shuffle<T>(T[] array, int seed)
  243. {
  244. var rng = new Random(seed);
  245. int n = array.Length;
  246. while (n > 1)
  247. {
  248. int k = rng.Next(n--);
  249. T temp = array[n];
  250. array[n] = array[k];
  251. array[k] = temp;
  252. }
  253. }
  254. }
  255. }