Guest User

Untitled

a guest
Oct 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. [Test]
  2. public void CollectionOrderedConstraint()
  3. {
  4. ///CollectionOrderedConstraint tests that an IEnumerable is ordered.
  5. ///If the actual value passed does not implement IEnumerable, an exception is thrown.
  6. ///The constraint supports both simple and property-based ordering (Ordered.By).
  7. ///
  8. ///Simple ordering is based on the values of the items themselves. It is implied when the By modifier is not used.
  9. ///
  10.  
  11. //Syntax: Is.Ordered
  12. //Modifiers:
  13. //...Ascending
  14. //...Descending
  15. //...Using(IComparer comparer)
  16. //...Using<T>(IComparer < T > comparer)
  17. //...Using<T>(Comparison < T > comparer)
  18.  
  19. //arrange
  20. int[] iarray = new int[] { 1, 2, 3 };
  21. string[] sarray = new string[] { "c", "b", "a" };
  22.  
  23. //act
  24.  
  25. //assert
  26. Assert.That(iarray, Is.Ordered);
  27. Assert.That(sarray, Is.Ordered.Descending);
  28.  
  29. ///Property-based ordering uses one or more properties that
  30. ///are common to every item in the enumeration. It is used when
  31. ///one or more instances of the By modifier appears in the ordering expression.
  32.  
  33. //Modifiers:
  34. //...Then
  35. //...Ascending
  36. //...Descending
  37. //...By(string propertyName)
  38. //...Using(IComparer comparer)
  39. //...Using<T>(IComparer < T > comparer)
  40. //...Using<T>(Comparison < T > comparer)
  41.  
  42. sarray = new string[] { "a", "aa", "aaa" };
  43. var sarray2 = new string[] { "aaa", "aa", "a" };
  44.  
  45. Assert.That(sarray, Is.Ordered.By("Length"));
  46. Assert.That(sarray2, Is.Ordered.Descending.By("Length"));
  47.  
  48. ///An ordering expression may use multiple By modifiers, each referring
  49. ///to a different property. The following examples assume a collection of
  50. ///items with properties named A and B.
  51.  
  52. //Assert.That(collection, Is.Ordered.By("A").Then.By("B"));
  53. //Assert.That(collection, Is.Ordered.By("A").Then.By("B").Descending);
  54. //Assert.That(collection, Is.Ordered.Ascending.By("A").Then.Descending.By("B"));
  55. //Assert.That(collection, Is.Ordered.Ascending.By("A").By("B").Descending);
  56. //Assert.That(collection, Is.Ordered.Ascending.By("A").Descending.By("B")); // Illegal!
  57.  
  58. //Notes:
  59. ///The Then modifier divides the expression into ordering steps.Each step may optionally
  60. ///contain one Ascending or Descending modifier and one Using modifier.
  61.  
  62. ///If Then is not used, each new By modifier marks the beginning of a step.The last example
  63. ///statement is illegal because the first group contains both Ascending and Descending.
  64. ///Use of Then is recommended for clarity.
  65.  
  66. }
Add Comment
Please, Sign In to add comment