opal 0.3.41 → 0.3.42

Sign up to get free protection for your applications and to get access to all the features.
Files changed (285) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.md +14 -1
  4. data/Gemfile +2 -5
  5. data/Rakefile +41 -3
  6. data/bin/opal +33 -0
  7. data/lib/opal.rb +2 -12
  8. data/lib/opal/core_ext.rb +5 -0
  9. data/lib/opal/grammar.rb +2207 -2138
  10. data/lib/opal/grammar.y +21 -0
  11. data/lib/opal/grammar_helpers.rb +360 -0
  12. data/lib/opal/lexer.rb +55 -401
  13. data/lib/opal/lexer_scope.rb +28 -0
  14. data/lib/opal/parser.rb +155 -171
  15. data/lib/opal/target_scope.rb +257 -0
  16. data/lib/opal/version.rb +1 -1
  17. data/opal.gemspec +6 -2
  18. data/opal/opal-parser.js.erb +3 -2
  19. data/opal/opal.rb +20 -18
  20. data/opal/opal/array.rb +21 -12
  21. data/opal/opal/basic_object.rb +2 -1
  22. data/opal/opal/boolean.rb +3 -0
  23. data/opal/opal/browser_loader.js +57 -0
  24. data/opal/opal/class.rb +51 -13
  25. data/opal/opal/date.rb +1 -20
  26. data/opal/opal/enumerable.rb +66 -33
  27. data/opal/opal/error.rb +2 -0
  28. data/opal/opal/hash.rb +1 -1
  29. data/opal/opal/kernel.rb +14 -3
  30. data/opal/opal/nil_class.rb +4 -0
  31. data/opal/opal/proc.rb +9 -1
  32. data/opal/opal/racc.rb +2 -2
  33. data/opal/opal/regexp.rb +1 -1
  34. data/opal/opal/runtime.js +14 -4
  35. data/opal/opal/string.rb +21 -4
  36. data/opal/opal/time.rb +27 -0
  37. data/spec/core/array/allocate_spec.rb +7 -1
  38. data/spec/core/array/append_spec.rb +18 -3
  39. data/spec/core/array/array_spec.rb +7 -0
  40. data/spec/core/array/assoc_spec.rb +23 -8
  41. data/spec/core/array/at_spec.rb +23 -3
  42. data/spec/core/array/choice_spec.rb +20 -0
  43. data/spec/core/array/clear_spec.rb +45 -4
  44. data/spec/core/array/combination_spec.rb +55 -0
  45. data/spec/core/array/compact_spec.rb +72 -1
  46. data/spec/core/array/constructor_spec.rb +13 -2
  47. data/spec/core/array/count_spec.rb +15 -7
  48. data/spec/core/array/delete_at_spec.rb +44 -1
  49. data/spec/core/array/delete_if_spec.rb +52 -2
  50. data/spec/core/array/delete_spec.rb +83 -2
  51. data/spec/core/array/drop_spec.rb +24 -16
  52. data/spec/core/array/drop_while_spec.rb +17 -0
  53. data/spec/core/array/each_index_spec.rb +11 -1
  54. data/spec/core/array/each_spec.rb +20 -2
  55. data/spec/core/array/empty_spec.rb +4 -1
  56. data/spec/core/array/eql_spec.rb +14 -0
  57. data/spec/core/array/fetch_spec.rb +31 -2
  58. data/spec/core/array/find_index_spec.rb +8 -0
  59. data/spec/core/array/first_spec.rb +45 -8
  60. data/spec/core/array/fixtures/classes.rb +538 -0
  61. data/spec/core/array/flatten_spec.rb +200 -7
  62. data/spec/core/array/frozen_spec.rb +32 -0
  63. data/spec/core/array/include_spec.rb +16 -1
  64. data/spec/core/array/index_spec.rb +5 -25
  65. data/spec/core/array/insert_spec.rb +37 -3
  66. data/spec/core/array/inspect_spec.rb +6 -12
  67. data/spec/core/array/intersection_spec.rb +55 -4
  68. data/spec/core/array/join_spec.rb +29 -4
  69. data/spec/core/array/keep_if_spec.rb +13 -6
  70. data/spec/core/array/last_spec.rb +35 -1
  71. data/spec/core/array/length_spec.rb +7 -4
  72. data/spec/core/array/map_spec.rb +9 -47
  73. data/spec/core/array/minus_spec.rb +68 -4
  74. data/spec/core/array/multiply_spec.rb +138 -6
  75. data/spec/core/array/new_spec.rb +92 -3
  76. data/spec/core/array/ntimes_spec.rb +26 -0
  77. data/spec/core/array/plus_spec.rb +48 -2
  78. data/spec/core/array/pop_spec.rb +159 -39
  79. data/spec/core/array/push_spec.rb +29 -1
  80. data/spec/core/array/rassoc_spec.rb +31 -2
  81. data/spec/core/array/reject_spec.rb +89 -2
  82. data/spec/core/array/replace_spec.rb +7 -29
  83. data/spec/core/array/reverse_each_spec.rb +25 -1
  84. data/spec/core/array/reverse_spec.rb +53 -1
  85. data/spec/core/array/rindex_spec.rb +55 -5
  86. data/spec/core/array/select_spec.rb +35 -8
  87. data/spec/core/array/shared/collect.rb +0 -0
  88. data/spec/core/array/shared/enumeratorize.rb +12 -0
  89. data/spec/core/array/shared/eql.rb +95 -0
  90. data/spec/core/array/shared/index.rb +37 -0
  91. data/spec/core/array/shared/inspect.rb +3 -0
  92. data/spec/core/array/shared/join.rb +7 -0
  93. data/spec/core/array/shared/keep_if.rb +0 -0
  94. data/spec/core/array/shared/length.rb +0 -0
  95. data/spec/core/array/shared/replace.rb +0 -0
  96. data/spec/core/array/shared/slice.rb +0 -0
  97. data/spec/core/array/shift_spec.rb +132 -23
  98. data/spec/core/array/shuffle_spec.rb +82 -6
  99. data/spec/core/array/size_spec.rb +7 -4
  100. data/spec/core/array/slice_spec.rb +132 -1
  101. data/spec/core/array/sort_spec.rb +263 -14
  102. data/spec/core/array/take_spec.rb +24 -16
  103. data/spec/core/array/take_while_spec.rb +14 -10
  104. data/spec/core/array/to_a_spec.rb +18 -1
  105. data/spec/core/array/to_ary_spec.rb +15 -1
  106. data/spec/core/array/try_convert_spec.rb +39 -2
  107. data/spec/core/array/uniq_spec.rb +148 -3
  108. data/spec/core/array/unshift_spec.rb +36 -1
  109. data/spec/core/array/zip_spec.rb +36 -1
  110. data/spec/core/class/new_spec.rb +8 -6
  111. data/spec/core/enumerable/all_spec.rb +37 -9
  112. data/spec/core/enumerable/any_spec.rb +45 -7
  113. data/spec/core/enumerable/collect_spec.rb +4 -1
  114. data/spec/core/enumerable/count_spec.rb +4 -1
  115. data/spec/core/enumerable/detect_spec.rb +2 -2
  116. data/spec/core/enumerable/drop_spec.rb +4 -1
  117. data/spec/core/enumerable/drop_while_spec.rb +4 -1
  118. data/spec/core/enumerable/each_slice_spec.rb +2 -1
  119. data/spec/core/enumerable/each_with_index_spec.rb +4 -1
  120. data/spec/core/enumerable/each_with_object_spec.rb +4 -1
  121. data/spec/core/enumerable/entries_spec.rb +4 -1
  122. data/spec/core/enumerable/find_all_spec.rb +4 -1
  123. data/spec/core/enumerable/find_index_spec.rb +4 -1
  124. data/spec/core/enumerable/find_spec.rb +5 -2
  125. data/spec/core/enumerable/first_spec.rb +4 -1
  126. data/spec/core/enumerable/fixtures/classes.rb +198 -2
  127. data/spec/core/enumerable/grep_spec.rb +4 -1
  128. data/spec/core/enumerable/take_spec.rb +4 -1
  129. data/spec/core/enumerable/to_a_spec.rb +4 -1
  130. data/spec/core/false/and_spec.rb +11 -0
  131. data/spec/core/false/inspect_spec.rb +7 -0
  132. data/spec/core/false/or_spec.rb +11 -0
  133. data/spec/core/false/to_s_spec.rb +7 -0
  134. data/spec/core/false/xor_spec.rb +11 -0
  135. data/spec/core/kernel/rand_spec.rb +5 -5
  136. data/spec/core/module/const_get_spec.rb +4 -4
  137. data/spec/core/module/fixtures/classes.rb +434 -0
  138. data/spec/core/module/method_defined_spec.rb +49 -0
  139. data/spec/core/module/module_function_spec.rb +28 -0
  140. data/spec/core/nil/and_spec.rb +3 -1
  141. data/spec/core/nil/dup_spec.rb +7 -0
  142. data/spec/core/nil/inspect_spec.rb +3 -1
  143. data/spec/core/nil/nil_spec.rb +3 -1
  144. data/spec/core/nil/or_spec.rb +4 -2
  145. data/spec/core/nil/to_a_spec.rb +3 -1
  146. data/spec/core/nil/to_f_spec.rb +3 -1
  147. data/spec/core/nil/to_i_spec.rb +3 -1
  148. data/spec/core/nil/to_s_spec.rb +3 -1
  149. data/spec/core/nil/xor_spec.rb +4 -2
  150. data/spec/core/string/element_reference_spec.rb +14 -1
  151. data/spec/core/string/fixtures/classes.rb +0 -0
  152. data/spec/core/true/and_spec.rb +11 -0
  153. data/spec/core/true/inspect_spec.rb +7 -0
  154. data/spec/core/true/or_spec.rb +11 -0
  155. data/spec/core/true/to_s_spec.rb +7 -0
  156. data/spec/core/true/xor_spec.rb +11 -0
  157. data/spec/{core → core_ext}/array/element_reference_spec.rb +0 -0
  158. data/spec/{core → core_ext}/array/equal_value_spec.rb +0 -0
  159. data/spec/{core → core_ext}/array/fill_spec.rb +0 -0
  160. data/spec/{core → core_ext}/array/reduce_spec.rb +0 -0
  161. data/spec/core_ext/basic_object/send_spec.rb +3 -3
  162. data/spec/{core → core_ext}/boolean/singleton_class_spec.rb +0 -0
  163. data/spec/{core → core_ext}/boolean/to_json_spec.rb +0 -0
  164. data/spec/core_ext/class/_inherited_spec.rb +3 -3
  165. data/spec/core_ext/class/proc_methods_spec.rb +2 -2
  166. data/spec/core_ext/class/singleton_methods_spec.rb +8 -8
  167. data/spec/core_ext/method_missing_spec.rb +3 -3
  168. data/spec/core_ext/native/method_missing_spec.rb +3 -2
  169. data/spec/core_ext/native/to_native_spec.rb +3 -2
  170. data/spec/{core → core_ext}/nil/to_json_spec.rb +0 -0
  171. data/spec/date.rb +0 -0
  172. data/spec/fileutils.rb +0 -0
  173. data/spec/filters/ancestors.rb +4 -0
  174. data/spec/filters/array_delete.rb +3 -0
  175. data/spec/filters/array_fetch.rb +3 -0
  176. data/spec/filters/array_first.rb +3 -0
  177. data/spec/filters/array_flatten.rb +14 -0
  178. data/spec/filters/array_intersection.rb +5 -0
  179. data/spec/filters/array_join.rb +6 -0
  180. data/spec/filters/array_subclasses.rb +4 -0
  181. data/spec/filters/block_args.rb +3 -0
  182. data/spec/filters/coerce_integer.rb +9 -0
  183. data/spec/filters/frozen.rb +4 -0
  184. data/spec/filters/mocks.rb +3 -0
  185. data/spec/filters/should_receive.rb +4 -0
  186. data/spec/filters/tainted.rb +7 -0
  187. data/spec/fixtures/class.rb +124 -0
  188. data/spec/fixtures/class_variables.rb +0 -0
  189. data/spec/fixtures/constants.rb +0 -0
  190. data/spec/grammar/alias_spec.rb +1 -1
  191. data/spec/grammar/def_spec.rb +1 -0
  192. data/spec/grammar/lvar_spec.rb +1 -2
  193. data/spec/grammar/nth_ref_spec.rb +13 -0
  194. data/spec/grammar/sclass_spec.rb +6 -7
  195. data/spec/grammar/str_spec.rb +4 -4
  196. data/spec/grammar/string_spec.rb +8 -0
  197. data/spec/grammar/xstr_spec.rb +4 -4
  198. data/spec/iconv.rb +0 -0
  199. data/spec/language/alias_spec.rb +140 -3
  200. data/spec/language/and_spec.rb +14 -7
  201. data/spec/language/array_spec.rb +57 -5
  202. data/spec/language/block_spec.rb +466 -49
  203. data/spec/language/break_spec.rb +294 -44
  204. data/spec/language/case_spec.rb +151 -3
  205. data/spec/language/class_spec.rb +196 -0
  206. data/spec/language/class_variable_spec.rb +56 -0
  207. data/spec/language/def_spec.rb +507 -4
  208. data/spec/language/defined_spec.rb +19 -7
  209. data/spec/language/ensure_spec.rb +26 -39
  210. data/spec/language/execution_spec.rb +15 -0
  211. data/spec/language/fixtures/array.rb +11 -0
  212. data/spec/language/fixtures/block.rb +57 -0
  213. data/spec/language/fixtures/break.rb +240 -0
  214. data/spec/language/fixtures/ensure.rb +72 -0
  215. data/spec/language/fixtures/literal_lambda.rb +7 -0
  216. data/spec/language/fixtures/metaclass.rb +33 -0
  217. data/spec/language/fixtures/module.rb +24 -0
  218. data/spec/language/fixtures/next.rb +78 -12
  219. data/spec/language/fixtures/return.rb +118 -0
  220. data/spec/language/fixtures/send.rb +110 -0
  221. data/spec/language/fixtures/send_1.9.rb +22 -0
  222. data/spec/language/fixtures/super.rb +308 -0
  223. data/spec/language/fixtures/variables.rb +58 -0
  224. data/spec/language/fixtures/yield.rb +5 -0
  225. data/spec/language/for_spec.rb +192 -0
  226. data/spec/language/hash_spec.rb +29 -5
  227. data/spec/language/if_spec.rb +90 -9
  228. data/spec/language/literal_lambda_spec.rb +1 -47
  229. data/spec/language/loop_spec.rb +39 -2
  230. data/spec/language/metaclass_spec.rb +151 -5
  231. data/spec/language/module_spec.rb +56 -0
  232. data/spec/language/next_spec.rb +370 -12
  233. data/spec/language/not_spec.rb +55 -0
  234. data/spec/language/numbers_spec.rb +56 -0
  235. data/spec/language/or_spec.rb +31 -3
  236. data/spec/language/order_spec.rb +79 -0
  237. data/spec/language/precedence_spec.rb +483 -0
  238. data/spec/language/proc_spec.rb +249 -21
  239. data/spec/language/redo_spec.rb +67 -0
  240. data/spec/language/rescue_spec.rb +121 -0
  241. data/spec/language/retry_spec.rb +56 -0
  242. data/spec/language/return_spec.rb +281 -0
  243. data/spec/language/send_spec.rb +141 -48
  244. data/spec/language/singleton_class_spec.rb +1 -1
  245. data/spec/language/string_spec.rb +11 -0
  246. data/spec/language/super_spec.rb +213 -133
  247. data/spec/language/symbol_spec.rb +2 -1
  248. data/spec/language/undef_spec.rb +3 -1
  249. data/spec/language/unless_spec.rb +6 -2
  250. data/spec/language/until_spec.rb +102 -3
  251. data/spec/language/variables_spec.rb +1212 -16
  252. data/spec/language/versions/array_1.9.rb +39 -0
  253. data/spec/language/versions/case_1.9.rb +20 -0
  254. data/spec/language/versions/hash_1.9.rb +18 -0
  255. data/spec/language/versions/literal_lambda_1.9.rb +143 -0
  256. data/spec/language/versions/not_1.9.rb +22 -0
  257. data/spec/language/versions/send_1.9.rb +241 -0
  258. data/spec/language/versions/symbol_1.9.rb +15 -0
  259. data/spec/language/versions/variables_1.9.rb +8 -0
  260. data/spec/language/while_spec.rb +70 -5
  261. data/spec/language/yield_spec.rb +32 -6
  262. data/spec/mspec/guards/block_device.rb +0 -0
  263. data/spec/mspec/guards/endian.rb +0 -0
  264. data/spec/mspec/helpers/environment.rb +0 -0
  265. data/spec/mspec/helpers/language_version.rb +0 -0
  266. data/spec/mspec/helpers/tmp.rb +0 -0
  267. data/spec/ospec/filter.rb +32 -0
  268. data/spec/ospec/main.rb.erb +18 -0
  269. data/spec/ospec/phantom.rb +97 -0
  270. data/spec/ospec/runner.rb +95 -0
  271. data/spec/ospec/sprockets.js +40 -0
  272. data/spec/pp.rb +3 -0
  273. data/spec/rbconfig.rb +5 -0
  274. data/spec/spec_helper.rb +53 -26
  275. data/spec/yaml.rb +0 -0
  276. metadata +275 -31
  277. data/config.ru +0 -8
  278. data/lib/opal/processor.rb +0 -47
  279. data/lib/opal/scope.rb +0 -236
  280. data/lib/opal/server.rb +0 -94
  281. data/spec/core/boolean/and_spec.rb +0 -17
  282. data/spec/core/boolean/inspect_spec.rb +0 -9
  283. data/spec/core/boolean/or_spec.rb +0 -17
  284. data/spec/core/boolean/to_s_spec.rb +0 -9
  285. data/spec/core/boolean/xor_spec.rb +0 -17
