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
@@ -1,11 +1,87 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
1
4
  describe "Array#shuffle" do
2
- it "returns an array mixed up" do
3
- ary = [1, 2, 3, 4, 5]
4
- [1, 2, 3, 4, 5].shuffle.size.should eq(ary.size)
5
+ ruby_version_is "1.8.7" do
6
+ it "returns the same values, in a usually different order" do
7
+ a = [1, 2, 3, 4]
8
+ different = false
9
+ 10.times do
10
+ s = a.shuffle
11
+ s.sort.should == a
12
+ different ||= (a != s)
13
+ end
14
+ different.should be_true # Will fail once in a blue moon (4!^10)
15
+ end
16
+
17
+ it "is not destructive" do
18
+ a = [1, 2, 3]
19
+ 10.times do
20
+ a.shuffle
21
+ a.should == [1, 2, 3]
22
+ end
23
+ end
24
+ end
25
+
26
+ ruby_version_is "1.8.7" ... "1.9.3" do
27
+ pending "returns subclass instances with Array subclass" do
28
+ ArraySpecs::MyArray[1, 2, 3].shuffle.should be_an_instance_of(ArraySpecs::MyArray)
29
+ end
5
30
  end
6
31
 
7
- it "returns an array mixed up with UTF-8 characters" do
8
- ary = ["á", "é", "à", "ñÑ", "¥ØŁØ"]
9
- ["á", "é", "à", "ñÑ", "¥ØŁØ"].shuffle.size.should eq(ary.size)
32
+ ruby_version_is "1.9.3" do
33
+ it "does not return subclass instances with Array subclass" do
34
+ ArraySpecs::MyArray[1, 2, 3].shuffle.should be_an_instance_of(Array)
35
+ end
36
+ end
37
+
38
+ ruby_version_is "1.9.3" do
39
+ it "attempts coercion via #to_hash" do
40
+ obj = mock('hash')
41
+ obj.should_receive(:to_hash).once.and_return({})
42
+ [2, 3].shuffle(obj)
43
+ end
44
+
45
+ it "uses default random generator" do
46
+ Kernel.should_receive(:rand).exactly(2).and_return(1, 0)
47
+ [2, 3].shuffle(:random => Object.new).should == [3, 2]
48
+ end
49
+
50
+ it "uses given random generator" do
51
+ random = Random.new
52
+ random.should_receive(:rand).exactly(2).and_return(1, 0)
53
+ [2, 3].shuffle(:random => random).should == [3, 2]
54
+ end
55
+ end
56
+ end
57
+
58
+ describe "Array#shuffle!" do
59
+ ruby_version_is "1.8.7" do
60
+ it "returns the same values, in a usually different order" do
61
+ a = [1, 2, 3, 4]
62
+ original = a
63
+ different = false
64
+ 10.times do
65
+ a = a.shuffle!
66
+ a.sort.should == [1, 2, 3, 4]
67
+ different ||= (a != [1, 2, 3, 4])
68
+ end
69
+ different.should be_true # Will fail once in a blue moon (4!^10)
70
+ a.should equal(original)
71
+ end
72
+
73
+ ruby_version_is ""..."1.9" do
74
+ it "raises a TypeError on a frozen array" do
75
+ lambda { ArraySpecs.frozen_array.shuffle! }.should raise_error(TypeError)
76
+ lambda { ArraySpecs.empty_frozen_array.shuffle! }.should raise_error(TypeError)
77
+ end
78
+ end
79
+
80
+ ruby_version_is "1.9" do
81
+ it "raises a RuntimeError on a frozen array" do
82
+ lambda { ArraySpecs.frozen_array.shuffle! }.should raise_error(RuntimeError)
83
+ lambda { ArraySpecs.empty_frozen_array.shuffle! }.should raise_error(RuntimeError)
84
+ end
85
+ end
10
86
  end
11
87
  end
@@ -1,6 +1,9 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/length', __FILE__)
4
+
1
5
  describe "Array#size" do
2
- it "returns the number of elements" do
3
- [].size.should == 0
4
- [1, 2, 3].size.should == 3
6
+ pending do
7
+ it_behaves_like(:array_length, :size)
5
8
  end
6
- end
9
+ end
@@ -1,3 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/slice', __FILE__)
4
+
1
5
  describe "Array#slice!" do
2
6
  it "removes and return the element at index" do
3
7
  a = [1, 2, 3, 4]
