opal 0.3.41 → 0.3.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ ruby_version_is "1.8.7" do
4
+ describe "Array#drop_while" do
5
+ it "removes elements from the start of the array while the block evaluates to true" do
6
+ [1, 2, 3, 4].drop_while { |n| n < 4 }.should == [4]
7
+ end
8
+
9
+ it "removes elements from the start of the array until the block returns nil" do
10
+ [1, 2, 3, nil, 5].drop_while { |n| n }.should == [nil, 5]
11
+ end
12
+
13
+ it "removes elements from the start of the array until the block returns false" do
14
+ [1, 2, 3, false, 5].drop_while { |n| n }.should == [false, 5]
15
+ end
16
+ end
17
+ end
@@ -1,3 +1,11 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/enumeratorize', __FILE__)
4
+
5
+ # Modifying a collection while the contents are being iterated
6
+ # gives undefined behavior. See
7
+ # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23633
8
+
1
9
  describe "Array#each_index" do
2
10
  before :each do
3
11
  ScratchPad.record []
@@ -27,4 +35,6 @@ describe "Array#each_index" do
27
35
  a.each_index { |i| ScratchPad << i }
28
36
  ScratchPad.recorded.should == [0]
29
37
  end
30
- end
38
+
39
+ it_behaves_like :enumeratorize, :each_index
40
+ end
@@ -1,3 +1,11 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/enumeratorize', __FILE__)
4
+
5
+ # Modifying a collection while the contents are being iterated
6
+ # gives undefined behavior. See
7
+ # http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/23633
8
+
1
9
  describe "Array#each" do
2
10
  it "yields each element to the block" do
3
11
  a = []
@@ -6,7 +14,17 @@ describe "Array#each" do
6
14
  a.should == [1, 2, 3]
7
15
  end
8
16
 
9
- it "returns an Enumerator if no block given" do
10
- [1, 2].each.should be_kind_of(Enumerator)
17
+ it "yields each element to a block that takes multiple arguments" do
18
+ a = [[1, 2], :a, [3, 4]]
19
+ b = []
20
+
21
+ a.each { |x, y| b << x }
22
+ b.should == [1, :a, 3]
23
+
24
+ b = []
25
+ a.each { |x, y| b << y }
26
+ b.should == [2, nil, 4]
11
27
  end
28
+
29
+ it_behaves_like :enumeratorize, :each
12
30
  end
@@ -1,7 +1,10 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#empty?" do
2
5
  it "returns true if the array has no elements" do
3
6
  [].empty?.should == true
4
7
  [1].empty?.should == false
5
8
  [1, 2].empty?.should == false
6
9
  end
7
- end
10
+ end
@@ -0,0 +1,14 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/eql', __FILE__)
4
+
5
+ # Do not use #should_receive(:eql?) mocks in these specs
6
+ # because MSpec uses Hash for mocks and Hash calls #eql?.
7
+
8
+ describe "Array#eql?" do
9
+ it_behaves_like :array_eql, :eql?
10
+
11
+ it "returns false if any corresponding elements are not #eql?" do
12
+ [1, 2, 3, 4].send(@method, [1, 2, 3, 4.0]).should be_false
13
+ end
14
+ 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#fetch" do
2
5
  it "returns the element at the passed index" do
3
6
  [1, 2, 3].fetch(1).should == 2
@@ -8,7 +11,7 @@ describe "Array#fetch" do
8
11
  [1, 2, 3, 4].fetch(-1).should == 4
9
12
  end
10
13
 
11
- it "raises an IndexError id there is no element at index" do
14
+ it "raises an IndexError if there is no element at index" do
12
15
  lambda { [1, 2, 3].fetch(3) }.should raise_error(IndexError)
13
16
  lambda { [1, 2, 3].fetch(-4) }.should raise_error(IndexError)
14
17
  lambda { [].fetch(0) }.should raise_error(IndexError)