data/opal/opal/kernel.rb CHANGED
@@ -92,6 +92,10 @@ module Kernel
92
92
  }
93
93
  end
94
94
 
95
+ def dup
96
+ self.class.allocate
97
+ end
98
+
95
99
  def enum_for(method = :each, *args)
96
100
  Enumerator.new self, method, *args
97
101
  end
@@ -272,7 +276,9 @@ module Kernel
272
276
  var result = [];
273
277
 
274
278
  for (var name in #{self}) {
275
- result.push(name);
279
+ if (name.charAt(0) !== '$') {
280
+ result.push(name);
281
+ }
276
282
  }
277
283
 
278
284
  return result;
@@ -328,7 +334,7 @@ module Kernel
328
334
  def proc(&block)
329
335
  %x{
330
336
  if (block === nil) {
331
- no_block_given();
337
+ #{ raise ArgumentError, 'no block given' };
332
338
  }
333
339
  block.is_lambda = false;
334
340
  return block;
@@ -355,7 +361,7 @@ module Kernel
355
361
 
356
362
  alias print puts
357
363
 
358
- def raise(exception, string)
364
+ def raise(exception = "", string = undefined)
359
365
  %x{
360
366
  if (typeof(exception) === 'string') {
361
367
  exception = #{RuntimeError.new exception};
@@ -389,6 +395,7 @@ module Kernel
389
395
  meta._klass = __opal.Class;
390
396
  #{self}._singleton = meta;
391
397
  meta.prototype = #{self};
398
+ meta._isSingleton = true;
392
399
 
393
400
  return meta;
394
401
  }
@@ -420,6 +427,10 @@ module Kernel
420
427
 
421
428
  alias sprintf format
422
429
 
430
+ def String(str)
431
+ `String(str)`
432
+ end
433
+
423
434
  def tap(&block)
424
435
  yield self
425
436
  self
@@ -19,6 +19,10 @@ class NilClass
19
19
  self
20
20
  end
21
21
 
22
+ def dup
23
+ raise TypeError
24
+ end
25
+
22
26
  def inspect
23
27
  'nil'
24
28
  end
data/opal/opal/proc.rb CHANGED
@@ -9,7 +9,15 @@ class Proc < `Function`
9
9
  end
10
10
 
11
11
  def call(*args)
12
- `#{self}.apply(null, #{args})`
12
+ %x{
13
+ var result = #{self}.apply(null, #{args});
14
+
15
+ if (result === __breaker) {
16
+ return __breaker.$v;
17
+ }
18
+
19
+ return result;
20
+ }
13
21
  end
14
22
 
15
23
  alias [] call
data/opal/opal/racc.rb CHANGED
@@ -157,7 +157,7 @@ module Racc
157
157
 
158
158
  elsif act == -reduce_n
159
159
  # reduce
160
- raise "Opal Syntax Error: unexpected '#{racc_tok.inspect}'"
160
+ raise SyntaxError, "unexpected '#{racc_tok.inspect}'"
161
161
 
162
162
  else
163
163
  raise "Rac: unknown action: #{act}"
@@ -212,4 +212,4 @@ module Racc
212
212
  puts ' ]'
213
213
  end
214
214
  end
215
- end
215
+ end
data/opal/opal/regexp.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  class Regexp < `RegExp`
2
2
  def self.escape(string)
3
- `string.replace(/([.*+?^=!:${}()|[\]\\/\\])/g, '\\$1')`
3
+ `string.replace(/[\\-\\[\\]\\/\\{\\}\\(\\)\\*\\+\\?\\.\\\\\^\\$\\|]/g, '\\\\$&')`
4
4
  end
5
5
 
6
6
  def self.new(string, options = undefined)
data/opal/opal/runtime.js CHANGED
@@ -235,12 +235,15 @@
235
235
  };
236
236
 
237
237
  Opal.puts = function(a) { console.log(a); };
238
-
238
+
239
239
  // Method missing dispatcher
240
240
  Opal.mm = function(mid) {
241
- return function() {
241
+ var dispatcher = function() {
242
+ this.$method_missing._p = dispatcher._p;
242
243
  return this.$method_missing.apply(this, [mid].concat(__slice.call(arguments)));
243
- }
244
+ };
245
+
246
+ return dispatcher;
244
247
  };
245
248
 
246
249
  // Const missing dispatcher
@@ -332,6 +335,12 @@
332
335
  }