@@ -34,4 +38,131 @@ describe "Array#slice!" do
34
38
  a.slice!(0, 4).should == []
35
39
  a.should == []
36
40
  end
37
- end
41
+
42
+ pending "properly handles recursive arrays" do
43
+ empty = ArraySpecs.empty_recursive_array
44
+ empty.slice(0).should == empty
45
+
46
+ array = ArraySpecs.recursive_array
47
+ array.slice(4).should == array
48
+ array.slice(0..3).should == [1, 'two', 3.0, array]
49
+ end
50
+
51
+ pending "calls to_int on start and length arguments" do
52
+ obj = mock('2')
53
+ def obj.to_int() 2 end
54
+
55
+ a = [1, 2, 3, 4, 5]
56
+ a.slice!(obj).should == 3
57
+ a.should == [1, 2, 4, 5]
58
+ a.slice!(obj, obj).should == [4, 5]
59
+ a.should == [1, 2]
60
+ a.slice!(0, obj).should == [1, 2]
61
+ a.should == []
62
+ end
63
+
64
+ pending "removes and return elements in range" do
65
+ a = [1, 2, 3, 4, 5, 6, 7, 8]
66
+ a.slice!(1..4).should == [2, 3, 4, 5]
67
+ a.should == [1, 6, 7, 8]
68
+ a.slice!(1...3).should == [6, 7]
69
+ a.should == [1, 8]
70
+ a.slice!(-1..-1).should == [8]
71
+ a.should == [1]
72
+ a.slice!(0...0).should == []
73
+ a.should == [1]
74
+ a.slice!(0..0).should == [1]
75
+ a.should == []
76
+
77
+ a = [1,2,3]
78
+ a.slice!(0..3).should == [1,2,3]
79
+ a.should == []
80
+ end
81
+
82
+ pending "calls to_int on range arguments" do
83
+ from = mock('from')
84
+ to = mock('to')
85
+
86
+ # So we can construct a range out of them...
87
+ def from.<=>(o) 0 end
88
+ def to.<=>(o) 0 end
89
+
90
+ def from.to_int() 1 end
91
+ # def to.to_int() -2 end
92
+
93
+ a = [1, 2, 3, 4, 5]
94
+
95
+ a.slice!(from .. to).should == [2, 3, 4]
96
+ a.should == [1, 5]
97
+
98
+ lambda { a.slice!("a" .. "b") }.should raise_error(TypeError)
99
+ lambda { a.slice!(from .. "b") }.should raise_error(TypeError)
100
+ end
101
+
102
+ it "returns last element for consecutive calls at zero index" do
103
+ a = [ 1, 2, 3 ]
104
+ a.slice!(0).should == 1
105
+ a.slice!(0).should == 2
106
+ a.slice!(0).should == 3
107
+ a.should == []
108
+ end
109
+
110
+ ruby_version_is "" ... "1.8.7" do
111
+ # See http://groups.google.com/group/ruby-core-google/t/af70e3d0e9b82f39
112
+ it "expands self when indices are out of bounds" do
113
+ a = [1, 2]
114
+ a.slice!(4).should == nil
115
+ a.should == [1, 2]
116
+ a.slice!(4, 0).should == nil
117
+ a.should == [1, 2, nil, nil]
118
+ a.slice!(6, 1).should == nil
119
+ a.should == [1, 2, nil, nil, nil, nil]
120
+ a.slice!(8...8).should == nil
121
+ a.should == [1, 2, nil, nil, nil, nil, nil, nil]
122
+ a.slice!(10..10).should == nil
123
+ a.should == [1, 2, nil, nil, nil, nil, nil, nil, nil, nil]
124
+ end
125
+ end
126
+
127
+ ruby_version_is "1.8.7" do
128
+ it "does not expand array with indices out of bounds" do
129
+ a = [1, 2]
130
+ a.slice!(4).should == nil
131
+ a.should == [1, 2]
132
+ a.slice!(4, 0).should == nil
133
+ a.should == [1, 2]
134
+ a.slice!(6, 1).should == nil
135
+ a.should == [1, 2]
136
+ a.slice!(8...8).should == nil
137
+ a.should == [1, 2]
138
+ a.slice!(10..10).should == nil
139
+ a.should == [1, 2]
140
+ end
141
+
142
+ it "does not expand array with negative indices out of bounds" do
143
+ a = [1, 2]
144
+ a.slice!(-3, 1).should == nil
145
+ a.should == [1, 2]
146
+ a.slice!(-3..2).should == nil
147
+ a.should == [1, 2]
148
+ end
149
+ end
150
+
151
+ ruby_version_is "" ... "1.9" do
152
+ it "raises a TypeError on a frozen array" do
153
+ lambda { ArraySpecs.frozen_array.slice!(0, 0) }.should raise_error(TypeError)
154
+ end
155
+ end
156
+
157
+ ruby_version_is "1.9" do
158
+ it "raises a RuntimeError on a frozen array" do
159
+ lambda { ArraySpecs.frozen_array.slice!(0, 0) }.should raise_error(RuntimeError)
160
+ end
161
+ end
162
+ end
163
+
164
+ describe "Array#slice" do
165
+ pending do
166
+ it_behaves_like(:array_slice, :slice)
167
+ end
168
+ end
@@ -1,22 +1,271 @@
1
- describe "Array#sort and Array#sort!" do
2
- it "return new sorted Array if #sort" do
3
- a = [2, 7, 5, 9]
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#sort" do
5
+ pending "returns a new array sorted based on comparing elements with <=>" do
6
+ a = [1, -2, 3, 9, 1, 5, -5, 1000, -5, 2, -10, 14, 6, 23, 0]
7
+ a.sort.should == [-10, -5, -5, -2, 0, 1, 1, 2, 3, 5, 6, 9, 14, 23, 1000]
8
+ end
9
+
10
+ pending "does not affect the original Array" do
11
+ a = [0, 15, 2, 3, 4, 6, 14, 5, 7, 12, 8, 9, 1, 10, 11, 13]
4
12
  b = a.sort
