uki 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (235) hide show
  1. data/.gitignore +7 -0
  2. data/.gitmodules +3 -0
  3. data/LICENSE +20 -0
  4. data/Rakefile +23 -0
  5. data/Readme.rdoc +9 -0
  6. data/VERSION +1 -0
  7. data/bin/uki +102 -0
  8. data/frameworks/jspec/lib/images/bg.png +0 -0
  9. data/frameworks/jspec/lib/images/hr.png +0 -0
  10. data/frameworks/jspec/lib/images/loading.gif +0 -0
  11. data/frameworks/jspec/lib/images/sprites.bg.png +0 -0
  12. data/frameworks/jspec/lib/images/sprites.png +0 -0
  13. data/frameworks/jspec/lib/images/vr.png +0 -0
  14. data/frameworks/jspec/lib/jspec.css +149 -0
  15. data/frameworks/jspec/lib/jspec.growl.js +115 -0
  16. data/frameworks/jspec/lib/jspec.jquery.js +72 -0
  17. data/frameworks/jspec/lib/jspec.js +1756 -0
  18. data/frameworks/jspec/lib/jspec.shell.js +39 -0
  19. data/frameworks/jspec/lib/jspec.timers.js +90 -0
  20. data/frameworks/jspec/lib/jspec.xhr.js +195 -0
  21. data/frameworks/uki/README.rdoc +179 -0
  22. data/frameworks/uki/compiler.jar +0 -0
  23. data/frameworks/uki/run.rb +2 -0
  24. data/frameworks/uki/spec/commands/example_command.rb +19 -0
  25. data/frameworks/uki/spec/dom.html +39 -0
  26. data/frameworks/uki/spec/support/images/bg.png +0 -0
  27. data/frameworks/uki/spec/support/images/hr.png +0 -0
  28. data/frameworks/uki/spec/support/images/loading.gif +0 -0
  29. data/frameworks/uki/spec/support/images/sprites.bg.png +0 -0
  30. data/frameworks/uki/spec/support/images/sprites.png +0 -0
  31. data/frameworks/uki/spec/support/images/vr.png +0 -0
  32. data/frameworks/uki/spec/support/jspec.css +149 -0
  33. data/frameworks/uki/spec/support/jspec.js +1773 -0
  34. data/frameworks/uki/spec/support/jspec.xhr.js +193 -0
  35. data/frameworks/uki/spec/support/spec.helper.js +1 -0
  36. data/frameworks/uki/spec/unit/background.spec.js +29 -0
  37. data/frameworks/uki/spec/unit/builder.spec.js +51 -0
  38. data/frameworks/uki/spec/unit/data/model.spec.js +29 -0
  39. data/frameworks/uki/spec/unit/dom/dnd.spec.js +71 -0
  40. data/frameworks/uki/spec/unit/dom/event.spec.js +78 -0
  41. data/frameworks/uki/spec/unit/dom.spec.js +28 -0
  42. data/frameworks/uki/spec/unit/geometry.spec.js +79 -0
  43. data/frameworks/uki/spec/unit/selector.spec.js +140 -0
  44. data/frameworks/uki/spec/unit/theme/template.spec.js +31 -0
  45. data/frameworks/uki/spec/unit/utils.spec.js +176 -0
  46. data/frameworks/uki/spec/unit/view/base.spec.js +86 -0
  47. data/frameworks/uki/spec/unit/view/container.spec.js +73 -0
  48. data/frameworks/uki/spec/unit/view.spec.js +13 -0
  49. data/frameworks/uki/src/airport.js +1 -0
  50. data/frameworks/uki/src/uki-core/attachment.js +175 -0
  51. data/frameworks/uki/src/uki-core/background/css.js +37 -0
  52. data/frameworks/uki/src/uki-core/background/cssBox.js +73 -0
  53. data/frameworks/uki/src/uki-core/background/multi.js +20 -0
  54. data/frameworks/uki/src/uki-core/background/null.js +10 -0
  55. data/frameworks/uki/src/uki-core/background/rows.js +77 -0
  56. data/frameworks/uki/src/uki-core/background/sliced9.js +206 -0
  57. data/frameworks/uki/src/uki-core/background.js +35 -0
  58. data/frameworks/uki/src/uki-core/builder.js +68 -0
  59. data/frameworks/uki/src/uki-core/collection.js +278 -0
  60. data/frameworks/uki/src/uki-core/const.js +17 -0
  61. data/frameworks/uki/src/uki-core/dom/dnd.js +93 -0
  62. data/frameworks/uki/src/uki-core/dom/event.js +194 -0
  63. data/frameworks/uki/src/uki-core/dom/nativeLayout.js +18 -0
  64. data/frameworks/uki/src/uki-core/dom/offset.js +130 -0
  65. data/frameworks/uki/src/uki-core/dom/w3cdnd.js +333 -0
  66. data/frameworks/uki/src/uki-core/dom.js +109 -0
  67. data/frameworks/uki/src/uki-core/geometry.js +658 -0
  68. data/frameworks/uki/src/uki-core/image.js +90 -0
  69. data/frameworks/uki/src/uki-core/selector.js +201 -0
  70. data/frameworks/uki/src/uki-core/theme/base.js +39 -0
  71. data/frameworks/uki/src/uki-core/theme/template.js +26 -0
  72. data/frameworks/uki/src/uki-core/theme.js +45 -0
  73. data/frameworks/uki/src/uki-core/uki.js +45 -0
  74. data/frameworks/uki/src/uki-core/utils.js +399 -0
  75. data/frameworks/uki/src/uki-core/view/base.js +480 -0
  76. data/frameworks/uki/src/uki-core/view/container.js +155 -0
  77. data/frameworks/uki/src/uki-core/view/focusable.js +93 -0
  78. data/frameworks/uki/src/uki-core/view/observable.js +66 -0
  79. data/frameworks/uki/src/uki-core/view/styleable.js +70 -0
  80. data/frameworks/uki/src/uki-core/view/utils.js +66 -0
  81. data/frameworks/uki/src/uki-core/view.js +21 -0
  82. data/frameworks/uki/src/uki-core.js +36 -0
  83. data/frameworks/uki/src/uki-data/data.js +1 -0
  84. data/frameworks/uki/src/uki-data/model.js +28 -0
  85. data/frameworks/uki/src/uki-data/observable.js +34 -0
  86. data/frameworks/uki/src/uki-data.js +1 -0
  87. data/frameworks/uki/src/uki-more/more/utils.js +20 -0
  88. data/frameworks/uki/src/uki-more/more/view/listContainer.js +4 -0
  89. data/frameworks/uki/src/uki-more/more/view/multiselectList.js +196 -0
  90. data/frameworks/uki/src/uki-more/more/view/radioButton.js +27 -0
  91. data/frameworks/uki/src/uki-more/more/view/splitTable.js +79 -0
  92. data/frameworks/uki/src/uki-more/more/view/toggleButton.js +24 -0
  93. data/frameworks/uki/src/uki-more/more/view/treeList/render.js +53 -0
  94. data/frameworks/uki/src/uki-more/more/view/treeList.js +110 -0
  95. data/frameworks/uki/src/uki-more/more/view.js +2 -0
  96. data/frameworks/uki/src/uki-more/more.js +1 -0
  97. data/frameworks/uki/src/uki-more.js +4 -0
  98. data/frameworks/uki/src/uki-theamless.js +15 -0
  99. data/frameworks/uki/src/uki-theme/airport/i/button/down-c.gif +0 -0
  100. data/frameworks/uki/src/uki-theme/airport/i/button/down-c.png +0 -0
  101. data/frameworks/uki/src/uki-theme/airport/i/button/down-h.gif +0 -0
  102. data/frameworks/uki/src/uki-theme/airport/i/button/down-h.png +0 -0
  103. data/frameworks/uki/src/uki-theme/airport/i/button/down-m.png +0 -0
  104. data/frameworks/uki/src/uki-theme/airport/i/button/down-v.png +0 -0
  105. data/frameworks/uki/src/uki-theme/airport/i/button/focusRing-c.png +0 -0
  106. data/frameworks/uki/src/uki-theme/airport/i/button/focusRing-h.png +0 -0
  107. data/frameworks/uki/src/uki-theme/airport/i/button/focusRing-m.png +0 -0
  108. data/frameworks/uki/src/uki-theme/airport/i/button/focusRing-v.png +0 -0
  109. data/frameworks/uki/src/uki-theme/airport/i/button/focusRing.png +0 -0
  110. data/frameworks/uki/src/uki-theme/airport/i/button/hover-c.gif +0 -0
  111. data/frameworks/uki/src/uki-theme/airport/i/button/hover-c.png +0 -0
  112. data/frameworks/uki/src/uki-theme/airport/i/button/hover-h.gif +0 -0
  113. data/frameworks/uki/src/uki-theme/airport/i/button/hover-h.png +0 -0
  114. data/frameworks/uki/src/uki-theme/airport/i/button/hover-m.png +0 -0
  115. data/frameworks/uki/src/uki-theme/airport/i/button/hover-v.png +0 -0
  116. data/frameworks/uki/src/uki-theme/airport/i/button/hover.png +0 -0
  117. data/frameworks/uki/src/uki-theme/airport/i/button/normal-c.gif +0 -0
  118. data/frameworks/uki/src/uki-theme/airport/i/button/normal-c.png +0 -0
  119. data/frameworks/uki/src/uki-theme/airport/i/button/normal-h.gif +0 -0
  120. data/frameworks/uki/src/uki-theme/airport/i/button/normal-h.png +0 -0
  121. data/frameworks/uki/src/uki-theme/airport/i/button/normal-m.png +0 -0
  122. data/frameworks/uki/src/uki-theme/airport/i/button/normal-v.png +0 -0
  123. data/frameworks/uki/src/uki-theme/airport/i/button/normal.png +0 -0
  124. data/frameworks/uki/src/uki-theme/airport/i/checkbox/checkbox.png +0 -0
  125. data/frameworks/uki/src/uki-theme/airport/i/checkbox/focus.png +0 -0
  126. data/frameworks/uki/src/uki-theme/airport/i/checkbox/normal.gif +0 -0
  127. data/frameworks/uki/src/uki-theme/airport/i/checkbox/normal.png +0 -0
  128. data/frameworks/uki/src/uki-theme/airport/i/panel/dark-h.gif +0 -0
  129. data/frameworks/uki/src/uki-theme/airport/i/panel/dark-h.png +0 -0
  130. data/frameworks/uki/src/uki-theme/airport/i/panel/dark-m.png +0 -0
  131. data/frameworks/uki/src/uki-theme/airport/i/panel/dark.png +0 -0
  132. data/frameworks/uki/src/uki-theme/airport/i/popup/normal.png +0 -0
  133. data/frameworks/uki/src/uki-theme/airport/i/radio/focus.png +0 -0
  134. data/frameworks/uki/src/uki-theme/airport/i/radio/normal.gif +0 -0
  135. data/frameworks/uki/src/uki-theme/airport/i/radio/normal.png +0 -0
  136. data/frameworks/uki/src/uki-theme/airport/i/radio/radio.png +0 -0
  137. data/frameworks/uki/src/uki-theme/airport/i/shadow/large-c.png +0 -0
  138. data/frameworks/uki/src/uki-theme/airport/i/shadow/large-h.png +0 -0
  139. data/frameworks/uki/src/uki-theme/airport/i/shadow/large-m.png +0 -0
  140. data/frameworks/uki/src/uki-theme/airport/i/shadow/large-v.png +0 -0
  141. data/frameworks/uki/src/uki-theme/airport/i/shadow/large.png +0 -0
  142. data/frameworks/uki/src/uki-theme/airport/i/slider/bar-m.gif +0 -0
  143. data/frameworks/uki/src/uki-theme/airport/i/slider/bar-m.png +0 -0
  144. data/frameworks/uki/src/uki-theme/airport/i/slider/bar-v.gif +0 -0
  145. data/frameworks/uki/src/uki-theme/airport/i/slider/bar-v.png +0 -0
  146. data/frameworks/uki/src/uki-theme/airport/i/slider/bar.png +0 -0
  147. data/frameworks/uki/src/uki-theme/airport/i/slider/focus.png +0 -0
  148. data/frameworks/uki/src/uki-theme/airport/i/slider/handle.gif +0 -0
  149. data/frameworks/uki/src/uki-theme/airport/i/splitPane/horizontal.gif +0 -0
  150. data/frameworks/uki/src/uki-theme/airport/i/splitPane/horizontal.png +0 -0
  151. data/frameworks/uki/src/uki-theme/airport/i/splitPane/vertical.gif +0 -0
  152. data/frameworks/uki/src/uki-theme/airport/i/x.gif +0 -0
  153. data/frameworks/uki/src/uki-theme/airport.js +322 -0
  154. data/frameworks/uki/src/uki-theme/aristo/i/button/down-c.gif +0 -0
  155. data/frameworks/uki/src/uki-theme/aristo/i/button/down-c.png +0 -0
  156. data/frameworks/uki/src/uki-theme/aristo/i/button/down-h.png +0 -0
  157. data/frameworks/uki/src/uki-theme/aristo/i/button/down-m.png +0 -0
  158. data/frameworks/uki/src/uki-theme/aristo/i/button/down-v.png +0 -0
  159. data/frameworks/uki/src/uki-theme/aristo/i/button/down.png +0 -0
  160. data/frameworks/uki/src/uki-theme/aristo/i/button/focusRing-c.png +0 -0
  161. data/frameworks/uki/src/uki-theme/aristo/i/button/focusRing-h.png +0 -0
  162. data/frameworks/uki/src/uki-theme/aristo/i/button/focusRing-m.png +0 -0
  163. data/frameworks/uki/src/uki-theme/aristo/i/button/focusRing-v.png +0 -0
  164. data/frameworks/uki/src/uki-theme/aristo/i/button/focusRing.png +0 -0
  165. data/frameworks/uki/src/uki-theme/aristo/i/button/normal-c.gif +0 -0
  166. data/frameworks/uki/src/uki-theme/aristo/i/button/normal-c.png +0 -0
  167. data/frameworks/uki/src/uki-theme/aristo/i/button/normal-h.png +0 -0
  168. data/frameworks/uki/src/uki-theme/aristo/i/button/normal-m.png +0 -0
  169. data/frameworks/uki/src/uki-theme/aristo/i/button/normal-v.png +0 -0
  170. data/frameworks/uki/src/uki-theme/aristo/i/button/normal.png +0 -0
  171. data/frameworks/uki/src/uki-theme/aristo/i/checkbox/focus.png +0 -0
  172. data/frameworks/uki/src/uki-theme/aristo/i/checkbox/normal.gif +0 -0
  173. data/frameworks/uki/src/uki-theme/aristo/i/checkbox/normal.png +0 -0
  174. data/frameworks/uki/src/uki-theme/aristo/i/panel/normal-h.png +0 -0
  175. data/frameworks/uki/src/uki-theme/aristo/i/panel/normal-m.png +0 -0
  176. data/frameworks/uki/src/uki-theme/aristo/i/panel/normal.png +0 -0
  177. data/frameworks/uki/src/uki-theme/aristo/i/popup/normal.png +0 -0
  178. data/frameworks/uki/src/uki-theme/aristo/i/radio/focus.png +0 -0
  179. data/frameworks/uki/src/uki-theme/aristo/i/radio/normal.gif +0 -0
  180. data/frameworks/uki/src/uki-theme/aristo/i/radio/normal.png +0 -0
  181. data/frameworks/uki/src/uki-theme/aristo/i/shadow/large-c.png +0 -0
  182. data/frameworks/uki/src/uki-theme/aristo/i/shadow/large-h.png +0 -0
  183. data/frameworks/uki/src/uki-theme/aristo/i/shadow/large-m.png +0 -0
  184. data/frameworks/uki/src/uki-theme/aristo/i/shadow/large-v.png +0 -0
  185. data/frameworks/uki/src/uki-theme/aristo/i/shadow/large.png +0 -0
  186. data/frameworks/uki/src/uki-theme/aristo/i/slider/bar-m.gif +0 -0
  187. data/frameworks/uki/src/uki-theme/aristo/i/slider/bar-m.png +0 -0
  188. data/frameworks/uki/src/uki-theme/aristo/i/slider/bar-v.gif +0 -0
  189. data/frameworks/uki/src/uki-theme/aristo/i/slider/bar-v.png +0 -0
  190. data/frameworks/uki/src/uki-theme/aristo/i/slider/bar.png +0 -0
  191. data/frameworks/uki/src/uki-theme/aristo/i/slider/handle.gif +0 -0
  192. data/frameworks/uki/src/uki-theme/aristo/i/slider/handle.png +0 -0
  193. data/frameworks/uki/src/uki-theme/aristo/i/splitPane/horizontal.gif +0 -0
  194. data/frameworks/uki/src/uki-theme/aristo/i/splitPane/horizontal.png +0 -0
  195. data/frameworks/uki/src/uki-theme/aristo/i/splitPane/vertical.gif +0 -0
  196. data/frameworks/uki/src/uki-theme/aristo/i/x.gif +0 -0
  197. data/frameworks/uki/src/uki-theme/aristo.js +217 -0
  198. data/frameworks/uki/src/uki-view/view/box.js +1 -0
  199. data/frameworks/uki/src/uki-view/view/button.js +126 -0
  200. data/frameworks/uki/src/uki-view/view/checkbox.js +36 -0
  201. data/frameworks/uki/src/uki-view/view/flow.js +48 -0
  202. data/frameworks/uki/src/uki-view/view/image.js +9 -0
  203. data/frameworks/uki/src/uki-view/view/label.js +123 -0
  204. data/frameworks/uki/src/uki-view/view/list/render.js +23 -0
  205. data/frameworks/uki/src/uki-view/view/list.js +442 -0
  206. data/frameworks/uki/src/uki-view/view/popup.js +113 -0
  207. data/frameworks/uki/src/uki-view/view/radio.js +57 -0
  208. data/frameworks/uki/src/uki-view/view/scrollPane.js +139 -0
  209. data/frameworks/uki/src/uki-view/view/slider.js +154 -0
  210. data/frameworks/uki/src/uki-view/view/splitPane.js +213 -0
  211. data/frameworks/uki/src/uki-view/view/table/column.js +96 -0
  212. data/frameworks/uki/src/uki-view/view/table/header.js +53 -0
  213. data/frameworks/uki/src/uki-view/view/table/render.js +25 -0
  214. data/frameworks/uki/src/uki-view/view/table.js +71 -0
  215. data/frameworks/uki/src/uki-view/view/textField.js +145 -0
  216. data/frameworks/uki/src/uki-view/view/toolbar.js +93 -0
  217. data/frameworks/uki/src/uki-view.js +15 -0
  218. data/frameworks/uki/src/uki.js +2 -0
  219. data/frameworks/uki/thin.yaml +11 -0
  220. data/frameworks/uki/uki.rb +38 -0
  221. data/frameworks/uki/uki.ru +2 -0
  222. data/lib/uki/include_js.rb +50 -0
  223. data/lib/uki/project.rb +207 -0
  224. data/lib/uki/routes.rb +20 -0
  225. data/lib/uki/server.rb +42 -0
  226. data/lib/uki.rb +9 -0
  227. data/templates/index.html.erb +10 -0
  228. data/templates/model.js.erb +5 -0
  229. data/templates/myapp.js.erb +44 -0
  230. data/templates/package.js.erb +3 -0
  231. data/templates/spec.html.erb +23 -0
  232. data/templates/spec.js.erb +6 -0
  233. data/templates/view.js.erb +10 -0
  234. data/uki.gemspec +281 -0
  235. metadata +317 -0
