Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. ## Tricks and tips for using Strikt assertions
  2.  
  3. Strikt is very powerful, however it is sometimes hard to find particular methods. This is a handy cheatsheet.
  4.  
  5. ### Basics
  6.  
  7. #### Top-level assertions
  8.  
  9. `expect`, `expectThat`, `expectCatching` and `expectThrows`
  10.  
  11. #### Narrowing
  12.  
  13. Non-null assertion and assertions on narrowed type:
  14.  
  15. ```kotlin
  16. expectThat(subject)
  17. .isNotNull()
  18. .and {
  19. // ...
  20. }
  21. ```
  22.  
  23. Use `.isNotNull()` to narrow nullability and `.isA<Int>()` to narrow to specific type.
  24.  
  25. #### Grouping
  26.  
  27. Use `.and { }` to group assertions on a subtree
  28.  
  29. #### Chained Assertions
  30.  
  31. These fail fast:
  32.  
  33. ```kotlin
  34. expectThat(subject)
  35. .isA<String>()
  36. .hasLength(1)
  37. ```
  38.  
  39. #### Block Assertions
  40.  
  41. Will run all assertions in the block before failing:
  42.  
  43. ```kotlin
  44. expectThat(subject) {
  45. isA<String>()
  46. hasLength(1)
  47. }
  48. ```
  49.  
  50. or as a subblock
  51.  
  52. ```kotlin
  53. .and(subject) {
  54. isA<String>()
  55. hasLength(1)
  56. }
  57. ```
  58.  
  59. #### Asserting Different Subjects
  60.  
  61. ```kotlin
  62. expect {
  63. that(varOne)
  64. .isA<String>()
  65. .hasLength(1)
  66. that(varTwo) {
  67. isLessThan(1L)
  68. }
  69. ```
  70.  
  71. ### Sub-trees
  72.  
  73. Using get to assert sub-properties
  74.  
  75. ```kotlin
  76. expectThat(subject) {
  77. get { name }.isEqualTo("Ziggy")
  78. get { birthDate.year }.isEqualTo(1971)
  79. }
  80. ```
  81. or with property/method references:
  82. ```kotlin
  83. expectThat(subject) {
  84. get(Person::name).isEqualTo("David")
  85. get(Person::birthDate).get(LocalDate::getYear).isEqualTo(1947)
  86. }
  87. ```
  88. or as separate assertions
  89. ```kotlin
  90. expect {
  91. that(person.name).isEqualTo("David")
  92. that(person.birthDate.year).isEqualTo(1947)
  93. }
  94. ```
  95.  
  96. #### Traversals and Modifers
  97.  
  98. Combine these with asserts to traverse to different objects or modify the object under test:
  99.  
  100. * CharSequence `.trim()` : trims a string
  101. * File `.toPath()` : returns the file path string
  102. * File `.lines()` : returns contents of file as a collection of lines
  103. * File `.text()` : returns contents of file as text
  104. * Iterable `.map()` : allows assertions to be mapped to each collection member
  105. * Iterable `.first()` : returns first element
  106. * Iterable `.first { predicate }` : returns first matching element
  107. * Iterable `.single()` : returns single element
  108. * Iterable `.last()` : returns last element
  109. * Iterable `.flatMap()` : Flattens nested collections
  110. * Iterable `.filter { predicate }` : Filters matching elements
  111. * Iterable `.filterNot { predicate }` : Filters non-matching elements
  112. * Iterable `.filterIsInstance<R>()` : Filters for matching instance types
  113. * Iterable `.get(n)` : returns element n
  114. * Iterable `.get(range)` : returns elements in range
  115. * Map `.get(key)` : returns element for key
  116. * Map `.getValue(key)` : returns element for key (asserts for existence)
  117. * Optional `.toNullable()` : returns kotlin nullable type from Java Optional
  118. * Path `.resolve(other)` : returns the combined paths
  119. * Path `.toFile()` : returns file for path
  120. * Path `.allBytes()` : returns bytes of all files on path???
  121. * Path `.allLines()` : returns lines of all files on path???
  122. * Path `.allLines(charset)` : returns lines of all files on path converted using charset???
  123.  
  124. ### Collections
  125.  
  126. Mapping elements of exceptions:
  127. ```kotlin
  128. expectThat(subject)
  129. .map(Person::name)
  130. .containsExactly("David", "Ziggy", "Aladdin", "Jareth")
  131. ```
  132. (See traversals, above, for more options)
  133.  
  134.  
  135. #### Asserting contents
  136.  
  137. `expectThat()` with `.all` / `.any` / `.none`
  138.  
  139. ### Failures
  140.  
  141. Checking that a block throws an exception:
  142.  
  143. ```kotlin
  144. expectCatching { identifyHotdog("hamburger") }
  145. .failed()
  146. .isA<NotHotdogException>()
  147. ```
  148.  
  149. or
  150.  
  151. ```kotlin
  152. expect {
  153. catching { identifyHotdog("hamburger") }
  154. .failed()
  155. .isA<NotHotdogException>()
  156. }
  157. ```
  158.  
  159. or
  160.  
  161. ```kotlin
  162. expectThrows<NotHotdogException> {
  163. identifyHotdog("hamburger")
  164. }
  165. ```
  166.  
  167. Checking that a block **does not** throw an exception:
  168.  
  169. ```kotlin
  170. expectCatching { identifyHotdog("hotdog") }
  171. .succeeded()
  172. ```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement