/test/passenger_pane/NuBacon/bacon.nu

http://github.com/tgunr/passengerpane · Nushell · 94 lines · 81 code · 13 blank · 0 comment · 1 complexity · 284279996fe2da6c27c69bace575749a MD5 · raw file

  1. (load "NuBacon/bacon_summary")
  2. (load "NuBacon/bacon_specification")
  3. (load "NuBacon/bacon_context")
  4. (load "NuBacon/bacon_should")
  5. (load "NuBacon/bacon_macros")
  6. (class Bacon is NSObject
  7. (ivars (id) contexts
  8. (id) currentContextIndex)
  9. (+ sharedInstance is $BaconSharedInstance)
  10. (- init is
  11. (super init)
  12. (set @contexts (NSMutableArray array))
  13. (set @currentContextIndex 0)
  14. self
  15. )
  16. (- addContext:(id)context is
  17. (@contexts addObject:context)
  18. )
  19. (- (id) run is
  20. (if (> (@contexts count) 0)
  21. (then
  22. (set context (self currentContext))
  23. (context setDelegate:self)
  24. (context performSelector:"run" withObject:nil afterDelay:0)
  25. ; TODO check if this is really the right way to do it?
  26. ; TODO make this work nicely when there is already a runloop, like in an (iOS) app runner
  27. ;((NSRunLoop mainRunLoop) runUntilDate:(NSDate dateWithTimeIntervalSinceNow:0.1))
  28. ;((NSRunLoop mainRunLoop) runUntilDate:(NSDate distantFuture))
  29. (try
  30. ((NSApplication sharedApplication) run)
  31. (catch (e))
  32. ; running on iOS most probably
  33. )
  34. )
  35. (else
  36. ; DONE
  37. ($BaconSummary print)
  38. )
  39. )
  40. )
  41. (- (id) currentContext is
  42. (@contexts objectAtIndex:@currentContextIndex)
  43. )
  44. (- (id) contextDidFinish:(id)context is
  45. (if (< (+ @currentContextIndex 1) (@contexts count))
  46. (then
  47. (set @currentContextIndex (+ @currentContextIndex 1))
  48. (self run)
  49. )
  50. (else
  51. ; DONE!
  52. ($BaconSummary print)
  53. (try
  54. ((NSApplication sharedApplication) terminate:self)
  55. (catch (e))
  56. ; running on iOS most probably
  57. )
  58. )
  59. )
  60. )
  61. )
  62. (set $BaconSharedInstance ((Bacon alloc) init))
  63. ; TODO How should I subclass NSException?
  64. (class BaconError is NSObject
  65. (ivar (id) description)
  66. (- (id) initWithDescription:(id)description is
  67. (self init)
  68. (set @description description)
  69. self
  70. )
  71. (- (id) name is "BaconError")
  72. (- (id) reason is @description)
  73. )
  74. (class NSObject
  75. (- (id) instanceEval:(id)block is
  76. (set c (send block context))
  77. (send block evalWithArguments:nil context:c self:self)
  78. )
  79. (- (id) should is ((BaconShould alloc) initWithObject:self))
  80. (- (id) should:(id)block is (((BaconShould alloc) initWithObject:self) satisfy:nil block:block))
  81. )