/test/passenger_pane/NuBacon/bacon_spec.nu

http://github.com/tgunr/passengerpane · Nushell · 437 lines · 359 code · 78 blank · 0 comment · 1 complexity · 229da3a18f4e217b1d63766cf39dc37d MD5 · raw file

  1. (load "bacon.nu")
  2. (macro catch-failure (block)
  3. `(try
  4. ,block
  5. (catch (e)
  6. (set failure e)
  7. )
  8. )
  9. )
  10. (macro createSpecification (description block report)
  11. `((BaconSpecification alloc) initWithContext:dummyContext
  12. description:,description
  13. block:,block
  14. before:emptyArray
  15. after:emptyArray
  16. report:,report)
  17. )
  18. ; Hooray for meta-testing.
  19. (set succeed
  20. (do (block)
  21. (((send block should) not) raise:"BaconError")
  22. t
  23. )
  24. )
  25. (set fail
  26. (do (block)
  27. ((send block should) raise:"BaconError")
  28. t
  29. )
  30. )
  31. (class DummyContext is NSObject
  32. (- (id) name is "Dummy context")
  33. )
  34. ; Just some test constants
  35. (set equalFoo (do (x) (eq x "foo")))
  36. (set equalBar (do (x) (eq x "bar")))
  37. (set aRequirement ("foo" should))
  38. (set emptyArray (NSArray array))
  39. (set notEmptyArray (`("foo") array))
  40. (set dummyContext (DummyContext new))
  41. (describe "An instance of BaconShould" `(
  42. (it "raises a BaconError if the assertion fails" (do ()
  43. (-> (~ "foo" should equal:"bar") should:fail)
  44. ))
  45. (it "does not raise an exception if the assertion passes" (do ()
  46. (-> (~ "foo" should equal:"foo") should:succeed)
  47. ))
  48. (it "catches any type of exception so the spec suite can continue" (do ()
  49. (-> ((createSpecification "throws" (do () throw "ohnoes") nil) run) should not raise)
  50. ))
  51. ; NOTE: this requirement will print that the requirement failed/flunked, but in fact it does not!
  52. (it "flunks a requirement if it contains no assertions" (do ()
  53. (set numberOfFailuresBefore ($BaconSummary failures))
  54. ((createSpecification "flunks" (do ()) t) run)
  55. (~ ($BaconSummary failures) should equal:(+ numberOfFailuresBefore 1))
  56. (($BaconSummary valueForIvar:"counters") setValue:numberOfFailuresBefore forKey:"failures")
  57. ))
  58. (it "checks if the given block satisfies" (do ()
  59. (-> (~ "foo" should satisfy:"pass" block:equalFoo) should:succeed)
  60. (-> (~ "foo" should satisfy:"fail" block:equalBar) should:fail)
  61. (-> (~ "foo" should not satisfy:"pass" block:equalBar) should:succeed)
  62. (-> (~ "foo" should not satisfy:"fail" block:equalFoo) should:fail)
  63. ))
  64. (it "negates an assertion" (do ()
  65. (-> (~ "foo" should not equal:"bar") should:succeed)
  66. (-> (~ "foo" should not equal:"foo") should:fail)
  67. ))
  68. (it "has `be', `a', and `an' syntactic sugar methods which add to the requirement description and return the BaconShould instance" (do ()
  69. (aRequirement setValue:"" forIvar:"descriptionBuffer")
  70. (~ (eq (aRequirement be) aRequirement) should be:t)
  71. (~ (aRequirement valueForIvar:"descriptionBuffer") should equal:" be")
  72. (~ (eq (aRequirement a) aRequirement) should be:t)
  73. (~ (aRequirement valueForIvar:"descriptionBuffer") should equal:" be a")
  74. (~ (eq (aRequirement an) aRequirement) should be:t)
  75. (~ (aRequirement valueForIvar:"descriptionBuffer") should equal:" be a an")
  76. ))
  77. (it "has a `be:' syntactic sugar method which checks for equality" (do ()
  78. ((-> (aRequirement be:"foo")) should:succeed)
  79. ((-> (aRequirement be:"bar")) should:fail)
  80. ))
  81. (it "has a `be:' syntactic sugar method which takes a block which in turn is passed to satisfy:block:" (do ()
  82. (-> (aRequirement be:(do (_) t)) should:succeed)
  83. (-> (aRequirement be:(do (_) nil)) should:fail)
  84. ))
  85. (it "has a `a:' syntactic sugar method which checks for equality" (do ()
  86. (-> (aRequirement a:"foo") should:succeed)
  87. (-> (aRequirement a:"bar") should:fail)
  88. ))
  89. (it "has a `a:' syntactic sugar method which takes a block which in turn is passed to satisfy:block:" (do ()
  90. (-> (aRequirement a:(do (_) t)) should:succeed)
  91. (-> (aRequirement a:(do (_) nil)) should:fail)
  92. ))
  93. (it "checks for equality" (do ()
  94. (-> (~ "foo" should equal:"foo") should:succeed)
  95. (-> (~ "foo" should not equal:"foo") should:fail)
  96. ))
  97. (it "checks if the number is close to a given number" (do ()
  98. (-> (~ 1.4 should be closeTo:1.4) should:succeed)
  99. (-> (~ 0.4 should be closeTo:0.5 delta:0.1) should:succeed)
  100. (-> (~ 0.4 should be closeTo:0.5) should:fail)
  101. (-> (~ 0.4 should be closeTo:0.5 delta:0.05) should:fail)
  102. ))
  103. (it "checks if the numbers in the list are close to the list of given numbers" (do ()
  104. (-> (~ `(1.4 2.5 3.6 4.7) should be closeTo:`(1.4 2.5 3.6 4.7)) should:succeed)
  105. (-> (~ `(1.4 2.5 3.6 4.7) should be closeTo:`(1.4 2.6 3.6 4.7)) should:fail)
  106. (-> (~ `(1.4 2.5 3.6 4.7) should be closeTo:`(1.4 2.6 3.6 4.7) delta:0.1) should:succeed)
  107. ))
  108. (it "checks if the string matches the given regexp" (do ()
  109. (-> (~ "string" should match:/strin./) should:succeed)
  110. (-> (~ "string" should match:/slin./) should:fail)
  111. (-> (~ "string" should not match:/slin./) should:succeed)
  112. (-> (~ "string" should not match:/strin./) should:fail)
  113. ))
  114. (it "checks if after executing a block a numeric value has changed at all" (do ()
  115. (set x 0)
  116. (set valueBlock (do () x)) ; simply returns the value of x
  117. (-> (-> (set x (+ x 1)) should change:valueBlock) should:succeed)
  118. (-> (-> (set x x) should change:valueBlock) should:fail)
  119. (-> (-> (set x x) should not change:valueBlock) should:succeed)
  120. (-> (-> (set x (+ x 1)) should not change:valueBlock) should:fail)
  121. ))
  122. (it "checks if after executing a block a numeric value has changed by a given delta" (do ()
  123. (set x 0)
  124. (set valueBlock (do () x)) ; simply returns the value of x
  125. (-> (-> (set x (+ x 1)) should change:valueBlock by:+1) should:succeed)
  126. (-> (-> (set x (+ x 2)) should change:valueBlock by:+1) should:fail)
  127. (-> (-> (set x (- x 1)) should change:valueBlock by:-1) should:succeed)
  128. (-> (-> (set x (- x 2)) should change:valueBlock by:-1) should:fail)
  129. (-> (-> (set x (+ x 1)) should not change:valueBlock by:-1) should:succeed)
  130. (-> (-> (set x (+ x 1)) should not change:valueBlock by:+1) should:fail)
  131. ))
  132. (it "checks if any exception is raised" (do ()
  133. (-> (emptyArray objectAtIndex:0) should raise)
  134. (-> (notEmptyArray objectAtIndex:0) should not raise)
  135. ))
  136. (it "checks if a specified exception is raised" (do ()
  137. (-> (emptyArray objectAtIndex:0) should raise:"NSRangeException")
  138. (-> (emptyArray objectAtIndex:0) should not raise:"SomeRandomException")
  139. ))
  140. (it "returns the raised exception" (do ()
  141. (set e (-> (emptyArray objectAtIndex:0) should raise:"NSRangeException"))
  142. (~ ((e class) name) should equal:"NuException")
  143. (~ (e name) should equal:"NSRangeException")
  144. ))
  145. (it "checks if the object has the method and, if so, forward the message" (do ()
  146. (-> (~ "/an/absolute/path" should be isAbsolutePath) should:succeed)
  147. (-> (~ "a/relative/path" should be isAbsolutePath) should:fail)
  148. ))
  149. (it "checks if the object has a predicate version of the method and if so forward the message" (do ()
  150. (-> (~ "/an/absolute/path" should be an absolutePath) should:succeed)
  151. (-> (~ "a/relative/path" should be an absolutePath) should:fail)
  152. ))
  153. (it "checks if the object has the first person version of the method and if so forward the message" (do ()
  154. (-> (~ "foo" should respondToSelector:"isAbsolutePath") should:succeed)
  155. (-> (~ "foo" should not respondToSelector:"noWay") should:succeed)
  156. (-> (~ "foo" should respondToSelector:"noWay") should:fail)
  157. (-> (~ "foo" should not respondToSelector:"isAbsolutePath") should:fail)
  158. ))
  159. (it "creates nice descriptions" (do ()
  160. (catch-failure (~ "foo" should be:42))
  161. (~ (failure reason) should equal:"expected `foo' to be `42'")
  162. (catch-failure (~ 0.4 should be closeTo:42))
  163. (~ (failure reason) should equal:"expected `0.4' to be close to `42'")
  164. (catch-failure (~ "foo" should not equal:"foo"))
  165. (~ (failure reason) should equal:"expected `foo' to not equal `foo'")
  166. (catch-failure (~ "foo" should match:/slin./))
  167. (~ (failure reason) should equal:"expected `foo' to match /slin./")
  168. (set x 0)
  169. (set valueBlock (do () x)) ; simply returns the value of x
  170. (catch-failure (-> (set x x) should change:valueBlock))
  171. (~ (failure reason) should equal:"expected `(do () ((set x x)))' to change `(x)'")
  172. (catch-failure (-> (set x x) should change:valueBlock by:-1))
  173. (~ (failure reason) should equal:"expected `(do () ((set x x)))' to change `(x)' by `-1'")
  174. (catch-failure (~ "foo" should be isEqualToString:"bar"))
  175. (~ (failure reason) should equal:"expected `foo' to be isEqualToString:(\"bar\")")
  176. (catch-failure (~ "foo" should equalToString:"bar"))
  177. (~ (failure reason) should equal:"expected `foo' to equalToString:(\"bar\")")
  178. (catch-failure (~ "foo" should be an absolutePath))
  179. (~ (failure reason) should equal:"expected `foo' to be an absolutePath")
  180. (catch-failure (~ "foo" should satisfy:nil block:(do (x) (eq x "bar"))))
  181. (~ (failure reason) should equal:"expected `foo' to satisfy `(do (x) ((eq x \"bar\")))'")
  182. (catch-failure (~ "foo" should:(do (x) (eq x "bar"))))
  183. (~ (failure reason) should equal:"expected `foo' to satisfy `(do (x) ((eq x \"bar\")))'")
  184. (catch-failure (~ "foo" should not:(do (x) (eq x "foo"))))
  185. (~ (failure reason) should equal:"expected `foo' to not satisfy `(do (x) ((eq x \"foo\")))'")
  186. ))
  187. ))
  188. (describe "The NuBacon helper macros" `(
  189. (it "includes the `~' macro, which dynamically dispatches the messages, in an unordered list, to the first object in the list" (do ()
  190. (-> (~ "foo" should be a kindOfClass:NSCFString) should:succeed)
  191. (-> (~ "foo" should not equal:"foo") should:fail)
  192. ))
  193. (describe "concerning the `->' macro" `(
  194. (it "is a shortcut for creating a block and returning a BaconShould instance for said block" (do ()
  195. (set @ivar "foo")
  196. (set lvar "foo")
  197. (set ran nil)
  198. (set result (-> (set ran (eq @ivar lvar))))
  199. (~ ((result class) name) should be:"BaconShould")
  200. (~ (send (result object) body) should equal:`((set ran (eq @ivar lvar))))
  201. (~ result should not raise) ; executes the block
  202. (~ ran should be:t)
  203. ))
  204. (it "forwards any extra messages to the `~' macro" (do ()
  205. (-> (-> (throw "oh noes") should not raise) should:fail)
  206. ))
  207. ))
  208. (it "includes the `wait' macro, which schedules the given block to run after n seconds, this will halt any further requirement execution as well" (do ()
  209. (set startedAt1 (NSDate date))
  210. (set startedAt2 (NSDate date))
  211. (set startedAt3 (NSDate date))
  212. (set numberOfSpecsBefore ($BaconSummary specifications))
  213. (wait 0.5 (do ()
  214. (~ ((NSDate date) timeIntervalSinceDate:startedAt1) should be closeTo:0.5 delta:0.01)
  215. ))
  216. (wait 1 (do ()
  217. (~ ((NSDate date) timeIntervalSinceDate:startedAt2) should be closeTo:1 delta:0.01)
  218. (wait 1.5 (do ()
  219. (~ ((NSDate date) timeIntervalSinceDate:startedAt3) should be closeTo:2.5 delta:0.01)
  220. ; no other specs should have ran in the meantime!
  221. (~ ($BaconSummary specifications) should be:numberOfSpecsBefore)
  222. ))
  223. ))
  224. ))
  225. (describe "concerning the `wait' macro" `(
  226. (after (do ()
  227. ; make sure the after block is in fact ran after the postponed block
  228. (~ @x should be:42)
  229. ))
  230. ; TODO when refactoring the specs, this should become specs that assert that:
  231. ; * no exceptions bubble up
  232. ; * failures/errors/flunk are reported the same way as in normal requirements
  233. (it "runs the postponed block in the same way as normal requirements" (do ()
  234. (wait 0.5 (do () (set @x 42)))
  235. ))
  236. ))
  237. ))
  238. (describe "NSObject, concerning Bacon extensions" `(
  239. (it "returns a BaconShould instance, wrapping that object" (do ()
  240. (~ "foo" should equal:"foo")
  241. ))
  242. (it "takes a block that's to be called with the `object', the return value indicates success or failure" (do ()
  243. (-> (~ "foo" should:equalFoo) should:succeed)
  244. (-> (~ "foo" should:equalBar) should:fail)
  245. (-> (~ "foo" should not:equalBar) should:succeed)
  246. (-> (~ "foo" should not:equalFoo) should:fail)
  247. ))
  248. ))
  249. (describe "before/after" `(
  250. (before (do ()
  251. (set @a 1)
  252. (set @b 2)
  253. ))
  254. (before (do ()
  255. (set @a 2)
  256. ))
  257. (after (do ()
  258. (~ @a should equal:2)
  259. (set @a 3)
  260. ))
  261. (after (do ()
  262. (~ @a should equal:3)
  263. ))
  264. (it "runs in the right order" (do ()
  265. (~ @a should equal:2)
  266. (~ @b should equal:2)
  267. ))
  268. (describe "when nested" `(
  269. (before (do ()
  270. (set @c 5)
  271. ))
  272. (it "runs from higher level" (do ()
  273. (~ @a should equal:2)
  274. (~ @b should equal:2)
  275. ))
  276. (it "runs at the nested level" (do ()
  277. (~ @c should equal:5)
  278. ))
  279. (before (do ()
  280. (set @a 5)
  281. ))
  282. (it "runs in the right order" (do ()
  283. (~ @a should equal:5)
  284. (set @a 2)
  285. ))
  286. ))
  287. (it "does not run from lower level" (do ()
  288. (~ @c should be:nil)
  289. ))
  290. (describe "when nested at a sibling level" `(
  291. (it "does not run from sibling level" (do ()
  292. (~ @c should be:nil)
  293. ))
  294. ))
  295. ))
  296. (shared "a shared context" `(
  297. (it "gets called where it is included" (do ()
  298. (~ t should be:t)
  299. ))
  300. ))
  301. (shared "another shared context" `(
  302. (it "can access data" (do ()
  303. (~ @magic should be:42)
  304. ))
  305. ))
  306. (describe "shared/behaves_like" `(
  307. (behaves_like "a shared context")
  308. (it "raises when the context is not found" (do ()
  309. (set e (-> (behaves_like "whoops") should raise))
  310. (~ e should equal:"No such context `whoops'")
  311. ))
  312. (behaves_like "a shared context")
  313. (before (do ()
  314. (set @magic 42)
  315. ))
  316. (behaves_like "another shared context")
  317. ))
  318. (describe "Regression specs" `(
  319. (describe "An empty context does not break, issue #5" `(
  320. ; EMPTY
  321. ))
  322. (describe "An completely empty spec (no contexts/specifications)" `(
  323. (it "does not break" (do ()
  324. (puts "\n[!] The following summary is from a regression spec and can be ignored:")
  325. (~ (system "nush -e '(load \"bacon\") ((Bacon sharedInstance) run)'") should be: 0)
  326. ))
  327. ))
  328. ))
  329. ;(describe "Regression specs" `(
  330. ;(before (do ()
  331. ;(set @a 42)
  332. ;))
  333. ;(describe "from a nested context" `(
  334. ;(before (do ()
  335. ;(set @b 42)
  336. ;))
  337. ;(describe "from a nested-nested context" `(
  338. ;(before (do ()
  339. ;(set @c 42)
  340. ;))
  341. ;(it "catches from any depth" (do ()
  342. ;;(set requirement `(self requirement:"catches from any depth" block:`(
  343. ;(set values `((1 (-1.23 -2.34)) (2 (-2.34 -3.45))))
  344. ;(values each:(do (x)
  345. ;(set a (x array))
  346. ;(set value (a objectAtIndex:0))
  347. ;(set valueList (a objectAtIndex:1))
  348. ;;(puts value)
  349. ;;(puts valueList)
  350. ;(((valueList should) be) closeTo:`(-1.23 -2.34)) ; this fails at the second value
  351. ;))
  352. ;;) report:nil))
  353. ;;((((eval requirement) should) not) raise)
  354. ;))
  355. ;))
  356. ;))
  357. ;))
  358. ((Bacon sharedInstance) run)
  359. ;($BaconSummary print)