@goast/kotlin 0.4.2 → 0.4.4-beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (500) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +45 -45
  3. package/assets/client/okhttp3/ApiClient.kt +250 -250
  4. package/esm/src/ast/references/swagger.d.ts +1 -0
  5. package/esm/src/ast/references/swagger.d.ts.map +1 -1
  6. package/esm/src/ast/references/swagger.js +1 -0
  7. package/esm/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +27 -27
  8. package/esm/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
  9. package/esm/src/generators/services/spring-controllers/spring-controller-generator.js +34 -7
  10. package/package.json +3 -3
  11. package/script/src/ast/references/swagger.d.ts +1 -0
  12. package/script/src/ast/references/swagger.d.ts.map +1 -1
  13. package/script/src/ast/references/swagger.js +2 -1
  14. package/script/src/generators/services/okhttp3-clients/okhttp3-client-generator.js +27 -27
  15. package/script/src/generators/services/spring-controllers/spring-controller-generator.d.ts.map +1 -1
  16. package/script/src/generators/services/spring-controllers/spring-controller-generator.js +34 -7
  17. package/esm/_dnt.test_polyfills.d.ts +0 -29
  18. package/esm/_dnt.test_polyfills.d.ts.map +0 -1
  19. package/esm/_dnt.test_polyfills.js +0 -31
  20. package/esm/_dnt.test_shims.d.ts +0 -6
  21. package/esm/_dnt.test_shims.d.ts.map +0 -1
  22. package/esm/_dnt.test_shims.js +0 -61
  23. package/esm/deps/jsr.io/@std/assert/1.0.6/assertion_error.d.ts +0 -26
  24. package/esm/deps/jsr.io/@std/assert/1.0.6/assertion_error.d.ts.map +0 -1
  25. package/esm/deps/jsr.io/@std/assert/1.0.6/assertion_error.js +0 -30
  26. package/esm/deps/jsr.io/@std/assert/1.0.6/equal.d.ts +0 -17
  27. package/esm/deps/jsr.io/@std/assert/1.0.6/equal.d.ts.map +0 -1
  28. package/esm/deps/jsr.io/@std/assert/1.0.6/equal.js +0 -136
  29. package/esm/deps/jsr.io/@std/assert/1.0.6/equals.d.ts +0 -22
  30. package/esm/deps/jsr.io/@std/assert/1.0.6/equals.d.ts.map +0 -1
  31. package/esm/deps/jsr.io/@std/assert/1.0.6/equals.js +0 -45
  32. package/esm/deps/jsr.io/@std/assert/1.0.6/false.d.ts +0 -18
  33. package/esm/deps/jsr.io/@std/assert/1.0.6/false.d.ts.map +0 -1
  34. package/esm/deps/jsr.io/@std/assert/1.0.6/false.js +0 -22
  35. package/esm/deps/jsr.io/@std/assert/1.0.6/instance_of.d.ts +0 -23
  36. package/esm/deps/jsr.io/@std/assert/1.0.6/instance_of.d.ts.map +0 -1
  37. package/esm/deps/jsr.io/@std/assert/1.0.6/instance_of.js +0 -53
  38. package/esm/deps/jsr.io/@std/assert/1.0.6/is_error.d.ts +0 -25
  39. package/esm/deps/jsr.io/@std/assert/1.0.6/is_error.d.ts.map +0 -1
  40. package/esm/deps/jsr.io/@std/assert/1.0.6/is_error.js +0 -54
  41. package/esm/deps/jsr.io/@std/assert/1.0.6/match.d.ts +0 -18
  42. package/esm/deps/jsr.io/@std/assert/1.0.6/match.d.ts.map +0 -1
  43. package/esm/deps/jsr.io/@std/assert/1.0.6/match.js +0 -26
  44. package/esm/deps/jsr.io/@std/assert/1.0.6/not_instance_of.d.ts +0 -20
  45. package/esm/deps/jsr.io/@std/assert/1.0.6/not_instance_of.d.ts.map +0 -1
  46. package/esm/deps/jsr.io/@std/assert/1.0.6/not_instance_of.js +0 -29
  47. package/esm/deps/jsr.io/@std/assert/1.0.6/not_match.d.ts +0 -18
  48. package/esm/deps/jsr.io/@std/assert/1.0.6/not_match.d.ts.map +0 -1
  49. package/esm/deps/jsr.io/@std/assert/1.0.6/not_match.js +0 -26
  50. package/esm/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.d.ts +0 -23
  51. package/esm/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.d.ts.map +0 -1
  52. package/esm/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.js +0 -32
  53. package/esm/deps/jsr.io/@std/assert/1.0.6/object_match.d.ts +0 -28
  54. package/esm/deps/jsr.io/@std/assert/1.0.6/object_match.d.ts.map +0 -1
  55. package/esm/deps/jsr.io/@std/assert/1.0.6/object_match.js +0 -159
  56. package/esm/deps/jsr.io/@std/assert/1.0.6/rejects.d.ts +0 -42
  57. package/esm/deps/jsr.io/@std/assert/1.0.6/rejects.d.ts.map +0 -1
  58. package/esm/deps/jsr.io/@std/assert/1.0.6/rejects.js +0 -53
  59. package/esm/deps/jsr.io/@std/assert/1.0.6/strict_equals.d.ts +0 -24
  60. package/esm/deps/jsr.io/@std/assert/1.0.6/strict_equals.d.ts.map +0 -1
  61. package/esm/deps/jsr.io/@std/assert/1.0.6/strict_equals.js +0 -57
  62. package/esm/deps/jsr.io/@std/expect/1.0.6/_assert_equals.d.ts +0 -20
  63. package/esm/deps/jsr.io/@std/expect/1.0.6/_assert_equals.d.ts.map +0 -1
  64. package/esm/deps/jsr.io/@std/expect/1.0.6/_assert_equals.js +0 -29
  65. package/esm/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.d.ts +0 -17
  66. package/esm/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.d.ts.map +0 -1
  67. package/esm/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.js +0 -26
  68. package/esm/deps/jsr.io/@std/expect/1.0.6/_assertion.d.ts +0 -3
  69. package/esm/deps/jsr.io/@std/expect/1.0.6/_assertion.d.ts.map +0 -1
  70. package/esm/deps/jsr.io/@std/expect/1.0.6/_assertion.js +0 -9
  71. package/esm/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.d.ts +0 -41
  72. package/esm/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.d.ts.map +0 -1
  73. package/esm/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.js +0 -143
  74. package/esm/deps/jsr.io/@std/expect/1.0.6/_build_message.d.ts +0 -6
  75. package/esm/deps/jsr.io/@std/expect/1.0.6/_build_message.d.ts.map +0 -1
  76. package/esm/deps/jsr.io/@std/expect/1.0.6/_build_message.js +0 -29
  77. package/esm/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.d.ts +0 -4
  78. package/esm/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.d.ts.map +0 -1
  79. package/esm/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.js +0 -11
  80. package/esm/deps/jsr.io/@std/expect/1.0.6/_equal.d.ts +0 -9
  81. package/esm/deps/jsr.io/@std/expect/1.0.6/_equal.d.ts.map +0 -1
  82. package/esm/deps/jsr.io/@std/expect/1.0.6/_equal.js +0 -191
  83. package/esm/deps/jsr.io/@std/expect/1.0.6/_extend.d.ts +0 -4
  84. package/esm/deps/jsr.io/@std/expect/1.0.6/_extend.d.ts.map +0 -1
  85. package/esm/deps/jsr.io/@std/expect/1.0.6/_extend.js +0 -11
  86. package/esm/deps/jsr.io/@std/expect/1.0.6/_inspect_args.d.ts +0 -3
  87. package/esm/deps/jsr.io/@std/expect/1.0.6/_inspect_args.d.ts.map +0 -1
  88. package/esm/deps/jsr.io/@std/expect/1.0.6/_inspect_args.js +0 -12
  89. package/esm/deps/jsr.io/@std/expect/1.0.6/_matchers.d.ts +0 -34
  90. package/esm/deps/jsr.io/@std/expect/1.0.6/_matchers.d.ts.map +0 -1
  91. package/esm/deps/jsr.io/@std/expect/1.0.6/_matchers.js +0 -564
  92. package/esm/deps/jsr.io/@std/expect/1.0.6/_mock_util.d.ts +0 -11
  93. package/esm/deps/jsr.io/@std/expect/1.0.6/_mock_util.d.ts.map +0 -1
  94. package/esm/deps/jsr.io/@std/expect/1.0.6/_mock_util.js +0 -10
  95. package/esm/deps/jsr.io/@std/expect/1.0.6/_types.d.ts +0 -838
  96. package/esm/deps/jsr.io/@std/expect/1.0.6/_types.d.ts.map +0 -1
  97. package/esm/deps/jsr.io/@std/expect/1.0.6/_types.js +0 -3
  98. package/esm/deps/jsr.io/@std/expect/1.0.6/_utils.d.ts +0 -8
  99. package/esm/deps/jsr.io/@std/expect/1.0.6/_utils.d.ts.map +0 -1
  100. package/esm/deps/jsr.io/@std/expect/1.0.6/_utils.js +0 -159
  101. package/esm/deps/jsr.io/@std/expect/1.0.6/expect.d.ts +0 -52
  102. package/esm/deps/jsr.io/@std/expect/1.0.6/expect.d.ts.map +0 -1
  103. package/esm/deps/jsr.io/@std/expect/1.0.6/expect.js +0 -441
  104. package/esm/deps/jsr.io/@std/internal/1.0.4/assertion_state.d.ts +0 -75
  105. package/esm/deps/jsr.io/@std/internal/1.0.4/assertion_state.d.ts.map +0 -1
  106. package/esm/deps/jsr.io/@std/internal/1.0.4/assertion_state.js +0 -101
  107. package/esm/deps/jsr.io/@std/internal/1.0.4/build_message.d.ts +0 -82
  108. package/esm/deps/jsr.io/@std/internal/1.0.4/build_message.d.ts.map +0 -1
  109. package/esm/deps/jsr.io/@std/internal/1.0.4/build_message.js +0 -111
  110. package/esm/deps/jsr.io/@std/internal/1.0.4/diff.d.ts +0 -140
  111. package/esm/deps/jsr.io/@std/internal/1.0.4/diff.d.ts.map +0 -1
  112. package/esm/deps/jsr.io/@std/internal/1.0.4/diff.js +0 -277
  113. package/esm/deps/jsr.io/@std/internal/1.0.4/diff_str.d.ts +0 -99
  114. package/esm/deps/jsr.io/@std/internal/1.0.4/diff_str.d.ts.map +0 -1
  115. package/esm/deps/jsr.io/@std/internal/1.0.4/diff_str.js +0 -180
  116. package/esm/deps/jsr.io/@std/internal/1.0.4/format.d.ts +0 -2
  117. package/esm/deps/jsr.io/@std/internal/1.0.4/format.d.ts.map +0 -1
  118. package/esm/deps/jsr.io/@std/internal/1.0.4/format.js +0 -37
  119. package/esm/deps/jsr.io/@std/internal/1.0.4/styles.d.ts +0 -159
  120. package/esm/deps/jsr.io/@std/internal/1.0.4/styles.d.ts.map +0 -1
  121. package/esm/deps/jsr.io/@std/internal/1.0.4/styles.js +0 -207
  122. package/esm/deps/jsr.io/@std/internal/1.0.4/types.d.ts +0 -16
  123. package/esm/deps/jsr.io/@std/internal/1.0.4/types.d.ts.map +0 -1
  124. package/esm/deps/jsr.io/@std/internal/1.0.4/types.js +0 -2
  125. package/esm/deps/jsr.io/@std/testing/1.0.3/_test_suite.d.ts +0 -74
  126. package/esm/deps/jsr.io/@std/testing/1.0.3/_test_suite.d.ts.map +0 -1
  127. package/esm/deps/jsr.io/@std/testing/1.0.3/_test_suite.js +0 -340
  128. package/esm/deps/jsr.io/@std/testing/1.0.3/bdd.d.ts +0 -731
  129. package/esm/deps/jsr.io/@std/testing/1.0.3/bdd.d.ts.map +0 -1
  130. package/esm/deps/jsr.io/@std/testing/1.0.3/bdd.js +0 -576
  131. package/esm/deps/jsr.io/@std/testing/1.0.3/mock.d.ts +0 -674
  132. package/esm/deps/jsr.io/@std/testing/1.0.3/mock.d.ts.map +0 -1
  133. package/esm/deps/jsr.io/@std/testing/1.0.3/mock.js +0 -1117
  134. package/esm/src/ast/nodes/annotation.test.d.ts +0 -2
  135. package/esm/src/ast/nodes/annotation.test.d.ts.map +0 -1
  136. package/esm/src/ast/nodes/annotation.test.js +0 -56
  137. package/esm/src/ast/nodes/argument.test.d.ts +0 -2
  138. package/esm/src/ast/nodes/argument.test.d.ts.map +0 -1
  139. package/esm/src/ast/nodes/argument.test.js +0 -33
  140. package/esm/src/ast/nodes/call.test.d.ts +0 -2
  141. package/esm/src/ast/nodes/call.test.d.ts.map +0 -1
  142. package/esm/src/ast/nodes/call.test.js +0 -35
  143. package/esm/src/ast/nodes/class.test.d.ts +0 -2
  144. package/esm/src/ast/nodes/class.test.d.ts.map +0 -1
  145. package/esm/src/ast/nodes/class.test.js +0 -219
  146. package/esm/src/ast/nodes/constructor.test.d.ts +0 -2
  147. package/esm/src/ast/nodes/constructor.test.d.ts.map +0 -1
  148. package/esm/src/ast/nodes/constructor.test.js +0 -139
  149. package/esm/src/ast/nodes/doc-tag.test.d.ts +0 -2
  150. package/esm/src/ast/nodes/doc-tag.test.d.ts.map +0 -1
  151. package/esm/src/ast/nodes/doc-tag.test.js +0 -67
  152. package/esm/src/ast/nodes/doc.test.d.ts +0 -2
  153. package/esm/src/ast/nodes/doc.test.d.ts.map +0 -1
  154. package/esm/src/ast/nodes/doc.test.js +0 -33
  155. package/esm/src/ast/nodes/enum-value.test.d.ts +0 -2
  156. package/esm/src/ast/nodes/enum-value.test.d.ts.map +0 -1
  157. package/esm/src/ast/nodes/enum-value.test.js +0 -115
  158. package/esm/src/ast/nodes/enum.test.d.ts +0 -2
  159. package/esm/src/ast/nodes/enum.test.d.ts.map +0 -1
  160. package/esm/src/ast/nodes/enum.test.js +0 -129
  161. package/esm/src/ast/nodes/function.test.d.ts +0 -2
  162. package/esm/src/ast/nodes/function.test.d.ts.map +0 -1
  163. package/esm/src/ast/nodes/function.test.js +0 -160
  164. package/esm/src/ast/nodes/generic-parameter.test.d.ts +0 -2
  165. package/esm/src/ast/nodes/generic-parameter.test.d.ts.map +0 -1
  166. package/esm/src/ast/nodes/generic-parameter.test.js +0 -49
  167. package/esm/src/ast/nodes/init-block.test.d.ts +0 -2
  168. package/esm/src/ast/nodes/init-block.test.d.ts.map +0 -1
  169. package/esm/src/ast/nodes/init-block.test.js +0 -28
  170. package/esm/src/ast/nodes/interface.test.d.ts +0 -2
  171. package/esm/src/ast/nodes/interface.test.d.ts.map +0 -1
  172. package/esm/src/ast/nodes/interface.test.js +0 -121
  173. package/esm/src/ast/nodes/object.test.d.ts +0 -2
  174. package/esm/src/ast/nodes/object.test.d.ts.map +0 -1
  175. package/esm/src/ast/nodes/object.test.js +0 -78
  176. package/esm/src/ast/nodes/parameter.test.d.ts +0 -2
  177. package/esm/src/ast/nodes/parameter.test.d.ts.map +0 -1
  178. package/esm/src/ast/nodes/parameter.test.js +0 -152
  179. package/esm/src/ast/nodes/property.test.d.ts +0 -2
  180. package/esm/src/ast/nodes/property.test.d.ts.map +0 -1
  181. package/esm/src/ast/nodes/property.test.js +0 -206
  182. package/esm/src/ast/nodes/reference.test.d.ts +0 -2
  183. package/esm/src/ast/nodes/reference.test.d.ts.map +0 -1
  184. package/esm/src/ast/nodes/reference.test.js +0 -36
  185. package/esm/src/ast/nodes/string.test.d.ts +0 -2
  186. package/esm/src/ast/nodes/string.test.d.ts.map +0 -1
  187. package/esm/src/ast/nodes/string.test.js +0 -48
  188. package/esm/src/import-collection.test.d.ts +0 -2
  189. package/esm/src/import-collection.test.d.ts.map +0 -1
  190. package/esm/src/import-collection.test.js +0 -99
  191. package/esm/tests/openapi-models.test.d.ts +0 -2
  192. package/esm/tests/openapi-models.test.d.ts.map +0 -1
  193. package/esm/tests/openapi-models.test.js +0 -45
  194. package/script/_dnt.test_polyfills.d.ts +0 -29
  195. package/script/_dnt.test_polyfills.d.ts.map +0 -1
  196. package/script/_dnt.test_polyfills.js +0 -32
  197. package/script/_dnt.test_shims.d.ts +0 -6
  198. package/script/_dnt.test_shims.d.ts.map +0 -1
  199. package/script/_dnt.test_shims.js +0 -65
  200. package/script/deps/jsr.io/@std/assert/1.0.6/assertion_error.d.ts +0 -26
  201. package/script/deps/jsr.io/@std/assert/1.0.6/assertion_error.d.ts.map +0 -1
  202. package/script/deps/jsr.io/@std/assert/1.0.6/assertion_error.js +0 -34
  203. package/script/deps/jsr.io/@std/assert/1.0.6/equal.d.ts +0 -17
  204. package/script/deps/jsr.io/@std/assert/1.0.6/equal.d.ts.map +0 -1
  205. package/script/deps/jsr.io/@std/assert/1.0.6/equal.js +0 -139
  206. package/script/deps/jsr.io/@std/assert/1.0.6/equals.d.ts +0 -22
  207. package/script/deps/jsr.io/@std/assert/1.0.6/equals.d.ts.map +0 -1
  208. package/script/deps/jsr.io/@std/assert/1.0.6/equals.js +0 -48
  209. package/script/deps/jsr.io/@std/assert/1.0.6/false.d.ts +0 -18
  210. package/script/deps/jsr.io/@std/assert/1.0.6/false.d.ts.map +0 -1
  211. package/script/deps/jsr.io/@std/assert/1.0.6/false.js +0 -25
  212. package/script/deps/jsr.io/@std/assert/1.0.6/instance_of.d.ts +0 -23
  213. package/script/deps/jsr.io/@std/assert/1.0.6/instance_of.d.ts.map +0 -1
  214. package/script/deps/jsr.io/@std/assert/1.0.6/instance_of.js +0 -56
  215. package/script/deps/jsr.io/@std/assert/1.0.6/is_error.d.ts +0 -25
  216. package/script/deps/jsr.io/@std/assert/1.0.6/is_error.d.ts.map +0 -1
  217. package/script/deps/jsr.io/@std/assert/1.0.6/is_error.js +0 -57
  218. package/script/deps/jsr.io/@std/assert/1.0.6/match.d.ts +0 -18
  219. package/script/deps/jsr.io/@std/assert/1.0.6/match.d.ts.map +0 -1
  220. package/script/deps/jsr.io/@std/assert/1.0.6/match.js +0 -29
  221. package/script/deps/jsr.io/@std/assert/1.0.6/not_instance_of.d.ts +0 -20
  222. package/script/deps/jsr.io/@std/assert/1.0.6/not_instance_of.d.ts.map +0 -1
  223. package/script/deps/jsr.io/@std/assert/1.0.6/not_instance_of.js +0 -32
  224. package/script/deps/jsr.io/@std/assert/1.0.6/not_match.d.ts +0 -18
  225. package/script/deps/jsr.io/@std/assert/1.0.6/not_match.d.ts.map +0 -1
  226. package/script/deps/jsr.io/@std/assert/1.0.6/not_match.js +0 -29
  227. package/script/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.d.ts +0 -23
  228. package/script/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.d.ts.map +0 -1
  229. package/script/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.js +0 -35
  230. package/script/deps/jsr.io/@std/assert/1.0.6/object_match.d.ts +0 -28
  231. package/script/deps/jsr.io/@std/assert/1.0.6/object_match.d.ts.map +0 -1
  232. package/script/deps/jsr.io/@std/assert/1.0.6/object_match.js +0 -162
  233. package/script/deps/jsr.io/@std/assert/1.0.6/rejects.d.ts +0 -42
  234. package/script/deps/jsr.io/@std/assert/1.0.6/rejects.d.ts.map +0 -1
  235. package/script/deps/jsr.io/@std/assert/1.0.6/rejects.js +0 -56
  236. package/script/deps/jsr.io/@std/assert/1.0.6/strict_equals.d.ts +0 -24
  237. package/script/deps/jsr.io/@std/assert/1.0.6/strict_equals.d.ts.map +0 -1
  238. package/script/deps/jsr.io/@std/assert/1.0.6/strict_equals.js +0 -60
  239. package/script/deps/jsr.io/@std/expect/1.0.6/_assert_equals.d.ts +0 -20
  240. package/script/deps/jsr.io/@std/expect/1.0.6/_assert_equals.d.ts.map +0 -1
  241. package/script/deps/jsr.io/@std/expect/1.0.6/_assert_equals.js +0 -32
  242. package/script/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.d.ts +0 -17
  243. package/script/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.d.ts.map +0 -1
  244. package/script/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.js +0 -29
  245. package/script/deps/jsr.io/@std/expect/1.0.6/_assertion.d.ts +0 -3
  246. package/script/deps/jsr.io/@std/expect/1.0.6/_assertion.d.ts.map +0 -1
  247. package/script/deps/jsr.io/@std/expect/1.0.6/_assertion.js +0 -13
  248. package/script/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.d.ts +0 -41
  249. package/script/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.d.ts.map +0 -1
  250. package/script/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.js +0 -161
  251. package/script/deps/jsr.io/@std/expect/1.0.6/_build_message.d.ts +0 -6
  252. package/script/deps/jsr.io/@std/expect/1.0.6/_build_message.d.ts.map +0 -1
  253. package/script/deps/jsr.io/@std/expect/1.0.6/_build_message.js +0 -33
  254. package/script/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.d.ts +0 -4
  255. package/script/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.d.ts.map +0 -1
  256. package/script/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.js +0 -15
  257. package/script/deps/jsr.io/@std/expect/1.0.6/_equal.d.ts +0 -9
  258. package/script/deps/jsr.io/@std/expect/1.0.6/_equal.d.ts.map +0 -1
  259. package/script/deps/jsr.io/@std/expect/1.0.6/_equal.js +0 -194
  260. package/script/deps/jsr.io/@std/expect/1.0.6/_extend.d.ts +0 -4
  261. package/script/deps/jsr.io/@std/expect/1.0.6/_extend.d.ts.map +0 -1
  262. package/script/deps/jsr.io/@std/expect/1.0.6/_extend.js +0 -15
  263. package/script/deps/jsr.io/@std/expect/1.0.6/_inspect_args.d.ts +0 -3
  264. package/script/deps/jsr.io/@std/expect/1.0.6/_inspect_args.d.ts.map +0 -1
  265. package/script/deps/jsr.io/@std/expect/1.0.6/_inspect_args.js +0 -17
  266. package/script/deps/jsr.io/@std/expect/1.0.6/_matchers.d.ts +0 -34
  267. package/script/deps/jsr.io/@std/expect/1.0.6/_matchers.d.ts.map +0 -1
  268. package/script/deps/jsr.io/@std/expect/1.0.6/_matchers.js +0 -598
  269. package/script/deps/jsr.io/@std/expect/1.0.6/_mock_util.d.ts +0 -11
  270. package/script/deps/jsr.io/@std/expect/1.0.6/_mock_util.d.ts.map +0 -1
  271. package/script/deps/jsr.io/@std/expect/1.0.6/_mock_util.js +0 -14
  272. package/script/deps/jsr.io/@std/expect/1.0.6/_types.d.ts +0 -838
  273. package/script/deps/jsr.io/@std/expect/1.0.6/_types.d.ts.map +0 -1
  274. package/script/deps/jsr.io/@std/expect/1.0.6/_types.js +0 -4
  275. package/script/deps/jsr.io/@std/expect/1.0.6/_utils.d.ts +0 -8
  276. package/script/deps/jsr.io/@std/expect/1.0.6/_utils.d.ts.map +0 -1
  277. package/script/deps/jsr.io/@std/expect/1.0.6/_utils.js +0 -166
  278. package/script/deps/jsr.io/@std/expect/1.0.6/expect.d.ts +0 -52
  279. package/script/deps/jsr.io/@std/expect/1.0.6/expect.d.ts.map +0 -1
  280. package/script/deps/jsr.io/@std/expect/1.0.6/expect.js +0 -445
  281. package/script/deps/jsr.io/@std/internal/1.0.4/assertion_state.d.ts +0 -75
  282. package/script/deps/jsr.io/@std/internal/1.0.4/assertion_state.d.ts.map +0 -1
  283. package/script/deps/jsr.io/@std/internal/1.0.4/assertion_state.js +0 -106
  284. package/script/deps/jsr.io/@std/internal/1.0.4/build_message.d.ts +0 -82
  285. package/script/deps/jsr.io/@std/internal/1.0.4/build_message.d.ts.map +0 -1
  286. package/script/deps/jsr.io/@std/internal/1.0.4/build_message.js +0 -116
  287. package/script/deps/jsr.io/@std/internal/1.0.4/diff.d.ts +0 -140
  288. package/script/deps/jsr.io/@std/internal/1.0.4/diff.d.ts.map +0 -1
  289. package/script/deps/jsr.io/@std/internal/1.0.4/diff.js +0 -284
  290. package/script/deps/jsr.io/@std/internal/1.0.4/diff_str.d.ts +0 -99
  291. package/script/deps/jsr.io/@std/internal/1.0.4/diff_str.d.ts.map +0 -1
  292. package/script/deps/jsr.io/@std/internal/1.0.4/diff_str.js +0 -186
  293. package/script/deps/jsr.io/@std/internal/1.0.4/format.d.ts +0 -2
  294. package/script/deps/jsr.io/@std/internal/1.0.4/format.d.ts.map +0 -1
  295. package/script/deps/jsr.io/@std/internal/1.0.4/format.js +0 -41
  296. package/script/deps/jsr.io/@std/internal/1.0.4/styles.d.ts +0 -159
  297. package/script/deps/jsr.io/@std/internal/1.0.4/styles.d.ts.map +0 -1
  298. package/script/deps/jsr.io/@std/internal/1.0.4/styles.js +0 -220
  299. package/script/deps/jsr.io/@std/internal/1.0.4/types.d.ts +0 -16
  300. package/script/deps/jsr.io/@std/internal/1.0.4/types.d.ts.map +0 -1
  301. package/script/deps/jsr.io/@std/internal/1.0.4/types.js +0 -3
  302. package/script/deps/jsr.io/@std/testing/1.0.3/_test_suite.d.ts +0 -74
  303. package/script/deps/jsr.io/@std/testing/1.0.3/_test_suite.d.ts.map +0 -1
  304. package/script/deps/jsr.io/@std/testing/1.0.3/_test_suite.js +0 -345
  305. package/script/deps/jsr.io/@std/testing/1.0.3/bdd.d.ts +0 -731
  306. package/script/deps/jsr.io/@std/testing/1.0.3/bdd.d.ts.map +0 -1
  307. package/script/deps/jsr.io/@std/testing/1.0.3/bdd.js +0 -587
  308. package/script/deps/jsr.io/@std/testing/1.0.3/mock.d.ts +0 -674
  309. package/script/deps/jsr.io/@std/testing/1.0.3/mock.d.ts.map +0 -1
  310. package/script/deps/jsr.io/@std/testing/1.0.3/mock.js +0 -1136
  311. package/script/src/ast/nodes/annotation.test.d.ts +0 -2
  312. package/script/src/ast/nodes/annotation.test.d.ts.map +0 -1
  313. package/script/src/ast/nodes/annotation.test.js +0 -58
  314. package/script/src/ast/nodes/argument.test.d.ts +0 -2
  315. package/script/src/ast/nodes/argument.test.d.ts.map +0 -1
  316. package/script/src/ast/nodes/argument.test.js +0 -35
  317. package/script/src/ast/nodes/call.test.d.ts +0 -2
  318. package/script/src/ast/nodes/call.test.d.ts.map +0 -1
  319. package/script/src/ast/nodes/call.test.js +0 -37
  320. package/script/src/ast/nodes/class.test.d.ts +0 -2
  321. package/script/src/ast/nodes/class.test.d.ts.map +0 -1
  322. package/script/src/ast/nodes/class.test.js +0 -221
  323. package/script/src/ast/nodes/constructor.test.d.ts +0 -2
  324. package/script/src/ast/nodes/constructor.test.d.ts.map +0 -1
  325. package/script/src/ast/nodes/constructor.test.js +0 -141
  326. package/script/src/ast/nodes/doc-tag.test.d.ts +0 -2
  327. package/script/src/ast/nodes/doc-tag.test.d.ts.map +0 -1
  328. package/script/src/ast/nodes/doc-tag.test.js +0 -69
  329. package/script/src/ast/nodes/doc.test.d.ts +0 -2
  330. package/script/src/ast/nodes/doc.test.d.ts.map +0 -1
  331. package/script/src/ast/nodes/doc.test.js +0 -35
  332. package/script/src/ast/nodes/enum-value.test.d.ts +0 -2
  333. package/script/src/ast/nodes/enum-value.test.d.ts.map +0 -1
  334. package/script/src/ast/nodes/enum-value.test.js +0 -117
  335. package/script/src/ast/nodes/enum.test.d.ts +0 -2
  336. package/script/src/ast/nodes/enum.test.d.ts.map +0 -1
  337. package/script/src/ast/nodes/enum.test.js +0 -131
  338. package/script/src/ast/nodes/function.test.d.ts +0 -2
  339. package/script/src/ast/nodes/function.test.d.ts.map +0 -1
  340. package/script/src/ast/nodes/function.test.js +0 -162
  341. package/script/src/ast/nodes/generic-parameter.test.d.ts +0 -2
  342. package/script/src/ast/nodes/generic-parameter.test.d.ts.map +0 -1
  343. package/script/src/ast/nodes/generic-parameter.test.js +0 -51
  344. package/script/src/ast/nodes/init-block.test.d.ts +0 -2
  345. package/script/src/ast/nodes/init-block.test.d.ts.map +0 -1
  346. package/script/src/ast/nodes/init-block.test.js +0 -30
  347. package/script/src/ast/nodes/interface.test.d.ts +0 -2
  348. package/script/src/ast/nodes/interface.test.d.ts.map +0 -1
  349. package/script/src/ast/nodes/interface.test.js +0 -123
  350. package/script/src/ast/nodes/object.test.d.ts +0 -2
  351. package/script/src/ast/nodes/object.test.d.ts.map +0 -1
  352. package/script/src/ast/nodes/object.test.js +0 -80
  353. package/script/src/ast/nodes/parameter.test.d.ts +0 -2
  354. package/script/src/ast/nodes/parameter.test.d.ts.map +0 -1
  355. package/script/src/ast/nodes/parameter.test.js +0 -154
  356. package/script/src/ast/nodes/property.test.d.ts +0 -2
  357. package/script/src/ast/nodes/property.test.d.ts.map +0 -1
  358. package/script/src/ast/nodes/property.test.js +0 -208
  359. package/script/src/ast/nodes/reference.test.d.ts +0 -2
  360. package/script/src/ast/nodes/reference.test.d.ts.map +0 -1
  361. package/script/src/ast/nodes/reference.test.js +0 -38
  362. package/script/src/ast/nodes/string.test.d.ts +0 -2
  363. package/script/src/ast/nodes/string.test.d.ts.map +0 -1
  364. package/script/src/ast/nodes/string.test.js +0 -50
  365. package/script/src/import-collection.test.d.ts +0 -2
  366. package/script/src/import-collection.test.d.ts.map +0 -1
  367. package/script/src/import-collection.test.js +0 -101
  368. package/script/tests/openapi-models.test.d.ts +0 -2
  369. package/script/tests/openapi-models.test.d.ts.map +0 -1
  370. package/script/tests/openapi-models.test.js +0 -48
  371. package/src/_dnt.test_polyfills.ts +0 -70
  372. package/src/_dnt.test_shims.ts +0 -64
  373. package/src/deps/jsr.io/@std/assert/1.0.6/assertion_error.ts +0 -31
  374. package/src/deps/jsr.io/@std/assert/1.0.6/equal.ts +0 -152
  375. package/src/deps/jsr.io/@std/assert/1.0.6/equals.ts +0 -52
  376. package/src/deps/jsr.io/@std/assert/1.0.6/false.ts +0 -26
  377. package/src/deps/jsr.io/@std/assert/1.0.6/instance_of.ts +0 -64
  378. package/src/deps/jsr.io/@std/assert/1.0.6/is_error.ts +0 -65
  379. package/src/deps/jsr.io/@std/assert/1.0.6/match.ts +0 -30
  380. package/src/deps/jsr.io/@std/assert/1.0.6/not_instance_of.ts +0 -33
  381. package/src/deps/jsr.io/@std/assert/1.0.6/not_match.ts +0 -30
  382. package/src/deps/jsr.io/@std/assert/1.0.6/not_strict_equals.ts +0 -42
  383. package/src/deps/jsr.io/@std/assert/1.0.6/object_match.ts +0 -202
  384. package/src/deps/jsr.io/@std/assert/1.0.6/rejects.ts +0 -123
  385. package/src/deps/jsr.io/@std/assert/1.0.6/strict_equals.ts +0 -67
  386. package/src/deps/jsr.io/@std/expect/1.0.6/_assert_equals.ts +0 -38
  387. package/src/deps/jsr.io/@std/expect/1.0.6/_assert_not_equals.ts +0 -35
  388. package/src/deps/jsr.io/@std/expect/1.0.6/_assertion.ts +0 -13
  389. package/src/deps/jsr.io/@std/expect/1.0.6/_asymmetric_matchers.ts +0 -177
  390. package/src/deps/jsr.io/@std/expect/1.0.6/_build_message.ts +0 -51
  391. package/src/deps/jsr.io/@std/expect/1.0.6/_custom_equality_tester.ts +0 -19
  392. package/src/deps/jsr.io/@std/expect/1.0.6/_equal.ts +0 -229
  393. package/src/deps/jsr.io/@std/expect/1.0.6/_extend.ts +0 -16
  394. package/src/deps/jsr.io/@std/expect/1.0.6/_inspect_args.ts +0 -15
  395. package/src/deps/jsr.io/@std/expect/1.0.6/_matchers.ts +0 -828
  396. package/src/deps/jsr.io/@std/expect/1.0.6/_mock_util.ts +0 -22
  397. package/src/deps/jsr.io/@std/expect/1.0.6/_types.ts +0 -917
  398. package/src/deps/jsr.io/@std/expect/1.0.6/_utils.ts +0 -201
  399. package/src/deps/jsr.io/@std/expect/1.0.6/expect.ts +0 -529
  400. package/src/deps/jsr.io/@std/internal/1.0.4/assertion_state.ts +0 -111
  401. package/src/deps/jsr.io/@std/internal/1.0.4/build_message.ts +0 -134
  402. package/src/deps/jsr.io/@std/internal/1.0.4/diff.ts +0 -317
  403. package/src/deps/jsr.io/@std/internal/1.0.4/diff_str.ts +0 -204
  404. package/src/deps/jsr.io/@std/internal/1.0.4/format.ts +0 -39
  405. package/src/deps/jsr.io/@std/internal/1.0.4/styles.ts +0 -233
  406. package/src/deps/jsr.io/@std/internal/1.0.4/types.ts +0 -18
  407. package/src/deps/jsr.io/@std/testing/1.0.3/_test_suite.ts +0 -389
  408. package/src/deps/jsr.io/@std/testing/1.0.3/bdd.ts +0 -1202
  409. package/src/deps/jsr.io/@std/testing/1.0.3/mock.ts +0 -1839
  410. package/src/mod.ts +0 -8
  411. package/src/src/assets.ts +0 -9
  412. package/src/src/ast/_index.ts +0 -66
  413. package/src/src/ast/common.ts +0 -1
  414. package/src/src/ast/index.ts +0 -1
  415. package/src/src/ast/node.ts +0 -10
  416. package/src/src/ast/nodes/annotation.test.ts +0 -69
  417. package/src/src/ast/nodes/annotation.ts +0 -79
  418. package/src/src/ast/nodes/argument.test.ts +0 -41
  419. package/src/src/ast/nodes/argument.ts +0 -62
  420. package/src/src/ast/nodes/call.test.ts +0 -44
  421. package/src/src/ast/nodes/call.ts +0 -66
  422. package/src/src/ast/nodes/class.test.ts +0 -274
  423. package/src/src/ast/nodes/class.ts +0 -178
  424. package/src/src/ast/nodes/collection-literal.ts +0 -49
  425. package/src/src/ast/nodes/constructor.test.ts +0 -178
  426. package/src/src/ast/nodes/constructor.ts +0 -126
  427. package/src/src/ast/nodes/doc-tag.test.ts +0 -84
  428. package/src/src/ast/nodes/doc-tag.ts +0 -138
  429. package/src/src/ast/nodes/doc.test.ts +0 -42
  430. package/src/src/ast/nodes/doc.ts +0 -111
  431. package/src/src/ast/nodes/enum-value.test.ts +0 -146
  432. package/src/src/ast/nodes/enum-value.ts +0 -100
  433. package/src/src/ast/nodes/enum.test.ts +0 -167
  434. package/src/src/ast/nodes/enum.ts +0 -163
  435. package/src/src/ast/nodes/function.test.ts +0 -194
  436. package/src/src/ast/nodes/function.ts +0 -176
  437. package/src/src/ast/nodes/generic-parameter.test.ts +0 -60
  438. package/src/src/ast/nodes/generic-parameter.ts +0 -54
  439. package/src/src/ast/nodes/init-block.test.ts +0 -36
  440. package/src/src/ast/nodes/init-block.ts +0 -38
  441. package/src/src/ast/nodes/interface.test.ts +0 -151
  442. package/src/src/ast/nodes/interface.ts +0 -133
  443. package/src/src/ast/nodes/lambda-type.ts +0 -68
  444. package/src/src/ast/nodes/lambda.ts +0 -68
  445. package/src/src/ast/nodes/object.test.ts +0 -101
  446. package/src/src/ast/nodes/object.ts +0 -102
  447. package/src/src/ast/nodes/parameter.test.ts +0 -191
  448. package/src/src/ast/nodes/parameter.ts +0 -118
  449. package/src/src/ast/nodes/property.test.ts +0 -253
  450. package/src/src/ast/nodes/property.ts +0 -225
  451. package/src/src/ast/nodes/reference.test.ts +0 -47
  452. package/src/src/ast/nodes/reference.ts +0 -178
  453. package/src/src/ast/nodes/string.test.ts +0 -61
  454. package/src/src/ast/nodes/string.ts +0 -114
  455. package/src/src/ast/nodes/types.ts +0 -23
  456. package/src/src/ast/references/index.ts +0 -8
  457. package/src/src/ast/references/jackson.ts +0 -16
  458. package/src/src/ast/references/jakarta.ts +0 -10
  459. package/src/src/ast/references/java.ts +0 -19
  460. package/src/src/ast/references/kotlin.ts +0 -40
  461. package/src/src/ast/references/okhttp3.ts +0 -5
  462. package/src/src/ast/references/reactor.ts +0 -5
  463. package/src/src/ast/references/spring.ts +0 -58
  464. package/src/src/ast/references/swagger.ts +0 -19
  465. package/src/src/ast/utils/get-kotlin-builder-options.ts +0 -19
  466. package/src/src/ast/utils/to-kt-node.ts +0 -31
  467. package/src/src/ast/utils/write-kt-annotations.ts +0 -15
  468. package/src/src/ast/utils/write-kt-arguments.ts +0 -45
  469. package/src/src/ast/utils/write-kt-enum-values.ts +0 -27
  470. package/src/src/ast/utils/write-kt-generic-parameters.ts +0 -12
  471. package/src/src/ast/utils/write-kt-members.ts +0 -25
  472. package/src/src/ast/utils/write-kt-node.ts +0 -37
  473. package/src/src/ast/utils/write-kt-parameters.ts +0 -25
  474. package/src/src/common-results.ts +0 -4
  475. package/src/src/config.ts +0 -39
  476. package/src/src/file-builder.ts +0 -108
  477. package/src/src/generators/file-generator.ts +0 -29
  478. package/src/src/generators/index.ts +0 -4
  479. package/src/src/generators/models/args.ts +0 -128
  480. package/src/src/generators/models/index.ts +0 -4
  481. package/src/src/generators/models/model-generator.ts +0 -602
  482. package/src/src/generators/models/models-generator.ts +0 -65
  483. package/src/src/generators/models/models.ts +0 -95
  484. package/src/src/generators/services/okhttp3-clients/args.ts +0 -78
  485. package/src/src/generators/services/okhttp3-clients/index.ts +0 -4
  486. package/src/src/generators/services/okhttp3-clients/models.ts +0 -60
  487. package/src/src/generators/services/okhttp3-clients/okhttp3-client-generator.ts +0 -557
  488. package/src/src/generators/services/okhttp3-clients/okhttp3-clients-generator.ts +0 -128
  489. package/src/src/generators/services/okhttp3-clients/refs.ts +0 -55
  490. package/src/src/generators/services/spring-controllers/args.ts +0 -71
  491. package/src/src/generators/services/spring-controllers/index.ts +0 -4
  492. package/src/src/generators/services/spring-controllers/models.ts +0 -58
  493. package/src/src/generators/services/spring-controllers/spring-controller-generator.ts +0 -605
  494. package/src/src/generators/services/spring-controllers/spring-controllers-generator.ts +0 -70
  495. package/src/src/import-collection.test.ts +0 -116
  496. package/src/src/import-collection.ts +0 -98
  497. package/src/src/types.ts +0 -3
  498. package/src/src/utils.ts +0 -39
  499. package/src/tests/openapi-models.test.ts +0 -63
  500. package/test_runner.js +0 -227
