PageRenderTime 48ms CodeModel.GetById 42ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/test/passenger_pane/NuBacon/bacon_spec.nu

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