@@ -20,4 +23,30 @@ describe "Array#fetch" do
20
23
  [1, 2, 3].fetch(-4, :not_found).should == :not_found
21
24
  [nil].fetch(0, :not_found).should == nil
22
25
  end
23
- end
26
+
27
+ it "returns the value of block if there is no element at index if passed a block" do
28
+ [1, 2, 3].fetch(9) { |i| i * i }.should == 81
29
+ [1, 2, 3].fetch(-9) { |i| i * i }.should == 81
30
+ end
31
+
32
+ it "passes the original index argument object to the block, not the converted Integer" do
33
+ o = mock('5')
34
+ def o.to_int(); 5; end
35
+
36
+ [1, 2, 3].fetch(o) { |i| i }.should equal(o)
37
+ end
38
+
39
+ it "gives precedence to the default block over the default argument" do
40
+ [1, 2, 3].fetch(9, :foo) { |i| i * i }.should == 81
41
+ end
42
+
43
+ it "tries to convert the passed argument to an Integer using #to_int" do
44
+ obj = mock('to_int')
45
+ obj.should_receive(:to_int).and_return(2)
46
+ ["a", "b", "c"].fetch(obj).should == "c"
47
+ end
48
+
49
+ it "raises a TypeError when the passed argument can't be coerced to Integer" do
50
+ lambda { [].fetch("cat") }.should raise_error(TypeError)
51
+ end
52
+ end
@@ -0,0 +1,8 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../shared/index', __FILE__)
3
+
4
+ describe "Array#find_index" do
5
+ ruby_version_is "1.8.7" do
6
+ it_behaves_like :array_index, :find_index
7
+ end
8
+ end
@@ -1,6 +1,9 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#first" do
2
5
  it "returns the first element" do
3
- %w[a b c].first.should == 'a'
6
+ %w{a b c}.first.should == 'a'
4
7
  [nil].first.should == nil
5
8
  end
6
9
 
@@ -26,18 +29,52 @@ describe "Array#first" do
26
29
  [1, 2, 3, 4, 5].first(1).should == [1]
27
30
  end
28
31
 
32
+ it "raises an ArgumentError when count is negative" do
33
+ lambda { [1, 2].first(-1) }.should raise_error(ArgumentError)
34
+ end
35
+
29
36
  it "returns the entire array when count > length" do
30
37
  [1, 2, 3, 4, 5, 9].first(10).should == [1, 2, 3, 4, 5, 9]
31
38
  end
32
39
 
33
40
  it "returns an array which is independent to the original when passed count" do
34
41
  ary = [1, 2, 3, 4, 5]
35
- ary.first(0).replace([1, 2])
36
- [1, 2, 3, 4, 5].should == ary
37
- ary.first(1).replace([1, 2])
38
- [1, 2, 3, 4, 5].should == ary
39
- ary.first(6).replace([1, 2])
40
- [1, 2, 3, 4, 5].should == ary
42
+ ary.first(0).replace([1,2])
43
+ ary.should == [1, 2, 3, 4, 5]
44
+ ary.first(1).replace([1,2])
45
+ ary.should == [1, 2, 3, 4, 5]
46
+ ary.first(6).replace([1,2])
47
+ ary.should == [1, 2, 3, 4, 5]
48
+ end
49
+
50
+ it "properly handles recursive arrays" do
51
+ empty = ArraySpecs.empty_recursive_array
52
+ empty.first.should equal(empty)
53
+
54
+ ary = ArraySpecs.head_recursive_array
55
+ ary.first.should equal(ary)
56
+ end
57
+
58
+ it "tries to convert the passed argument to an Integer using #to_int" do
59
+ obj = mock('to_int')
60
+ obj.should_receive(:to_int).and_return(2)
61
+ [1, 2, 3, 4, 5].first(obj).should == [1, 2]
62
+ end
63
+
64
+ it "raises a TypeError if the passed argument is not numeric" do
65
+ lambda { [1,2].first(nil) }.should raise_error(TypeError)
66
+ lambda { [1,2].first("a") }.should raise_error(TypeError)
67
+
68
+ obj = mock("nonnumeric")
69
+ lambda { [1,2].first(obj) }.should raise_error(TypeError)
70
+ end
71
+
72
+ it "does not return subclass instance when passed count on Array subclasses" do
73
+ ArraySpecs::MyArray[].first(0).should be_kind_of(Array)
74
+ ArraySpecs::MyArray[].first(2).should be_kind_of(Array)
75
+ ArraySpecs::MyArray[1, 2, 3].first(0).should be_kind_of(Array)
76
+ ArraySpecs::MyArray[1, 2, 3].first(1).should be_kind_of(Array)
77
+ ArraySpecs::MyArray[1, 2, 3].first(2).should be_kind_of(Array)
41
78
  end
