opal 0.3.11 → 0.3.15

Sign up to get free protection for your applications and to get access to all the features.
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,36 @@
1
+ module LangSendSpecs
2
+
3
+ def self.fooM0; 100; end
4
+ def self.fooM1(a); [a]; end
5
+ def self.fooM2(a,b); [a,b]; end
6
+ def self.fooM3(a,b,c); [a,b,c]; end
7
+ def self.fooM4(a,b,c,d); [a,b,c,d]; end
8
+ def self.fooM5(a,b,c,d,e); [a,b,c,d,e]; end
9
+ def self.fooM0O1(a=1); [a]; end
10
+ def self.fooM1O1(a,b=1); [a,b]; end
11
+ def self.fooM2O1(a,b,c=1); [a,b,c]; end
12
+ def self.fooM3O1(a,b,c,d=1); [a,b,c,d]; end
13
+ def self.fooM4O1(a,b,c,d,e=1); [a,b,c,d,e]; end
14
+ def self.fooM0O2(a=1,b=2); [a,b]; end
15
+ def self.fooM0R(*r); r; end
16
+ def self.fooM1R(a, *r); [a, r]; end
17
+ def self.fooM0O1R(a=1, *r); [a, r]; end
18
+ def self.fooM1O1R(a, b=1, *r); [a, b, r]; end
19
+
20
+ def self.one(a); a; end
21
+ def self.oneb(a,&b); [a,yield(b)]; end
22
+
23
+ def self.makeproc(&b) b end
24
+
25
+ def self.yield_now; yield; end
26
+
27
+ class ToProc
28
+ def initialize(val)
29
+ @val = val
30
+ end
31
+
32
+ def to_proc
33
+ Proc.new { @val }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,43 @@
1
+ module Super
2
+ module S1
3
+ class A
4
+ def foo(a)
5
+ a << "A#foo"
6
+ bar(a)
7
+ end
8
+ def bar(a)
9
+ a << "A#bar"
10
+ end
11
+ end
12
+ class B < A
13
+ def foo(a)
14
+ a << "B#foo"
15
+ super(a)
16
+ end
17
+ def bar(a)
18
+ a << "B#bar"
19
+ super(a)
20
+ end
21
+ end
22
+ end
23
+
24
+ module S2
25
+ class A
26
+ def baz(a)
27
+ a << "A#baz"
28
+ end
29
+ end
30
+ class B < A
31
+ def foo(a)
32
+ a << "B#foo"
33
+ baz(a)
34
+ end
35
+ end
36
+ class C < B
37
+ def baz(a)
38
+ a << "C#baz"
39
+ super(a)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,43 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Hash literal" do
4
+ it "{} should return an empty hash" do
5
+ {}.size.should == 0
6
+ {}.should == {}
7
+ end
8
+
9
+ it "{} should return a new hash populated with the given elements" do
10
+ h = {:a => 'a', 'b' => 3, 44 => 2.3}
11
+ h.size.should == 3
12
+ h.should == {:a => 'a', 'b' => 3, 44 => 2.3}
13
+ end
14
+
15
+ it "treats empty expressions an nils" do
16
+ h = {() => ()}
17
+ h.keys.should == [nil]
18
+ h.values.should == [nil]
19
+ h[nil].should == nil
20
+
21
+ h = {() => :value}
22
+ h.keys.should == [nil]
23
+ h.values.should == [:value]
24
+ h[nil].should == :value
25
+
26
+ h = {:keys => ()}
27
+ h.keys.should == [:keys]
28
+ h.values.should == [nil]
29
+ h[:key].should == nil
30
+ end
31
+
32
+ it "checks duplicated keys on initialization" do
33
+ h = {:foo => :bar, :foo => :foo}
34
+ h.keys.size.should == 1
35
+ h.should == {:foo => :foo}
36
+ end
37
+
38
+ it "accepts a hanging comma" do
39
+ h = {:a => 1, :b => 2,}
40
+ h.size.should == 2
41
+ h.should == {:a => 1, :b => 2}
42
+ end
43
+ end
@@ -0,0 +1,278 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The if expression" do
4
+ it "evaluates body if expression is true" do
5
+ a = []
6
+ if true
7
+ a << 123
8
+ end
9
+ a.should == [123]
10
+ end
11
+
12
+ it "does not evaluate body if expression if false" do
13
+ a = []
14
+ if false
15
+ a << 123
16
+ end
17
+ a.should == []
18
+ end
19
+
20
+ it "does not evaluate body if expression is empty" do
21
+ a = []
22
+ if ()
23
+ a << 123
24
+ end
25
+ a.should == []
26
+ end
27
+
28
+ it "does not evaluate else-body if expression is true" do
29
+ a = []
30
+ if true
31
+ a << 123
32
+ else
33
+ a << 456
34
+ end
35
+ a.should == [123]
36
+ end
37
+
38
+ it "evaluates only else-body if expression is false" do
39
+ a = []
40
+ if false
41
+ a << 123
42
+ else
43
+ a << 456
44
+ end
45
+ a.should == [456]
46
+ end
47
+
48
+ it "returns result of then-body evaluation if expression is true" do
49
+ if true
50
+ 123
51
+ end.should == 123
52
+ end
53
+
54
+ it "returns result of last statement in then-body if expression is true" do
55
+ if true
56
+ 'foo'
57
+ 'bar'
58
+ 'baz'
59
+ end.should == 'baz'
60
+ end
61
+
62
+ it "returns result of then-body evaluation if expression is true and else part is present" do
63
+ if true
64
+ 123
65
+ else
66
+ 456
67
+ end.should == 123
68
+ end
69
+
70
+ it "returns result of else-body evaluation if expression is false" do
71
+ if false
72
+ 123
73
+ else
74
+ 456
75
+ end.should == 456
76
+ end
77
+
78
+ it "returns nil if then-body is empty and expression is true" do
79
+ if true
80
+ end.should == nil
81
+ end
82
+
83
+ it "returns nil if then-body is empty, expression is true and else part is present" do
84
+ if true
85
+ else
86
+ 456
87
+ end.should == nil
88
+ end
89
+
90
+ it "returns nil if then-body is empty, expression is true and else part is empty" do
91
+ if true
92
+ else
93
+ end.should == nil
94
+ end
95
+
96
+ it "returns nil if else-body is empty and expression is false" do
97
+ if false
98
+ 123
99
+ else
100
+ end.should == nil
101
+ end
102
+
103
+ it "returns nil if else-body is empty, expression is false and then-body is empty" do
104
+ if false
105
+ else
106
+ end.should == nil
107
+ end
108
+
109
+ it "considers an expression with nil result as false" do
110
+ if nil
111
+ 123
112
+ else
113
+ 456
114
+ end.should == 456
115
+ end
116
+
117
+ it "considers a non-nil and non-boolean object in expression result as true" do
118
+ if 'x'
119
+ 123
120
+ else
121
+ 456
122
+ end.should == 123
123
+ end
124
+
125
+ it "considers a zero integer in expression result as true" do
126
+ if 0
127
+ 123
128
+ else
129
+ 456
130
+ end.should == 123
131
+ end
132
+
133
+ it "allows starting else-body on the same line" do
134
+ if false
135
+ 123
136
+ else 456
137
+ end.should == 456
138
+ end
139
+
140
+ it "evaluates subsequent elsif statements and executes body of first matching" do
141
+ if false
142
+ 123
143
+ elsif false
144
+ 234
145
+ elsif true
146
+ 345
147
+ elsif true
148
+ 456
149
+ end.should == 345
150
+ end
151
+
152
+ it "evaluates else-body if no if/elsif statements match" do
153
+ if false
154
+ 123
155
+ elsif false
156
+ 234
157
+ elsif false
158
+ 345
159
+ else
160
+ 456
161
+ end.should == 456
162
+ end
163
+
164
+ it "allows 'then' after expression when then-body is on the next line" do
165
+ if true then
166
+ 123
167
+ end.should == 123
168
+
169
+ if true then ; 123; end.should == 123
170
+ end
171
+
172
+ it "allows then-body on the same line separated with 'then'" do
173
+ if true then 123
174
+ end.should == 123
175
+
176
+ if true then 123; end.should == 123
177
+ end
178
+
179
+ it "returns nil when then-body on the same line separated with 'then' and expression is false" do
180
+ if false then 123
181
+ end.should == nil
182
+
183
+ if false then 123; end.should == nil
184
+ end
185
+
186
+ it "returns nil when then-body separated by 'then' is empty and expression is true" do
187
+ if true then
188
+ end.should == nil
189
+
190
+ if true then ; end.should == nil
191
+ end
192
+
193
+ it "returns nil when then-body separated by 'then', expression is false and no else part" do
194
+ if false then
195
+ end.should == nil
196
+
197
+ if false then ; end.should == nil
198
+ end
199
+
200
+ it "evaluates then-body when then-body separated by 'then', expression is true and else part is present" do
201
+ if true then 123
202
+ else 456
203
+ end.should == 123
204
+
205
+ if true then 123; else 456; end.should == 123
206
+ end
207
+
208
+ it "evaluates else-body when then-body separated by 'then' and expression is false" do
209
+ if false then 123
210
+ else 456
211
+ end.should == 456
212
+
213
+ if false then 123; else 456; end.should == 456
214
+ end
215
+ end
216
+
217
+ describe "The postfix if form" do
218
+ it "evaluates statement if expression is true" do
219
+ a = []
220
+ a << 123 if true
221
+ a.should == [123]
222
+ end
223
+
224
+ it "does not evaluate statement if expression if false" do
225
+ a = []
226
+ a << 123 if false
227
+ a.should == []
228
+ end
229
+
230
+ it "returns result of expression if value is true" do
231
+ (123 if true).should == 123
232
+ end
233
+
234
+ it "returns nil if value is false" do
235
+ (123 if false).should == nil
236
+ end
237
+
238
+ it "considers a nil expression as false" do
239
+ (123 if nil).should == nil
240
+ end
241
+
242
+ it "considers a non-nil object as true" do
243
+ (123 if 'x').should == 123
244
+ end
245
+
246
+ it "evaluates then-body in containing scope" do
247
+ a = 123
248
+ if true
249
+ b = a+1
250
+ end
251
+ b.should == 124
252
+ end
253
+
254
+ it "evaluates else-body if containing scope" do
255
+ a = 123
256
+ if false
257
+ b = a+1
258
+ else
259
+ b= a+2
260
+ end
261
+ b.should == 125
262
+ end
263
+
264
+ it "evaluates elsif-body in containing scope" do
265
+ a = 123
266
+ if false
267
+ b = a+1
268
+ elsif false
269
+ b = a+2
270
+ elsif true
271
+ b = a+3
272
+ else
273
+ b = a+4
274
+ end
275
+ b.should == 126
276
+ end
277
+ end
278
+
@@ -0,0 +1,32 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The loop expression" do
4
+ it "repeats the given block until a break is called" do
5
+ outer_loop = 0
6
+ loop do
7
+ outer_loop += 1
8
+ break if outer_loop == 10
9
+ end
10
+ outer_loop.should == 10
11
+ end
12
+
13
+ it "executes code in its own scope" do
14
+ loop do
15
+ inner_loop = 123
16
+ break
17
+ end
18
+ lambda { inner_loop }.should raise_error(NameError)
19
+ end
20
+
21
+ it "returns the value passed to break if interrupted by break" do
22
+ loop do
23
+ break 123
24
+ end.should == 123
25
+ end
26
+
27
+ it "returns nil if interrupted by break with no arguments" do
28
+ loop do
29
+ break
30
+ end.should == nil
31
+ end
32
+ end
@@ -0,0 +1,128 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/next', __FILE__)
3
+
4
+ describe "The next statement from within the block" do
5
+ before :each do
6
+ ScratchPad.record []
7
+ end
8
+
9
+ it "ends block execution" do
10
+ a = []
11
+ lambda {
12
+ a << 1
13
+ next
14
+ a << 2
15
+ }.call
16
+ a.should == [1]
17
+ end
18
+
19
+ it "causes block to return nil if invoked without arguments" do
20
+ lambda { 123; next; 456 }.call.should == nil
21
+ end
22
+
23
+ it "causes block to return nil if invoked with an empty expression" do
24
+ lambda { next (); 456 }.call.should be_nil
25
+ end
26
+
27
+ it "returns the argument passed" do
28
+ lambda { 123; next 234; 345 }.call.should == 234
29
+ end
30
+
31
+ it "returns to the invoking method" do
32
+ NextSpecs.yielding_method(nil) { next }.should == :method_return_value
33
+ end
34
+
35
+ it "returns to the invoking method, with the specified value" do
36
+ NextSpecs.yielding_method(nil) {
37
+ next nil;
38
+ fail("next didn't end the block execution")
39
+ }.should == :method_return_value
40
+
41
+ NextSpecs.yielding_method(1) {
42
+ next 1;
43
+ fail("next didn't end the block execution")
44
+ }.should == :method_return_value
45
+
46
+ NextSpecs.yielding_method([1, 2, 3]) {
47
+ next 1, 2, 3;
48
+ fail("next didn't end the block execution")
49
+ }.should == :method_return_value
50
+ end
51
+
52
+ it "returns to the currently yielding method in case of chained calls" do
53
+ class ChainedNextTest
54
+ def self.meth_with_yield(&b)
55
+ yield.should == :next_return_value
56
+ :method_return_value
57
+ end
58
+ def self.invoking_method(&b)
59
+ meth_with_yield(&b)
60
+ end
61
+ def self.enclosing_method
62
+ invoking_method do
63
+ next :next_return_value
64
+ :wrong_return_value
65
+ end
66
+ end
67
+ end
68
+
69
+ ChainedNextTest.enclosing_method.should == :method_return_value
70
+ end
71
+
72
+ it "causes ensure blocks to run" do
73
+ [1].each do |i|
74
+ begin
75
+ ScratchPad << :begin
76
+ next
77
+ ensure
78
+ ScratchPad << :ensure
79
+ end
80
+ end
81
+
82
+ ScratchPad.recorded.should == [:begin, :ensure]
83
+ end
84
+ end
85
+
86
+ describe "The next statement" do
87
+ before :each do
88
+ ScratchPad.record []
89
+ end
90
+
91
+ describe "in a while loop" do
92
+ describe "when not passed an argument" do
93
+ before :each do
94
+ ScratchPad.record []
95
+ end
96
+
97
+ it "causes ensure blocks to run" do
98
+ NextSpecs.while_next(false)
99
+
100
+ ScratchPad.recorded.should == [:begin, :ensure]
101
+ end
102
+
103
+ it "causes ensure blocks to run when nested in an block" do
104
+ NextSpecs.while_within_iter(false)
105
+
106
+ ScratchPad.recorded.should == [:begin, :ensure]
107
+ end
108
+ end
109
+
110
+ describe "when passed an argument" do
111
+ before :each do
112
+ ScratchPad.record []
113
+ end
114
+
115
+ it "causes ensure blocks to run" do
116
+ NextSpecs.while_next(true)
117
+
118
+ ScratchPad.recorded.should == [:begin, :ensure]
119
+ end
120
+
121
+ it "causes ensure blocks to run when nested in an block" do
122
+ NextSpecs.while_within_iter(true)
123
+
124
+ ScratchPad.recorded.should == [:begin, :ensure]
125
+ end
126
+ end
127
+ end
128
+ end