333
336
  }
334
337
 
338
+ // Defines methods onto Object (which are then donated to bridged classes)
339
+ Object._defn = function (mid, body) {
340
+ this.prototype[mid] = body;
341
+ this._donate([mid]);
342
+ };
343
+
335
344
  var bridged_classes = Object.$included_in = [];
336
345
 
337
346
  BasicObject._scope = Object._scope = Opal;
@@ -350,7 +359,8 @@
350
359
  Opal.top = new Object;
351
360
 
352
361
  Opal.klass(Object, Object, 'NilClass', NilClass)
353
- Opal.nil = new NilClass;
362
+ var nil = Opal.nil = new NilClass;
363
+ nil.call = nil.apply = function() { throw Opal.LocalJumpError.$new('no block given'); };
354
364
 
355
365
  Opal.breaker = new Error('unexpected break');
356
366
  }).call(this);
data/opal/opal/string.rb CHANGED
@@ -99,15 +99,28 @@ class String < `String`
99
99
  length = index.end,
100
100
  index = index.begin;
101
101
 
102
+ if (index < 0) {
103
+ index += size;
104
+ }
105
+
106
+ if (length < 0) {
107
+ length += size;
108
+ }
109
+
110
+ if (!exclude) {
111
+ length += 1;
112
+ }
113
+
102
114
  if (index > size) {
103
115
  return nil;
104
116
  }
