ExpressionObserverTests_Indexer.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Reactive.Linq;
  5. using System.Threading.Tasks;
  6. using Avalonia.Collections;
  7. using Avalonia.Diagnostics;
  8. using Avalonia.Data.Core;
  9. using Avalonia.UnitTests;
  10. using Xunit;
  11. using Avalonia.Markup.Parsers;
  12. namespace Avalonia.Base.UnitTests.Data.Core
  13. {
  14. public class ExpressionObserverTests_Indexer
  15. {
  16. [Fact]
  17. public async Task Should_Get_Array_Value()
  18. {
  19. var data = new { Foo = new [] { "foo", "bar" } };
  20. var target = ExpressionObserver.Create(data, x => x.Foo[1]);
  21. var result = await target.Take(1);
  22. Assert.Equal("bar", result);
  23. GC.KeepAlive(data);
  24. }
  25. [Fact]
  26. public async Task Should_Get_MultiDimensional_Array_Value()
  27. {
  28. var data = new { Foo = new[,] { { "foo", "bar" }, { "baz", "qux" } } };
  29. var target = ExpressionObserver.Create(data, o => o.Foo[1, 1]);
  30. var result = await target.Take(1);
  31. Assert.Equal("qux", result);
  32. GC.KeepAlive(data);
  33. }
  34. [Fact]
  35. public async Task Should_Get_Value_For_String_Indexer()
  36. {
  37. var data = new { Foo = new Dictionary<string, string> { { "foo", "bar" }, { "baz", "qux" } } };
  38. var target = ExpressionObserver.Create(data, o => o.Foo["foo"]);
  39. var result = await target.Take(1);
  40. Assert.Equal("bar", result);
  41. GC.KeepAlive(data);
  42. }
  43. [Fact]
  44. public async Task Should_Get_Value_For_Non_String_Indexer()
  45. {
  46. var data = new { Foo = new Dictionary<double, string> { { 1.0, "bar" }, { 2.0, "qux" } } };
  47. var target = ExpressionObserver.Create(data, o => o.Foo[1.0]);
  48. var result = await target.Take(1);
  49. Assert.Equal("bar", result);
  50. GC.KeepAlive(data);
  51. }
  52. [Fact]
  53. public async Task Array_Out_Of_Bounds_Should_Return_UnsetValue()
  54. {
  55. var data = new { Foo = new[] { "foo", "bar" } };
  56. var target = ExpressionObserver.Create(data, o => o.Foo[2]);
  57. var result = await target.Take(1);
  58. Assert.Equal(AvaloniaProperty.UnsetValue, result);
  59. GC.KeepAlive(data);
  60. }
  61. [Fact]
  62. public async Task List_Out_Of_Bounds_Should_Return_UnsetValue()
  63. {
  64. var data = new { Foo = new List<string> { "foo", "bar" } };
  65. var target = ExpressionObserver.Create(data, o => o.Foo[2]);
  66. var result = await target.Take(1);
  67. Assert.Equal(AvaloniaProperty.UnsetValue, result);
  68. GC.KeepAlive(data);
  69. }
  70. [Fact]
  71. public async Task Should_Get_List_Value()
  72. {
  73. var data = new { Foo = new List<string> { "foo", "bar" } };
  74. var target = ExpressionObserver.Create(data, o => o.Foo[1]);
  75. var result = await target.Take(1);
  76. Assert.Equal("bar", result);
  77. GC.KeepAlive(data);
  78. }
  79. [Fact]
  80. public void Should_Track_INCC_Add()
  81. {
  82. var data = new { Foo = new AvaloniaList<string> { "foo", "bar" } };
  83. var target = ExpressionObserver.Create(data, o => o.Foo[2]);
  84. var result = new List<object>();
  85. using (var sub = target.Subscribe(x => result.Add(x)))
  86. {
  87. data.Foo.Add("baz");
  88. }
  89. Assert.Equal(new[] { AvaloniaProperty.UnsetValue, "baz" }, result);
  90. Assert.Null(((INotifyCollectionChangedDebug)data.Foo).GetCollectionChangedSubscribers());
  91. GC.KeepAlive(data);
  92. }
  93. [Fact]
  94. public void Should_Track_INCC_Remove()
  95. {
  96. var data = new { Foo = new AvaloniaList<string> { "foo", "bar" } };
  97. var target = ExpressionObserver.Create(data, o => o.Foo[0]);
  98. var result = new List<object>();
  99. using (var sub = target.Subscribe(x => result.Add(x)))
  100. {
  101. data.Foo.RemoveAt(0);
  102. }
  103. Assert.Equal(new[] { "foo", "bar" }, result);
  104. Assert.Null(((INotifyCollectionChangedDebug)data.Foo).GetCollectionChangedSubscribers());
  105. GC.KeepAlive(data);
  106. }
  107. [Fact]
  108. public void Should_Track_INCC_Replace()
  109. {
  110. var data = new { Foo = new AvaloniaList<string> { "foo", "bar" } };
  111. var target = ExpressionObserver.Create(data, o => o.Foo[1]);
  112. var result = new List<object>();
  113. using (var sub = target.Subscribe(x => result.Add(x)))
  114. {
  115. data.Foo[1] = "baz";
  116. }
  117. Assert.Equal(new[] { "bar", "baz" }, result);
  118. Assert.Null(((INotifyCollectionChangedDebug)data.Foo).GetCollectionChangedSubscribers());
  119. GC.KeepAlive(data);
  120. }
  121. [Fact]
  122. public void Should_Track_INCC_Move()
  123. {
  124. // Using ObservableCollection here because AvaloniaList does not yet have a Move
  125. // method, but even if it did we need to test with ObservableCollection as well
  126. // as AvaloniaList as it implements PropertyChanged as an explicit interface event.
  127. var data = new { Foo = new ObservableCollection<string> { "foo", "bar" } };
  128. var target = ExpressionObserver.Create(data, o => o.Foo[1]);
  129. var result = new List<object>();
  130. var sub = target.Subscribe(x => result.Add(x));
  131. data.Foo.Move(0, 1);
  132. Assert.Equal(new[] { "bar", "foo" }, result);
  133. GC.KeepAlive(sub);
  134. GC.KeepAlive(data);
  135. }
  136. [Fact]
  137. public void Should_Track_INCC_Reset()
  138. {
  139. var data = new { Foo = new AvaloniaList<string> { "foo", "bar" } };
  140. var target = ExpressionObserver.Create(data, o => o.Foo[1]);
  141. var result = new List<object>();
  142. var sub = target.Subscribe(x => result.Add(x));
  143. data.Foo.Clear();
  144. Assert.Equal(new[] { "bar", AvaloniaProperty.UnsetValue }, result);
  145. GC.KeepAlive(sub);
  146. GC.KeepAlive(data);
  147. }
  148. [Fact]
  149. public void Should_Track_NonIntegerIndexer()
  150. {
  151. var data = new { Foo = new NonIntegerIndexer() };
  152. data.Foo["foo"] = "bar";
  153. data.Foo["baz"] = "qux";
  154. var target = ExpressionObserver.Create(data, o => o.Foo["foo"]);
  155. var result = new List<object>();
  156. using (var sub = target.Subscribe(x => result.Add(x)))
  157. {
  158. data.Foo["foo"] = "bar2";
  159. }
  160. var expected = new[] { "bar", "bar2" };
  161. Assert.Equal(expected, result);
  162. Assert.Equal(0, data.Foo.PropertyChangedSubscriptionCount);
  163. GC.KeepAlive(data);
  164. }
  165. [Fact]
  166. public void Should_SetArrayIndex()
  167. {
  168. var data = new { Foo = new[] { "foo", "bar" } };
  169. var target = ExpressionObserver.Create(data, o => o.Foo[1]);
  170. using (target.Subscribe(_ => { }))
  171. {
  172. Assert.True(target.SetValue("baz"));
  173. }
  174. Assert.Equal("baz", data.Foo[1]);
  175. GC.KeepAlive(data);
  176. }
  177. [Fact]
  178. public void Should_Set_ExistingDictionaryEntry()
  179. {
  180. var data = new
  181. {
  182. Foo = new Dictionary<string, int>
  183. {
  184. {"foo", 1 }
  185. }
  186. };
  187. var target = ExpressionObserver.Create(data, o => o.Foo["foo"]);
  188. using (target.Subscribe(_ => { }))
  189. {
  190. Assert.True(target.SetValue(4));
  191. }
  192. Assert.Equal(4, data.Foo["foo"]);
  193. GC.KeepAlive(data);
  194. }
  195. [Fact]
  196. public void Should_Add_NewDictionaryEntry()
  197. {
  198. var data = new
  199. {
  200. Foo = new Dictionary<string, int>
  201. {
  202. {"foo", 1 }
  203. }
  204. };
  205. var target = ExpressionObserver.Create(data, o => o.Foo["bar"]);
  206. using (target.Subscribe(_ => { }))
  207. {
  208. Assert.True(target.SetValue(4));
  209. }
  210. Assert.Equal(4, data.Foo["bar"]);
  211. GC.KeepAlive(data);
  212. }
  213. [Fact]
  214. public void Should_Set_NonIntegerIndexer()
  215. {
  216. var data = new { Foo = new NonIntegerIndexer() };
  217. data.Foo["foo"] = "bar";
  218. data.Foo["baz"] = "qux";
  219. var target = ExpressionObserver.Create(data, o => o.Foo["foo"]);
  220. using (target.Subscribe(_ => { }))
  221. {
  222. Assert.True(target.SetValue("bar2"));
  223. }
  224. Assert.Equal("bar2", data.Foo["foo"]);
  225. GC.KeepAlive(data);
  226. }
  227. [Fact]
  228. public async Task Indexer_Only_Binding_Works()
  229. {
  230. var data = new[] { 1, 2, 3 };
  231. var target = ExpressionObserver.Create(data, o => o[1]);
  232. var value = await target.Take(1);
  233. Assert.Equal(data[1], value);
  234. }
  235. private class NonIntegerIndexer : NotifyingBase
  236. {
  237. private readonly Dictionary<string, string> _storage = new Dictionary<string, string>();
  238. public string this[string key]
  239. {
  240. get
  241. {
  242. return _storage[key];
  243. }
  244. set
  245. {
  246. _storage[key] = value;
  247. RaisePropertyChanged(CommonPropertyNames.IndexerName);
  248. }
  249. }
  250. }
  251. }
  252. }