PerspexListTests.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // -----------------------------------------------------------------------
  2. // <copyright file="PerspexListTests.cs" company="Steven Kirk">
  3. // Copyright 2015 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Base.UnitTests.Collections
  7. {
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Collections.Specialized;
  11. using System.Linq;
  12. using Perspex.Collections;
  13. using Xunit;
  14. public class PerspexListTests
  15. {
  16. [Fact]
  17. public void Items_Passed_To_Constructor_Should_Appear_In_List()
  18. {
  19. var items = new[] { 1, 2, 3 };
  20. var target = new PerspexList<int>(items);
  21. Assert.Equal(items, target);
  22. }
  23. [Fact]
  24. public void AddRange_With_Null_Should_Throw_Exception()
  25. {
  26. var target = new PerspexList<int>();
  27. Assert.Throws<ArgumentNullException>(() => target.AddRange(null));
  28. }
  29. [Fact]
  30. public void RemoveAll_With_Null_Should_Throw_Exception()
  31. {
  32. var target = new PerspexList<int>();
  33. Assert.Throws<ArgumentNullException>(() => target.RemoveAll(null));
  34. }
  35. [Fact]
  36. public void InsertRange_With_Null_Should_Throw_Exception()
  37. {
  38. var target = new PerspexList<int>();
  39. Assert.Throws<ArgumentNullException>(() => target.InsertRange(1, null));
  40. }
  41. [Fact]
  42. public void InsertRange_Past_End_Should_Throw_Exception()
  43. {
  44. var target = new PerspexList<int>();
  45. Assert.Throws<ArgumentOutOfRangeException>(() => target.InsertRange(1, new List<int>() { 1 }));
  46. }
  47. [Fact]
  48. public void Adding_Item_Should_Raise_CollectionChanged()
  49. {
  50. var target = new PerspexList<int>(new[] { 1, 2 });
  51. var raised = false;
  52. target.CollectionChanged += (s, e) =>
  53. {
  54. Assert.Equal(target, s);
  55. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  56. Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>());
  57. Assert.Equal(2, e.NewStartingIndex);
  58. raised = true;
  59. };
  60. target.Add(3);
  61. Assert.True(raised);
  62. }
  63. [Fact]
  64. public void Adding_Items_Should_Raise_CollectionChanged()
  65. {
  66. var target = new PerspexList<int>(new[] { 1, 2 });
  67. var raised = false;
  68. target.CollectionChanged += (s, e) =>
  69. {
  70. Assert.Equal(target, s);
  71. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  72. Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast<int>());
  73. Assert.Equal(2, e.NewStartingIndex);
  74. raised = true;
  75. };
  76. target.AddRange(new[] { 3, 4 });
  77. Assert.True(raised);
  78. }
  79. [Fact]
  80. public void Inserting_Item_Should_Raise_CollectionChanged()
  81. {
  82. var target = new PerspexList<int>(new[] { 1, 2 });
  83. var raised = false;
  84. target.CollectionChanged += (s, e) =>
  85. {
  86. Assert.Equal(target, s);
  87. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  88. Assert.Equal(new[] { 3 }, e.NewItems.Cast<int>());
  89. Assert.Equal(1, e.NewStartingIndex);
  90. raised = true;
  91. };
  92. target.Insert(1, 3);
  93. Assert.True(raised);
  94. }
  95. [Fact]
  96. public void Inserting_Items_Should_Raise_CollectionChanged()
  97. {
  98. var target = new PerspexList<int>(new[] { 1, 2 });
  99. var raised = false;
  100. target.CollectionChanged += (s, e) =>
  101. {
  102. Assert.Equal(target, s);
  103. Assert.Equal(NotifyCollectionChangedAction.Add, e.Action);
  104. Assert.Equal(new[] { 3, 4 }, e.NewItems.Cast<int>());
  105. Assert.Equal(1, e.NewStartingIndex);
  106. raised = true;
  107. };
  108. target.InsertRange(1, new[] { 3, 4 });
  109. Assert.True(raised);
  110. }
  111. [Fact]
  112. public void Removing_Item_Should_Raise_CollectionChanged()
  113. {
  114. var target = new PerspexList<int>(new[] { 1, 2, 3 });
  115. var raised = false;
  116. target.CollectionChanged += (s, e) =>
  117. {
  118. Assert.Equal(target, s);
  119. Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
  120. Assert.Equal(new[] { 3 }, e.OldItems.Cast<int>());
  121. Assert.Equal(2, e.OldStartingIndex);
  122. raised = true;
  123. };
  124. target.Remove(3);
  125. Assert.True(raised);
  126. }
  127. [Fact]
  128. public void Clearing_Items_Should_Raise_CollectionChanged_Remove()
  129. {
  130. var target = new PerspexList<int>(new[] { 1, 2, 3 });
  131. var raised = false;
  132. target.CollectionChanged += (s, e) =>
  133. {
  134. Assert.Equal(target, s);
  135. Assert.Equal(NotifyCollectionChangedAction.Remove, e.Action);
  136. Assert.Equal(new[] { 1, 2, 3 }, e.OldItems.Cast<int>());
  137. Assert.Equal(0, e.OldStartingIndex);
  138. raised = true;
  139. };
  140. target.Clear();
  141. Assert.True(raised);
  142. }
  143. }
  144. }