105
117
 
118
+ length = length - index;
119
+
106
120
  if (length < 0) {
107
- length += size;
121
+ length = 0;
108
122
  }
109
123
 
110
- if (exclude) length -= 1;
111
124
  return #{self}.substr(index, length);
112
125
  }
113
126
 
@@ -243,7 +256,7 @@ class String < `String`
243
256
 
244
257
  alias_native :getbyte, :charCodeAt
245
258
 
246
- def gsub(pattern, replace = undefined)
259
+ def gsub(pattern, replace = undefined, &block)
247
260
  if pattern.is_a?(String)
248
261
  pattern = /#{Regexp.escape(pattern)}/
249
262
  end
@@ -253,6 +266,7 @@ class String < `String`
253
266
  options = pattern.substr(pattern.lastIndexOf('/') + 1) + 'g',
254
267
  regexp = pattern.substr(1, pattern.lastIndexOf('/') - 1);
255
268
 
269
+ #{self}.$sub._p = block;
256
270
  return #{self}.$sub(new RegExp(regexp, options), replace);
257
271
  }
258
272
  end
@@ -507,7 +521,10 @@ class String < `String`
507
521
  %x{
508
522
  var name = '$' + #{self};
509
523
 
510
- return function(arg) { return arg[name](); };
524
+ return function(arg) {
525
+ var meth = arg[name];
526
+ return meth ? meth.call(arg) : arg.$method_missing(name);
527
+ };
511
528
  }
