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,54 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#reject" do
5
+ it "returns a new array without elements for which block is true" do
6
+ ary = [1, 2, 3, 4, 5]
7
+ ary.reject { true }.should == []
8
+ ary.reject { false }.should == ary
9
+ ary.reject { false }.object_id.should_not == ary.object_id
10
+ ary.reject { nil }.should == ary
11
+ ary.reject { nil }.object_id.should_not == ary.object_id
12
+ ary.reject { |i| i < 3 }.should == [3, 4, 5]
13
+ ary.reject { |i| i % 2 == 0 }.should == [1, 3, 5]
14
+ end
15
+
16
+ it "returns self when called on an Array emptied with #shift" do
17
+ array = [1]
18
+ array.shift
19
+ array.reject { |x| true }.should == []
20
+ end
21
+ end
22
+
23
+ describe "Array#reject!" do
24
+ it "removes elements for which block is true" do
25
+ a = [3, 4, 5, 6, 7, 8, 9, 10, 11]
26
+ a.reject! { |i| i % 2 == 0 }.should equal(a)
27
+ a.should == [3, 5, 7, 9, 11]
28
+ a.reject! { |i| i > 8 }
29
+ a.should == [3, 5, 7]
30
+ a.reject! { |i| i < 4 }
31
+ a.should == [5, 7]
32
+ a.reject! { |i| i == 5 }
33
+ a.should == [7]
34
+ a.reject! { true }
35
+ a.should == []
36
+ a.reject! { true }
37
+ a.should == []
38
+ end
39
+
40
+ it "returns nil when called on an array emptied with #shift" do
41
+ array = [1]
42
+ array.shift
43
+ array.reject! { |x| true }.should == nil
44
+ end
45
+
46
+ it "returns nil if no changes are made" do
47
+ a = [1, 2, 3]
48
+
49
+ a.reject! { |i| i < 0 }.should == nil
50
+
51
+ a.reject! { true }
52
+ a.reject! { true }.should == nil
53
+ end
54
+ 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/replace', __FILE__)
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#reverse_each" do
5
+ before :each do
6
+ ScratchPad.record []
7
+ end
8
+
9
+ it "traverses array in reverse order and pass each element to block" do
10
+ [1, 3, 4, 6].reverse_each { |i| ScratchPad << i }
11
+ ScratchPad.recorded.should == [6, 4, 3, 1]
12
+ end
13
+
14
+ it "returns self" do
15
+ a = [:a, :b, :c]
16
+ a.reverse_each { |x| }.should equal(a)
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Array#reverse" do
5
+ it "returns a new array with the elements in reverse order" do
6
+ [].reverse.should == []
7
+ [1, 3, 5, 2].reverse.should == [2, 5, 3, 1]
8
+ end
9
+ end
@@ -0,0 +1,53 @@
1
+ describe "Array#collect" do
2
+ it "returns a copy of array with each element replaced by the value returned by block" do
3
+ a = ['a', 'b', 'c', 'd']
4
+ b = a.collect { |i| i + '!' }
5
+ b.should == ['a!', 'b!', 'c!', 'd!']
6
+ b.object_id.should_not == a.object_id
7
+ end
8
+
9
+ it "does not change self" do
10
+ a = ['a', 'b', 'c', 'd']
11
+ b = a.collect { |i| i + '!' }
12
+ a.should == ['a', 'b', 'c', 'd']
13
+ end
14
+
15
+ it "returns the evaluated value of block if it broke in the block" do
16
+ a = ['a', 'b', 'c', 'd']
17
+ b = a.collect {|i|
18
+ if i == 'c'
19
+ break 0
20
+ else
21
+ i + '!'
22
+ end
23
+ }
24
+ b.should == 0
25
+ end
26
+ end
27
+
28
+ describe "Array#collect!" do
29
+ it "replaces each element with the value returned by block" do
30
+ a = [7, 9, 3, 5]
31
+ a.collect! { |i| i - 1 }.should equal(a)
32
+ a.should == [6, 8, 2, 4]
33
+ end
34
+
35
+ it "returns self" do
36
+ a = [1, 2, 3, 4, 5]
37
+ b = a.collect! {|i| i+1 }
38
+ a.object_id.should == b.object_id
39
+ end
40
+
41
+ it "returns the evaluated value of block but its contents is partially modified, if it broke in the block" do
42
+ a = ['a', 'b', 'c', 'd']
43
+ b = a.collect! {|i|
44
+ if i == 'c'
45
+ break 0
46
+ else
47
+ i + '!'
48
+ end
49
+ }
50
+ b.should == 0
51
+ a.should == ['a!', 'b!', 'c', 'd']
52
+ end
53
+ end
@@ -0,0 +1,19 @@
1
+ describe "Array#==" do
2
+ it "returns true if other is the same array" do
3
+ a = [1]
4
+ (a == a).should be_true
5
+ end
6
+
7
+ it "returns true if corresponding elements are #eql?" do
8
+ ([] == []).should be_true
9
+ ([1, 2, 3, 4] == [1, 2, 3, 4]).should be_true
10
+ end
11
+
12
+ it "returns false if other is shorter than self" do
13
+ ([1, 2, 3, 4] == [1, 2, 3]).should be_false
14
+ end
15
+
16
+ it "returns false if other is longer than self" do
17
+ ([1, 2, 3, 4] == [1, 2, 3, 4, 5]).should be_false
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ describe "Array#length" do
2
+ it "returns the number of elements" do
3
+ [].length.should == 0
4
+ [1, 2, 3].length.should == 3
5
+ end
6
+ end
@@ -0,0 +1,31 @@
1
+ describe "Array#replace" do
2
+ it "replaces the elements with elements from other array" do
3
+ a = [1, 2, 3, 4, 5]
4
+ b = ['a', 'b', 'c']
5
+ a.replace(b).should equal(a)
6
+ a.should == b
7
+ a.should_not equal(b)
8
+
9
+ a.replace([4] * 10)
10
+ a.should == [4] * 10
11
+
12
+ a.replace([])
13
+ a.should == []
14
+ end
15
+
16
+ it "returns self" do
17
+ ary = [1, 2, 3]
18
+ other = [:a, :b, :c]
19
+ ary.replace(other).should equal(ary)
20
+ end
21
+
22
+ it "does not make self dependent to the original array" do
23
+ ary = [1, 2, 3]
24
+ other = [:a, :b, :c]
25
+ ary.replace(other)
26
+ ary.should == [:a, :b, :c]
27
+ ary << :d
28
+ ary.should == [:a, :b, :c, :d]
29
+ other.should == [:a, :b, :c]
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+
3
+ describe "Class.new with a block given" do
4
+ it "uses the given block as the class' body" do
5
+ klass = Class.new do
6
+ def self.message
7
+ "text"
8
+ end
9
+
10
+ def hello
11
+ "hello again"
12
+ end
13
+ end
14
+
15
+ klass.message.should == "text"
16
+ klass.new.hello.should == "hello again"
17
+ end
18
+ end
19
+
@@ -0,0 +1,102 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Enumerable#all?" do
5
+
6
+ before :each do
7
+ @enum = EnumerableSpecs::Numerous.new
8
+ @empty = EnumerableSpecs::Empty.new()
9
+ @enum1 =[0, 1, 2, -1]
10
+ @enum2 = [nil, false, true]
11
+ end
12
+
13
+ it "always returns true on empty enumeration" do
14
+ @empty.all?.should == true
15
+ @empty.all? { nil }.should == true
16
+
17
+ [].all?.should == true
18
+ [].all? { false }.should == true
19
+
20
+ {}.all?.should == true
21
+ {}.all? { nil }.should == true
22
+ end
23
+
24
+ it "does not hide exceptions out of #each" do
25
+ lambda {
26
+ EnumerableSpecs::ThrowingEach.new.all?
27
+ }.should raise_error(RuntimeError)
28
+
29
+ lambda {
30
+ EnumerableSpecs::ThrowingEach.new.all? { false }
31
+ }.should raise_error(RuntimeError)
32
+ end
33
+
34
+ #describe "with no block" do
35
+ it "returns true if no elements are false or nil" do
36
+ @enum.all?.should == true
37
+ @enum1.all?.should == true
38
+ @enum2.all?.should == false
39
+
40
+ EnumerableSpecs::Numerous.new('a','b','c').all?.should == true
41
+ EnumerableSpecs::Numerous.new(0, "x", true).all?.should == true
42
+ end
43
+
44
+ it "returns false if there are false or nil elements" do
45
+ EnumerableSpecs::Numerous.new(false).all?.should == false
46
+ EnumerableSpecs::Numerous.new(false, false).all?.should == false
47
+
48
+ EnumerableSpecs::Numerous.new(nil).all?.should == false
49
+ EnumerableSpecs::Numerous.new(nil, nil).all?.should == false
50
+
51
+ EnumerableSpecs::Numerous.new(1, nil, 2).all?.should == false
52
+ EnumerableSpecs::Numerous.new(0, "x", false, true).all?.should == false
53
+ @enum2.all?.should == false
54
+ end
55
+ #end
56
+
57
+ #describe "with block" do
58
+ it "returns true if the block never returns false or nil" do
59
+ @enum.all? { true }.should == true
60
+ @enum1.all? { |o| o < 5 }.should == true
61
+ @enum1.all? { |o| 5 }.should == true
62
+ end
63
+
64
+ it "returns false if the block ever returns false or nil" do
65
+ @enum.all? { false }.should == false
66
+ @enum.all? { nil }.should == false
67
+ @enum1.all? { |o| o > 2 }.should == false
68
+
69
+ EnumerableSpecs::Numerous.new.all? { |i| i > 5 }.should == false
70
+ EnumerableSpecs::Numerous.new.all? { |i| i == 3 ? nil : true }.should == false
71
+ end
72
+
73
+ it "stops iterating once the return value is determined" do
74
+ yielded = []
75
+ EnumerableSpecs::Numerous.new(:one, :two, :three).all? do |e|
76
+ yielded << e
77
+ false
78
+ end.should == false
79
+ yielded.should == [:one]
80
+
81
+ yielded = []
82
+ EnumerableSpecs::Numerous.new(true, true, false, true).all? do |e|
83
+ yielded << e
84
+ e
85
+ end.should == false
86
+ yielded.should == [true, true, false]
87
+
88
+ yielded = []
89
+ EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).all? do |e|
90
+ yielded << e
91
+ e
92
+ end.should == true
93
+ yielded.should == [1, 2, 3, 4, 5]
94
+ end
95
+
96
+ it "does not hide exceptions out of the block" do
97
+ lambda {
98
+ @enum.all? { raise "from block" }
99
+ }.should raise_error(RuntimeError)
100
+ end
101
+ #end
102
+ end
@@ -0,0 +1,115 @@
1
+ require File.expand_path('../../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/classes', __FILE__)
3
+
4
+ describe "Enumerable#any?" do
5
+ before :each do
6
+ @enum = EnumerableSpecs::Numerous.new
7
+ @empty = EnumerableSpecs::Empty.new()
8
+ @enum1 = [0, 1, 2, -1]
9
+ @enum2 = [nil, false, true]
10
+ end
11
+
12
+ it "always returns false on empty enumeration" do
13
+ @empty.any?.should == false
14
+ @empty.any? { nil }.should == false
15
+
16
+ [].any?.should == false
17
+ [].any? { false }.should == false
18
+
19
+ {}.any?.should == false
20
+ {}.any? { nil }.should == false
21
+ end
22
+
23
+ it "does not hide exceptions out of #each" do
24
+ lambda {
25
+ EnumerableSpecs::ThrowingEach.new.any?
26
+ }.should raise_error(RuntimeError)
27
+
28
+ lambda {
29
+ EnumerableSpecs::ThrowingEach.new.any? { false }
30
+ }.should raise_error(RuntimeError)
31
+ end
32
+
33
+ #describe "with no block" do
34
+ it "returns true if any element is not false or nil" do
35
+ @enum.any?.should == true
36
+ @enum1.any?.should == true
37
+ @enum2.any?.should == true
38
+ EnumerableSpecs::Numerous.new(true).any?.should == true
39
+ EnumerableSpecs::Numerous.new('a','b','c').any?.should == true
40
+ EnumerableSpecs::Numerous.new('a','b','c', nil).any?.should == true
41
+ EnumerableSpecs::Numerous.new(1, nil, 2).any?.should == true
42
+ EnumerableSpecs::Numerous.new(1, false).any?.should == true
43
+ EnumerableSpecs::Numerous.new(false, nil, 1, false).any?.should == true
44
+ EnumerableSpecs::Numerous.new(false, 0, nil).any?.should == true
45
+ end
46
+
47
+ it "returns false if all the elements are false or nil" do
48
+ EnumerableSpecs::Numerous.new(false).any?.should == false
49
+ EnumerableSpecs::Numerous.new(false, false).any?.should == false
50
+ EnumerableSpecs::Numerous.new(nil).any?.should == false
51
+ EnumerableSpecs::Numerous.new(nil, nil).any?.should == false
52
+ EnumerableSpecs::Numerous.new(nil, false, nil).any?.should == false
53
+ end
54
+ #end
55
+
56
+ #describe "with block" do
57
+ it "returns true if the block ever returns other than false or nil" do
58
+ @enum.any? { true } == true
59
+ @enum.any? { 0 } == true
60
+ @enum.any? { 1 } == true
61
+
62
+ @enum1.any? { Object.new } == true
63
+ @enum1.any?{ |o| o < 1 }.should == true
64
+ @enum1.any?{ |o| 5 }.should == true
65
+
66
+ @enum2.any? { |i| i == nil }.should == true
67
+ end
68
+
69
+ it "any? should return false if the block never returns other than false or nil" do
70
+ @enum.any? { false }.should == false
71
+ @enum.any? { false }.should == false
72
+
73
+ @enum1.any?{ |o| o < -10 }.should == false
74
+ @enum1.any?{ |o| nil }.should == false
75
+
76
+ @enum2.any? { |i| i == :stuff }.should == false
77
+ end
78
+
79
+ it "stops iterating once the return value is determined" do
80
+ yielded = []
81
+ EnumerableSpecs::Numerous.new(:one, :two, :three).any? do |e|
82
+ yielded << e
83
+ false
84
+ end.should == false
85
+ yielded.should == [:one, :two, :three]
86
+
87
+ yielded = []
88
+ EnumerableSpecs::Numerous.new(true, true, false, true).any? do |e|
89
+ yielded << e
90
+ e
91
+ end.should == true
92
+ yielded.should == [true]
93
+
94
+ yielded = []
95
+ EnumerableSpecs::Numerous.new(false, nil, false, true, false).any? do |e|
96
+ yielded << e
97
+ e
98
+ end.should == true
99
+ yielded.should == [false, nil, false, true]
100
+
101
+ yielded = []
102
+ EnumerableSpecs::Numerous.new(1, 2, 3, 4, 5).any? do |e|
103
+ yielded << e
104
+ e
105
+ end.should == true
106
+ yielded.should == [1]
107
+ end
108
+
109
+ it "does not hide exceptions out of the block" do
110
+ lambda {
111
+ @enum.any? { raise "from block" }
112
+ }.should raise_error(RuntimeError)
113
+ end
114
+ #end
115
+ end