@@ -0,0 +1,1756 @@
1
+
2
+ // JSpec - Core - Copyright TJ Holowaychuk <tj@vision-media.ca> (MIT Licensed)
3
+
4
+ ;(function(){
5
+
6
+ JSpec = {
7
+ version : '3.3.2',
8
+ assert : true,
9
+ cache : {},
10
+ suites : [],
11
+ modules : [],
12
+ allSuites : [],
13
+ matchers : {},
14
+ stubbed : [],
15
+ options : {},
16
+ request : 'XMLHttpRequest' in this ? XMLHttpRequest : null,
17
+ stats : { specs: 0, assertions: 0, failures: 0, passes: 0, specsFinished: 0, suitesFinished: 0 },
18
+
19
+ /**
20
+ * Default context in which bodies are evaluated.
21
+ *
22
+ * Replace context simply by setting JSpec.context
23
+ * to your own like below:
24
+ *
25
+ * JSpec.context = { foo : 'bar' }
26
+ *
27
+ * Contexts can be changed within any body, this can be useful
28
+ * in order to provide specific helper methods to specific suites.
29
+ *
30
+ * To reset (usually in after hook) simply set to null like below:
31
+ *
32
+ * JSpec.context = null
33
+ *
34
+ */
35
+
36
+ defaultContext : {
37
+
38
+ /**
39
+ * Return an object used for proxy assertions.
40
+ * This object is used to indicate that an object
41
+ * should be an instance of _object_, not the constructor
42
+ * itself.
43
+ *
44
+ * @param {function} constructor
45
+ * @return {hash}
46
+ * @api public
47
+ */
48
+
49
+ an_instance_of : function(constructor) {
50
+ return { an_instance_of : constructor }
51
+ },
52
+
53
+ /**
54
+ * Load fixture at _path_.
55
+ *
56
+ * Fixtures are resolved as:
57
+ *
58
+ * - <path>
59
+ * - <path>.html
60
+ *
61
+ * @param {string} path
62
+ * @return {string}
63
+ * @api public
64
+ */
65
+
66
+ fixture : function(path) {
67
+ if (JSpec.cache[path]) return JSpec.cache[path]
68
+ return JSpec.cache[path] =
69
+ JSpec.tryLoading(JSpec.options.fixturePath + '/' + path) ||
70
+ JSpec.tryLoading(JSpec.options.fixturePath + '/' + path + '.html')
71
+ }
72
+ },
73
+
74
+ // --- Objects
75
+
76
+ reporters : {
77
+
78
+ /**
79
+ * Report to server.
80
+ *
81
+ * Options:
82
+ * - uri specific uri to report to.
83
+ * - verbose weither or not to output messages
84
+ * - failuresOnly output failure messages only
85
+ *
86
+ * @api public
87
+ */
88
+
89
+ Server : function(results, options) {
90
+ var uri = options.uri || 'http://' + window.location.host + '/results'
91
+ JSpec.post(uri, {
92
+ stats: JSpec.stats,
93
+ options: options,
94
+ results: map(results.allSuites, function(suite) {
95
+ if (suite.hasSpecs())
96
+ return {
97
+ description: suite.description,
98
+ specs: map(suite.specs, function(spec) {
99
+ return {
100
+ description: spec.description,
101
+ message: !spec.passed() ? spec.failure().message : null,
102
+ status: spec.requiresImplementation() ? 'pending' :
103
+ spec.passed() ? 'pass' :
104
+ 'fail',
105
+ assertions: map(spec.assertions, function(assertion){
106
+ return {
107
+ passed: assertion.passed
108
+ }
109
+ })
110
+ }
111
+ })
112
+ }
113
+ })
114
+ })
115
+ if ('close' in main) main.close()
116
+ },
117
+
118
+ /**
119
+ * Default reporter, outputting to the DOM.
120
+ *
121
+ * Options:
122
+ * - reportToId id of element to output reports to, defaults to 'jspec'
123
+ * - failuresOnly displays only suites with failing specs
124
+ *
125
+ * @api public
126
+ */
127
+
128
+ DOM : function(results, options) {
129
+ var id = option('reportToId') || 'jspec',
130
+ report = document.getElementById(id),
131
+ failuresOnly = option('failuresOnly'),
132
+ classes = results.stats.failures ? 'has-failures' : ''
133
+ if (!report) throw 'JSpec requires the element #' + id + ' to output its reports'
134
+
135
+ function bodyContents(body) {
136
+ return JSpec.
137
+ escape(JSpec.contentsOf(body)).
138
+ replace(/^ */gm, function(a){ return (new Array(Math.round(a.length / 3))).join(' ') }).
139
+ replace(/\r\n|\r|\n/gm, '<br/>')
140
+ }
141
+
142
+ report.innerHTML = '<div id="jspec-report" class="' + classes + '"><div class="heading"> \
143
+ <span class="passes">Passes: <em>' + results.stats.passes + '</em></span> \
144
+ <span class="failures">Failures: <em>' + results.stats.failures + '</em></span> \
145
+ <span class="passes">Duration: <em>' + results.duration + '</em> ms</span> \
146
+ </div><table class="suites">' + map(results.allSuites, function(suite) {
147
+ var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran
148
+ if (displaySuite && suite.hasSpecs())
149
+ return '<tr class="description"><td colspan="2">' + escape(suite.description) + '</td></tr>' +
150
+ map(suite.specs, function(i, spec) {
151
+ return '<tr class="' + (i % 2 ? 'odd' : 'even') + '">' +
152
+ (spec.requiresImplementation() ?
153
+ '<td class="requires-implementation" colspan="2">' + escape(spec.description) + '</td>' :
154
+ (spec.passed() && !failuresOnly) ?
155
+ '<td class="pass">' + escape(spec.description)+ '</td><td>' + spec.assertionsGraph() + '</td>' :
156
+ !spec.passed() ?
157
+ '<td class="fail">' + escape(spec.description) +
158
+ map(spec.failures(), function(a){ return '<em>' + escape(a.message) + '</em>' }).join('') +
159
+ '</td><td>' + spec.assertionsGraph() + '</td>' :
160
+ '') +
161
+ '<tr class="body"><td colspan="2"><pre>' + bodyContents(spec.body) + '</pre></td></tr>'
162
+ }).join('') + '</tr>'
163
+ }).join('') + '</table></div>'
164
+ },
165
+
166
+ /**
167
+ * Terminal reporter.
168
+ *
169
+ * @api public
170
+ */
171
+
172
+ Terminal : function(results, options) {
173
+ var failuresOnly = option('failuresOnly')
174
+ print(color("\n Passes: ", 'bold') + color(results.stats.passes, 'green') +
175
+ color(" Failures: ", 'bold') + color(results.stats.failures, 'red') +
176
+ color(" Duration: ", 'bold') + color(results.duration, 'green') + " ms \n")
177
+
178
+ function indent(string) {
179
+ return string.replace(/^(.)/gm, ' $1')
180
+ }
181
+
182
+ each(results.allSuites, function(suite) {
183
+ var displaySuite = failuresOnly ? suite.ran && !suite.passed() : suite.ran
184
+ if (displaySuite && suite.hasSpecs()) {
185
+ print(color(' ' + suite.description, 'bold'))
186
+ each(suite.specs, function(spec){
187
+ var assertionsGraph = inject(spec.assertions, '', function(graph, assertion){
188
+ return graph + color('.', assertion.passed ? 'green' : 'red')
189
+ })
190
+ if (spec.requiresImplementation())
191
+ print(color(' ' + spec.description, 'blue') + assertionsGraph)
192
+ else if (spec.passed() && !failuresOnly)
193
+ print(color(' ' + spec.description, 'green') + assertionsGraph)
194
+ else if (!spec.passed())
195
+ print(color(' ' + spec.description, 'red') + assertionsGraph +
196
+ "\n" + indent(map(spec.failures(), function(a){ return a.message }).join("\n")) + "\n")
197
+ })
198
+ print("")
199
+ }
200
+ })
201
+
202
+ quit(results.stats.failures)
203
+ }
204
+ },
205
+
206
+ Assertion : function(matcher, actual, expected, negate) {
207
+ extend(this, {
208
+ message: '',
209
+ passed: false,
210
+ actual: actual,
211
+ negate: negate,
212
+ matcher: matcher,
213
+ expected: expected,
214
+
215
+ // Report assertion results
216
+
217
+ report : function() {
218
+ if (JSpec.assert)
219
+ this.passed ? JSpec.stats.passes++ : JSpec.stats.failures++
220
+ return this
221
+ },
222
+
223
+ // Run the assertion
224
+
225
+ run : function() {
226
+ // TODO: remove unshifting
227
+ expected.unshift(actual)
228
+ this.result = matcher.match.apply(this, expected)
229
+ this.passed = negate ? !this.result : this.result
230
+ if (!this.passed) this.message = matcher.message.call(this, actual, expected, negate, matcher.name)
231
+ return this
232
+ }
233
+ })
234
+ },
235
+
236
+ ProxyAssertion : function(object, method, times, negate) {
237
+ var self = this
238
+ var old = object[method]
239
+
240
+ // Proxy
241
+
242
+ object[method] = function(){
243
+ args = toArray(arguments)
244
+ result = old.apply(object, args)
245
+ self.calls.push({ args : args, result : result })
246
+ return result
247
+ }
248
+
249
+ // Times
250
+
251
+ this.times = {
252
+ once : 1,
253
+ twice : 2
254
+ }[times] || times || 1
255
+
256
+ extend(this, {
257
+ calls: [],
258
+ message: '',
259
+ defer: true,
260
+ passed: false,
261
+ negate: negate,
262
+ object: object,
263
+ method: method,
264
+
265
+ // Proxy return value
266
+
267
+ and_return : function(result) {
268
+ this.expectedResult = result
269
+ return this
270
+ },
271
+
272
+ // Proxy arguments passed
273
+
274
+ with_args : function() {
275
+ this.expectedArgs = toArray(arguments)
276
+ return this
277
+ },
278
+
279
+ // Check if any calls have failing results
280
+
281
+ anyResultsFail : function() {
282
+ return any(this.calls, function(call){
283
+ return self.expectedResult.an_instance_of ?
284
+ call.result.constructor != self.expectedResult.an_instance_of:
285
+ !equal(self.expectedResult, call.result)
286
+ })
287
+ },
288
+
289
+ // Check if any calls have passing results
290
+
291
+ anyResultsPass : function() {
292
+ return any(this.calls, function(call){
293
+ return self.expectedResult.an_instance_of ?
294
+ call.result.constructor == self.expectedResult.an_instance_of:
295
+ equal(self.expectedResult, call.result)
296
+ })
297
+ },
298
+
299
+ // Return the passing result
300
+
301
+ passingResult : function() {
302
+ return this.anyResultsPass().result
303
+ },
304
+
305
+ // Return the failing result
306
+
307
+ failingResult : function() {
308
+ return this.anyResultsFail().result
309
+ },
310
+
311
+ // Check if any arguments fail
312
+
313
+ anyArgsFail : function() {
314
+ return any(this.calls, function(call){
315
+ return any(self.expectedArgs, function(i, arg){
316
+ if (arg == null) return call.args[i] == null
317
+ return arg.an_instance_of ?
318
+ call.args[i].constructor != arg.an_instance_of:
319
+ !equal(arg, call.args[i])
320
+
321
+ })
322
+ })
323
+ },
324
+
325
+ // Check if any arguments pass
326
+
327
+ anyArgsPass : function() {
328
+ return any(this.calls, function(call){
329
+ return any(self.expectedArgs, function(i, arg){
330
+ return arg.an_instance_of ?
331
+ call.args[i].constructor == arg.an_instance_of:
332
+ equal(arg, call.args[i])
333
+
334
+ })
335
+ })
336
+ },
337
+
338
+ // Return the passing args
339
+
340
+ passingArgs : function() {
341
+ return this.anyArgsPass().args
342
+ },
343
+
344
+ // Return the failing args
345
+
346
+ failingArgs : function() {
347
+ return this.anyArgsFail().args
348
+ },
349
+
350
+ // Report assertion results
351
+
352
+ report : function() {
353
+ if (JSpec.assert)
354
+ this.passed ? ++JSpec.stats.passes : ++JSpec.stats.failures
355
+ return this
356
+ },
357
+
358
+ // Run the assertion
359
+
360
+ run : function() {
361
+ var methodString = 'expected ' + object.toString() + '.' + method + '()' + (negate ? ' not' : '' )
362
+
363
+ function times(n) {
364
+ return n > 2 ? n + ' times' : { 1: 'once', 2: 'twice' }[n]
365
+ }
366
+
367
+ if (this.expectedResult != null && (negate ? this.anyResultsPass() : this.anyResultsFail()))
368
+ this.message = methodString + ' to return ' + puts(this.expectedResult) +
369
+ ' but ' + (negate ? 'it did' : 'got ' + puts(this.failingResult()))
370
+
371
+ if (this.expectedArgs && (negate ? !this.expectedResult && this.anyArgsPass() : this.anyArgsFail()))
372
+ this.message = methodString + ' to be called with ' + puts.apply(this, this.expectedArgs) +
373
+ ' but was' + (negate ? '' : ' called with ' + puts.apply(this, this.failingArgs()))
374
+
375
+ if (negate ? !this.expectedResult && !this.expectedArgs && this.calls.length >= this.times : this.calls.length != this.times)
376
+ this.message = methodString + ' to be called ' + times(this.times) +
377
+ ', but ' + (this.calls.length == 0 ? ' was not called' : ' was called ' + times(this.calls.length))
378
+
379
+ if (!this.message.length)
380
+ this.passed = true
381
+
382
+ return this
383
+ }
384
+ })
385
+ },
386
+
387
+ /**
388
+ * Specification Suite block object.
389
+ *
390
+ * @param {string} description
391
+ * @param {function} body
392
+ * @api private
393
+ */
394
+
395
+ Suite : function(description, body) {
396
+ var self = this
397
+ extend(this, {
398
+ body: body,
399
+ description: description,
400
+ suites: [],
401
+ specs: [],
402
+ ran: false,
403
+ hooks: { 'before' : [], 'after' : [], 'before_each' : [], 'after_each' : [] },
404
+
405
+ // Add a spec to the suite
406
+
407
+ addSpec : function(description, body) {
408
+ var spec = new JSpec.Spec(description, body)
409
+ this.specs.push(spec)
410
+ JSpec.stats.specs++ // TODO: abstract
411
+ spec.suite = this
412
+ },
413
+
414
+ // Add a hook to the suite
415
+
416
+ addHook : function(hook, body) {
417
+ this.hooks[hook].push(body)
418
+ },
419
+
420
+ // Add a nested suite
421
+
422
+ addSuite : function(description, body) {
423
+ var suite = new JSpec.Suite(description, body)
424
+ JSpec.allSuites.push(suite)
425
+ suite.name = suite.description
426
+ suite.description = this.description + ' ' + suite.description
427
+ this.suites.push(suite)
428
+ suite.suite = this
429
+ },
430
+
431
+ // Invoke a hook in context to this suite
432
+
433
+ hook : function(hook) {
434
+ if (this.suite) this.suite.hook(hook)
435
+ each(this.hooks[hook], function(body) {
436
+ JSpec.evalBody(body, "Error in hook '" + hook + "', suite '" + self.description + "': ")
437
+ })
438
+ },
439
+
440
+ // Check if nested suites are present
441
+
442
+ hasSuites : function() {
443
+ return this.suites.length
444
+ },
445
+
446
+ // Check if this suite has specs
447
+
448
+ hasSpecs : function() {
449
+ return this.specs.length
450
+ },
451
+
452
+ // Check if the entire suite passed
453
+
454
+ passed : function() {
455
+ return !any(this.specs, function(spec){
456
+ return !spec.passed()
457
+ })
458
+ }
459
+ })
460
+ },
461
+
462
+ /**
463
+ * Specification block object.
464
+ *
465
+ * @param {string} description
466
+ * @param {function} body
467
+ * @api private
468
+ */
469
+
470
+ Spec : function(description, body) {
471
+ extend(this, {
472
+ body: body,
473
+ description: description,
474
+ assertions: [],
475
+
476
+ // Add passing assertion
477
+
478
+ pass : function(message) {
479
+ this.assertions.push({ passed: true, message: message })
480
+ if (JSpec.assert) ++JSpec.stats.passes
481
+ },
482
+
483
+ // Add failing assertion
484
+
485
+ fail : function(message) {
486
+ this.assertions.push({ passed: false, message: message })
487
+ if (JSpec.assert) ++JSpec.stats.failures
488
+ },
489
+
490
+ // Run deferred assertions
491
+
492
+ runDeferredAssertions : function() {
493
+ each(this.assertions, function(assertion){
494
+ if (assertion.defer) assertion.run().report(), hook('afterAssertion', assertion)
495
+ })
496
+ },
497
+
498
+ // Find first failing assertion
499
+
500
+ failure : function() {
501
+ return find(this.assertions, function(assertion){
502
+ return !assertion.passed
503
+ })
504
+ },
505
+
506
+ // Find all failing assertions
507
+
508
+ failures : function() {
509
+ return select(this.assertions, function(assertion){
510
+ return !assertion.passed
511
+ })
512
+ },
513
+
514
+ // Weither or not the spec passed
515
+
516
+ passed : function() {
517
+ return !this.failure()
518
+ },
519
+
520
+ // Weither or not the spec requires implementation (no assertions)
521
+
522
+ requiresImplementation : function() {
523
+ return this.assertions.length == 0
524
+ },
525
+
526
+ // Sprite based assertions graph
527
+
528
+ assertionsGraph : function() {
529
+ return map(this.assertions, function(assertion){
530
+ return '<span class="assertion ' + (assertion.passed ? 'passed' : 'failed') + '"></span>'
531
+ }).join('')
532
+ }
533
+ })
534
+ },
535
+
536
+ Module : function(methods) {
537
+ extend(this, methods)
538
+ },
539
+
540
+ JSON : {
541
+
542
+ /**
543
+ * Generic sequences.
544
+ */
545
+
546
+ meta : {
547
+ '\b' : '\\b',
548
+ '\t' : '\\t',
549
+ '\n' : '\\n',
550
+ '\f' : '\\f',
551
+ '\r' : '\\r',
552
+ '"' : '\\"',
553
+ '\\' : '\\\\'
554
+ },
555
+
556
+ /**
557
+ * Escapable sequences.
558
+ */
559
+
560
+ escapable : /[\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
561
+
562
+ /**
563
+ * JSON encode _object_.
564
+ *
565
+ * @param {mixed} object
566
+ * @return {string}
567
+ * @api private
568
+ */
569
+
570
+ encode : function(object) {
571
+ var self = this
572
+ if (object == undefined || object == null) return 'null'
573
+ if (object === true) return 'true'
574
+ if (object === false) return 'false'
575
+ switch (typeof object) {
576
+ case 'number': return object
577
+ case 'string': return this.escapable.test(object) ?
578
+ '"' + object.replace(this.escapable, function (a) {
579
+ return typeof self.meta[a] === 'string' ? self.meta[a] :
580
+ '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4)
581
+ }) + '"' :
582
+ '"' + object + '"'
583
+ case 'object':
584
+ if (object.constructor == Array)
585
+ return '[' + map(object, function(val){
586
+ return self.encode(val)
587
+ }).join(', ') + ']'
588
+ else if (object)
589
+ return '{' + map(object, function(key, val){
590
+ return self.encode(key) + ':' + self.encode(val)
591
+ }).join(', ') + '}'
592
+ }
593
+ return 'null'
594
+ }
595
+ },
596
+
597
+ // --- DSLs
598
+
599
+ DSLs : {
600
+ snake : {
601
+ expect : function(actual){
602
+ return JSpec.expect(actual)
603
+ },
604
+
605
+ describe : function(description, body) {
606
+ return JSpec.currentSuite.addSuite(description, body)
607
+ },
608
+
609
+ it : function(description, body) {
610
+ return JSpec.currentSuite.addSpec(description, body)
611
+ },
612
+
613
+ before : function(body) {
614
+ return JSpec.currentSuite.addHook('before', body)
615
+ },
616
+
617
+ after : function(body) {
618
+ return JSpec.currentSuite.addHook('after', body)
619
+ },
620
+
621
+ before_each : function(body) {
622
+ return JSpec.currentSuite.addHook('before_each', body)
623
+ },
624
+
625
+ after_each : function(body) {
626
+ return JSpec.currentSuite.addHook('after_each', body)
627
+ },
628
+
629
+ should_behave_like : function(description) {
630
+ return JSpec.shareBehaviorsOf(description)
631
+ }
632
+ }
633
+ },
634
+
635
+ // --- Methods
636
+
637
+ /**
638
+ * Check if _value_ is 'stop'. For use as a
639
+ * utility callback function.
640
+ *
641
+ * @param {mixed} value
642
+ * @return {bool}
643
+ * @api public
644
+ */
645
+
646
+ haveStopped : function(value) {
647
+ return value === 'stop'
648
+ },
649
+
650
+ /**
651
+ * Include _object_ which may be a hash or Module instance.
652
+ *
653
+ * @param {hash, Module} object
654
+ * @return {JSpec}
655
+ * @api public
656
+ */
657
+
658
+ include : function(object) {
659
+ var module = object.constructor == JSpec.Module ? object : new JSpec.Module(object)
660
+ this.modules.push(module)
661
+ if ('init' in module) module.init()
662
+ if ('utilities' in module) extend(this.defaultContext, module.utilities)
663
+ if ('matchers' in module) this.addMatchers(module.matchers)
664
+ if ('reporters' in module) extend(this.reporters, module.reporters)
665
+ if ('DSLs' in module)
666
+ each(module.DSLs, function(name, methods){
667
+ JSpec.DSLs[name] = JSpec.DSLs[name] || {}
668
+ extend(JSpec.DSLs[name], methods)
669
+ })
670
+ return this
671
+ },
672
+
673
+ /**
674
+ * Add a module hook _name_, which is immediately
675
+ * called per module with the _args_ given. An array of
676
+ * hook return values is returned.
677
+ *
678
+ * @param {name} string
679
+ * @param {...} args
680
+ * @return {array}
681
+ * @api private
682
+ */
683
+
684
+ hook : function(name, args) {
685
+ args = toArray(arguments, 1)
686
+ return inject(JSpec.modules, [], function(results, module){
687
+ if (typeof module[name] == 'function')
688
+ results.push(JSpec.evalHook(module, name, args))
689
+ })
690
+ },
691
+
692
+ /**
693
+ * Eval _module_ hook _name_ with _args_. Evaluates in context
694
+ * to the module itself, JSpec, and JSpec.context.
695
+ *
696
+ * @param {Module} module
697
+ * @param {string} name
698
+ * @param {array} args
699
+ * @return {mixed}
700
+ * @api private
701
+ */
702
+
703
+ evalHook : function(module, name, args) {
704
+ hook('evaluatingHookBody', module, name)
705
+ try { return module[name].apply(module, args) }
706
+ catch(e) { error('Error in hook ' + module.name + '.' + name + ': ', e) }
707
+ },
708
+
709
+ /**
710
+ * Same as hook() however accepts only one _arg_ which is
711
+ * considered immutable. This function passes the arg
712
+ * to the first module, then passes the return value of the last
713
+ * module called, to the following module.
714
+ *
715
+ * @param {string} name
716
+ * @param {mixed} arg
717
+ * @return {mixed}
718
+ * @api private
719
+ */
720
+
721
+ hookImmutable : function(name, arg) {
722
+ return inject(JSpec.modules, arg, function(result, module){
723
+ if (typeof module[name] == 'function')
724
+ return JSpec.evalHook(module, name, [result])
725
+ })
726
+ },
727
+
728
+ /**
729
+ * Find a suite by its description or name.
730
+ *
731
+ * @param {string} description
732
+ * @return {Suite}
733
+ * @api private
734
+ */
735
+
736
+ findSuite : function(description) {
737
+ return find(this.allSuites, function(suite){
738
+ return suite.name == description || suite.description == description
739
+ })
740
+ },
741
+
742
+ /**
743
+ * Share behaviors (specs) of the given suite with
744
+ * the current suite.
745
+ *
746
+ * @param {string} description
747
+ * @api public
748
+ */
749
+
750
+ shareBehaviorsOf : function(description) {
751
+ if (suite = this.findSuite(description)) this.copySpecs(suite, this.currentSuite)
752
+ else throw 'failed to share behaviors. ' + puts(description) + ' is not a valid Suite name'
753
+ },
754
+
755
+ /**
756
+ * Copy specs from one suite to another.
757
+ *
758
+ * @param {Suite} fromSuite
759
+ * @param {Suite} toSuite
760
+ * @api public
761
+ */
762
+
763
+ copySpecs : function(fromSuite, toSuite) {
764
+ each(fromSuite.specs, function(spec){
765
+ var newSpec = new Object();
766
+ extend(newSpec, spec);
767
+ newSpec.assertions = [];
768
+ toSuite.specs.push(newSpec);
769
+ })
770
+ },
771
+
772
+ /**
773
+ * Convert arguments to an array.
774
+ *
775
+ * @param {object} arguments
776
+ * @param {int} offset
777
+ * @return {array}
778
+ * @api public
779
+ */
780
+
781
+ toArray : function(arguments, offset) {
782
+ return Array.prototype.slice.call(arguments, offset || 0)
783
+ },
784
+
785
+ /**
786
+ * Return ANSI-escaped colored string.
787
+ *
788
+ * @param {string} string
789
+ * @param {string} color
790
+ * @return {string}
791
+ * @api public
792
+ */
793
+
794
+ color : function(string, color) {
795
+ return "\u001B[" + {
796
+ bold : 1,
797
+ black : 30,
798
+ red : 31,
799
+ green : 32,
800
+ yellow : 33,
801
+ blue : 34,
802
+ magenta : 35,
803
+ cyan : 36,
804
+ white : 37
805
+ }[color] + 'm' + string + "\u001B[0m"
806
+ },
807
+
808
+ /**
809
+ * Default matcher message callback.
810
+ *
811
+ * @api private
812
+ */
813
+
814
+ defaultMatcherMessage : function(actual, expected, negate, name) {
815
+ return 'expected ' + puts(actual) + ' to ' +
816
+ (negate ? 'not ' : '') +
817
+ name.replace(/_/g, ' ') +
818
+ ' ' + (expected.length > 1 ?
819
+ puts.apply(this, expected.slice(1)) :
820
+ '')
821
+ },
822
+
823
+ /**
824
+ * Normalize a matcher message.
825
+ *
826
+ * When no messge callback is present the defaultMatcherMessage
827
+ * will be assigned, will suffice for most matchers.
828
+ *
829
+ * @param {hash} matcher
830
+ * @return {hash}
831
+ * @api public
832
+ */
833
+
834
+ normalizeMatcherMessage : function(matcher) {
835
+ if (typeof matcher.message != 'function')
836
+ matcher.message = this.defaultMatcherMessage
837
+ return matcher
838
+ },
839
+
840
+ /**
841
+ * Normalize a matcher body
842
+ *
843
+ * This process allows the following conversions until
844
+ * the matcher is in its final normalized hash state.
845
+ *
846
+ * - '==' becomes 'actual == expected'
847
+ * - 'actual == expected' becomes 'return actual == expected'
848
+ * - function(actual, expected) { return actual == expected } becomes
849
+ * { match : function(actual, expected) { return actual == expected }}
850
+ *
851
+ * @param {mixed} body
852
+ * @return {hash}
853
+ * @api public
854
+ */
855
+
856
+ normalizeMatcherBody : function(body) {
857
+ switch (body.constructor) {
858
+ case String:
859
+ if (captures = body.match(/^alias (\w+)/)) return JSpec.matchers[last(captures)]
860
+ if (body.length < 4) body = 'actual ' + body + ' expected'
861
+ return { match: function(actual, expected) { return eval(body) }}
862
+
863
+ case Function:
864
+ return { match: body }
865
+
866
+ default:
867
+ return body
868
+ }
869
+ },
870
+
871
+ /**
872
+ * Get option value. This method first checks if
873
+ * the option key has been set via the query string,
874
+ * otherwise returning the options hash value.
875
+ *
876
+ * @param {string} key
877
+ * @return {mixed}
878
+ * @api public
879
+ */
880
+
881
+ option : function(key) {
882
+ return (value = query(key)) !== null ? value :
883
+ JSpec.options[key] || null
884
+ },
885
+
886
+ /**
887
+ * Check if object _a_, is equal to object _b_.
888
+ *
889
+ * @param {object} a
890
+ * @param {object} b
891
+ * @return {bool}
892
+ * @api private
893
+ */
894
+
895
+ equal: function(a, b) {
896
+ if (typeof a != typeof b) return
897
+ if (a === b) return true
898
+ if (a instanceof RegExp)
899
+ return a.toString() === b.toString()
900
+ if (a instanceof Date)
901
+ return Number(a) === Number(b)
902
+ if (typeof a != 'object') return
903
+ if (a.length !== undefined)
904
+ if (a.length !== b.length) return
905
+ else
906
+ for (var i = 0, len = a.length; i < len; ++i)
907
+ if (!equal(a[i], b[i]))
908
+ return
909
+ for (var key in a)
910
+ if (!equal(a[key], b[key]))
911
+ return
912
+ return true
913
+ },
914
+
915
+ /**
916
+ * Return last element of an array.
917
+ *
918
+ * @param {array} array
919
+ * @return {object}
920
+ * @api public
921
+ */
922
+
923
+ last : function(array) {
924
+ return array[array.length - 1]
925
+ },
926
+
927
+ /**
928
+ * Convert object(s) to a print-friend string.
929
+ *
930
+ * @param {...} object
931
+ * @return {string}
932
+ * @api public
933
+ */
934
+
935
+ puts : function(object) {
936
+ if (arguments.length > 1)
937
+ return map(toArray(arguments), function(arg){
938
+ return puts(arg)
939
+ }).join(', ')
940
+ if (object === undefined) return 'undefined'
941
+ if (object === null) return 'null'
942
+ if (object === true) return 'true'
943
+ if (object === false) return 'false'
944
+ if (object.an_instance_of) return 'an instance of ' + object.an_instance_of.name
945
+ if (object.jquery && object.selector.length > 0) return 'selector ' + puts(object.selector)
946
+ if (object.jquery) return object.get(0).outerHTML
947
+ if (object.nodeName) return object.outerHTML
948
+ switch (object.constructor) {
949
+ case Function: return object.name || object
950
+ case String:
951
+ return '"' + object
952
+ .replace(/"/g, '\\"')
953
+ .replace(/\n/g, '\\n')
954
+ .replace(/\t/g, '\\t')
955
+ + '"'
956
+ case Array:
957
+ return inject(object, '[', function(b, v){
958
+ return b + ', ' + puts(v)
959
+ }).replace('[,', '[') + ' ]'
960
+ case Object:
961
+ object.__hit__ = true
962
+ return inject(object, '{', function(b, k, v) {
963
+ if (k == '__hit__') return b
964
+ return b + ', ' + k + ': ' + (v && v.__hit__ ? '<circular reference>' : puts(v))
965
+ }).replace('{,', '{') + ' }'
966
+ default:
967
+ return object.toString()
968
+ }
969
+ },
970
+
971
+ /**
972
+ * Escape HTML.
973
+ *
974
+ * @param {string} html
975
+ * @return {string}
976
+ * @api public
977
+ */
978
+
979
+ escape : function(html) {
980
+ return html.toString()
981
+ .replace(/&/gmi, '&amp;')
982
+ .replace(/"/gmi, '&quot;')
983
+ .replace(/>/gmi, '&gt;')
984
+ .replace(/</gmi, '&lt;')
985
+ },
986
+
987
+ /**
988
+ * Perform an assertion without reporting.
989
+ *
990
+ * This method is primarily used for internal
991
+ * matchers in order retain DRYness. May be invoked
992
+ * like below:
993
+ *
994
+ * does('foo', 'eql', 'foo')
995
+ * does([1,2], 'include', 1, 2)
996
+ *
997
+ * External hooks are not run for internal assertions
998
+ * performed by does().
999
+ *
1000
+ * @param {mixed} actual
1001
+ * @param {string} matcher
1002
+ * @param {...} expected
1003
+ * @return {mixed}
1004
+ * @api private
1005
+ */
1006
+
1007
+ does : function(actual, matcher, expected) {
1008
+ var assertion = new JSpec.Assertion(JSpec.matchers[matcher], actual, toArray(arguments, 2))
1009
+ return assertion.run().result
1010
+ },
1011
+
1012
+ /**
1013
+ * Perform an assertion.
1014
+ *
1015
+ * expect(true).to('be', true)
1016
+ * expect('foo').not_to('include', 'bar')
1017
+ * expect([1, [2]]).to('include', 1, [2])
1018
+ *
1019
+ * @param {mixed} actual
1020
+ * @return {hash}
1021
+ * @api public
1022
+ */
1023
+
1024
+ expect : function(actual) {
1025
+ function assert(matcher, args, negate) {
1026
+ var expected = toArray(args, 1)
1027
+ matcher.negate = negate
1028
+ assertion = new JSpec.Assertion(matcher, actual, expected, negate)
1029
+ hook('beforeAssertion', assertion)
1030
+ if (matcher.defer) assertion.run()
1031
+ else JSpec.currentSpec.assertions.push(assertion.run().report()), hook('afterAssertion', assertion)
1032
+ return assertion.result
1033
+ }
1034
+
1035
+ function to(matcher) {
1036
+ return assert(matcher, arguments, false)
1037
+ }
1038
+
1039
+ function not_to(matcher) {
1040
+ return assert(matcher, arguments, true)
1041
+ }
1042
+
1043
+ return {
1044
+ to : to,
1045
+ should : to,
1046
+ not_to: not_to,
1047
+ should_not : not_to
1048
+ }
1049
+ },
1050
+
1051
+ /**
1052
+ * Strim whitespace or chars.
1053
+ *
1054
+ * @param {string} string
1055
+ * @param {string} chars
1056
+ * @return {string}
1057
+ * @api public
1058
+ */
1059
+
1060
+ strip : function(string, chars) {
1061
+ return string.
1062
+ replace(new RegExp('[' + (chars || '\\s') + ']*$'), '').
1063
+ replace(new RegExp('^[' + (chars || '\\s') + ']*'), '')
1064
+ },
1065
+
1066
+ /**
1067
+ * Call an iterator callback with arguments a, or b
1068
+ * depending on the arity of the callback.
1069
+ *
1070
+ * @param {function} callback
1071
+ * @param {mixed} a
1072
+ * @param {mixed} b
1073
+ * @return {mixed}
1074
+ * @api private
1075
+ */
1076
+
1077
+ callIterator : function(callback, a, b) {
1078
+ return callback.length == 1 ? callback(b) : callback(a, b)
1079
+ },
1080
+
1081
+ /**
1082
+ * Extend an object with another.
1083
+ *
1084
+ * @param {object} object
1085
+ * @param {object} other
1086
+ * @api public
1087
+ */
1088
+
1089
+ extend : function(object, other) {
1090
+ each(other, function(property, value){
1091
+ object[property] = value
1092
+ })
1093
+ },
1094
+
1095
+ /**
1096
+ * Iterate an object, invoking the given callback.
1097
+ *
1098
+ * @param {hash, array} object
1099
+ * @param {function} callback
1100
+ * @return {JSpec}
1101
+ * @api public
1102
+ */
1103
+
1104
+ each : function(object, callback) {
1105
+ if (object.constructor == Array)
1106
+ for (var i = 0, len = object.length; i < len; ++i)
1107
+ callIterator(callback, i, object[i])
1108
+ else
1109
+ for (var key in object)
1110
+ if (object.hasOwnProperty(key))
1111
+ callIterator(callback, key, object[key])
1112
+ },
1113
+
1114
+ /**
1115
+ * Iterate with memo.
1116
+ *
1117
+ * @param {hash, array} object
1118
+ * @param {object} memo
1119
+ * @param {function} callback
1120
+ * @return {object}
1121
+ * @api public
1122
+ */
1123
+
1124
+ inject : function(object, memo, callback) {
1125
+ each(object, function(key, value){
1126
+ memo = (callback.length == 2 ?
1127
+ callback(memo, value):
1128
+ callback(memo, key, value)) ||
1129
+ memo
1130
+ })
1131
+ return memo
1132
+ },
1133
+
1134
+ /**
1135
+ * Destub _object_'s _method_. When no _method_ is passed
1136
+ * all stubbed methods are destubbed. When no arguments
1137
+ * are passed every object found in JSpec.stubbed will be
1138
+ * destubbed.
1139
+ *
1140
+ * @param {mixed} object
1141
+ * @param {string} method
1142
+ * @api public
1143
+ */
1144
+
1145
+ destub : function(object, method) {
1146
+ if (method) {
1147
+ if (object['__prototype__' + method])
1148
+ delete object[method]
1149
+ else
1150
+ object[method] = object['__original__' + method]
1151
+ delete object['__prototype__' + method]
1152
+ delete object['__original____' + method]
1153
+ }
1154
+ else if (object) {
1155
+ for (var key in object)
1156
+ if (captures = key.match(/^(?:__prototype__|__original__)(.*)/))
1157
+ destub(object, captures[1])
1158
+ }
1159
+ else
1160
+ while (JSpec.stubbed.length)
1161
+ destub(JSpec.stubbed.shift())
1162
+ },
1163
+
1164
+ /**
1165
+ * Stub _object_'s _method_.
1166
+ *
1167
+ * stub(foo, 'toString').and_return('bar')
1168
+ *
1169
+ * @param {mixed} object
1170
+ * @param {string} method
1171
+ * @return {hash}
1172
+ * @api public
1173
+ */
1174
+
1175
+ stub : function(object, method) {
1176
+ hook('stubbing', object, method)
1177
+ JSpec.stubbed.push(object)
1178
+ var type = object.hasOwnProperty(method) ? '__original__' : '__prototype__'
1179
+ object[type + method] = object[method]
1180
+ object[method] = function(){}
1181
+ return {
1182
+ and_return : function(value) {
1183
+ if (typeof value == 'function') object[method] = value
1184
+ else object[method] = function(){ return value }
1185
+ }
1186
+ }
1187
+ },
1188
+
1189
+ /**
1190
+ * Map callback return values.
1191
+ *
1192
+ * @param {hash, array} object
1193
+ * @param {function} callback
1194
+ * @return {array}
1195
+ * @api public
1196
+ */
1197
+
1198
+ map : function(object, callback) {
1199
+ return inject(object, [], function(memo, key, value){
1200
+ memo.push(callIterator(callback, key, value))
1201
+ })
1202
+ },
1203
+
1204
+ /**
1205
+ * Returns the first matching expression or null.
1206
+ *
1207
+ * @param {hash, array} object
1208
+ * @param {function} callback
1209
+ * @return {mixed}
1210
+ * @api public
1211
+ */
1212
+
1213
+ any : function(object, callback) {
1214
+ return inject(object, null, function(state, key, value){
1215
+ if (state == undefined)
1216
+ return callIterator(callback, key, value) ? value : state
1217
+ })
1218
+ },
1219
+
1220
+ /**
1221
+ * Returns an array of values collected when the callback
1222
+ * given evaluates to true.
1223
+ *
1224
+ * @param {hash, array} object
1225
+ * @return {function} callback
1226
+ * @return {array}
1227
+ * @api public
1228
+ */
1229
+
1230
+ select : function(object, callback) {
1231
+ return inject(object, [], function(selected, key, value){
1232
+ if (callIterator(callback, key, value))
1233
+ selected.push(value)
1234
+ })
1235
+ },
1236
+
1237
+ /**
1238
+ * Define matchers.
1239
+ *
1240
+ * @param {hash} matchers
1241
+ * @api public
1242
+ */
1243
+
1244
+ addMatchers : function(matchers) {
1245
+ each(matchers, function(name, body){
1246
+ JSpec.addMatcher(name, body)
1247
+ })
1248
+ },
1249
+
1250
+ /**
1251
+ * Define a matcher.
1252
+ *
1253
+ * @param {string} name
1254
+ * @param {hash, function, string} body
1255
+ * @api public
1256
+ */
1257
+
1258
+ addMatcher : function(name, body) {
1259
+ hook('addingMatcher', name, body)
1260
+ if (name.indexOf(' ') != -1) {
1261
+ var matchers = name.split(/\s+/)
1262
+ var prefix = matchers.shift()
1263
+ each(matchers, function(name) {
1264
+ JSpec.addMatcher(prefix + '_' + name, body(name))
1265
+ })
1266
+ }
1267
+ this.matchers[name] = this.normalizeMatcherMessage(this.normalizeMatcherBody(body))
1268
+ this.matchers[name].name = name
1269
+ },
1270
+
1271
+ /**
1272
+ * Add a root suite to JSpec.
1273
+ *
1274
+ * @param {string} description
1275
+ * @param {body} function
1276
+ * @api public
1277
+ */
1278
+
1279
+ describe : function(description, body) {
1280
+ var suite = new JSpec.Suite(description, body)
1281
+ hook('addingSuite', suite)
1282
+ this.allSuites.push(suite)
1283
+ this.suites.push(suite)
1284
+ },
1285
+
1286
+ /**
1287
+ * Return the contents of a function body.
1288
+ *
1289
+ * @param {function} body
1290
+ * @return {string}
1291
+ * @api public
1292
+ */
1293
+
1294
+ contentsOf : function(body) {
1295
+ return body.toString().match(/^[^\{]*{((.*\n*)*)}/m)[1]
1296
+ },
1297
+
1298
+ /**
1299
+ * Evaluate a JSpec capture body.
1300
+ *
1301
+ * @param {function} body
1302
+ * @param {string} errorMessage (optional)
1303
+ * @return {Type}
1304
+ * @api private
1305
+ */
1306
+
1307
+ evalBody : function(body, errorMessage) {
1308
+ var dsl = this.DSL || this.DSLs.snake
1309
+ var matchers = this.matchers
1310
+ var context = this.context || this.defaultContext
1311
+ var contents = this.contentsOf(body)
1312
+ hook('evaluatingBody', dsl, matchers, context, contents)
1313
+ try { with (dsl){ with (context) { with (matchers) { eval(contents) }}} }
1314
+ catch(e) { error(errorMessage, e) }
1315
+ },
1316
+
1317
+ /**
1318
+ * Pre-process a string of JSpec.
1319
+ *
1320
+ * @param {string} input
1321
+ * @return {string}
1322
+ * @api private
1323
+ */
1324
+
1325
+ preprocess : function(input) {
1326
+ if (typeof input != 'string') return
1327
+ input = hookImmutable('preprocessing', input)
1328
+ return input.
1329
+ replace(/\t/g, ' ').
1330
+ replace(/\r\n|\n|\r/g, '\n').
1331
+ split('__END__')[0].
1332
+ replace(/([\w\.]+)\.(stub|destub)\((.*?)\)$/gm, '$2($1, $3)').
1333
+ replace(/describe\s+(.*?)$/gm, 'describe($1, function(){').
1334
+ replace(/^\s+it\s+(.*?)$/gm, ' it($1, function(){').
1335
+ replace(/^ *(before_each|after_each|before|after)(?= |\n|$)/gm, 'JSpec.currentSuite.addHook("$1", function(){').
1336
+ replace(/^\s*end(?=\s|$)/gm, '});').
1337
+ replace(/-\{/g, 'function(){').
1338
+ replace(/(\d+)\.\.(\d+)/g, function(_, a, b){ return range(a, b) }).
1339
+ replace(/\.should([_\.]not)?[_\.](\w+)(?: |;|$)(.*)$/gm, '.should$1_$2($3)').
1340
+ replace(/([\/\s]*)(.+?)\.(should(?:[_\.]not)?)[_\.](\w+)\((.*)\)\s*;?$/gm, '$1 expect($2).$3($4, $5)').
1341
+ replace(/, \)/g, ')').
1342
+ replace(/should\.not/g, 'should_not')
1343
+ },
1344
+
1345
+ /**
1346
+ * Create a range string which can be evaluated to a native array.
1347
+ *
1348
+ * @param {int} start
1349
+ * @param {int} end
1350
+ * @return {string}
1351
+ * @api public
1352
+ */
1353
+
1354
+ range : function(start, end) {
1355
+ var current = parseInt(start), end = parseInt(end), values = [current]
1356
+ if (end > current) while (++current <= end) values.push(current)
1357
+ else while (--current >= end) values.push(current)
1358
+ return '[' + values + ']'
1359
+ },
1360
+
1361
+ /**
1362
+ * Report on the results.
1363
+ *
1364
+ * @api public
1365
+ */
1366
+
1367
+ report : function() {
1368
+ this.duration = Number(new Date) - this.start
1369
+ hook('reporting', JSpec.options)
1370
+ new (JSpec.options.reporter || JSpec.reporters.DOM)(JSpec, JSpec.options)
1371
+ },
1372
+
1373
+ /**
1374
+ * Run the spec suites. Options are merged
1375
+ * with JSpec options when present.
1376
+ *
1377
+ * @param {hash} options
1378
+ * @return {JSpec}
1379
+ * @api public
1380
+ */
1381
+
1382
+ run : function(options) {
1383
+ if (any(hook('running'), haveStopped)) return this
1384
+ if (options) extend(this.options, options)
1385
+ this.start = Number(new Date)
1386
+ each(this.suites, function(suite) { JSpec.runSuite(suite) })
1387
+ return this
1388
+ },
1389
+
1390
+ /**
1391
+ * Run a suite.
1392
+ *
1393
+ * @param {Suite} suite
1394
+ * @api public
1395
+ */
1396
+
1397
+ runSuite : function(suite) {
1398
+ this.currentSuite = suite
1399
+ this.evalBody(suite.body)
1400
+ suite.ran = true
1401
+ hook('beforeSuite', suite), suite.hook('before')
1402
+ each(suite.specs, function(spec) {
1403
+ hook('beforeSpec', spec)
1404
+ suite.hook('before_each')
1405
+ JSpec.runSpec(spec)
1406
+ hook('afterSpec', spec)
1407
+ suite.hook('after_each')
1408
+ })
1409
+ if (suite.hasSuites()) {
1410
+ each(suite.suites, function(suite) {
1411
+ JSpec.runSuite(suite)
1412
+ })
1413
+ }
1414
+ hook('afterSuite', suite), suite.hook('after')
1415
+ this.stats.suitesFinished++
1416
+ },
1417
+
1418
+ /**
1419
+ * Report a failure for the current spec.
1420
+ *
1421
+ * @param {string} message
1422
+ * @api public
1423
+ */
1424
+
1425
+ fail : function(message) {
1426
+ JSpec.currentSpec.fail(message)
1427
+ },
1428
+
1429
+ /**
1430
+ * Report a passing assertion for the current spec.
1431
+ *
1432
+ * @param {string} message
1433
+ * @api public
1434
+ */
1435
+
1436
+ pass : function(message) {
1437
+ JSpec.currentSpec.pass(message)
1438
+ },
1439
+
1440
+ /**
1441
+ * Run a spec.
1442
+ *
1443
+ * @param {Spec} spec
1444
+ * @api public
1445
+ */
1446
+
1447
+ runSpec : function(spec) {
1448
+ this.currentSpec = spec
1449
+ try { this.evalBody(spec.body) }
1450
+ catch (e) { fail(e) }
1451
+ spec.runDeferredAssertions()
1452
+ destub()
1453
+ this.stats.specsFinished++
1454
+ this.stats.assertions += spec.assertions.length
1455
+ },
1456
+
1457
+ /**
1458
+ * Require a dependency, with optional message.
1459
+ *
1460
+ * @param {string} dependency
1461
+ * @param {string} message (optional)
1462
+ * @return {JSpec}
1463
+ * @api public
1464
+ */
1465
+
1466
+ requires : function(dependency, message) {
1467
+ hook('requiring', dependency, message)
1468
+ try { eval(dependency) }
1469
+ catch (e) { throw 'JSpec depends on ' + dependency + ' ' + message }
1470
+ return this
1471
+ },
1472
+
1473
+ /**
1474
+ * Query against the current query strings keys
1475
+ * or the queryString specified.
1476
+ *
1477
+ * @param {string} key
1478
+ * @param {string} queryString
1479
+ * @return {string, null}
1480
+ * @api private
1481
+ */
1482
+
1483
+ query : function(key, queryString) {
1484
+ var queryString = (queryString || (main.location ? main.location.search : null) || '').substring(1)
1485
+ return inject(queryString.split('&'), null, function(value, pair){
1486
+ parts = pair.split('=')
1487
+ return parts[0] == key ? parts[1].replace(/%20|\+/gmi, ' ') : value
1488
+ })
1489
+ },
1490
+
1491
+ /**
1492
+ * Throw a JSpec related error.
1493
+ *
1494
+ * @param {string} message
1495
+ * @param {Exception} e
1496
+ * @api public
1497
+ */
1498
+
1499
+ error : function(message, e) {
1500
+ throw (message ? message : '') + e.toString() +
1501
+ (e.line ? ' near line ' + e.line : '')
1502
+ },
1503
+
1504
+ /**
1505
+ * Ad-hoc POST request for JSpec server usage.
1506
+ *
1507
+ * @param {string} uri
1508
+ * @param {string} data
1509
+ * @api private
1510
+ */
1511
+
1512
+ post : function(uri, data) {
1513
+ if (any(hook('posting', uri, data), haveStopped)) return
1514
+ var request = this.xhr()
1515
+ request.open('POST', uri, false)
1516
+ request.setRequestHeader('Content-Type', 'application/json')
1517
+ request.send(JSpec.JSON.encode(data))
1518
+ },
1519
+
1520
+ /**
1521
+ * Instantiate an XMLHttpRequest.
1522
+ *
1523
+ * Here we utilize IE's lame ActiveXObjects first which
1524
+ * allow IE access serve files via the file: protocol, otherwise
1525
+ * we then default to XMLHttpRequest.
1526
+ *
1527
+ * @return {XMLHttpRequest, ActiveXObject}
1528
+ * @api private
1529
+ */
1530
+
1531
+ xhr : function() {
1532
+ return this.ieXhr() || new JSpec.request
1533
+ },
1534
+
1535
+ /**
1536
+ * Return Microsoft piece of crap ActiveXObject.
1537
+ *
1538
+ * @return {ActiveXObject}
1539
+ * @api public
1540
+ */
1541
+
1542
+ ieXhr : function() {
1543
+ function object(str) {
1544
+ try { return new ActiveXObject(str) } catch(e) {}
1545
+ }
1546
+ return object('Msxml2.XMLHTTP.6.0') ||
1547
+ object('Msxml2.XMLHTTP.3.0') ||
1548
+ object('Msxml2.XMLHTTP') ||
1549
+ object('Microsoft.XMLHTTP')
1550
+ },
1551
+
1552
+ /**
1553
+ * Check for HTTP request support.
1554
+ *
1555
+ * @return {bool}
1556
+ * @api private
1557
+ */
1558
+
1559
+ hasXhr : function() {
1560
+ return JSpec.request || 'ActiveXObject' in main
1561
+ },
1562
+
1563
+ /**
1564
+ * Try loading _file_ returning the contents
1565
+ * string or null. Chain to locate / read a file.
1566
+ *
1567
+ * @param {string} file
1568
+ * @return {string}
1569
+ * @api public
1570
+ */
1571
+
1572
+ tryLoading : function(file) {
1573
+ try { return JSpec.load(file) } catch (e) {}
1574
+ },
1575
+
1576
+ /**
1577
+ * Load a _file_'s contents.
1578
+ *
1579
+ * @param {string} file
1580
+ * @param {function} callback
1581
+ * @return {string}
1582
+ * @api public
1583
+ */
1584
+
1585
+ load : function(file, callback) {
1586
+ if (any(hook('loading', file), haveStopped)) return
1587
+ if ('readFile' in main)
1588
+ return readFile(file)
1589
+ else if (this.hasXhr()) {
1590
+ var request = this.xhr()
1591
+ request.open('GET', file, false)
1592
+ request.send(null)
1593
+ if (request.readyState == 4 &&
1594
+ (request.status == 0 ||
1595
+ request.status.toString().charAt(0) == 2))
1596
+ return request.responseText
1597
+ }
1598
+ else
1599
+ error("failed to load `" + file + "'")
1600
+ },
1601
+
1602
+ /**
1603
+ * Load, pre-process, and evaluate a file.
1604
+ *
1605
+ * @param {string} file
1606
+ * @param {JSpec}
1607
+ * @api public
1608
+ */
1609
+
1610
+ exec : function(file) {
1611
+ if (any(hook('executing', file), haveStopped)) return this
1612
+ eval('with (JSpec){' + this.preprocess(this.load(file)) + '}')
1613
+ return this
1614
+ }
1615
+ }
1616
+
1617
+ // --- Node.js support
1618
+
1619
+ if (typeof GLOBAL === 'object' && typeof exports === 'object')
1620
+ quit = process.exit,
1621
+ print = require('sys').puts,
1622
+ readFile = require('fs').readFileSync
1623
+
1624
+ // --- Utility functions
1625
+
1626
+ var main = this,
1627
+ find = JSpec.any,
1628
+ utils = 'haveStopped stub hookImmutable hook destub map any last pass fail range each option inject select \
1629
+ error escape extend puts query strip color does addMatchers callIterator toArray equal'.split(/\s+/)
1630
+ while (utils.length) eval('var ' + utils[0] + ' = JSpec.' + utils.shift())
1631
+ if (!main.setTimeout) main.setTimeout = function(callback){ callback() }
1632
+
1633
+ // --- Matchers
1634
+
1635
+ addMatchers({
1636
+ equal : "===",
1637
+ eql : "equal(actual, expected)",
1638
+ be : "alias equal",
1639
+ be_greater_than : ">",
1640
+ be_less_than : "<",
1641
+ be_at_least : ">=",
1642
+ be_at_most : "<=",
1643
+ be_a : "actual.constructor == expected",
1644
+ be_an : "alias be_a",
1645
+ be_an_instance_of : "actual instanceof expected",
1646
+ be_null : "actual == null",
1647
+ be_true : "actual == true",
1648
+ be_false : "actual == false",
1649
+ be_undefined : "typeof actual == 'undefined'",
1650
+ be_type : "typeof actual == expected",
1651
+ match : "typeof actual == 'string' ? actual.match(expected) : false",
1652
+ respond_to : "typeof actual[expected] == 'function'",
1653
+ have_length : "actual.length == expected",
1654
+ be_within : "actual >= expected[0] && actual <= last(expected)",
1655
+ have_length_within : "actual.length >= expected[0] && actual.length <= last(expected)",
1656
+
1657
+ receive : { defer : true, match : function(actual, method, times) {
1658
+ proxy = new JSpec.ProxyAssertion(actual, method, times, this.negate)
1659
+ JSpec.currentSpec.assertions.push(proxy)
1660
+ return proxy
1661
+ }},
1662
+
1663
+ be_empty : function(actual) {
1664
+ if (actual.constructor == Object && actual.length == undefined)
1665
+ for (var key in actual)
1666
+ return false;
1667
+ return !actual.length
1668
+ },
1669
+
1670
+ include : function(actual) {
1671
+ for (state = true, i = 1; i < arguments.length; i++) {
1672
+ arg = arguments[i]
1673
+ switch (actual.constructor) {
1674
+ case String:
1675
+ case Number:
1676
+ case RegExp:
1677
+ case Function:
1678
+ state = actual.toString().indexOf(arg) !== -1
1679
+ break
1680
+
1681
+ case Object:
1682
+ state = arg in actual
1683
+ break
1684
+
1685
+ case Array:
1686
+ state = any(actual, function(value){ return equal(value, arg) })
1687
+ break
1688
+ }
1689
+ if (!state) return false
1690
+ }
1691
+ return true
1692
+ },
1693
+
1694
+ throw_error : { match : function(actual, expected, message) {
1695
+ try { actual() }
1696
+ catch (e) {
1697
+ this.e = e
1698
+ var assert = function(arg) {
1699
+ switch (arg.constructor) {
1700
+ case RegExp : return arg.test(e.message || e.toString())
1701
+ case String : return arg == (e.message || e.toString())
1702
+ case Function : return e instanceof arg || e.name == arg.name
1703
+ }
1704
+ }
1705
+ return message ? assert(expected) && assert(message) :
1706
+ expected ? assert(expected) :
1707
+ true
1708
+ }
1709
+ }, message : function(actual, expected, negate) {
1710
+ // TODO: refactor when actual is not in expected [0]
1711
+ var message_for = function(i) {
1712
+ if (expected[i] == undefined) return 'exception'
1713
+ switch (expected[i].constructor) {
1714
+ case RegExp : return 'exception matching ' + puts(expected[i])
1715
+ case String : return 'exception of ' + puts(expected[i])
1716
+ case Function : return expected[i].name || 'Error'
1717
+ }
1718
+ }
1719
+ exception = message_for(1) + (expected[2] ? ' and ' + message_for(2) : '')
1720
+ return 'expected ' + exception + (negate ? ' not ' : '' ) +
1721
+ ' to be thrown, but ' + (this.e ? 'got ' + puts(this.e) : 'nothing was')
1722
+ }},
1723
+
1724
+ have : function(actual, length, property) {
1725
+ return actual[property].length == length
1726
+ },
1727
+
1728
+ have_at_least : function(actual, length, property) {
1729
+ return actual[property].length >= length
1730
+ },
1731
+
1732
+ have_at_most :function(actual, length, property) {
1733
+ return actual[property].length <= length
1734
+ },
1735
+
1736
+ have_within : function(actual, range, property) {
1737
+ length = actual[property].length
1738
+ return length >= range.shift() && length <= range.pop()
1739
+ },
1740
+
1741
+ have_prop : function(actual, property, value) {
1742
+ return actual[property] == null ||
1743
+ actual[property] instanceof Function ? false:
1744
+ value == null ? true:
1745
+ does(actual[property], 'eql', value)
1746
+ },
1747
+
1748
+ have_property : function(actual, property, value) {
1749
+ return actual[property] == null ||
1750
+ actual[property] instanceof Function ? false:
1751
+ value == null ? true:
1752
+ value === actual[property]
1753
+ }
1754
+ })
1755
+
1756
+ })()