512
529
  end
513
530
 
data/opal/opal/time.rb CHANGED
@@ -1,3 +1,8 @@
1
+ days_of_week = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday]
2
+ short_days = %w[Sun Mon Tue Wed Thu Fri Sat]
3
+ short_months = %w[Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec]
4
+ long_months = %w[January Febuary March April May June July August September October November December]
5
+
1
6
  class Time < `Date`
2
7
  include Comparable
3
8
 
@@ -32,6 +37,10 @@ class Time < `Date`
32
37
  `new Date()`
33
38
  end
34
39
 
40
+ def self.parse(str)
41
+ `Date.parse(str)`
42
+ end
43
+
35
44
  def +(other)
36
45
  Time.allocate(self.to_f + other.to_f)
37
46
  end
@@ -78,6 +87,24 @@ class Time < `Date`
78
87
 
79
88
  alias_native :sec, :getSeconds
80
89
 
90
+ def strftime(format = '')
91
+ %x{
92
+ var d = #{self};
93
+
94
+ return format.replace(/%(-?.)/g, function(full, m) {
95
+ switch (m) {
96
+ case 'a': return short_days[d.getDay()];
97
+ case 'A': return days_of_week[d.getDay()];
98
+ case 'b': return short_months[d.getMonth()];
99
+ case 'B': return long_months[d.getMonth()];
100
+ case '-d': return d.getDate();
101
+ case 'Y': return d.getFullYear();
102
+ default: return m ;
103
+ }
104
+ });
105
+ }
106
+ end
107
+
81
108
  def sunday?
