/test/passenger_pane/NuBacon/bacon_should.nu

http://github.com/tgunr/passengerpane · Nushell · 227 lines · 215 code · 12 blank · 0 comment · 1 complexity · 5f5c789dbeec8b76f9d4cdbedc84ee04 MD5 · raw file

  1. (class BaconShould is NSObject
  2. (ivar (id) object
  3. (id) negated
  4. (id) descriptionBuffer
  5. )
  6. (- (id) initWithObject:(id)object is
  7. (self init) ;; TODO check if it's nil
  8. (set @object object)
  9. (set @negated nil)
  10. (set @descriptionBuffer "")
  11. self
  12. )
  13. (- (id) object is
  14. @object
  15. )
  16. (- (id) should is
  17. self
  18. )
  19. (- (id) should:(id)block is
  20. (self satisfy:nil block:block)
  21. )
  22. (- (id) not is
  23. (set @negated t)
  24. (@descriptionBuffer appendString:" not")
  25. self
  26. )
  27. (- (id) not:(id)block is
  28. (set @negated t)
  29. (@descriptionBuffer appendString:" not")
  30. (self satisfy:nil block:block)
  31. )
  32. (- (id) be is
  33. (@descriptionBuffer appendString:" be")
  34. self
  35. )
  36. (- (id) a is
  37. (@descriptionBuffer appendString:" a")
  38. self
  39. )
  40. (- (id) an is
  41. (@descriptionBuffer appendString:" an")
  42. self
  43. )
  44. (- (id) satisfy:(id)description block:(id)block is
  45. ($BaconSummary addRequirement)
  46. (unless description (set description "satisfy `#{block}'"))
  47. (set description "expected `#{@object}' to#{@descriptionBuffer} #{description}")
  48. (set passed (block @object))
  49. (if (passed)
  50. (then
  51. (if (@negated)
  52. (throw ((BaconError alloc) initWithDescription:description))
  53. )
  54. )
  55. (else
  56. (unless (@negated)
  57. (throw ((BaconError alloc) initWithDescription:description))
  58. )
  59. )
  60. )
  61. )
  62. (- (id) be:(id)value is
  63. (if (send value isKindOfClass:NuBlock)
  64. (then
  65. (self satisfy:"be `#{value}'" block:value)
  66. )
  67. (else
  68. (self satisfy:"be `#{value}'" block:(do (object)
  69. (eq object value)
  70. ))
  71. )
  72. )
  73. )
  74. (- (id) a:(id)value is
  75. (if (send value isKindOfClass:NuBlock)
  76. (then
  77. (self satisfy:"a `#{value}'" block:value)
  78. )
  79. (else
  80. (self satisfy:"a `#{value}'" block:(do (object)
  81. (eq object value)
  82. ))
  83. )
  84. )
  85. )
  86. (- (id) equal:(id)value is
  87. (self satisfy:"equal `#{value}'" block:(do (object)
  88. (eq object value)
  89. ))
  90. )
  91. (- (id) closeTo:(id)otherValue is
  92. (self closeTo:otherValue delta:0.00001)
  93. )
  94. (- (id) closeTo:(id)otherValue delta:(id)delta is
  95. (if (eq (otherValue class) NuCell)
  96. (then
  97. (set otherValues (otherValue array))
  98. (self satisfy:"close to `#{otherValue}'" block:(do (values)
  99. (set result t)
  100. (values eachWithIndex:(do (value index)
  101. (set otherValue (otherValues objectAtIndex:index))
  102. (set result (and result (and (>= otherValue (- value delta)) (<= otherValue (+ value delta)))))
  103. ))
  104. result
  105. ))
  106. )
  107. (else
  108. (self satisfy:"close to `#{otherValue}'" block:(do (value)
  109. (and (>= otherValue (- value delta)) (<= otherValue (+ value delta)))
  110. ))
  111. )
  112. )
  113. )
  114. (- (id) match:(id)regexp is
  115. (self satisfy:"match /#{(regexp pattern)}/" block:(do (string)
  116. (regexp findInString:string)
  117. ))
  118. )
  119. (- (id) change:(id)valueBlock by:(id)delta is
  120. (self satisfy:"change `#{(send valueBlock body)}' by `#{delta}'" block:(do (changeBlock)
  121. (set before (call valueBlock))
  122. (call changeBlock)
  123. (eq (+ before delta) (call valueBlock))
  124. ))
  125. )
  126. (- (id) change:(id)valueBlock is
  127. (self satisfy:"change `#{(send valueBlock body)}'" block:(do (changeBlock)
  128. (set before (call valueBlock))
  129. (call changeBlock)
  130. (not (eq before (call valueBlock)))
  131. ))
  132. )
  133. (- (id) raise is
  134. (set result nil)
  135. (self satisfy:"raise any exception" block:(do (block)
  136. (try
  137. (call block)
  138. (catch (e)
  139. (set result e)
  140. t
  141. )
  142. nil
  143. )
  144. ))
  145. result
  146. )
  147. (- (id) raise:(id)exceptionName is
  148. (set result nil)
  149. (self satisfy:"raise an exception of type `#{exceptionName}'" block:(do (block)
  150. (try
  151. (call block)
  152. (catch (e)
  153. (set result e)
  154. (eq (e name) exceptionName)
  155. )
  156. )
  157. ))
  158. result
  159. )
  160. (- (id) handleUnknownMessage:(id)methodName withContext:(id)context is
  161. (set name ((first methodName) stringValue))
  162. (set args (cdr methodName))
  163. (set description name)
  164. (if (args) (then (set description "#{description}#{args}")))
  165. (if (@object respondsToSelector:name)
  166. (then
  167. ; forward the message as-is
  168. (self satisfy:description block:(do (object)
  169. (object sendMessage:methodName withContext:context)
  170. ))
  171. )
  172. (else
  173. (set predicate "is#{((name substringToIndex:1) uppercaseString)}#{(name substringFromIndex:1)}")
  174. (if (@object respondsToSelector:predicate)
  175. (then
  176. ; forward the predicate version of the message with the args
  177. (self satisfy:description block:(do (object)
  178. (set symbol ((NuSymbolTable sharedSymbolTable) symbolWithString:predicate))
  179. (sendMessageWithList object (append (list symbol) (cdr methodName)))
  180. ))
  181. )
  182. (else
  183. (set parts ((regex "([A-Z][a-z]*)") splitString:name))
  184. (set firstPart (parts objectAtIndex:0))
  185. (set firstPart (firstPart stringByAppendingString:"s"))
  186. (parts replaceObjectAtIndex:0 withObject:firstPart)
  187. (set thirdPersonForm (parts componentsJoinedByString:""))
  188. (if (@object respondsToSelector:thirdPersonForm)
  189. (then
  190. ; example: respondsToSelector: is matched as respondToSelector:
  191. (self satisfy:description block:(do (object)
  192. (set symbol ((NuSymbolTable sharedSymbolTable) symbolWithString:thirdPersonForm))
  193. (sendMessageWithList object (append (list symbol) (cdr methodName)))
  194. ))
  195. )
  196. (else
  197. ; the object does not respond to any of the messages
  198. (super handleUnknownMessage:methodName withContext:context)
  199. )
  200. )
  201. )
  202. )
  203. )
  204. )
  205. )
  206. )