@@ -1,1839 +0,0 @@
1
- // Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
2
-
3
- /** A mocking and spying library.
4
- *
5
- * Test spies are function stand-ins that are used to assert if a function's
6
- * internal behavior matches expectations. Test spies on methods keep the original
7
- * behavior but allow you to test how the method is called and what it returns.
8
- * Test stubs are an extension of test spies that also replaces the original
9
- * methods behavior.
10
- *
11
- * ## Spying
12
- *
13
- * Say we have two functions, `square` and `multiply`, if we want to assert that
14
- * the `multiply` function is called during execution of the `square` function we
15
- * need a way to spy on the `multiply` function. There are a few ways to achieve
16
- * this with Spies, one is to have the `square` function take the `multiply`
17
- * multiply as a parameter.
18
- *
19
- * This way, we can call `square(multiply, value)` in the application code or wrap
20
- * a spy function around the `multiply` function and call
21
- * `square(multiplySpy, value)` in the testing code.
22
- *
23
- * ```ts
24
- * import {
25
- * assertSpyCall,
26
- * assertSpyCalls,
27
- * spy,
28
- * } from "@std/testing/mock";
29
- * import { assertEquals } from "@std/assert";
30
- *
31
- * function multiply(a: number, b: number): number {
32
- * return a * b;
33
- * }
34
- *
35
- * function square(
36
- * multiplyFn: (a: number, b: number) => number,
37
- * value: number,
38
- * ): number {
39
- * return multiplyFn(value, value);
40
- * }
41
- *
42
- * Deno.test("square calls multiply and returns results", () => {
43
- * const multiplySpy = spy(multiply);
44
- *
45
- * assertEquals(square(multiplySpy, 5), 25);
46
- *
47
- * // asserts that multiplySpy was called at least once and details about the first call.
48
- * assertSpyCall(multiplySpy, 0, {
49
- * args: [5, 5],
50
- * returned: 25,
51
- * });
52
- *
53
- * // asserts that multiplySpy was only called once.
54
- * assertSpyCalls(multiplySpy, 1);
55
- * });
56
- * ```
57
- *
58
- * If you prefer not adding additional parameters for testing purposes only, you
59
- * can use spy to wrap a method on an object instead. In the following example, the
60
- * exported `_internals` object has the `multiply` function we want to call as a
61
- * method and the `square` function calls `_internals.multiply` instead of
62
- * `multiply`.
63
- *
64
- * This way, we can call `square(value)` in both the application code and testing
65
- * code. Then spy on the `multiply` method on the `_internals` object in the
66
- * testing code to be able to spy on how the `square` function calls the `multiply`
67
- * function.
68
- *
69
- * ```ts
70
- * import {
71
- * assertSpyCall,
72
- * assertSpyCalls,
73
- * spy,
74
- * } from "@std/testing/mock";
75
- * import { assertEquals } from "@std/assert";
76
- *
77
- * function multiply(a: number, b: number): number {
78
- * return a * b;
79
- * }
80
- *
81
- * function square(value: number): number {
82
- * return _internals.multiply(value, value);
83
- * }
84
- *
85
- * const _internals = { multiply };
86
- *
87
- * Deno.test("square calls multiply and returns results", () => {
88
- * const multiplySpy = spy(_internals, "multiply");
89
- *
90
- * try {
91
- * assertEquals(square(5), 25);
92
- * } finally {
93
- * // unwraps the multiply method on the _internals object
94
- * multiplySpy.restore();
95
- * }
96
- *
97
- * // asserts that multiplySpy was called at least once and details about the first call.
98
- * assertSpyCall(multiplySpy, 0, {
99
- * args: [5, 5],
100
- * returned: 25,
101
- * });
102
- *
103
- * // asserts that multiplySpy was only called once.
104
- * assertSpyCalls(multiplySpy, 1);
105
- * });
106
- * ```
107
- *
108
- * One difference you may have noticed between these two examples is that in the
109
- * second we call the `restore` method on `multiplySpy` function. That is needed to
110
- * remove the spy wrapper from the `_internals` object's `multiply` method. The
111
- * `restore` method is called in a finally block to ensure that it is restored
112
- * whether or not the assertion in the try block is successful. The `restore`
113
- * method didn't need to be called in the first example because the `multiply`
114
- * function was not modified in any way like the `_internals` object was in the
115
- * second example.
116
- *
117
- * Method spys are disposable, meaning that you can have them automatically restore
118
- * themselves with the `using` keyword. Using this approach is cleaner because you
119
- * do not need to wrap your assertions in a try statement to ensure you restore the
120
- * methods before the tests finish.
121
- *
122
- * ```ts
123
- * import {
124
- * assertSpyCall,
125
- * assertSpyCalls,
126
- * spy,
127
- * } from "@std/testing/mock";
128
- * import { assertEquals } from "@std/assert";
129
- *
130
- * function multiply(a: number, b: number): number {
131
- * return a * b;
132
- * }
133
- *
134
- * function square(value: number): number {
135
- * return _internals.multiply(value, value);
136
- * }
137
- *
138
- * const _internals = { multiply };
139
- *
140
- * Deno.test("square calls multiply and returns results", () => {
141
- * using multiplySpy = spy(_internals, "multiply");
142
- *
143
- * assertEquals(square(5), 25);
144
- *
145
- * // asserts that multiplySpy was called at least once and details about the first call.
146
- * assertSpyCall(multiplySpy, 0, {
147
- * args: [5, 5],
148
- * returned: 25,
149
- * });
150
- *
151
- * // asserts that multiplySpy was only called once.
152
- * assertSpyCalls(multiplySpy, 1);
153
- * });
154
- * ```
155
- *
156
- * ## Stubbing
157
- *
158
- * Say we have two functions, `randomMultiple` and `randomInt`, if we want to
159
- * assert that `randomInt` is called during execution of `randomMultiple` we need a
160
- * way to spy on the `randomInt` function. That could be done with either of the
161
- * spying techniques previously mentioned. To be able to verify that the
162
- * `randomMultiple` function returns the value we expect it to for what `randomInt`
163
- * returns, the easiest way would be to replace the `randomInt` function's behavior
164
- * with more predictable behavior.
165
- *
166
- * You could use the first spying technique to do that but that would require
167
- * adding a `randomInt` parameter to the `randomMultiple` function.
168
- *
169
- * You could also use the second spying technique to do that, but your assertions
170
- * would not be as predictable due to the `randomInt` function returning random
171
- * values.
172
- *
173
- * Say we want to verify it returns correct values for both negative and positive
174
- * random integers. We could easily do that with stubbing. The below example is
175
- * similar to the second spying technique example but instead of passing the call
176
- * through to the original `randomInt` function, we are going to replace
177
- * `randomInt` with a function that returns pre-defined values.
178
- *
179
- * The mock module includes some helper functions to make creating common stubs
180
- * easy. The `returnsNext` function takes an array of values we want it to return
181
- * on consecutive calls.
182
- *
183
- * ```ts
184
- * import {
185
- * assertSpyCall,
186
- * assertSpyCalls,
187
- * returnsNext,
188
- * stub,
189
- * } from "@std/testing/mock";
190
- * import { assertEquals } from "@std/assert";
191
- *
192
- * function randomInt(lowerBound: number, upperBound: number): number {
193
- * return lowerBound + Math.floor(Math.random() * (upperBound - lowerBound));
194
- * }
195
- *
196
- * function randomMultiple(value: number): number {
197
- * return value * _internals.randomInt(-10, 10);
198
- * }
199
- *
200
- * const _internals = { randomInt };
201
- *
202
- * Deno.test("randomMultiple uses randomInt to generate random multiples between -10 and 10 times the value", () => {
203
- * const randomIntStub = stub(_internals, "randomInt", returnsNext([-3, 3]));
204
- *
205
- * try {
206
- * assertEquals(randomMultiple(5), -15);
207
- * assertEquals(randomMultiple(5), 15);
208
- * } finally {
209
- * // unwraps the randomInt method on the _internals object
210
- * randomIntStub.restore();
211
- * }
212
- *
213
- * // asserts that randomIntStub was called at least once and details about the first call.
214
- * assertSpyCall(randomIntStub, 0, {
215
- * args: [-10, 10],
216
- * returned: -3,
217
- * });
218
- * // asserts that randomIntStub was called at least twice and details about the second call.
219
- * assertSpyCall(randomIntStub, 1, {
220
- * args: [-10, 10],
221
- * returned: 3,
222
- * });
223
- *
224
- * // asserts that randomIntStub was only called twice.
225
- * assertSpyCalls(randomIntStub, 2);
226
- * });
227
- * ```
228
- *
229
- * Like method spys, stubs are disposable, meaning that you can have them automatically
230
- * restore themselves with the `using` keyword. Using this approach is cleaner because
231
- * you do not need to wrap your assertions in a try statement to ensure you restore the
232
- * methods before the tests finish.
233
- *
234
- * ```ts
235
- * import {
236
- * assertSpyCall,
237
- * assertSpyCalls,
238
- * returnsNext,
239
- * stub,
240
- * } from "@std/testing/mock";
241
- * import { assertEquals } from "@std/assert";
242
- *
243
- * function randomInt(lowerBound: number, upperBound: number): number {
244
- * return lowerBound + Math.floor(Math.random() * (upperBound - lowerBound));
245
- * }
246
- *
247
- * function randomMultiple(value: number): number {
248
- * return value * _internals.randomInt(-10, 10);
249
- * }
250
- *
251
- * const _internals = { randomInt };
252
- *
253
- * Deno.test("randomMultiple uses randomInt to generate random multiples between -10 and 10 times the value", () => {
254
- * using randomIntStub = stub(_internals, "randomInt", returnsNext([-3, 3]));
255
- *
256
- * assertEquals(randomMultiple(5), -15);
257
- * assertEquals(randomMultiple(5), 15);
258
- *
259
- * // asserts that randomIntStub was called at least once and details about the first call.
260
- * assertSpyCall(randomIntStub, 0, {
261
- * args: [-10, 10],
262
- * returned: -3,
263
- * });
264
- * // asserts that randomIntStub was called at least twice and details about the second call.
265
- * assertSpyCall(randomIntStub, 1, {
266
- * args: [-10, 10],
267
- * returned: 3,
268
- * });
269
- *
270
- * // asserts that randomIntStub was only called twice.
271
- * assertSpyCalls(randomIntStub, 2);
272
- * });
273
- * ```
274
- *
275
- * ## Faking time
276
- *
277
- * Say we have a function that has time based behavior that we would like to test.
278
- * With real time, that could cause tests to take much longer than they should. If
279
- * you fake time, you could simulate how your function would behave over time
280
- * starting from any point in time. Below is an example where we want to test that
281
- * the callback is called every second.
282
- *
283
- * With `FakeTime` we can do that. When the `FakeTime` instance is created, it
284
- * splits from real time. The `Date`, `setTimeout`, `clearTimeout`, `setInterval`
285
- * and `clearInterval` globals are replaced with versions that use the fake time
286
- * until real time is restored. You can control how time ticks forward with the
287
- * `tick` method on the `FakeTime` instance.
288
- *
289
- * ```ts
290
- * import {
291
- * assertSpyCalls,
292
- * spy,
293
- * } from "@std/testing/mock";
294
- * import { FakeTime } from "@std/testing/time";
295
- *
296
- * function secondInterval(cb: () => void): number {
297
- * return setInterval(cb, 1000);
298
- * }
299
- *
300
- * Deno.test("secondInterval calls callback every second and stops after being cleared", () => {
301
- * using time = new FakeTime();
302
- *
303
- * const cb = spy();
304
- * const intervalId = secondInterval(cb);
305
- * assertSpyCalls(cb, 0);
306
- * time.tick(500);
307
- * assertSpyCalls(cb, 0);
308
- * time.tick(500);
309
- * assertSpyCalls(cb, 1);
310
- * time.tick(3500);
311
- * assertSpyCalls(cb, 4);
312
- *
313
- * clearInterval(intervalId);
314
- * time.tick(1000);
315
- * assertSpyCalls(cb, 4);
316
- * });
317
- * ```
318
- *
319
- * @module
320
- */
321
-
322
- import { assertEquals } from "../../assert/1.0.6/equals.js";
323
- import { assertIsError } from "../../assert/1.0.6/is_error.js";
324
- import { assertRejects } from "../../assert/1.0.6/rejects.js";
325
- import { AssertionError } from "../../assert/1.0.6/assertion_error.js";
326
-
327
- /**
328
- * An error related to spying on a function or instance method.
329
- *
330
- * @example Usage
331
- * ```ts
332
- * import { MockError, spy } from "@std/testing/mock";
333
- * import { assertThrows } from "@std/assert";
334
- *
335
- * assertThrows(() => {
336
- * spy({} as any, "no-such-method");
337
- * }, MockError);
338
- * ```
339
- */
340
- export class MockError extends Error {
341
- /**
342
- * Construct MockError
343
- *
344
- * @param message The error message.
345
- */
346
- constructor(message: string) {
347
- super(message);
348
- this.name = "MockError";
349
- }
350
- }
351
-
352
- /** Call information recorded by a spy. */
353
- export interface SpyCall<
354
- // deno-lint-ignore no-explicit-any
355
- Self = any,
356
- // deno-lint-ignore no-explicit-any
357
- Args extends unknown[] = any[],
358
- // deno-lint-ignore no-explicit-any
359
- Return = any,
360
- > {
361
- /** Arguments passed to a function when called. */
362
- args: Args;
363
- /** The value that was returned by a function. */
364
- returned?: Return;
365
- /** The error value that was thrown by a function. */
366
- error?: Error;
367
- /** The instance that a method was called on. */
368
- self?: Self;
369
- }
370
-
371
- /** A function or instance method wrapper that records all calls made to it. */
372
- export interface Spy<
373
- // deno-lint-ignore no-explicit-any
374
- Self = any,
375
- // deno-lint-ignore no-explicit-any
376
- Args extends unknown[] = any[],
377
- // deno-lint-ignore no-explicit-any
378
- Return = any,
379
- > {
380
- (this: Self, ...args: Args): Return;
381
- /** The function that is being spied on. */
382
- original: (this: Self, ...args: Args) => Return;
383
- /** Information about calls made to the function or instance method. */
384
- calls: SpyCall<Self, Args, Return>[];
385
- /** Whether or not the original instance method has been restored. */
386
- restored: boolean;
387
- /** If spying on an instance method, this restores the original instance method. */
388
- restore(): void;
389
- }
390
-
391
- /** An instance method wrapper that records all calls made to it. */
392
- export interface MethodSpy<
393
- // deno-lint-ignore no-explicit-any
394
- Self = any,
395
- // deno-lint-ignore no-explicit-any
396
- Args extends unknown[] = any[],
397
- // deno-lint-ignore no-explicit-any
398
- Return = any,
399
- > extends Spy<Self, Args, Return>, Disposable {}
400
-
401
- /** Wraps a function with a Spy. */
402
- function functionSpy<
403
- Self,
404
- Args extends unknown[],
405
- Return,
406
- >(func?: (this: Self, ...args: Args) => Return): Spy<Self, Args, Return> {
407
- const original = func ?? (() => {}) as (this: Self, ...args: Args) => Return;
408
- const calls: SpyCall<Self, Args, Return>[] = [];
409
- const spy = function (this: Self, ...args: Args): Return {
410
- const call: SpyCall<Self, Args, Return> = { args };
411
- if (this) call.self = this;
412
- try {
413
- call.returned = original.apply(this, args);
414
- } catch (error) {
415
- call.error = error as Error;
416
- calls.push(call);
417
- throw error;
418
- }
419
- calls.push(call);
420
- return call.returned;
421
- } as Spy<Self, Args, Return>;
422
- Object.defineProperties(spy, {
423
- original: {
424
- enumerable: true,
425
- value: original,
426
- },
427
- calls: {
428
- enumerable: true,
429
- value: calls,
430
- },
431
- restored: {
432
- enumerable: true,
433
- get: () => false,
434
- },
435
- restore: {
436
- enumerable: true,
437
- value: () => {
438
- throw new MockError(
439
- "Cannot restore: function cannot be restored",
440
- );
441
- },
442
- },
443
- });
444
- return spy;
445
- }
446
-
447
- /**
448
- * Checks if a function is a spy.
449
- *
450
- * @typeParam Self The self type of the function.
451
- * @typeParam Args The arguments type of the function.
452
- * @typeParam Return The return type of the function.
453
- * @param func The function to check
454
- * @return `true` if the function is a spy, `false` otherwise.
455
- */
456
- function isSpy<Self, Args extends unknown[], Return>(
457
- func: ((this: Self, ...args: Args) => Return) | unknown,
458
- ): func is Spy<Self, Args, Return> {
459
- const spy = func as Spy<Self, Args, Return>;
460
- return typeof spy === "function" &&
461
- typeof spy.original === "function" &&
462
- typeof spy.restored === "boolean" &&
463
- typeof spy.restore === "function" &&
464
- Array.isArray(spy.calls);
465
- }
466
-
467
- // deno-lint-ignore no-explicit-any
468
- const sessions: Set<Spy<any, any[], any>>[] = [];
469
- // deno-lint-ignore no-explicit-any
470
- function getSession(): Set<Spy<any, any[], any>> {
471
- if (sessions.length === 0) sessions.push(new Set());
472
- return sessions.at(-1)!;
473
- }
474
- // deno-lint-ignore no-explicit-any
475
- function registerMock(spy: Spy<any, any[], any>) {
476
- const session = getSession();
477
- session.add(spy);
478
- }
479
- // deno-lint-ignore no-explicit-any
480
- function unregisterMock(spy: Spy<any, any[], any>) {
481
- const session = getSession();
482
- session.delete(spy);
483
- }
484
-
485
- /**
486
- * Creates a session that tracks all mocks created before it's restored.
487
- * If a callback is provided, it restores all mocks created within it.
488
- *
489
- * @example Usage
490
- * ```ts
491
- * import { mockSession, restore, stub } from "@std/testing/mock";
492
- * import { assertEquals, assertNotEquals } from "@std/assert";
493
- *
494
- * const setTimeout = globalThis.setTimeout;
495
- * const id = mockSession();
496
- *
497
- * stub(globalThis, "setTimeout");
498
- *
499
- * assertNotEquals(globalThis.setTimeout, setTimeout);
500
- *
501
- * restore(id);
502
- *
503
- * assertEquals(globalThis.setTimeout, setTimeout);
504
- * ```
505
- *
506
- * @returns The id of the created session.
507
- */
508
- export function mockSession(): number;
509
- /**
510
- * Creates a session that tracks all mocks created before it's restored.
511
- * If a callback is provided, it restores all mocks created within it.
512
- *
513
- * @example Usage
514
- * ```ts
515
- * import { mockSession, restore, stub } from "@std/testing/mock";
516
- * import { assertEquals, assertNotEquals } from "@std/assert";
517
- *
518
- * const setTimeout = globalThis.setTimeout;
519
- * const session = mockSession(() => {
520
- * stub(globalThis, "setTimeout");
521
- * assertNotEquals(globalThis.setTimeout, setTimeout);
522
- * });
523
- *
524
- * session();
525
- *
526
- * assertEquals(globalThis.setTimeout, setTimeout); // stub is restored
527
- * ```
528
- *
529
- * @typeParam Self The self type of the function.
530
- * @typeParam Args The arguments type of the function.
531
- * @typeParam Return The return type of the function.
532
- * @param func The function to be used for the created session.
533
- * @returns The function to execute the session.
534
- */
535
- export function mockSession<
536
- Self,
537
- Args extends unknown[],
538
- Return,
539
- >(
540
- func: (this: Self, ...args: Args) => Return,
541
- ): (this: Self, ...args: Args) => Return;
542
- export function mockSession<
543
- Self,
544
- Args extends unknown[],
545
- Return,
546
- >(
547
- func?: (this: Self, ...args: Args) => Return,
548
- ): number | ((this: Self, ...args: Args) => Return) {
549
- if (func) {
550
- return function (this: Self, ...args: Args): Return {
551
- const id = sessions.length;
552
- sessions.push(new Set());
553
- try {
554
- return func.apply(this, args);
555
- } finally {
556
- restore(id);
557
- }
558
- };
559
- } else {
560
- sessions.push(new Set());
561
- return sessions.length - 1;
562
- }
563
- }
564
-
565
- /**
566
- * Creates an async session that tracks all mocks created before the promise resolves.
567
- *
568
- * @example Usage
569
- * ```ts
570
- * import { mockSessionAsync, restore, stub } from "@std/testing/mock";
571
- * import { assertEquals, assertNotEquals } from "@std/assert";
572
- *
573
- * const setTimeout = globalThis.setTimeout;
574
- * const session = mockSessionAsync(async () => {
575
- * stub(globalThis, "setTimeout");
576
- * assertNotEquals(globalThis.setTimeout, setTimeout);
577
- * });
578
- *
579
- * await session();
580
- *
581
- * assertEquals(globalThis.setTimeout, setTimeout); // stub is restored
582
- * ```
583
- * @typeParam Self The self type of the function.
584
- * @typeParam Args The arguments type of the function.
585
- * @typeParam Return The return type of the function.
586
- * @param func The function.
587
- * @returns The return value of the function.
588
- */
589
- export function mockSessionAsync<
590
- Self,
591
- Args extends unknown[],
592
- Return,
593
- >(
594
- func: (this: Self, ...args: Args) => Promise<Return>,
595
- ): (this: Self, ...args: Args) => Promise<Return> {
596
- return async function (this: Self, ...args: Args): Promise<Return> {
597
- const id = sessions.length;
598
- sessions.push(new Set());
599
- try {
600
- return await func.apply(this, args);
601
- } finally {
602
- restore(id);
603
- }
604
- };
605
- }
606
-
607
- /**
608
- * Restores all mocks registered in the current session that have not already been restored.
609
- * If an id is provided, it will restore all mocks registered in the session associed with that id that have not already been restored.
610
- *
611
- * @example Usage
612
- * ```ts
613
- * import { mockSession, restore, stub } from "@std/testing/mock";
614
- * import { assertEquals, assertNotEquals } from "@std/assert";
615
- *
616
- * const setTimeout = globalThis.setTimeout;
617
- *
618
- * stub(globalThis, "setTimeout");
619
- *
620
- * assertNotEquals(globalThis.setTimeout, setTimeout);
621
- *
622
- * restore();
623
- *
624
- * assertEquals(globalThis.setTimeout, setTimeout);
625
- * ```
626
- *
627
- * @param id The id of the session to restore. If not provided, all mocks registered in the current session are restored.
628
- */
629
- export function restore(id?: number) {
630
- id ??= (sessions.length || 1) - 1;
631
- while (id < sessions.length) {
632
- const session = sessions.pop();
633
- if (session) {
634
- for (const value of session) {
635
- value.restore();
636
- }
637
- }
638
- }
639
- }
640
-
641
- /** Wraps an instance method with a Spy. */
642
- function methodSpy<
643
- Self,
644
- Args extends unknown[],
645
- Return,
646
- >(self: Self, property: keyof Self): MethodSpy<Self, Args, Return> {
647
- if (typeof self[property] !== "function") {
648
- throw new MockError(
649
- "Cannot spy: property is not an instance method",
650
- );
651
- }
652
- if (isSpy(self[property])) {
653
- throw new MockError(
654
- "Cannot spy: already spying on instance method",
655
- );
656
- }
657
-
658
- const propertyDescriptor = Object.getOwnPropertyDescriptor(self, property);
659
- if (propertyDescriptor && !propertyDescriptor.configurable) {
660
- throw new MockError(
661
- "Cannot spy: non-configurable instance method",
662
- );
663
- }
664
-
665
- const original = self[property] as unknown as (
666
- this: Self,
667
- ...args: Args
668
- ) => Return;
669
- const calls: SpyCall<Self, Args, Return>[] = [];
670
- let restored = false;
671
- const spy = function (this: Self, ...args: Args): Return {
672
- const call: SpyCall<Self, Args, Return> = { args };
673
- if (this) call.self = this;
674
- try {
675
- call.returned = original.apply(this, args);
676
- } catch (error) {
677
- call.error = error as Error;
678
- calls.push(call);
679
- throw error;
680
- }
681
- calls.push(call);
682
- return call.returned;
683
- } as MethodSpy<Self, Args, Return>;
684
- Object.defineProperties(spy, {
685
- original: {
686
- enumerable: true,
687
- value: original,
688
- },
689
- calls: {
690
- enumerable: true,
691
- value: calls,
692
- },
693
- restored: {
694
- enumerable: true,
695
- get: () => restored,
696
- },
697
- restore: {
698
- enumerable: true,
699
- value: () => {
700
- if (restored) {
701
- throw new MockError(
702
- "Cannot restore: instance method already restored",
703
- );
704
- }
705
- if (propertyDescriptor) {
706
- Object.defineProperty(self, property, propertyDescriptor);
707
- } else {
708
- delete self[property];
709
- }
710
- restored = true;
711
- unregisterMock(spy);
712
- },
713
- },
714
- [Symbol.dispose]: {
715
- value: () => {
716
- spy.restore();
717
- },
718
- },
719
- });
720
-
721
- Object.defineProperty(self, property, {
722
- configurable: true,
723
- enumerable: propertyDescriptor?.enumerable ?? false,
724
- writable: propertyDescriptor?.writable ?? false,
725
- value: spy,
726
- });
727
-
728
- registerMock(spy);
729
- return spy;
730
- }
731
-
732
- /** A constructor wrapper that records all calls made to it. */
733
- export interface ConstructorSpy<
734
- // deno-lint-ignore no-explicit-any
735
- Self = any,
736
- // deno-lint-ignore no-explicit-any
737
- Args extends unknown[] = any[],
738
- > {
739
- /** Construct an instance. */
740
- new (...args: Args): Self;
741
- /** The function that is being spied on. */
742
- original: new (...args: Args) => Self;
743
- /** Information about calls made to the function or instance method. */
744
- calls: SpyCall<Self, Args, Self>[];
745
- /** Whether or not the original instance method has been restored. */
746
- restored: boolean;
747
- /** If spying on an instance method, this restores the original instance method. */
748
- restore(): void;
749
- }
750
-
751
- /** Wraps a constructor with a Spy. */
752
- function constructorSpy<
753
- Self,
754
- Args extends unknown[],
755
- >(
756
- constructor: new (...args: Args) => Self,
757
- ): ConstructorSpy<Self, Args> {
758
- const original = constructor;
759
- const calls: SpyCall<Self, Args, Self>[] = [];
760
- // @ts-ignore TS2509: Can't know the type of `original` statically.
761
- const spy = class extends original {
762
- // deno-lint-ignore constructor-super
763
- constructor(...args: Args) {
764
- const call: SpyCall<Self, Args, Self> = { args };
765
- try {
766
- super(...args);
767
- call.returned = this as unknown as Self;
768
- } catch (error) {
769
- call.error = error as Error;
770
- calls.push(call);
771
- throw error;
772
- }
773
- calls.push(call);
774
- }
775
- static readonly name = original.name;
776
- static readonly original = original;
777
- static readonly calls = calls;
778
- static readonly restored = false;
779
- static restore() {
780
- throw new MockError(
781
- "Cannot restore: constructor cannot be restored",
782
- );
783
- }
784
- } as ConstructorSpy<Self, Args>;
785
- return spy;
786
- }
787
-
788
- /**
789
- * Utility for extracting the arguments type from a property
790
- *
791
- * @internal
792
- */
793
- export type GetParametersFromProp<
794
- Self,
795
- Prop extends keyof Self,
796
- > = Self[Prop] extends (...args: infer Args) => unknown ? Args
797
- : unknown[];
798
-
799
- /**
800
- * Utility for extracting the return type from a property
801
- *
802
- * @internal
803
- */
804
- export type GetReturnFromProp<
805
- Self,
806
- Prop extends keyof Self,
807
- > // deno-lint-ignore no-explicit-any
808
- = Self[Prop] extends (...args: any[]) => infer Return ? Return
809
- : unknown;
810
-
811
- /** SpyLink object type. */
812
- export type SpyLike<
813
- // deno-lint-ignore no-explicit-any
814
- Self = any,
815
- // deno-lint-ignore no-explicit-any
816
- Args extends unknown[] = any[],
817
- // deno-lint-ignore no-explicit-any
818
- Return = any,
819
- > = Spy<Self, Args, Return> | ConstructorSpy<Self, Args>;
820
-
821
- /** Creates a spy function.
822
- *
823
- * @example Usage
824
- * ```ts
825
- * import {
826
- * assertSpyCall,
827
- * assertSpyCalls,
828
- * spy,
829
- * } from "@std/testing/mock";
830
- *
831
- * const func = spy();
832
- *
833
- * func();
834
- * func(1);
835
- * func(2, 3);
836
- *
837
- * assertSpyCalls(func, 3);
838
- *
839
- * // asserts each call made to the spy function.
840
- * assertSpyCall(func, 0, { args: [] });
841
- * assertSpyCall(func, 1, { args: [1] });
842
- * assertSpyCall(func, 2, { args: [2, 3] });
843
- * ```
844
- *
845
- * @typeParam Self The self type of the function.
846
- * @typeParam Args The arguments type of the function.
847
- * @typeParam Return The return type of the function.
848
- * @returns The spy function.
849
- */
850
- export function spy<
851
- // deno-lint-ignore no-explicit-any
852
- Self = any,
853
- // deno-lint-ignore no-explicit-any
854
- Args extends unknown[] = any[],
855
- Return = undefined,
856
- >(): Spy<Self, Args, Return>;
857
- /**
858
- * Create a spy function with the given implementation.
859
- *
860
- * @example Usage
861
- * ```ts
862
- * import {
863
- * assertSpyCall,
864
- * assertSpyCalls,
865
- * spy,
866
- * } from "@std/testing/mock";
867
- *
868
- * const func = spy((a: number, b: number) => a + b);
869
- *
870
- * func(3, 4);
871
- * func(5, 6);
872
- *
873
- * assertSpyCalls(func, 2);
874
- *
875
- * // asserts each call made to the spy function.
876
- * assertSpyCall(func, 0, { args: [3, 4], returned: 7 });
877
- * assertSpyCall(func, 1, { args: [5, 6], returned: 11 });
878
- * ```
879
- *
880
- * @typeParam Self The self type of the function to wrap
881
- * @typeParam Args The arguments type of the function to wrap
882
- * @typeParam Return The return type of the function to wrap
883
- * @param func The function to wrap
884
- * @returns The wrapped function.
885
- */
886
- export function spy<
887
- Self,
888
- Args extends unknown[],
889
- Return,
890
- >(func: (this: Self, ...args: Args) => Return): Spy<Self, Args, Return>;
891
- /**
892
- * Create a spy constructor.
893
- *
894
- * @example Usage
895
- * ```ts
896
- * import {
897
- * assertSpyCall,
898
- * assertSpyCalls,
899
- * spy,
900
- * } from "@std/testing/mock";
901
- *
902
- * class Foo {
903
- * constructor(value: string) {}
904
- * };
905
- *
906
- * const Constructor = spy(Foo);
907
- *
908
- * new Constructor("foo");
909
- * new Constructor("bar");
910
- *
911
- * assertSpyCalls(Constructor, 2);
912
- *
913
- * // asserts each call made to the spy function.
914
- * assertSpyCall(Constructor, 0, { args: ["foo"] });
915
- * assertSpyCall(Constructor, 1, { args: ["bar"] });
916
- * ```
917
- *
918
- * @typeParam Self The type of the instance of the class.
919
- * @typeParam Args The arguments type of the constructor
920
- * @param constructor The constructor to spy.
921
- * @returns The wrapped constructor.
922
- */
923
- export function spy<
924
- Self,
925
- Args extends unknown[],
926
- >(
927
- constructor: new (...args: Args) => Self,
928
- ): ConstructorSpy<Self, Args>;
929
- /**
930
- * Wraps a instance method with a Spy.
931
- *
932
- * @example Usage
933
- * ```ts
934
- * import {
935
- * assertSpyCall,
936
- * assertSpyCalls,
937
- * spy,
938
- * } from "@std/testing/mock";
939
- *
940
- * const obj = {
941
- * method(a: number, b: number): number {
942
- * return a + b;
943
- * },
944
- * };
945
- *
946
- * const methodSpy = spy(obj, "method");
947
- *
948
- * obj.method(1, 2);
949
- * obj.method(3, 4);
950
- *
951
- * assertSpyCalls(methodSpy, 2);
952
- *
953
- * // asserts each call made to the spy function.
954
- * assertSpyCall(methodSpy, 0, { args: [1, 2], returned: 3 });
955
- * assertSpyCall(methodSpy, 1, { args: [3, 4], returned: 7 });
956
- * ```
957
- *
958
- * @typeParam Self The type of the instance to spy the method of.
959
- * @typeParam Prop The property to spy.
960
- * @param self The instance to spy.
961
- * @param property The property of the method to spy.
962
- * @returns The spy function.
963
- */
964
- export function spy<
965
- Self,
966
- Prop extends keyof Self,
967
- >(
968
- self: Self,
969
- property: Prop,
970
- ): MethodSpy<
971
- Self,
972
- GetParametersFromProp<Self, Prop>,
973
- GetReturnFromProp<Self, Prop>
974
- >;
975
- export function spy<
976
- Self,
977
- Args extends unknown[],
978
- Return,
979
- >(
980
- funcOrConstOrSelf?:
981
- | ((this: Self, ...args: Args) => Return)
982
- | (new (...args: Args) => Self)
983
- | Self,
984
- property?: keyof Self,
985
- ): SpyLike<Self, Args, Return> {
986
- if (!funcOrConstOrSelf) {
987
- return functionSpy<Self, Args, Return>();
988
- } else if (property !== undefined) {
989
- return methodSpy<Self, Args, Return>(funcOrConstOrSelf as Self, property);
990
- } else if (funcOrConstOrSelf.toString().startsWith("class")) {
991
- return constructorSpy<Self, Args>(
992
- funcOrConstOrSelf as new (...args: Args) => Self,
993
- );
994
- } else {
995
- return functionSpy<Self, Args, Return>(
996
- funcOrConstOrSelf as (this: Self, ...args: Args) => Return,
997
- );
998
- }
999
- }
1000
-
1001
- /** An instance method replacement that records all calls made to it. */
1002
- export interface Stub<
1003
- // deno-lint-ignore no-explicit-any
1004
- Self = any,
1005
- // deno-lint-ignore no-explicit-any
1006
- Args extends unknown[] = any[],
1007
- // deno-lint-ignore no-explicit-any
1008
- Return = any,
1009
- > extends MethodSpy<Self, Args, Return> {
1010
- /** The function that is used instead of the original. */
1011
- fake: (this: Self, ...args: Args) => Return;
1012
- }
1013
-
1014
- /**
1015
- * Replaces an instance method with a Stub with empty implementation.
1016
- *
1017
- * @example Usage
1018
- * ```ts
1019
- * import { stub, assertSpyCalls } from "@std/testing/mock";
1020
- *
1021
- * const obj = {
1022
- * method() {
1023
- * // some inconventient feature for testing
1024
- * },
1025
- * };
1026
- *
1027
- * const methodStub = stub(obj, "method");
1028
- *
1029
- * for (const _ of Array(5)) {
1030
- * obj.method();
1031
- * }
1032
- *
1033
- * assertSpyCalls(methodStub, 5);
1034
- * ```
1035
-
1036
- *
1037
- * @typeParam Self The self type of the instance to replace a method of.
1038
- * @typeParam Prop The property of the instance to replace.
1039
- * @param self The instance to replace a method of.
1040
- * @param property The property of the instance to replace.
1041
- * @returns The stub function which replaced the original.
1042
- */
1043
- export function stub<
1044
- Self,
1045
- Prop extends keyof Self,
1046
- >(
1047
- self: Self,
1048
- property: Prop,
1049
- ): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>;
1050
- /**
1051
- * Replaces an instance method with a Stub with the given implementation.
1052
- *
1053
- * @example Usage
1054
- * ```ts
1055
- * import { stub } from "@std/testing/mock";
1056
- * import { assertEquals } from "@std/assert";
1057
- *
1058
- * const obj = {
1059
- * method(): number {
1060
- * return Math.random();
1061
- * },
1062
- * };
1063
- *
1064
- * const methodStub = stub(obj, "method", () => 0.5);
1065
- *
1066
- * assertEquals(obj.method(), 0.5);
1067
- * ```
1068
- *
1069
- * @typeParam Self The self type of the instance to replace a method of.
1070
- * @typeParam Prop The property of the instance to replace.
1071
- * @param self The instance to replace a method of.
1072
- * @param property The property of the instance to replace.
1073
- * @param func The fake implementation of the function.
1074
- * @returns The stub function which replaced the original.
1075
- */
1076
- export function stub<
1077
- Self,
1078
- Prop extends keyof Self,
1079
- >(
1080
- self: Self,
1081
- property: Prop,
1082
- func: (
1083
- this: Self,
1084
- ...args: GetParametersFromProp<Self, Prop>
1085
- ) => GetReturnFromProp<Self, Prop>,
1086
- ): Stub<Self, GetParametersFromProp<Self, Prop>, GetReturnFromProp<Self, Prop>>;
1087
- export function stub<
1088
- Self,
1089
- Args extends unknown[],
1090
- Return,
1091
- >(
1092
- self: Self,
1093
- property: keyof Self,
1094
- func?: (this: Self, ...args: Args) => Return,
1095
- ): Stub<Self, Args, Return> {
1096
- if (self[property] !== undefined && typeof self[property] !== "function") {
1097
- throw new MockError(
1098
- "Cannot stub: property is not an instance method",
1099
- );
1100
- }
1101
- if (isSpy(self[property])) {
1102
- throw new MockError(
1103
- "Cannot stub: already spying on instance method",
1104
- );
1105
- }
1106
-
1107
- const propertyDescriptor = Object.getOwnPropertyDescriptor(self, property);
1108
- if (propertyDescriptor && !propertyDescriptor.configurable) {
1109
- throw new MockError("Cannot stub: non-configurable instance method");
1110
- }
1111
-
1112
- const fake = func ?? (() => {}) as (this: Self, ...args: Args) => Return;
1113
-
1114
- const original = self[property] as unknown as (
1115
- this: Self,
1116
- ...args: Args
1117
- ) => Return;
1118
- const calls: SpyCall<Self, Args, Return>[] = [];
1119
- let restored = false;
1120
- const stub = function (this: Self, ...args: Args): Return {
1121
- const call: SpyCall<Self, Args, Return> = { args };
1122
- if (this) call.self = this;
1123
- try {
1124
- call.returned = fake.apply(this, args);
1125
- } catch (error) {
1126
- call.error = error as Error;
1127
- calls.push(call);
1128
- throw error;
1129
- }
1130
- calls.push(call);
1131
- return call.returned;
1132
- } as Stub<Self, Args, Return>;
1133
- Object.defineProperties(stub, {
1134
- original: {
1135
- enumerable: true,
1136
- value: original,
1137
- },
1138
- fake: {
1139
- enumerable: true,
1140
- value: fake,
1141
- },
1142
- calls: {
1143
- enumerable: true,
1144
- value: calls,
1145
- },
1146
- restored: {
1147
- enumerable: true,
1148
- get: () => restored,
1149
- },
1150
- restore: {
1151
- enumerable: true,
1152
- value: () => {
1153
- if (restored) {
1154
- throw new MockError(
1155
- "Cannot restore: instance method already restored",
1156
- );
1157
- }
1158
- if (propertyDescriptor) {
1159
- Object.defineProperty(self, property, propertyDescriptor);
1160
- } else {
1161
- delete self[property];
1162
- }
1163
- restored = true;
1164
- unregisterMock(stub);
1165
- },
1166
- },
1167
- [Symbol.dispose]: {
1168
- value: () => {
1169
- stub.restore();
1170
- },
1171
- },
1172
- });
1173
-
1174
- Object.defineProperty(self, property, {
1175
- configurable: true,
1176
- enumerable: propertyDescriptor?.enumerable ?? false,
1177
- writable: propertyDescriptor?.writable ?? false,
1178
- value: stub,
1179
- });
1180
-
1181
- registerMock(stub);
1182
- return stub;
1183
- }
1184
-
1185
- /**
1186
- * Asserts that a spy is called as much as expected and no more.
1187
- *
1188
- * @example Usage
1189
- * ```ts
1190
- * import { assertSpyCalls, spy } from "@std/testing/mock";
1191
- *
1192
- * const func = spy();
1193
- *
1194
- * func();
1195
- * func();
1196
- *
1197
- * assertSpyCalls(func, 2);
1198
- * ```
1199
- *
1200
- * @typeParam Self The self type of the spy function.
1201
- * @typeParam Args The arguments type of the spy function.
1202
- * @typeParam Return The return type of the spy function.
1203
- * @param spy The spy to check
1204
- * @param expectedCalls The number of the expected calls.
1205
- */
1206
- export function assertSpyCalls<
1207
- Self,
1208
- Args extends unknown[],
1209
- Return,
1210
- >(
1211
- spy: SpyLike<Self, Args, Return>,
1212
- expectedCalls: number,
1213
- ) {
1214
- try {
1215
- assertEquals(spy.calls.length, expectedCalls);
1216
- } catch (e) {
1217
- assertIsError(e);
1218
- let message = spy.calls.length < expectedCalls
1219
- ? "Spy not called as much as expected:\n"
1220
- : "Spy called more than expected:\n";
1221
- message += e.message.split("\n").slice(1).join("\n");
1222
- throw new AssertionError(message);
1223
- }
1224
- }
1225
-
1226
- /** Call information recorded by a spy. */
1227
- export interface ExpectedSpyCall<
1228
- // deno-lint-ignore no-explicit-any
1229
- Self = any,
1230
- // deno-lint-ignore no-explicit-any
1231
- Args extends unknown[] = any[],
1232
- // deno-lint-ignore no-explicit-any
1233
- Return = any,
1234
- > {
1235
- /** Arguments passed to a function when called. */
1236
- args?: [...Args, ...unknown[]];
1237
- /** The instance that a method was called on. */
1238
- self?: Self;
1239
- /**
1240
- * The value that was returned by a function.
1241
- * If you expect a promise to reject, expect error instead.
1242
- */
1243
- returned?: Return;
1244
- /** The expected thrown error. */
1245
- error?: {
1246
- /** The class for the error that was thrown by a function. */
1247
- // deno-lint-ignore no-explicit-any
1248
- Class?: new (...args: any[]) => Error;
1249
- /** Part of the message for the error that was thrown by a function. */
1250
- msgIncludes?: string;
1251
- };
1252
- }
1253
-
1254
- function getSpyCall<
1255
- Self,
1256
- Args extends unknown[],
1257
- Return,
1258
- >(
1259
- spy: SpyLike<Self, Args, Return>,
1260
- callIndex: number,
1261
- ): SpyCall {
1262
- if (spy.calls.length < (callIndex + 1)) {
1263
- throw new AssertionError("Spy not called as much as expected");
1264
- }
1265
- return spy.calls[callIndex]!;
1266
- }
1267
- /**
1268
- * Asserts that a spy is called as expected.
1269
- *
1270
- * @example Usage
1271
- * ```ts
1272
- * import { assertSpyCall, spy } from "@std/testing/mock";
1273
- *
1274
- * const func = spy((a: number, b: number) => a + b);
1275
- *
1276
- * func(3, 4);
1277
- * func(5, 6);
1278
- *
1279
- * // asserts each call made to the spy function.
1280
- * assertSpyCall(func, 0, { args: [3, 4], returned: 7 });
1281
- * assertSpyCall(func, 1, { args: [5, 6], returned: 11 });
1282
- * ```
1283
- *
1284
- * @typeParam Self The self type of the spy function.
1285
- * @typeParam Args The arguments type of the spy function.
1286
- * @typeParam Return The return type of the spy function.
1287
- * @param spy The spy to check
1288
- * @param callIndex The index of the call to check
1289
- * @param expected The expected spy call.
1290
- */
1291
- export function assertSpyCall<
1292
- Self,
1293
- Args extends unknown[],
1294
- Return,
1295
- >(
1296
- spy: SpyLike<Self, Args, Return>,
1297
- callIndex: number,
1298
- expected?: ExpectedSpyCall<Self, Args, Return>,
1299
- ) {
1300
- const call = getSpyCall(spy, callIndex);
1301
- if (expected) {
1302
- if (expected.args) {
1303
- try {
1304
- assertEquals(call.args, expected.args);
1305
- } catch (e) {
1306
- assertIsError(e);
1307
- throw new AssertionError(
1308
- "Spy not called with expected args:\n" +
1309
- e.message.split("\n").slice(1).join("\n"),
1310
- );
1311
- }
1312
- }
1313
-
1314
- if ("self" in expected) {
1315
- try {
1316
- assertEquals(call.self, expected.self);
1317
- } catch (e) {
1318
- assertIsError(e);
1319
- let message = expected.self
1320
- ? "Spy not called as method on expected self:\n"
1321
- : "Spy not expected to be called as method on object:\n";
1322
- message += e.message.split("\n").slice(1).join("\n");
1323
- throw new AssertionError(message);
1324
- }
1325
- }
1326
-
1327
- if ("returned" in expected) {
1328
- if ("error" in expected) {
1329
- throw new TypeError(
1330
- "Do not expect error and return, only one should be expected",
1331
- );
1332
- }
1333
- if (call.error) {
1334
- throw new AssertionError(
1335
- "Spy call did not return expected value, an error was thrown.",
1336
- );
1337
- }
1338
- try {
1339
- assertEquals(call.returned, expected.returned);
1340
- } catch (e) {
1341
- assertIsError(e);
1342
- throw new AssertionError(
1343
- "Spy call did not return expected value:\n" +
1344
- e.message.split("\n").slice(1).join("\n"),
1345
- );
1346
- }
1347
- }
1348
-
1349
- if ("error" in expected) {
1350
- if ("returned" in call) {
1351
- throw new AssertionError(
1352
- "Spy call did not throw an error, a value was returned.",
1353
- );
1354
- }
1355
- assertIsError(
1356
- call.error,
1357
- expected.error?.Class,
1358
- expected.error?.msgIncludes,
1359
- );
1360
- }
1361
- }
1362
- }
1363
-
1364
- /**
1365
- * Asserts that an async spy is called as expected.
1366
- *
1367
- * @example Usage
1368
- * ```ts
1369
- * import { assertSpyCallAsync, spy } from "@std/testing/mock";
1370
- *
1371
- * const func = spy((a: number, b: number) => new Promise((resolve) => {
1372
- * setTimeout(() => resolve(a + b), 100)
1373
- * }));
1374
- *
1375
- * await func(3, 4);
1376
- * await func(5, 6);
1377
- *
1378
- * // asserts each call made to the spy function.
1379
- * await assertSpyCallAsync(func, 0, { args: [3, 4], returned: 7 });
1380
- * await assertSpyCallAsync(func, 1, { args: [5, 6], returned: 11 });
1381
- * ```
1382
- *
1383
- * @typeParam Self The self type of the spy function.
1384
- * @typeParam Args The arguments type of the spy function.
1385
- * @typeParam Return The return type of the spy function.
1386
- * @param spy The spy to check
1387
- * @param callIndex The index of the call to check
1388
- * @param expected The expected spy call.
1389
- */
1390
- export async function assertSpyCallAsync<
1391
- Self,
1392
- Args extends unknown[],
1393
- Return,
1394
- >(
1395
- spy: SpyLike<Self, Args, Promise<Return>>,
1396
- callIndex: number,
1397
- expected?: ExpectedSpyCall<Self, Args, Promise<Return> | Return>,
1398
- ) {
1399
- const expectedSync = expected && { ...expected };
1400
- if (expectedSync) {
1401
- delete expectedSync.returned;
1402
- delete expectedSync.error;
1403
- }
1404
- assertSpyCall(spy, callIndex, expectedSync);
1405
- const call = getSpyCall(spy, callIndex);
1406
-
1407
- if (call.error) {
1408
- throw new AssertionError(
1409
- "Spy call did not return a promise, an error was thrown.",
1410
- );
1411
- }
1412
- if (call.returned !== Promise.resolve(call.returned)) {
1413
- throw new AssertionError(
1414
- "Spy call did not return a promise, a value was returned.",
1415
- );
1416
- }
1417
-
1418
- if (expected) {
1419
- if ("returned" in expected) {
1420
- if ("error" in expected) {
1421
- throw new TypeError(
1422
- "Do not expect error and return, only one should be expected",
1423
- );
1424
- }
1425
- let expectedResolved;
1426
- try {
1427
- expectedResolved = await expected.returned;
1428
- } catch {
1429
- throw new TypeError(
1430
- "Do not expect rejected promise, expect error instead",
1431
- );
1432
- }
1433
-
1434
- let resolved;
1435
- try {
1436
- resolved = await call.returned;
1437
- } catch {
1438
- throw new AssertionError("Spy call returned promise was rejected");
1439
- }
1440
-
1441
- try {
1442
- assertEquals(resolved, expectedResolved);
1443
- } catch (e) {
1444
- assertIsError(e);
1445
- throw new AssertionError(
1446
- "Spy call did not resolve to expected value:\n" +
1447
- e.message.split("\n").slice(1).join("\n"),
1448
- );
1449
- }
1450
- }
1451
-
1452
- if ("error" in expected) {
1453
- await assertRejects(
1454
- () => Promise.resolve(call.returned),
1455
- expected.error?.Class ?? Error,
1456
- expected.error?.msgIncludes ?? "",
1457
- );
1458
- }
1459
- }
1460
- }
1461
-
1462
- /**
1463
- * Asserts that a spy is called with a specific arg as expected.
1464
- *
1465
- * @example Usage
1466
- * ```ts
1467
- * import { assertSpyCallArg, spy } from "@std/testing/mock";
1468
- *
1469
- * const func = spy((a: number, b: number) => a + b);
1470
- *
1471
- * func(3, 4);
1472
- * func(5, 6);
1473
- *
1474
- * // asserts each call made to the spy function.
1475
- * assertSpyCallArg(func, 0, 0, 3);
1476
- * assertSpyCallArg(func, 0, 1, 4);
1477
- * assertSpyCallArg(func, 1, 0, 5);
1478
- * assertSpyCallArg(func, 1, 1, 6);
1479
- * ```
1480
- *
1481
- * @typeParam Self The self type of the spy function.
1482
- * @typeParam Args The arguments type of the spy function.
1483
- * @typeParam Return The return type of the spy function.
1484
- * @typeParam ExpectedArg The expected type of the argument for the spy to be called.
1485
- * @param spy The spy to check.
1486
- * @param callIndex The index of the call to check.
1487
- * @param argIndex The index of the arguments to check.
1488
- * @param expected The expected argument.
1489
- * @returns The actual argument.
1490
- */
1491
- export function assertSpyCallArg<
1492
- Self,
1493
- Args extends unknown[],
1494
- Return,
1495
- ExpectedArg,
1496
- >(
1497
- spy: SpyLike<Self, Args, Return>,
1498
- callIndex: number,
1499
- argIndex: number,
1500
- expected: ExpectedArg,
1501
- ): ExpectedArg {
1502
- const call = getSpyCall(spy, callIndex);
1503
- const arg = call?.args[argIndex];
1504
- assertEquals(arg, expected);
1505
- return arg as ExpectedArg;
1506
- }
1507
-
1508
- /**
1509
- * Asserts that an spy is called with a specific range of args as expected.
1510
- * If a start and end index is not provided, the expected will be compared against all args.
1511
- * If a start is provided without an end index, the expected will be compared against all args from the start index to the end.
1512
- * The end index is not included in the range of args that are compared.
1513
- *
1514
- * @example Usage
1515
- * ```ts
1516
- * import { assertSpyCallArgs, spy } from "@std/testing/mock";
1517
- *
1518
- * const func = spy((a: number, b: number) => a + b);
1519
- *
1520
- * func(3, 4);
1521
- * func(5, 6);
1522
- *
1523
- * // asserts each call made to the spy function.
1524
- * assertSpyCallArgs(func, 0, [3, 4]);
1525
- * assertSpyCallArgs(func, 1, [5, 6]);
1526
- * ```
1527
- *
1528
- * @typeParam Self The self type of the spy function.
1529
- * @typeParam Args The arguments type of the spy function.
1530
- * @typeParam Return The return type of the spy function.
1531
- * @typeParam ExpectedArgs The expected type of the arguments for the spy to be called.
1532
- * @param spy The spy to check.
1533
- * @param callIndex The index of the call to check.
1534
- * @param expected The expected arguments.
1535
- * @returns The actual arguments.
1536
- */
1537
- export function assertSpyCallArgs<
1538
- Self,
1539
- Args extends unknown[],
1540
- Return,
1541
- ExpectedArgs extends unknown[],
1542
- >(
1543
- spy: SpyLike<Self, Args, Return>,
1544
- callIndex: number,
1545
- expected: ExpectedArgs,
1546
- ): ExpectedArgs;
1547
- /**
1548
- * Asserts that an spy is called with a specific range of args as expected.
1549
- * If a start and end index is not provided, the expected will be compared against all args.
1550
- * If a start is provided without an end index, the expected will be compared against all args from the start index to the end.
1551
- * The end index is not included in the range of args that are compared.
1552
- *
1553
- * @example Usage
1554
- * ```ts
1555
- * import { assertSpyCallArgs, spy } from "@std/testing/mock";
1556
- *
1557
- * const func = spy((...args) => {});
1558
- *
1559
- * func(0, 1, 2, 3, 4, 5);
1560
- *
1561
- * assertSpyCallArgs(func, 0, 3, [3, 4, 5]);
1562
- * ```
1563
- *
1564
- * @typeParam Self The self type of the spy function.
1565
- * @typeParam Args The arguments type of the spy function.
1566
- * @typeParam Return The return type of the spy function.
1567
- * @typeParam ExpectedArgs The expected type of the arguments for the spy to be called.
1568
- * @param spy The spy to check.
1569
- * @param callIndex The index of the call to check.
1570
- * @param argsStart The start index of the arguments to check. If not specified, it checks the arguments from the beignning.
1571
- * @param expected The expected arguments.
1572
- * @returns The actual arguments.
1573
- */
1574
- export function assertSpyCallArgs<
1575
- Self,
1576
- Args extends unknown[],
1577
- Return,
1578
- ExpectedArgs extends unknown[],
1579
- >(
1580
- spy: SpyLike<Self, Args, Return>,
1581
- callIndex: number,
1582
- argsStart: number,
1583
- expected: ExpectedArgs,
1584
- ): ExpectedArgs;
1585
- /**
1586
- * Asserts that an spy is called with a specific range of args as expected.
1587
- * If a start and end index is not provided, the expected will be compared against all args.
1588
- * If a start is provided without an end index, the expected will be compared against all args from the start index to the end.
1589
- * The end index is not included in the range of args that are compared.
1590
- *
1591
- * @example Usage
1592
- * ```ts
1593
- * import { assertSpyCallArgs, spy } from "@std/testing/mock";
1594
- *
1595
- * const func = spy((...args) => {});
1596
- *
1597
- * func(0, 1, 2, 3, 4, 5);
1598
- *
1599
- * assertSpyCallArgs(func, 0, 3, 4, [3]);
1600
- * ```
1601
- *
1602
- * @typeParam Self The self type of the spy function.
1603
- * @typeParam Args The arguments type of the spy function.
1604
- * @typeParam Return The return type of the spy function.
1605
- * @typeParam ExpectedArgs The expected type of the arguments for the spy to be called.
1606
- * @param spy The spy to check
1607
- * @param callIndex The index of the call to check
1608
- * @param argsStart The start index of the arguments to check. If not specified, it checks the arguments from the beignning.
1609
- * @param argsEnd The end index of the arguments to check. If not specified, it checks the arguments until the end.
1610
- * @param expected The expected arguments.
1611
- * @returns The actual arguments
1612
- */
1613
- export function assertSpyCallArgs<
1614
- Self,
1615
- Args extends unknown[],
1616
- Return,
1617
- ExpectedArgs extends unknown[],
1618
- >(
1619
- spy: SpyLike<Self, Args, Return>,
1620
- callIndex: number,
1621
- argsStart: number,
1622
- argsEnd: number,
1623
- expected: ExpectedArgs,
1624
- ): ExpectedArgs;
1625
- export function assertSpyCallArgs<
1626
- ExpectedArgs extends unknown[],
1627
- Args extends unknown[],
1628
- Return,
1629
- Self,
1630
- >(
1631
- spy: SpyLike<Self, Args, Return>,
1632
- callIndex: number,
1633
- argsStart?: number | ExpectedArgs,
1634
- argsEnd?: number | ExpectedArgs,
1635
- expected?: ExpectedArgs,
1636
- ): ExpectedArgs {
1637
- const call = getSpyCall(spy, callIndex);
1638
- if (!expected) {
1639
- expected = argsEnd as ExpectedArgs;
1640
- argsEnd = undefined;
1641
- }
1642
- if (!expected) {
1643
- expected = argsStart as ExpectedArgs;
1644
- argsStart = undefined;
1645
- }
1646
- const args = typeof argsEnd === "number"
1647
- ? call.args.slice(argsStart as number, argsEnd)
1648
- : typeof argsStart === "number"
1649
- ? call.args.slice(argsStart)
1650
- : call.args;
1651
- assertEquals(args, expected);
1652
- return args as ExpectedArgs;
1653
- }
1654
-
1655
- /**
1656
- * Creates a function that returns the instance the method was called on.
1657
- *
1658
- * @example Usage
1659
- * ```ts
1660
- * import { returnsThis } from "@std/testing/mock";
1661
- * import { assertEquals } from "@std/assert";
1662
- *
1663
- * const func = returnsThis();
1664
- * const obj = { func };
1665
- * assertEquals(obj.func(), obj);
1666
- * ```
1667
- *
1668
- * @typeParam Self The self type of the returned function.
1669
- * @typeParam Args The arguments type of the returned function.
1670
- * @returns A function that returns the instance the method was called on.
1671
- */
1672
- export function returnsThis<
1673
- // deno-lint-ignore no-explicit-any
1674
- Self = any,
1675
- // deno-lint-ignore no-explicit-any
1676
- Args extends unknown[] = any[],
1677
- >(): (this: Self, ...args: Args) => Self {
1678
- return function (this: Self): Self {
1679
- return this;
1680
- };
1681
- }
1682
-
1683
- /**
1684
- * Creates a function that returns one of its arguments.
1685
- *
1686
- * @example Usage
1687
- * ```ts
1688
- * import { returnsArg } from "@std/testing/mock";
1689
- * import { assertEquals } from "@std/assert";
1690
- *
1691
- * const func = returnsArg(1);
1692
- * assertEquals(func(1, 2, 3), 2);
1693
- * ```
1694
- *
1695
- * @typeParam Arg The type of returned argument.
1696
- * @typeParam Self The self type of the returned function.
1697
- * @param idx The index of the arguments to use.
1698
- * @returns A function that returns one of its arguments.
1699
- */
1700
- export function returnsArg<
1701
- Arg,
1702
- // deno-lint-ignore no-explicit-any
1703
- Self = any,
1704
- >(
1705
- idx: number,
1706
- ): (this: Self, ...args: Arg[]) => Arg | undefined {
1707
- return function (...args: Arg[]): Arg | undefined {
1708
- return args[idx];
1709
- };
1710
- }
1711
-
1712
- /**
1713
- * Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end.
1714
- *
1715
- * @example Usage
1716
- * ```ts
1717
- * import { returnsArgs } from "@std/testing/mock";
1718
- * import { assertEquals } from "@std/assert";
1719
- *
1720
- * const func = returnsArgs();
1721
- * assertEquals(func(1, 2, 3), [1, 2, 3]);
1722
- * ```
1723
- *
1724
- * @typeParam Args The arguments type of the returned function
1725
- * @typeParam Self The self type of the returned function
1726
- * @param start The start index of the arguments to return. Default is 0.
1727
- * @param end The end index of the arguments to return.
1728
- * @returns A function that returns its arguments or a subset of them.
1729
- */
1730
- export function returnsArgs<
1731
- Args extends unknown[],
1732
- // deno-lint-ignore no-explicit-any
1733
- Self = any,
1734
- >(
1735
- start = 0,
1736
- end?: number,
1737
- ): (this: Self, ...args: Args) => Args {
1738
- return function (this: Self, ...args: Args): Args {
1739
- return args.slice(start, end) as Args;
1740
- };
1741
- }
1742
-
1743
- /**
1744
- * Creates a function that returns the iterable values. Any iterable values that are errors will be thrown.
1745
- *
1746
- * @example Usage
1747
- * ```ts
1748
- * import { returnsNext } from "@std/testing/mock";
1749
- * import { assertEquals, assertThrows } from "@std/assert";
1750
- *
1751
- * const func = returnsNext([1, 2, new Error("foo"), 3]);
1752
- * assertEquals(func(), 1);
1753
- * assertEquals(func(), 2);
1754
- * assertThrows(() => func(), Error, "foo");
1755
- * assertEquals(func(), 3);
1756
- * ```
1757
- *
1758
- * @typeParam Return The type of each item of the iterable
1759
- * @typeParam Self The self type of the returned function
1760
- * @typeParam Args The arguments type of the returned function
1761
- * @param values The iterable values
1762
- * @return A function that returns the iterable values
1763
- */
1764
- export function returnsNext<
1765
- Return,
1766
- // deno-lint-ignore no-explicit-any
1767
- Self = any,
1768
- // deno-lint-ignore no-explicit-any
1769
- Args extends unknown[] = any[],
1770
- >(
1771
- values: Iterable<Return | Error>,
1772
- ): (this: Self, ...args: Args) => Return {
1773
- const gen = (function* returnsValue() {
1774
- yield* values;
1775
- })();
1776
- let calls = 0;
1777
- return function () {
1778
- const next = gen.next();
1779
- if (next.done) {
1780
- throw new MockError(
1781
- `Not expected to be called more than ${calls} time(s)`,
1782
- );
1783
- }
1784
- calls++;
1785
- const { value } = next;
1786
- if (value instanceof Error) throw value;
1787
- return value;
1788
- };
1789
- }
1790
-
1791
- /**
1792
- * Creates a function that resolves the awaited iterable values. Any awaited iterable values that are errors will be thrown.
1793
- *
1794
- * @example Usage
1795
- * ```ts
1796
- * import { resolvesNext } from "@std/testing/mock";
1797
- * import { assertEquals, assertRejects } from "@std/assert";
1798
- *
1799
- * const func = resolvesNext([1, 2, new Error("foo"), 3]);
1800
- * assertEquals(await func(), 1);
1801
- * assertEquals(await func(), 2);
1802
- * assertRejects(() => func(), Error, "foo");
1803
- * assertEquals(await func(), 3);
1804
- * ```
1805
- *
1806
- * @typeParam Return The type of each item of the iterable
1807
- * @typeParam Self The self type of the returned function
1808
- * @typeParam Args The type of arguments of the returned function
1809
- * @param iterable The iterable to use
1810
- * @returns A function that resolves the awaited iterable values
1811
- */
1812
- export function resolvesNext<
1813
- Return,
1814
- // deno-lint-ignore no-explicit-any
1815
- Self = any,
1816
- // deno-lint-ignore no-explicit-any
1817
- Args extends unknown[] = any[],
1818
- >(
1819
- iterable:
1820
- | Iterable<Return | Error | Promise<Return | Error>>
1821
- | AsyncIterable<Return | Error | Promise<Return | Error>>,
1822
- ): (this: Self, ...args: Args) => Promise<Return> {
1823
- const gen = (async function* returnsValue() {
1824
- yield* iterable;
1825
- })();
1826
- let calls = 0;
1827
- return async function () {
1828
- const next = await gen.next();
1829
- if (next.done) {
1830
- throw new MockError(
1831
- `Not expected to be called more than ${calls} time(s)`,
1832
- );
1833
- }
1834
- calls++;
1835
- const { value } = next;
1836
- if (value instanceof Error) throw value;
1837
- return value;
1838
- };
1839
- }