82
109
  `#{self}.getDay() === 0`
83
110
  end
@@ -1,3 +1,5 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
1
3
  describe "Array.allocate" do
2
4
  it "returns an instance of Array" do
3
5
  ary = Array.allocate
@@ -10,4 +12,8 @@ describe "Array.allocate" do
10
12
  ary << 1
11
13
  ary.should == [1]
12
14
  end
13
- end
15
+
16
+ it "does not accept any arguments" do
17
+ lambda { Array.allocate(1) }.should raise_error(ArgumentError)
18
+ end
19
+ end
@@ -1,3 +1,6 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#<<" do
2
5
  it "pushes the object onto the end of the array" do
3
6
  ([ 1, 2 ] << "c" << "d" << [ 3, 4 ]).should == [1, 2, "c", "d", [3, 4]]
@@ -6,8 +9,8 @@ describe "Array#<<" do
6
9
  it "returns self to allow chaining" do
7
10
  a = []
8
11
  b = a
9
- (a << 1).should == b
10
- (a << 2 << 3).should == b
12
+ (a << 1).should equal(b)
13
+ (a << 2 << 3).should equal(b)
11
14
  end
12
15
 
13
16
  it "correctly resizes the Array" do
@@ -25,4 +28,16 @@ describe "Array#<<" do
25
28
  a << :foo