13
+ a.should == [0, 15, 2, 3, 4, 6, 14, 5, 7, 12, 8, 9, 1, 10, 11, 13]
14
+ b.should == (0..15).to_a
15
+ end
16
+
17
+ pending "sorts already-sorted Arrays" do
18
+ (0..15).to_a.sort.should == (0..15).to_a
19
+ end
20
+
21
+ pending "sorts reverse-sorted Arrays" do
22
+ (0..15).to_a.reverse.sort.should == (0..15).to_a
23
+ end
24
+
25
+ it "sorts Arrays that consist entirely of equal elements" do
26
+ a = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
27
+ a.sort.should == a
28
+ b = Array.new(15).map { ArraySpecs::SortSame.new }
29
+ b.sort.should == b
30
+ end
31
+
32
+ it "sorts Arrays that consist mostly of equal elements" do
33
+ a = [1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
34
+ a.sort.should == [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
35
+ end
36
+
37
+ it "does not return self even if the array would be already sorted" do
38
+ a = [1, 2, 3]
39
+ sorted = a.sort
40
+ sorted.should == a
41
+ sorted.should_not equal(a)
42
+ end
43
+
44
+ pending "properly handles recursive arrays" do
45
+ empty = ArraySpecs.empty_recursive_array
46
+ empty.sort.should == empty
47
+
48
+ array = [[]]; array << array
49
+ array.sort.should == [[], array]
50
+ end
51
+
52
+ pending "uses #<=> of elements in order to sort" do
53
+ a = ArraySpecs::MockForCompared.new
54
+ b = ArraySpecs::MockForCompared.new
55
+ c = ArraySpecs::MockForCompared.new
56
+
57
+ ArraySpecs::MockForCompared.compared?.should == false
58
+ [a, b, c].sort.should == [c, b, a]
59
+ ArraySpecs::MockForCompared.compared?.should == true
60
+ end
5
61
 
6
- b.should == [2, 5, 7, 9]
7
- b.object_id.should_not == a.object_id
62
+ pending "does not deal with exceptions raised by unimplemented or incorrect #<=>" do
63
+ o = Object.new
64
+
65
+ lambda { [o, 1].sort }.should raise_error
66
+ end
67
+
68
+ it "may take a block which is used to determine the order of objects a and b described as -1, 0 or +1" do
69
+ a = [5, 1, 4, 3, 2]
70
+ a.sort.should == [1, 2, 3, 4, 5]
71
+ a.sort {|x, y| y <=> x}.should == [5, 4, 3, 2, 1]
72
+ end
73
+
74
+ pending "raises an error when a given block returns nil" do
75
+ lambda { [1, 2].sort {} }.should raise_error(ArgumentError)
76
+ end
77
+
78
+ it "does not call #<=> on contained objects when invoked with a block" do
79
+ a = Array.new(25)
80
+ (0...25).each {|i| a[i] = ArraySpecs::UFOSceptic.new }
81
+
82
+ a.sort { -1 }.should be_kind_of(Array)
83
+ end
84
+
85
+ it "does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)" do
86
+ a = Array.new(1500)
87
+ (0...1500).each {|i| a[i] = ArraySpecs::UFOSceptic.new }
88
+
89
+ a.sort { -1 }.should be_kind_of(Array)
90
+ end
91
+
92
+ it "completes when supplied a block that always returns the same result" do
93
+ a = [2, 3, 5, 1, 4]
94
+ a.sort { 1 }.should be_kind_of(Array)
95
+ a.sort { 0 }.should be_kind_of(Array)
96
+ a.sort { -1 }.should be_kind_of(Array)
97
+ end
98
+
99
+ pending "does not freezes self during being sorted" do
100
+ a = [1, 2, 3]
101
+ a.sort { |x,y| a.frozen?.should == false; x <=> y }
8
102
  end
9
103
 
10
- it "return same sorted Array if #sort!" do
11
- a = [2, 7, 5, 9]
12
- b = a.sort!
104
+ pending "returns the specified value when it would break in the given block" do
105
+ [1, 2, 3].sort{ break :a }.should == :a
106
+ end
107
+
108
+ it "uses the sign of Bignum block results as the sort result" do
109
+ a = [1, 2, 5, 10, 7, -4, 12]
110
+ begin
111
+ class Bignum;
112
+ alias old_spaceship <=>
113
+ def <=>(other)
114
+ raise
115
+ end
116
+ end
117
+ a.sort {|n, m| (n - m) * (2 ** 200)}.should == [-4, 1, 2, 5, 7, 10, 12]
118
+ ensure
119
+ class Bignum
120
+ alias <=> old_spaceship
121
+ end
122
+ end
123
+ end
124
+
125
+ pending "compares values returned by block with 0" do
126
+ a = [1, 2, 5, 10, 7, -4, 12]
127
+ a.sort { |n, m| n - m }.should == [-4, 1, 2, 5, 7, 10, 12]
128
+ a.sort { |n, m|
129
+ ArraySpecs::ComparableWithFixnum.new(n-m)
130
+ }.should == [-4, 1, 2, 5, 7, 10, 12]
131
+ lambda {
132
+ a.sort { |n, m| (n - m).to_s }
133
+ }.should raise_error(ArgumentError)
134
+ end
135
+
136
+ it "sorts an array that has a value shifted off without a block" do
137
+ a = Array.new(20, 1)
138
+ a.shift
139
+ a[0] = 2
140
+ a.sort.last.should == 2
141
+ end
142
+
143
+ it "sorts an array that has a value shifted off with a block" do
144
+ a = Array.new(20, 1)
145
+ a.shift
146
+ a[0] = 2
147
+ a.sort {|a, b| a <=> b }.last.should == 2
148
+ end
149
+
150
+ pending "raises an error if objects can't be compared" do
151
+ a=[ArraySpecs::Uncomparable.new, ArraySpecs::Uncomparable.new]
152
+ lambda {a.sort}.should raise_error(ArgumentError)
153
+ end
154
+
155
+ # From a strange Rubinius bug
156
+ it "handles a large array that has been pruned" do
157
+ pruned = ArraySpecs::LargeArray.dup.delete_if { |n| n !~ /^test./ }
158
+ pruned.sort.should == ArraySpecs::LargeTestArraySorted
159
+ end
160
+
161
+ ruby_version_is "" ... "1.9.3" do
162
+ pending "returns subclass instance on Array subclasses" do
163
+ ary = ArraySpecs::MyArray[1, 2, 3]
164
+ ary.sort.should be_kind_of(ArraySpecs::MyArray)
165
+ end
166
+ end
167
+
168
+ ruby_version_is "1.9.3" do
169
+ it "does not return subclass instance on Array subclasses" do
170
+ ary = ArraySpecs::MyArray[1, 2, 3]
171
+ ary.sort.should be_kind_of(Array)
172
+ end
173
+ end
174
+ end
175
+
176
+ describe "Array#sort!" do
177
+ pending "sorts array in place using <=>" do
178
+ a = [1, -2, 3, 9, 1, 5, -5, 1000, -5, 2, -10, 14, 6, 23, 0]
179
+ a.sort!
180
+ a.should == [-10, -5, -5, -2, 0, 1, 1, 2, 3, 5, 6, 9, 14, 23, 1000]
181
+ end
182
+
183
+ it "sorts array in place using block value if a block given" do
184
+ a = [0, 15, 2, 3, 4, 6, 14, 5, 7, 12, 8, 9, 1, 10, 11, 13]
185
+ a.sort! { |x, y| y <=> x }.should == (0..15).to_a.reverse
186
+ end
187
+
188
+ it "returns self if the order of elements changed" do
189
+ a = [6, 7, 2, 3, 7]
190
+ a.sort!.should equal(a)
191
+ a.should == [2, 3, 6, 7, 7]
192
+ end
193
+
194
+ it "returns self even if makes no modification" do
195
+ a = [1, 2, 3, 4, 5]
196
+ a.sort!.should equal(a)
197
+ a.should == [1, 2, 3, 4, 5]
198
+ end
199
+
200
+ pending "properly handles recursive arrays" do
201
+ empty = ArraySpecs.empty_recursive_array
202
+ empty.sort!.should == empty
203
+
204
+ array = [[]]; array << array
205
+ array.sort!.should == array
206
+ end
207
+
208
+ pending "uses #<=> of elements in order to sort" do
209
+ a = ArraySpecs::MockForCompared.new
210
+ b = ArraySpecs::MockForCompared.new
211
+ c = ArraySpecs::MockForCompared.new
212
+
213
+ ArraySpecs::MockForCompared.compared?.should == false
214
+ [a, b, c].sort!.should == [c, b, a]
215
+ ArraySpecs::MockForCompared.compared?.should == true
216
+ end
217
+
218
+ it "does not call #<=> on contained objects when invoked with a block" do
219
+ a = Array.new(25)
220
+ (0...25).each {|i| a[i] = ArraySpecs::UFOSceptic.new }
221
+
222
+ a.sort! { -1 }.should be_kind_of(Array)
223
+ end
224
+
225
+ it "does not call #<=> on elements when invoked with a block even if Array is large (Rubinius #412)" do
226
+ a = Array.new(1500)
227
+ (0...1500).each {|i| a[i] = ArraySpecs::UFOSceptic.new }
228
+
229
+ a.sort! { -1 }.should be_kind_of(Array)
230
+ end
231
+
232
+ it "completes when supplied a block that always returns the same result" do
233
+ a = [2, 3, 5, 1, 4]
234
+ a.sort!{ 1 }.should be_kind_of(Array)
235
+ a.sort!{ 0 }.should be_kind_of(Array)
236
+ a.sort!{ -1 }.should be_kind_of(Array)
237
+ end
238
+
239
+ ruby_version_is '' ... '1.9' do
240
+ it "raises a TypeError on a frozen array" do
241
+ lambda { ArraySpecs.frozen_array.sort! }.should raise_error(TypeError)
242
+ end
243
+
244
+ not_compliant_on :rubinius do
245
+ it "temporarily freezes self and recovers after sorted" do
246
+ a = [1, 2, 3]
247
+ a.sort! { |x,y| a.frozen?.should == true; x <=> y }
248
+ a.frozen?.should == false
249
+ end
250
+ end
251
+ end
252
+
253
+ ruby_version_is '1.9' do
254
+ it "raises a RuntimeError on a frozen array" do
255
+ lambda { ArraySpecs.frozen_array.sort! }.should raise_error(RuntimeError)
256
+ end
257
+ end
13
258
 
14
- b.should == [2, 5, 7, 9]
15
- b.object_id.should == a.object_id
259
+ pending "returns the specified value when it would break in the given block" do
260
+ [1, 2, 3].sort{ break :a }.should == :a
16
261
  end
17
262
 
18
- it "#sort and #sort! should support sorting functions" do
19
- ["one", "two", "three", "four"].sort {|a , b| a.length <=> b.length }.should == ["one", "two", "four", "three"]
20
- [2, 7, 5, 9].sort! {|a , b| b <=> a }.should == [9, 7, 5, 2]
263
+ it "makes some modification even if finished sorting when it would break in the given block" do
264
+ partially_sorted = (1..5).map{|i|
265
+ ary = [5, 4, 3, 2, 1]
266
+ ary.sort!{|x,y| break if x==i; x<=>y}
267
+ ary
268
+ }
269
+ partially_sorted.any?{|ary| ary != [1, 2, 3, 4, 5]}.should be_true
21
270
  end
22
- end
271
+ end