42
79
 
43
80
  it "is not destructive" do
@@ -49,4 +86,4 @@ describe "Array#first" do
49
86
  a.first(3)
50
87
  a.should == [1, 2, 3]
51
88
  end
52
- end
89
+ end
@@ -0,0 +1,538 @@
1
+ class Object
2
+ # This helper is defined here rather than in MSpec because
3
+ # it is only used in #pack specs.
4
+ def pack_format(count=nil, repeat=nil)
5
+ format = "#{instance_variable_get(:@method)}#{count}"
6
+ format *= repeat if repeat
7
+ format
8
+ end
9
+ end
10
+
11
+ module ArraySpecs
12
+ SampleRange = 0..1000
13
+ SampleCount = 1000
14
+
15
+ not_compliant_on :rubinius do
16
+ def self.max_32bit_size
17
+ 2**32/4
18
+ end
19
+
20
+ def self.max_64bit_size
21
+ 2**64/8
22
+ end
23
+ end if false
24
+
25
+ deviates_on :rubinius do
26
+ def self.max_32bit_size
27
+ 2**30-1
28
+ end
29
+
30
+ def self.max_64bit_size
31
+ 2**62-1
32
+ end
33
+ end if false
34
+
35
+ def self.frozen_array
36
+ frozen_array = [1,2,3]
37
+ frozen_array.freeze
38
+ frozen_array
39
+ end
40
+
41
+ def self.empty_frozen_array
42
+ frozen_array = []
43
+ frozen_array.freeze
44
+ frozen_array
45
+ end
46
+
47
+ def self.recursive_array
48
+ a = [1, 'two', 3.0]
49
+ 5.times { a << a }
50
+ a
51
+ end
52
+
53
+ def self.head_recursive_array
54
+ a = []
55
+ 5.times { a << a }
56
+ a << 1 << 'two' << 3.0
57
+ a
58
+ end
59
+
60
+ def self.empty_recursive_array
61
+ a = []
62
+ a << a
63
+ a
64
+ end
65
+
66
+ class MyArray < Array
67
+ # The #initialize method has a different signature than Array to help
68
+ # catch places in the specs that do not assert the #initialize is not
69
+ # called when Array methods make new instances.
70
+ def initialize(a, b)
71
+ self << a << b
72
+ ScratchPad.record :my_array_initialize
73
+ end
74
+ end
75
+
76
+ class Sexp < Array
77
+ def initialize(*args)
78
+ super(args)
79
+ end
80
+ end
81
+
82
+ # TODO: replace specs that use this with #should_not_receive(:to_ary)
83
+ # expectations on regular objects (e.g. Array instances).
84
+ class ToAryArray < Array
85
+ def to_ary() ["to_ary", "was", "called!"] end
86
+ end
87
+
88
+ class MyRange < Range; end
89
+
90
+ class AssocKey
91
+ def ==(other); other == 'it'; end
92
+ end
93
+
94
+ class D
95
+ def <=>(obj)
96
+ return 4 <=> obj unless obj.class == D
97
+ 0
98
+ end
99
+ end
100
+
101
+ class SubArray < Array
102
+ def initialize(*args)
103
+ ScratchPad.record args
104
+ end
105
+ end
106
+
107
+ class ArrayConvertable
108
+ attr_accessor :called
109
+ def initialize(*values, &block)
110
+ @values = values;
111
+ end
112
+
113
+ def to_a
114
+ self.called = :to_a
115
+ @values
116
+ end
117
+
118
+ def to_ary
119
+ self.called = :to_ary
120
+ @values
121
+ end
122
+ end
123
+
124
+ class SortSame
125
+ def <=>(other); 0; end
126
+ def ==(other); true; end
127
+ end
128
+
129
+ class UFOSceptic
130
+ def <=>(other); raise "N-uh, UFO:s do not exist!"; end
131
+ end
132
+
133
+ class MockForCompared
134
+ @@count = 0
135
+ @@compared = false
136
+ def initialize
137
+ @@compared = false
138
+ @order = (@@count += 1)
139
+ end
140
+ def <=>(rhs)
141
+ @@compared = true
142
+ return rhs.order <=> self.order
143
+ end
144
+ def self.compared?
145
+ @@compared
146
+ end
147
+
148
+ protected
149
+ attr_accessor :order
150
+ end
151
+
152
+ class ComparableWithFixnum
153
+ include Comparable
154
+ def initialize(num)
155
+ @num = num
156
+ end
157
+
158
+ def <=>(fixnum)
159
+ @num <=> fixnum
160
+ end
161
+ end
162
+
163
+ class Uncomparable
164
+ def <=>(obj)
165
+ nil
166
+ end
167
+ end
168
+
169
+ def self.universal_pack_object
170
+ obj = mock("string float int")
171
+ obj.stub!(:to_int).and_return(1)
172
+ obj.stub!(:to_str).and_return("1")
173
+ obj.stub!(:to_f).and_return(1.0)
174
+ obj
175
+ end
176
+
177
+ LargeArray = ["test_create_table_with_force_true_does_not_drop_nonexisting_table",
178
+ "test_add_table",
179
+ "assert_difference",
180
+ "assert_operator",
181
+ "instance_variables",
182
+ "class",
183
+ "instance_variable_get",
184
+ "__class__",
185
+ "expects",
186
+ "assert_no_difference",
187
+ "name",
188
+ "assert_blank",
189
+ "assert_not_same",
190
+ "is_a?",
191
+ "test_add_table_with_decimals",
192
+ "test_create_table_with_timestamps_should_create_datetime_columns",
193
+ "assert_present",
194
+ "assert_no_match",
195
+ "__instance_of__",
196
+ "assert_deprecated",
197
+ "assert",
198
+ "assert_throws",
199
+ "kind_of?",
200
+ "try",
201
+ "__instance_variable_get__",
202
+ "object_id",
203
+ "timeout",
204
+ "instance_variable_set",
205
+ "assert_nothing_thrown",
206
+ "__instance_variable_set__",
207
+ "copy_object",
208
+ "test_create_table_with_timestamps_should_create_datetime_columns_with_options",
209
+ "assert_not_deprecated",
210
+ "assert_in_delta",
211
+ "id",
212
+ "copy_metaclass",
213
+ "test_create_table_without_a_block",
214
+ "dup",
215
+ "assert_not_nil",
216
+ "send",
217
+ "__instance_variables__",
218
+ "to_sql",
219
+ "mock",
220
+ "assert_send",
221
+ "instance_variable_defined?",
222
+ "clone",
223
+ "require",
224
+ "test_migrator",
225
+ "__instance_variable_defined_eh__",
226
+ "frozen?",
227
+ "test_add_column_not_null_with_default",
228
+ "freeze",
229
+ "test_migrator_one_up",
230
+ "test_migrator_one_down",
231
+ "singleton_methods",
232
+ "method_exists?",
233
+ "create_fixtures",
234
+ "test_migrator_one_up_one_down",
235
+ "test_native_decimal_insert_manual_vs_automatic",
236
+ "instance_exec",
237
+ "__is_a__",
238
+ "test_migrator_double_up",
239
+ "stub",
240
+ "private_methods",
241
+ "stubs",
242
+ "test_migrator_double_down",
243
+ "fixture_path",
244
+ "private_singleton_methods",
245
+ "stub_everything",
246
+ "test_migrator_one_up_with_exception_and_rollback",
247
+ "sequence",
248
+ "protected_methods",
249
+ "enum_for",
250
+ "test_finds_migrations",
251
+ "run_before_mocha",
252
+ "states",
253
+ "protected_singleton_methods",
254
+ "to_json",
255
+ "instance_values",
256
+ "==",
257
+ "mocha_setup",
258
+ "public_methods",
259
+ "test_finds_pending_migrations",
260
+ "mocha_verify",
261
+ "assert_kind_of",
262
+ "===",
263
+ "=~",
264
+ "test_relative_migrations",
265
+ "mocha_teardown",
266
+ "gem",
267
+ "mocha",
268
+ "test_only_loads_pending_migrations",
269
+ "test_add_column_with_precision_and_scale",
270
+ "require_or_load",
271
+ "eql?",
272
+ "require_dependency",
273
+ "test_native_types",
274
+ "test_target_version_zero_should_run_only_once",
275
+ "extend",
276
+ "to_matcher",
277
+ "unloadable",
278
+ "require_association",
279
+ "hash",
280
+ "__id__",
281
+ "load_dependency",
282
+ "equals",
283
+ "test_migrator_db_has_no_schema_migrations_table",
284
+ "test_migrator_verbosity",
285
+ "kind_of",
286
+ "to_yaml",
287
+ "to_bool",
288
+ "test_migrator_verbosity_off",
289
+ "taint",
290
+ "test_migrator_going_down_due_to_version_target",
291
+ "tainted?",
292
+ "mocha_inspect",
293
+ "test_migrator_rollback",
294
+ "vim",
295
+ "untaint",
296
+ "taguri=",
297
+ "test_migrator_forward",
298
+ "test_schema_migrations_table_name",
299
+ "test_proper_table_name",
300
+ "all_of",
301
+ "test_add_drop_table_with_prefix_and_suffix",
302
+ "_setup_callbacks",
303
+ "setup",
304
+ "Not",
305
+ "test_create_table_with_binary_column",
306
+ "assert_not_equal",
307
+ "enable_warnings",
308
+ "acts_like?",
309
+ "Rational",
310
+ "_removed_setup_callbacks",
311
+ "Table",
312
+ "bind",
313
+ "any_of",
314
+ "__method__",
315
+ "test_migrator_with_duplicates",
316
+ "_teardown_callbacks",
317
+ "method",
318
+ "test_migrator_with_duplicate_names",
319
+ "_removed_teardown_callbacks",
320
+ "any_parameters",
321
+ "test_migrator_with_missing_version_numbers",
322
+ "test_add_remove_single_field_using_string_arguments",
323
+ "test_create_table_with_custom_sequence_name",
324
+ "test_add_remove_single_field_using_symbol_arguments",
325
+ "_one_time_conditions_valid_14?",
326
+ "_one_time_conditions_valid_16?",
327
+ "run_callbacks",
328
+ "anything",
329
+ "silence_warnings",
330
+ "instance_variable_names",
331
+ "_fixture_path",
332
+ "copy_instance_variables_from",
333
+ "fixture_path?",
334
+ "has_entry",
335
+ "__marshal__",
336
+ "_fixture_table_names",
337
+ "__kind_of__",
338
+ "fixture_table_names?",
339
+ "test_add_rename",
340
+ "assert_equal",
341
+ "_fixture_class_names",
342
+ "fixture_class_names?",
343
+ "has_entries",
344
+ "_use_transactional_fixtures",
345
+ "people",
346
+ "test_rename_column_using_symbol_arguments",
347
+ "use_transactional_fixtures?",
348
+ "instance_eval",
349
+ "blank?",
350
+ "with_warnings",
351
+ "__nil__",
352
+ "load",
353
+ "metaclass",
354
+ "_use_instantiated_fixtures",
355
+ "has_key",
356
+ "class_eval",
357
+ "present?",
358
+ "test_rename_column",
359
+ "teardown",
360
+ "use_instantiated_fixtures?",
361
+ "method_name",
362
+ "silence_stderr",
363
+ "presence",
364
+ "test_rename_column_preserves_default_value_not_null",
365
+ "silence_stream",
366
+ "_pre_loaded_fixtures",
367
+ "__metaclass__",
368
+ "__fixnum__",
369
+ "pre_loaded_fixtures?",
370
+ "has_value",
371
+ "suppress",
372
+ "to_yaml_properties",
373
+ "test_rename_nonexistent_column",
374
+ "test_add_index",
375
+ "includes",
376
+ "find_correlate_in",
377
+ "equality_predicate_sql",
378
+ "assert_nothing_raised",
379
+ "let",
380
+ "not_predicate_sql",
381
+ "test_rename_column_with_sql_reserved_word",
382
+ "singleton_class",
383
+ "test_rename_column_with_an_index",
384
+ "display",
385
+ "taguri",
386
+ "to_yaml_style",
387
+ "test_remove_column_with_index",
388
+ "size",
389
+ "current_adapter?",
390
+ "test_remove_column_with_multi_column_index",
391
+ "respond_to?",
392
+ "test_change_type_of_not_null_column",
393
+ "is_a",
394
+ "to_a",
395
+ "test_rename_table_for_sqlite_should_work_with_reserved_words",
396
+ "require_library_or_gem",
397
+ "setup_fixtures",
398
+ "equal?",
399
+ "teardown_fixtures",
400
+ "nil?",
401
+ "fixture_table_names",
402
+ "fixture_class_names",
403
+ "test_create_table_without_id",
404
+ "use_transactional_fixtures",
405
+ "test_add_column_with_primary_key_attribute",
406
+ "repair_validations",
407
+ "use_instantiated_fixtures",
408
+ "instance_of?",
409
+ "test_create_table_adds_id",
410
+ "test_rename_table",
411
+ "pre_loaded_fixtures",
412
+ "to_enum",
413
+ "test_create_table_with_not_null_column",
414
+ "instance_of",
415
+ "test_change_column_nullability",
416
+ "optionally",
417
+ "test_rename_table_with_an_index",
418
+ "run",
419
+ "test_change_column",
420
+ "default_test",
421
+ "assert_raise",
422
+ "test_create_table_with_defaults",
423
+ "assert_nil",
424
+ "flunk",
425
+ "regexp_matches",
426
+ "duplicable?",
427
+ "reset_mocha",
428
+ "stubba_method",
429
+ "filter_backtrace",
430
+ "test_create_table_with_limits",
431
+ "responds_with",
432
+ "stubba_object",
433
+ "test_change_column_with_nil_default",
434
+ "assert_block",
435
+ "__show__",
436
+ "assert_date_from_db",
437
+ "__respond_to_eh__",
438
+ "run_in_transaction?",
439
+ "inspect",
440
+ "assert_sql",
441
+ "test_change_column_with_new_default",
442
+ "yaml_equivalent",
443
+ "build_message",
444
+ "to_s",
445
+ "test_change_column_default",
446
+ "assert_queries",
447
+ "pending",
448
+ "as_json",
449
+ "assert_no_queries",
450
+ "test_change_column_quotes_column_names",
451
+ "assert_match",
452
+ "test_keeping_default_and_notnull_constaint_on_change",
453
+ "methods",
454
+ "connection_allow_concurrency_setup",
455
+ "connection_allow_concurrency_teardown",
456
+ "test_create_table_with_primary_key_prefix_as_table_name_with_underscore",
457
+ "__send__",
458
+ "make_connection",
459
+ "assert_raises",
460
+ "tap",
461
+ "with_kcode",
462
+ "assert_instance_of",
463
+ "test_create_table_with_primary_key_prefix_as_table_name",
464
+ "assert_respond_to",
465
+ "test_change_column_default_to_null",
466
+ "assert_same",
467
+ "__extend__"]
468
+
469
+ LargeTestArraySorted = ["test_add_column_not_null_with_default",
470
+ "test_add_column_with_precision_and_scale",
471
+ "test_add_column_with_primary_key_attribute",
472
+ "test_add_drop_table_with_prefix_and_suffix",
473
+ "test_add_index",
474
+ "test_add_remove_single_field_using_string_arguments",
475
+ "test_add_remove_single_field_using_symbol_arguments",
476
+ "test_add_rename",
477
+ "test_add_table",
478
+ "test_add_table_with_decimals",
479
+ "test_change_column",
480
+ "test_change_column_default",
481
+ "test_change_column_default_to_null",
482
+ "test_change_column_nullability",
483
+ "test_change_column_quotes_column_names",
484
+ "test_change_column_with_new_default",
485
+ "test_change_column_with_nil_default",
486
+ "test_change_type_of_not_null_column",
487
+ "test_create_table_adds_id",
488
+ "test_create_table_with_binary_column",
489
+ "test_create_table_with_custom_sequence_name",
490
+ "test_create_table_with_defaults",
491
+ "test_create_table_with_force_true_does_not_drop_nonexisting_table",
492
+ "test_create_table_with_limits",
493
+ "test_create_table_with_not_null_column",
494
+ "test_create_table_with_primary_key_prefix_as_table_name",
495
+ "test_create_table_with_primary_key_prefix_as_table_name_with_underscore",
496
+ "test_create_table_with_timestamps_should_create_datetime_columns",
497
+ "test_create_table_with_timestamps_should_create_datetime_columns_with_options",
498
+ "test_create_table_without_a_block",
499
+ "test_create_table_without_id",
500
+ "test_finds_migrations",
501
+ "test_finds_pending_migrations",
502
+ "test_keeping_default_and_notnull_constaint_on_change",
503
+ "test_migrator",
504
+ "test_migrator_db_has_no_schema_migrations_table",
505
+ "test_migrator_double_down",
506
+ "test_migrator_double_up",
507
+ "test_migrator_forward",
508
+ "test_migrator_going_down_due_to_version_target",
509
+ "test_migrator_one_down",
510
+ "test_migrator_one_up",
511
+ "test_migrator_one_up_one_down",
512
+ "test_migrator_one_up_with_exception_and_rollback",
513
+ "test_migrator_rollback",
514
+ "test_migrator_verbosity",
515
+ "test_migrator_verbosity_off",
516
+ "test_migrator_with_duplicate_names",
517
+ "test_migrator_with_duplicates",
518
+ "test_migrator_with_missing_version_numbers",
519
+ "test_native_decimal_insert_manual_vs_automatic",
520
+ "test_native_types",
521
+ "test_only_loads_pending_migrations",
522
+ "test_proper_table_name",
523
+ "test_relative_migrations",
524
+ "test_remove_column_with_index",
525
+ "test_remove_column_with_multi_column_index",
526
+ "test_rename_column",
527
+ "test_rename_column_preserves_default_value_not_null",
528
+ "test_rename_column_using_symbol_arguments",
529
+ "test_rename_column_with_an_index",
530
+ "test_rename_column_with_sql_reserved_word",
531
+ "test_rename_nonexistent_column",
532
+ "test_rename_table",
533
+ "test_rename_table_for_sqlite_should_work_with_reserved_words",
534
+ "test_rename_table_with_an_index",
535
+ "test_schema_migrations_table_name",
536
+ "test_target_version_zero_should_run_only_once"]
537
+
538
+ end