26
29
  a.should == [:foo]
27
30
  end
28
- end
31
+
32
+ ruby_version_is '' ... '1.9' do
33
+ it "raises a TypeError on a frozen array" do
34
+ lambda { ArraySpecs.frozen_array << 5 }.should raise_error(TypeError)
35
+ end
36
+ end
37
+
38
+ ruby_version_is '1.9' do
39
+ it "raises a RuntimeError on a frozen array" do
40
+ lambda { ArraySpecs.frozen_array << 5 }.should raise_error(RuntimeError)
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Array" do
4
+ it "includes Enumerable" do
5
+ Array.ancestors.include?(Enumerable).should == true
6
+ end
7
+ end
@@ -1,3 +1,6 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#assoc" do
2
5
  it "returns the first array whose 1st item is == obj or nil" do
3
6
  s1 = ["colors", "red", "blue", "green"]
@@ -7,11 +10,23 @@ describe "Array#assoc" do
7
10
  s5 = [:letters, "a", "i", "u"]
8
11
  s_nil = [nil, nil]
9
12
  a = [s1, s2, s3, s4, s5, s_nil]
10
- a.assoc(s1.first).should == s1
11
- a.assoc(s2.first).should == s2
12
- a.assoc(s3.first).should == s3
13
- a.assoc(s4.first).should == s1
14
- a.assoc(s5.first).should == s2
13
+ a.assoc(s1.first).should equal(s1)
14
+ a.assoc(s2.first).should equal(s2)
15
+ a.assoc(s3.first).should equal(s3)
16
+ a.assoc(s4.first).should equal(s1)
17
+ a.assoc(s5.first).should equal(s2)
18
+ a.assoc(s_nil.first).should equal(s_nil)
19
+ a.assoc(4).should equal(s3)
20
+ a.assoc("key not in array").should be_nil
21
+ end
22
+
23
+ it "calls == on first element of each array" do
24
+ key1 = 'it'
25
+ key2 = mock('key2')
26
+ items = [['not it', 1], [ArraySpecs::AssocKey.new, 2], ['na', 3]]
27
+
28
+ items.assoc(key1).should equal(items[1])
29
+ items.assoc(key2).should be_nil
15
30
  end
16
31
 
17
32
  it "ignores any non-Array elements" do
@@ -19,7 +34,7 @@ describe "Array#assoc" do
19
34
  s1 = [4]
20
35
  s2 = [5, 4, 3]
21
36
  a = ["foo", [], s1, s2, nil, []]
22
- a.assoc(s1.first).should == s1
23
- a.assoc(s2.first).should == s2
37
+ a.assoc(s1.first).should equal(s1)
38
+ a.assoc(s2.first).should equal(s2)
24
39
  end
