opal 0.3.11 → 0.3.15

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 (282) hide show
  1. data/.gitignore +13 -0
  2. data/Gemfile +10 -0
  3. data/LICENSE +20 -0
  4. data/README.md +11 -116
  5. data/Rakefile +126 -0
  6. data/bin/opal +1 -2
  7. data/docs/spec_runner.html +16 -0
  8. data/index.html +434 -0
  9. data/lib/opal.rb +14 -15
  10. data/lib/opal/builder.rb +46 -148
  11. data/lib/opal/command.rb +45 -115
  12. data/lib/opal/context.rb +139 -78
  13. data/lib/opal/dependency_builder.rb +34 -0
  14. data/lib/opal/environment.rb +92 -0
  15. data/lib/opal/parser/grammar.rb +4915 -0
  16. data/lib/opal/{parser.y → parser/grammar.y} +430 -284
  17. data/lib/opal/parser/lexer.rb +1329 -0
  18. data/lib/opal/parser/parser.rb +1460 -0
  19. data/lib/opal/parser/scope.rb +140 -0
  20. data/lib/opal/parser/sexp.rb +17 -0
  21. data/lib/opal/version.rb +2 -1
  22. data/opal.gemspec +23 -0
  23. data/opal.js +3149 -4162
  24. data/runtime/README.md +25 -0
  25. data/runtime/corelib/alpha.rb +10 -0
  26. data/runtime/corelib/array.rb +962 -0
  27. data/runtime/corelib/basic_object.rb +66 -0
  28. data/runtime/corelib/boolean.rb +44 -0
  29. data/runtime/corelib/class.rb +43 -0
  30. data/runtime/corelib/comparable.rb +25 -0
  31. data/runtime/corelib/complex.rb +2 -0
  32. data/runtime/corelib/dir.rb +29 -0
  33. data/runtime/corelib/enumerable.rb +316 -0
  34. data/runtime/corelib/enumerator.rb +80 -0
  35. data/runtime/corelib/error.rb +25 -0
  36. data/runtime/corelib/file.rb +80 -0
  37. data/runtime/corelib/hash.rb +503 -0
  38. data/runtime/corelib/io.rb +44 -0
  39. data/runtime/corelib/kernel.rb +237 -0
  40. data/runtime/corelib/load_order +29 -0
  41. data/runtime/corelib/match_data.rb +37 -0
  42. data/runtime/corelib/module.rb +171 -0
  43. data/runtime/corelib/native.rb +50 -0
  44. data/runtime/corelib/nil_class.rb +47 -0
  45. data/runtime/corelib/numeric.rb +219 -0
  46. data/runtime/corelib/object.rb +21 -0
  47. data/runtime/corelib/proc.rb +42 -0
  48. data/runtime/corelib/range.rb +38 -0
  49. data/runtime/corelib/rational.rb +16 -0
  50. data/runtime/corelib/regexp.rb +63 -0
  51. data/runtime/corelib/string.rb +185 -0
  52. data/runtime/corelib/struct.rb +97 -0
  53. data/runtime/corelib/time.rb +196 -0
  54. data/runtime/corelib/top_self.rb +7 -0
  55. data/runtime/gemlib/alpha.rb +5 -0
  56. data/runtime/gemlib/kernel.rb +17 -0
  57. data/runtime/gemlib/load_order +2 -0
  58. data/runtime/kernel/class.js +256 -0
  59. data/runtime/kernel/debug.js +42 -0
  60. data/runtime/kernel/init.js +114 -0
  61. data/runtime/kernel/load_order +5 -0
  62. data/runtime/kernel/loader.js +151 -0
  63. data/runtime/kernel/runtime.js +414 -0
  64. data/runtime/spec/README.md +34 -0
  65. data/runtime/spec/core/array/allocate_spec.rb +15 -0
  66. data/runtime/spec/core/array/append_spec.rb +31 -0
  67. data/runtime/spec/core/array/assoc_spec.rb +29 -0
  68. data/runtime/spec/core/array/at_spec.rb +38 -0
  69. data/runtime/spec/core/array/clear_spec.rb +22 -0
  70. data/runtime/spec/core/array/collect_spec.rb +3 -0
  71. data/runtime/spec/core/array/compact_spec.rb +42 -0
  72. data/runtime/spec/core/array/concat_spec.rb +21 -0
  73. data/runtime/spec/core/array/constructor_spec.rb +24 -0
  74. data/runtime/spec/core/array/count_spec.rb +11 -0
  75. data/runtime/spec/core/array/delete_at_spec.rb +31 -0
  76. data/runtime/spec/core/array/delete_if_spec.rb +24 -0
  77. data/runtime/spec/core/array/delete_spec.rb +26 -0
  78. data/runtime/spec/core/array/each_index_spec.rb +33 -0
  79. data/runtime/spec/core/array/each_spec.rb +11 -0
  80. data/runtime/spec/core/array/element_reference_spec.rb +136 -0
  81. data/runtime/spec/core/array/element_set_spec.rb +7 -0
  82. data/runtime/spec/core/array/empty_spec.rb +10 -0
  83. data/runtime/spec/core/array/eql_spec.rb +3 -0
  84. data/runtime/spec/core/array/equal_value_spec.rb +3 -0
  85. data/runtime/spec/core/array/fetch_spec.rb +26 -0
  86. data/runtime/spec/core/array/first_spec.rb +54 -0
  87. data/runtime/spec/core/array/fixtures/classes.rb +8 -0
  88. data/runtime/spec/core/array/flatten_spec.rb +41 -0
  89. data/runtime/spec/core/array/include_spec.rb +20 -0
  90. data/runtime/spec/core/array/insert_spec.rb +59 -0
  91. data/runtime/spec/core/array/last_spec.rb +57 -0
  92. data/runtime/spec/core/array/length_spec.rb +3 -0
  93. data/runtime/spec/core/array/map_spec.rb +3 -0
  94. data/runtime/spec/core/array/plus_spec.rb +16 -0
  95. data/runtime/spec/core/array/pop_spec.rb +79 -0
  96. data/runtime/spec/core/array/push_spec.rb +19 -0
  97. data/runtime/spec/core/array/rassoc_spec.rb +12 -0
  98. data/runtime/spec/core/array/reject_spec.rb +54 -0
  99. data/runtime/spec/core/array/replace_spec.rb +3 -0
  100. data/runtime/spec/core/array/reverse_each_spec.rb +18 -0
  101. data/runtime/spec/core/array/reverse_spec.rb +9 -0
  102. data/runtime/spec/core/array/shared/collect.rb +53 -0
  103. data/runtime/spec/core/array/shared/eql.rb +19 -0
  104. data/runtime/spec/core/array/shared/length.rb +6 -0
  105. data/runtime/spec/core/array/shared/replace.rb +31 -0
  106. data/runtime/spec/core/class/new_spec.rb +19 -0
  107. data/runtime/spec/core/enumerable/all_spec.rb +102 -0
  108. data/runtime/spec/core/enumerable/any_spec.rb +115 -0
  109. data/runtime/spec/core/enumerable/collect_spec.rb +3 -0
  110. data/runtime/spec/core/enumerable/count_spec.rb +29 -0
  111. data/runtime/spec/core/enumerable/detect_spec.rb +3 -0
  112. data/runtime/spec/core/enumerable/find_spec.rb +3 -0
  113. data/runtime/spec/core/enumerable/fixtures/classes.rb +26 -0
  114. data/runtime/spec/core/enumerable/shared/collect.rb +12 -0
  115. data/runtime/spec/core/enumerable/shared/entries.rb +7 -0
  116. data/runtime/spec/core/enumerable/shared/find.rb +49 -0
  117. data/runtime/spec/core/enumerable/to_a_spec.rb +7 -0
  118. data/runtime/spec/core/false/and_spec.rb +11 -0
  119. data/runtime/spec/core/false/inspect_spec.rb +7 -0
  120. data/runtime/spec/core/false/or_spec.rb +11 -0
  121. data/runtime/spec/core/false/to_s_spec.rb +7 -0
  122. data/runtime/spec/core/false/xor_spec.rb +11 -0
  123. data/runtime/spec/core/hash/allocate_spec.rb +15 -0
  124. data/runtime/spec/core/hash/assoc_spec.rb +29 -0
  125. data/runtime/spec/core/hash/clear_spec.rb +21 -0
  126. data/runtime/spec/core/hash/clone_spec.rb +12 -0
  127. data/runtime/spec/core/hash/default_spec.rb +6 -0
  128. data/runtime/spec/core/hash/delete_if_spec.rb +15 -0
  129. data/runtime/spec/core/hash/element_reference_spec.rb +16 -0
  130. data/runtime/spec/core/hash/element_set_spec.rb +8 -0
  131. data/runtime/spec/core/hash/new_spec.rb +13 -0
  132. data/runtime/spec/core/matchdata/to_a_spec.rb +7 -0
  133. data/runtime/spec/core/nil/and_spec.rb +12 -0
  134. data/runtime/spec/core/nil/inspect_spec.rb +8 -0
  135. data/runtime/spec/core/nil/nil_spec.rb +8 -0
  136. data/runtime/spec/core/nil/or_spec.rb +12 -0
  137. data/runtime/spec/core/nil/to_a_spec.rb +8 -0
  138. data/runtime/spec/core/nil/to_f_spec.rb +12 -0
  139. data/runtime/spec/core/nil/to_i_spec.rb +12 -0
  140. data/runtime/spec/core/nil/to_s_spec.rb +8 -0
  141. data/runtime/spec/core/nil/xor_spec.rb +12 -0
  142. data/runtime/spec/core/numeric/equal_value_spec.rb +11 -0
  143. data/runtime/spec/core/object/is_a_spec.rb +2 -0
  144. data/runtime/spec/core/object/shared/kind_of.rb +0 -0
  145. data/runtime/spec/core/regexp/match_spec.rb +23 -0
  146. data/runtime/spec/core/regexp/shared/match.rb +11 -0
  147. data/runtime/spec/core/symbol/to_proc_spec.rb +8 -0
  148. data/runtime/spec/core/true/and_spec.rb +11 -0
  149. data/runtime/spec/core/true/inspect_spec.rb +7 -0
  150. data/runtime/spec/core/true/or_spec.rb +11 -0
  151. data/runtime/spec/core/true/to_s_spec.rb +7 -0
  152. data/runtime/spec/core/true/xor_spec.rb +11 -0
  153. data/runtime/spec/language/alias_spec.rb +25 -0
  154. data/runtime/spec/language/and_spec.rb +62 -0
  155. data/runtime/spec/language/array_spec.rb +68 -0
  156. data/runtime/spec/language/block_spec.rb +105 -0
  157. data/runtime/spec/language/break_spec.rb +49 -0
  158. data/runtime/spec/language/case_spec.rb +165 -0
  159. data/runtime/spec/language/defined_spec.rb +80 -0
  160. data/runtime/spec/language/ensure_spec.rb +82 -0
  161. data/runtime/spec/language/fixtures/block.rb +19 -0
  162. data/runtime/spec/language/fixtures/break.rb +39 -0
  163. data/runtime/spec/language/fixtures/defined.rb +9 -0
  164. data/runtime/spec/language/fixtures/ensure.rb +37 -0
  165. data/runtime/spec/language/fixtures/next.rb +46 -0
  166. data/runtime/spec/language/fixtures/send.rb +36 -0
  167. data/runtime/spec/language/fixtures/super.rb +43 -0
  168. data/runtime/spec/language/hash_spec.rb +43 -0
  169. data/runtime/spec/language/if_spec.rb +278 -0
  170. data/runtime/spec/language/loop_spec.rb +32 -0
  171. data/runtime/spec/language/next_spec.rb +128 -0
  172. data/runtime/spec/language/or_spec.rb +65 -0
  173. data/runtime/spec/language/predefined_spec.rb +21 -0
  174. data/runtime/spec/language/regexp/interpolation_spec.rb +9 -0
  175. data/runtime/spec/language/regexp_spec.rb +7 -0
  176. data/runtime/spec/language/send_spec.rb +105 -0
  177. data/runtime/spec/language/string_spec.rb +4 -0
  178. data/runtime/spec/language/super_spec.rb +18 -0
  179. data/runtime/spec/language/symbol_spec.rb +41 -0
  180. data/runtime/spec/language/undef_spec.rb +16 -0
  181. data/runtime/spec/language/unless_spec.rb +44 -0
  182. data/runtime/spec/language/until_spec.rb +137 -0
  183. data/runtime/spec/language/variables_spec.rb +28 -0
  184. data/runtime/spec/language/versions/hash_1.9.rb +20 -0
  185. data/runtime/spec/language/while_spec.rb +175 -0
  186. data/runtime/spec/library/stringscanner/scan_spec.rb +36 -0
  187. data/runtime/spec/opal/forwardable/def_instance_delegator_spec.rb +49 -0
  188. data/runtime/spec/opal/opal/defined_spec.rb +15 -0
  189. data/runtime/spec/opal/opal/function_spec.rb +11 -0
  190. data/runtime/spec/opal/opal/native_spec.rb +16 -0
  191. data/runtime/spec/opal/opal/null_spec.rb +10 -0
  192. data/runtime/spec/opal/opal/number_spec.rb +11 -0
  193. data/runtime/spec/opal/opal/object_spec.rb +16 -0
  194. data/runtime/spec/opal/opal/string_spec.rb +11 -0
  195. data/runtime/spec/opal/opal/typeof_spec.rb +9 -0
  196. data/runtime/spec/opal/opal/undefined_spec.rb +10 -0
  197. data/runtime/spec/opal/true/case_compare_spec.rb +12 -0
  198. data/runtime/spec/opal/true/class_spec.rb +10 -0
  199. data/runtime/spec/spec_helper.rb +25 -0
  200. data/runtime/stdlib/base64.rb +91 -0
  201. data/runtime/stdlib/date.rb +4 -0
  202. data/{stdlib → runtime/stdlib}/dev.rb +0 -0
  203. data/runtime/stdlib/forwardable.rb +33 -0
  204. data/runtime/stdlib/optparse.rb +0 -0
  205. data/runtime/stdlib/pp.rb +6 -0
  206. data/{stdlib → runtime/stdlib}/racc/parser.rb +0 -0
  207. data/runtime/stdlib/rbconfig.rb +0 -0
  208. data/runtime/stdlib/si.rb +17 -0
  209. data/runtime/stdlib/strscan.rb +53 -0
  210. data/runtime/stdlib/uri.rb +111 -0
  211. data/runtime/stdlib/uri/common.rb +1014 -0
  212. data/runtime/stdlib/uri/ftp.rb +261 -0
  213. data/runtime/stdlib/uri/generic.rb +1599 -0
  214. data/runtime/stdlib/uri/http.rb +106 -0
  215. data/runtime/stdlib/uri/https.rb +22 -0
  216. data/runtime/stdlib/uri/ldap.rb +260 -0
  217. data/runtime/stdlib/uri/ldaps.rb +20 -0
  218. data/runtime/stdlib/uri/mailto.rb +280 -0
  219. data/spec/builder/build_source_spec.rb +52 -0
  220. data/spec/builder/fixtures/build_source/adam.rb +0 -0
  221. data/spec/builder/fixtures/build_source/bar/a.rb +0 -0
  222. data/spec/builder/fixtures/build_source/bar/wow/b.rb +0 -0
  223. data/spec/builder/fixtures/build_source/bar/wow/cow/c.rb +0 -0
  224. data/spec/builder/fixtures/build_source/beynon.rb +0 -0
  225. data/spec/builder/fixtures/build_source/charles.js +0 -0
  226. data/spec/builder/fixtures/build_source/foo/a.rb +0 -0
  227. data/spec/builder/fixtures/build_source/foo/b.rb +0 -0
  228. data/spec/builder/fixtures/build_source/foo/x.js +0 -0
  229. data/spec/builder/fixtures/build_source/foo/y.js +0 -0
  230. data/spec/builder/output_path_spec.rb +50 -0
  231. data/spec/grammar/alias_spec.rb +26 -0
  232. data/spec/grammar/and_spec.rb +13 -0
  233. data/spec/grammar/array_spec.rb +22 -0
  234. data/spec/grammar/attrasgn_spec.rb +28 -0
  235. data/spec/grammar/begin_spec.rb +38 -0
  236. data/spec/grammar/block_spec.rb +12 -0
  237. data/spec/grammar/break_spec.rb +17 -0
  238. data/spec/grammar/call_spec.rb +58 -0
  239. data/spec/grammar/class_spec.rb +35 -0
  240. data/spec/grammar/const_spec.rb +13 -0
  241. data/spec/grammar/cvar_spec.rb +11 -0
  242. data/spec/grammar/def_spec.rb +60 -0
  243. data/spec/grammar/false_spec.rb +17 -0
  244. data/spec/grammar/file_spec.rb +7 -0
  245. data/spec/grammar/gvar_spec.rb +13 -0
  246. data/spec/grammar/hash_spec.rb +17 -0
  247. data/spec/grammar/iasgn_spec.rb +9 -0
  248. data/spec/grammar/if_spec.rb +26 -0
  249. data/spec/grammar/iter_spec.rb +59 -0
  250. data/spec/grammar/ivar_spec.rb +9 -0
  251. data/spec/grammar/lasgn_spec.rb +8 -0
  252. data/spec/grammar/line_spec.rb +8 -0
  253. data/spec/grammar/lvar_spec.rb +38 -0
  254. data/spec/grammar/module_spec.rb +27 -0
  255. data/spec/grammar/nil_spec.rb +17 -0
  256. data/spec/grammar/not_spec.rb +27 -0
  257. data/spec/grammar/op_asgn1_spec.rb +23 -0
  258. data/spec/grammar/op_asgn2_spec.rb +23 -0
  259. data/spec/grammar/or_spec.rb +13 -0
  260. data/spec/grammar/return_spec.rb +17 -0
  261. data/spec/grammar/sclass_spec.rb +20 -0
  262. data/spec/grammar/self_spec.rb +17 -0
  263. data/spec/grammar/str_spec.rb +96 -0
  264. data/spec/grammar/super_spec.rb +20 -0
  265. data/spec/grammar/true_spec.rb +17 -0
  266. data/spec/grammar/undef_spec.rb +15 -0
  267. data/spec/grammar/unless_spec.rb +13 -0
  268. data/spec/grammar/while_spec.rb +15 -0
  269. data/spec/grammar/xstr_spec.rb +116 -0
  270. data/spec/grammar/yield_spec.rb +20 -0
  271. data/spec/spec_helper.rb +9 -0
  272. metadata +346 -21
  273. data/lib/opal/bundle.rb +0 -34
  274. data/lib/opal/lexer.rb +0 -902
  275. data/lib/opal/nodes.rb +0 -2150
  276. data/lib/opal/parser.rb +0 -4894
  277. data/lib/opal/rake/bundle_task.rb +0 -63
  278. data/opal-parser.js +0 -8343
  279. data/stdlib/strscan.rb +0 -52
  280. data/templates/init/Rakefile +0 -7
  281. data/templates/init/index.html +0 -17
  282. data/templates/init/lib/__NAME__.rb +0 -2
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#[]=" do
5
+ it "sets the value of the element at index" do
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#empty?" do
5
+ it "returns true if the array has no elements" do
6
+ [].empty?.should == true
7
+ [1].empty?.should == false
8
+ [1, 2].empty?.should == false
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/eql', __FILE__)
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/eql', __FILE__)
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#fetch" do
5
+ it "returns the element at the passed index" do
6
+ [1, 2, 3].fetch(1).should == 2
7
+ [nil].fetch(0).should == nil
8
+ end
9
+
10
+ it "counts negative indices backwards from end" do
11
+ [1, 2, 3, 4].fetch(-1).should == 4
12
+ end
13
+
14
+ it "raises an IndexError id there is no element at index" do
15
+ lambda { [1, 2, 3].fetch(3) }.should raise_error(IndexError)
16
+ lambda { [1, 2, 3].fetch(-4) }.should raise_error(IndexError)
17
+ lambda { [].fetch(0) }.should raise_error(IndexError)
18
+ end
19
+
20
+ it "returns default if there is no element at index if passed a default value" do
21
+ [1, 2, 3].fetch(5, :not_found).should == :not_found
22
+ [1, 2, 3].fetch(5, nil).should == nil
23
+ [1, 2, 3].fetch(-4, :not_found).should == :not_found
24
+ [nil].fetch(0, :not_found).should == nil
25
+ end
26
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Array#first" do
4
+ it "returns the first element" do
5
+ %w[a b c].first.should == 'a'
6
+ [nil].first.should == nil
7
+ end
8
+
9
+ it "returns nil if self is empty" do
10
+ [].first.should == nil
11
+ end
12
+
13
+ it "returns the first count elements if given a count" do
14
+ [true, false, true, nil, false].first(2).should == [true, false]
15
+ end
16
+
17
+ it "returns an empty array when passed count on an empty array" do
18
+ [].first(0).should == []
19
+ [].first(1).should == []
20
+ [].first(2).should == []
21
+ end
22
+
23
+ it "returns an empty array when passed count == 0" do
24
+ [1, 2, 3, 4, 5].first(0).should == []
25
+ end
26
+
27
+ it "returns an array containing the first element when passed count == 1" do
28
+ [1, 2, 3, 4, 5].first(1).should == [1]
29
+ end
30
+
31
+ it "returns the entire array when count > length" do
32
+ [1, 2, 3, 4, 5, 9].first(10).should == [1, 2, 3, 4, 5, 9]
33
+ end
34
+
35
+ it "returns an array which is independent to the original when passed count" do
36
+ ary = [1, 2, 3, 4, 5]
37
+ ary.first(0).replace([1, 2])
38
+ [1, 2, 3, 4, 5].should == ary
39
+ ary.first(1).replace([1, 2])
40
+ [1, 2, 3, 4, 5].should == ary
41
+ ary.first(6).replace([1, 2])
42
+ [1, 2, 3, 4, 5].should == ary
43
+ end
44
+
45
+ it "is not destructive" do
46
+ a = [1, 2, 3]
47
+ a.first
48
+ a.should == [1, 2, 3]
49
+ a.first(2)
50
+ a.should == [1, 2, 3]
51
+ a.first(3)
52
+ a.should == [1, 2, 3]
53
+ end
54
+ end
@@ -0,0 +1,8 @@
1
+ module ArraySpecs
2
+ class MyArray < Array
3
+ def initialize(a, b)
4
+ self << a << b
5
+ ScratchPad.record :my_array_initialize
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,41 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Array#flatten" do
4
+ it "returns a one-dimensional flattening recursively" do
5
+ [[[1, [2, 3]], [2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []].flatten.should == [1, 2, 3, 2, 3, 4, 4, 5, 5, 1, 2, 3, 4]
6
+ end
7
+
8
+ it "takes an optional argument that determines the level of recursion" do
9
+ [1, 2, [3, [4, 5]]].flatten(1).should == [1, 2, 3, [4, 5]]
10
+ end
11
+
12
+ it "is not destructive" do
13
+ ary = [1, [2, 3]]
14
+ ary.flatten
15
+ ary.should == [1, [2, 3]]
16
+ end
17
+ end
18
+
19
+ describe "Array#flatten!" do
20
+ it "modified array to produce a one-dimensional flattening recursively" do
21
+ a = [[[1, [2, 3]],[2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []]
22
+ a.flatten!
23
+ a.should == [1, 2, 3, 2, 3, 4, 4, 5, 5, 1, 2, 3, 4]
24
+ end
25
+
26
+ it "returns self if made some modifications" do
27
+ a = [[[1, [2, 3]],[2, 3, [4, [4, [5, 5]], [1, 2, 3]]], [4]], []]
28
+ a.flatten!.should equal(a)
29
+ end
30
+
31
+ it "returns nil if no modifications took place" do
32
+ a = [1, 2, 3]
33
+ a.flatten!.should == nil
34
+ a = [1, [2, 3]]
35
+ a.flatten!.should_not == nil
36
+ end
37
+
38
+ it "takes an optional argument that determines the level of recursion" do
39
+ [1, 2, [3, [4, 5]]].flatten!(1).should == [1, 2, 3, [4, 5]]
40
+ end
41
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Array#include?" do
4
+ it "returns true if object is present, false otherwise" do
5
+ [1, 2, "a", "b"].include?("c").should == false
6
+ [1, 2, "a", "b"].include?("a").should == true
7
+ end
8
+
9
+ it "determines presence by using element == obj" do
10
+ o = mock('')
11
+
12
+ [1, 2, "a", "b"].include?(o).should == false
13
+
14
+ def o.==(other); other == 'a'; end
15
+
16
+ [1, 2, o, "b"].include?('a').should == true
17
+
18
+ [1, 2.0, 3].include?(2).should == true
19
+ end
20
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#insert" do
5
+ it "returns self" do
6
+ ary = []
7
+ ary.insert(0).should equal(ary)
8
+ ary.insert(0, :a).should equal(ary)
9
+ end
10
+
11
+ it "inserts objects before the element at index for non-negative index" do
12
+ ary = []
13
+ ary.insert(0, 3).should == [3]
14
+ ary.insert(0, 1, 2).should == [1, 2, 3]
15
+ ary.insert(0).should == [1, 2, 3]
16
+
17
+ # Let's just assume insert() always modifies the array from now on.
18
+ ary.insert(1, 'a').should == [1, 'a', 2, 3]
19
+ ary.insert(0, 'b').should == ['b', 1, 'a', 2, 3]
20
+ ary.insert(5, 'c').should == ['b', 1, 'a', 2, 3, 'c']
21
+ ary.insert(7, 'd').should == ['b', 1, 'a', 2, 3, 'c', nil, 'd']
22
+ ary.insert(10, 5, 4).should == ['b', 1, 'a', 2, 3, 'c', nil, 'd', nil, nil, 5, 4]
23
+ end
24
+
25
+ it "appends objects to the end of the array for index == -1" do
26
+ [1, 3, 3].insert(-1, 2, 'x', 0.5).should == [1, 3, 3, 2, 'x', 0.5]
27
+ end
28
+
29
+ it "inserts objects after the element at index with negative index" do
30
+ ary = []
31
+ ary.insert(-1, 3).should == [3]
32
+ ary.insert(-2, 2).should == [2, 3]
33
+ ary.insert(-3, 1).should == [1, 2, 3]
34
+ ary.insert(-2, -3).should == [1, 2, -3, 3]
35
+ ary.insert(-1, []).should == [1, 2, -3, 3, []]
36
+ ary.insert(-2, 'x', 'y').should == [1, 2, -3, 3, 'x', 'y', []]
37
+ ary = [1, 2, 3]
38
+ end
39
+
40
+ it "pads with nils if the index to be inserted to is past the end" do
41
+ [].insert(5, 5).should == [nil, nil, nil, nil, nil, 5]
42
+ end
43
+
44
+ it "can insert before the first element with a negative index" do
45
+ [1, 2, 3].insert(-4, -3).should == [-3, 1, 2, 3]
46
+ end
47
+
48
+ it "raises an IndexError if the negative index is out of bounds" do
49
+ lambda { [].insert(-2, 1) }.should raise_error(IndexError)
50
+ lambda { [1].insert(-3, 2) }.should raise_error(IndexError)
51
+ end
52
+
53
+ it "does nothing if no object is passed" do
54
+ [].insert(0).should == []
55
+ [].insert(-1).should == []
56
+ [].insert(10).should == []
57
+ [].insert(-2).should == []
58
+ end
59
+ end
@@ -0,0 +1,57 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#last" do
5
+ it "returns the last element" do
6
+ [1, 1, 1, 1, 2].last.should == 2
7
+ end
8
+
9
+ it "returns nil if self is empty" do
10
+ [].last.should == nil
11
+ end
12
+
13
+ it "returns the last count elements if given a count" do
14
+ [1, 2, 3, 4, 5, 9].last(3).should == [4, 5, 9]
15
+ end
16
+
17
+ it "returns an empty array when passed a count on an empty array" do
18
+ [].last(0).should == []
19
+ [].last(1).should == []
20
+ end
21
+
22
+ it "returns an empty array when count == 0" do
23
+ [1, 2, 3, 4, 5].last(0).should == []
24
+ end
25
+
26
+ it "returns an array containing the last element when passed count == 1" do
27
+ [1, 2, 3, 4, 5].last(1).should == [5]
28
+ end
29
+
30
+ it "raises an ArgumentError when count is negative" do
31
+ lambda { [1, 2].last(-1) }.should raise_error(ArgumentError)
32
+ end
33
+
34
+ it "returns the entire array when count > length" do
35
+ [1, 2, 3, 4, 5, 9].last(10).should == [1, 2, 3, 4, 5, 9]
36
+ end
37
+
38
+ it "returns an array which is independent to the original when passed count" do
39
+ ary = [1, 2, 3, 4, 5]
40
+ ary.last(0).replace([1,2])
41
+ ary.should == [1, 2, 3, 4, 5]
42
+ ary.last(1).replace([1,2])
43
+ ary.should == [1, 2, 3, 4, 5]
44
+ ary.last(6).replace([1,2])
45
+ ary.should == [1, 2, 3, 4, 5]
46
+ end
47
+
48
+ it "is not destructive" do
49
+ a = [1, 2, 3]
50
+ a.last
51
+ a.should == [1, 2, 3]
52
+ a.last(2)
53
+ a.should == [1, 2, 3]
54
+ a.last(3)
55
+ a.should == [1, 2, 3]
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/length', __FILE__)
@@ -0,0 +1,3 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+ require File.expand_path('../shared/collect', __FILE__)
@@ -0,0 +1,16 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#+" do
5
+ it "concatenates two arrays" do
6
+ ([ 1, 2, 3 ] + [ 3, 4, 5 ] ).should == [1, 2, 3, 3, 4, 5]
7
+ ([ 1, 2, 3 ] + []).should == [1, 2, 3]
8
+ ([] + [ 1, 2, 3 ]).should == [1, 2, 3]
9
+ ([] + []).should == []
10
+ end
11
+
12
+ it "can concatenate an array with itself" do
13
+ ary = [1, 2, 3]
14
+ (ary + ary).should == [1, 2, 3, 1, 2, 3]
15
+ end
16
+ end
@@ -0,0 +1,79 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#pop" do
5
+ it "removes and returns the last element of the array" do
6
+ a = ["a", 1, nil, true]
7
+
8
+ a.pop.should == true
9
+ a.should == ["a", 1, nil]
10
+
11
+ a.pop.should == nil
12
+ a.should == ["a", 1]
13
+
14
+ a.pop.should == 1
15
+ a.should == ["a"]
16
+
17
+ a.pop.should == "a"
18
+ a.should == []
19
+ end
20
+
21
+ it "returns nil if there are no more elements" do
22
+ [].pop.should == nil
23
+ end
24
+
25
+ describe "passed a number n as an argument" do
26
+ it "removes and returns an array with the last n elements of the array" do
27
+ a = [1, 2, 3, 4, 5, 6]
28
+
29
+ a.pop(0).should == []
30
+ a.should == [1, 2, 3, 4, 5, 6]
31
+
32
+ a.pop(1).should == [6]
33
+ a.should == [1, 2, 3, 4, 5]
34
+
35
+ a.pop(2).should == [4, 5]
36
+ a.should == [1, 2, 3]
37
+
38
+ a.pop(3).should == [1, 2, 3]
39
+ a.should == []
40
+ end
41
+
42
+ it "returns an array wih the last n elements even if shift was invoked" do
43
+ a = [1, 2, 3, 4]
44
+ a.shift
45
+ a.pop(3).should == [2, 3, 4]
46
+ end
47
+
48
+ it "returns a new empty array if there are no more elmeents" do
49
+ a = []
50
+ popped1 = a.pop(1)
51
+ popped1.should == []
52
+ a.should == []
53
+
54
+ popped2 = a.pop(2)
55
+ popped2.should == []
56
+ a.should == []
57
+
58
+ popped1.should_not equal(popped2)
59
+ end
60
+
61
+ it "returns whole elements if n exceeds size of the array" do
62
+ a = [1, 2, 3, 4, 5]
63
+ a.pop(6).should == [1, 2, 3, 4, 5]
64
+ a.should == []
65
+ end
66
+
67
+ it "does not return self even when it returns whole elements" do
68
+ a = [1, 2, 3, 4, 5]
69
+ a.pop(5).should_not equal(a)
70
+
71
+ a = [1, 2, 3, 4, 5]
72
+ a.pop(6).should_not equal(a)
73
+ end
74
+
75
+ it "raises an ArgumentError if n is negative" do
76
+ lambda { [1, 2, 3].pop(-1) }.should raise_error(ArgumentError)
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#push" do
5
+ it "appends the arguments to the array" do
6
+ a = [ "a", "b", "c" ]
7
+ a.push("d", "e", "f").should equal(a)
8
+ a.push().should == ["a", "b", "c", "d", "e", "f"]
9
+ a.push(5)
10
+ a.should == ["a", "b", "c", "d", "e", "f", 5]
11
+ end
12
+
13
+ it "isn't confused by previous shift" do
14
+ a = [ "a", "b", "c" ]
15
+ a.shift
16
+ a.push("foo")
17
+ a.should == ["b", "c", "foo"]
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#rassoc" do
5
+ it "returns the first contained array whose second element it == object" do
6
+ ary = [[1, "a", 0.5], [2, "b"], [3, "b"], [4, "c"], [], [5], [6, "d"]]
7
+ ary.rassoc("a").should == [1, "a", 0.5]
8
+ ary.rassoc("b").should == [2, "b"]
9
+ ary.rassoc("d").should == [6, "d"]
10
+ ary.rassoc("z").should == nil
11
+ end
12
+ end