25
- end
40
+ end
@@ -1,3 +1,6 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#at" do
2
5
  it "returns the (n+1)'th element for the passed index n" do
3
6
  a = [1, 2, 3, 4, 5, 6]
@@ -12,14 +15,14 @@ describe "Array#at" do
12
15
  a.at(7).should == nil
13
16
  end
14
17
 
15
- it "returns the (-n)'th element from the last, for the given negative index n" do
18
+ it "returns the (-n)'th elemet from the last, for the given negative index n" do
16
19
  a = [1, 2, 3, 4, 5, 6]
17
20
  a.at(-1).should == 6
18
21
  a.at(-2).should == 5
19
22
  a.at(-6).should == 1
20
23
  end
21
24
 
22
- it "returns nil if the given index is less than -len, where len is length of the array" do
25
+ it "returns nil if the given index is less than -len, where len is length of the array" do
23
26
  a = [1, 2, 3, 4, 5, 6]
24
27
  a.at(-7).should == nil
25
28
  a.at(-8).should == nil
@@ -33,4 +36,21 @@ describe "Array#at" do
33
36
  a.at(-100)
34
37
  a.length.should == 6
35
38
  end
36
- end
39
+
40
+ it "tries to convert the passed argument to an Integer using #to_int" do
41
+ a = ["a", "b", "c"]
42
+ a.at(0.5).should == "a"
43
+
44
+ obj = mock('to_int')
45
+ obj.should_receive(:to_int).and_return(2)
46
+ a.at(obj).should == "c"
47
+ end
48
+
49
+ it "raises a TypeError when the passed argument can't be coerced to Integer" do
50
+ lambda { [].at("cat") }.should raise_error(TypeError)
51
+ end
52
+
53
+ it "raises an ArgumentError when 2 or more arguments is passed" do
54
+ lambda { [:a, :b].at(0,1) }.should raise_error(ArgumentError)
55
+ end
56
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#choice" do
5
+ ruby_version_is "1.8.7"..."1.9" do
6
+ it "returns a value from the array" do
7
+ [4].choice.should eql(4)
8
+ end
9
+
10
+ it "returns a distribution of results" do
11
+ source = [0,1,2,3,4]
12
+ choices = ArraySpecs::SampleRange.collect { |el| source.choice }
13
+ choices.uniq.sort.should == source
14
+ end
15
+
16
+ it "returns nil for empty arrays" do
17
+ [].choice.should be_nil
18
+ end
19
+ end
20
+ end
@@ -1,14 +1,17 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#clear" do
2
5
  it "removes all elements" do
3
6
  a = [1, 2, 3, 4]
4
- a.clear
7
+ a.clear.should equal(a)
5
8
  a.should == []
6
9
  end
7
10
 
8
11
  it "returns self" do
9
12
  a = [1]
10
- old = a.object_id
11
- a.clear.object_id.should == old
13
+ oid = a.object_id
14
+ a.clear.object_id.should == oid
12
15
  end
13
16
 
14
17
  it "leaves the Array empty" do
@@ -17,4 +20,42 @@ describe "Array#clear" do
17
20
  a.empty?.should == true
18
21
  a.size.should == 0
19
22
  end
20
- end
23
+
24
+ it "keeps tainted status" do
25
+ a = [1]
26
+ a.taint
27
+ a.tainted?.should be_true
28
+ a.clear
29
+ a.tainted?.should be_true
30
+ end
31
+
32
+ it "does not accept any arguments" do
33
+ lambda { [1].clear(true) }.should raise_error(ArgumentError)
34
+ end
35
+
36
+ ruby_version_is '1.9' do
37
+ it "keeps untrusted status" do
38
+ a = [1]
39
+ a.untrust
40
+ a.untrusted?.should be_true
41
+ a.clear
42
+ a.untrusted?.should be_true
43
+ end
44
+ end
45
+
46
+ ruby_version_is '' ... '1.9' do
47
+ it "raises a TypeError on a frozen array" do
48
+ a = [1]
49
+ a.freeze
50
+ lambda { a.clear }.should raise_error(TypeError)
51
+ end
52
+ end
53
+
54
+ ruby_version_is '1.9' do
55
+ it "raises a RuntimeError on a frozen array" do
56
+ a = [1]
57
+ a.freeze
58
+ lambda { a.clear }.should raise_error(RuntimeError)
59
+ end
60
+ end
61
+ end