opal 0.3.41 → 0.3.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (285) hide show
  1. data/.gitignore +2 -0
  2. data/.travis.yml +3 -0
  3. data/CHANGELOG.md +14 -1
  4. data/Gemfile +2 -5
  5. data/Rakefile +41 -3
  6. data/bin/opal +33 -0
  7. data/lib/opal.rb +2 -12
  8. data/lib/opal/core_ext.rb +5 -0
  9. data/lib/opal/grammar.rb +2207 -2138
  10. data/lib/opal/grammar.y +21 -0
  11. data/lib/opal/grammar_helpers.rb +360 -0
  12. data/lib/opal/lexer.rb +55 -401
  13. data/lib/opal/lexer_scope.rb +28 -0
  14. data/lib/opal/parser.rb +155 -171
  15. data/lib/opal/target_scope.rb +257 -0
  16. data/lib/opal/version.rb +1 -1
  17. data/opal.gemspec +6 -2
  18. data/opal/opal-parser.js.erb +3 -2
  19. data/opal/opal.rb +20 -18
  20. data/opal/opal/array.rb +21 -12
  21. data/opal/opal/basic_object.rb +2 -1
  22. data/opal/opal/boolean.rb +3 -0
  23. data/opal/opal/browser_loader.js +57 -0
  24. data/opal/opal/class.rb +51 -13
  25. data/opal/opal/date.rb +1 -20
  26. data/opal/opal/enumerable.rb +66 -33
  27. data/opal/opal/error.rb +2 -0
  28. data/opal/opal/hash.rb +1 -1
  29. data/opal/opal/kernel.rb +14 -3
  30. data/opal/opal/nil_class.rb +4 -0
  31. data/opal/opal/proc.rb +9 -1
  32. data/opal/opal/racc.rb +2 -2
  33. data/opal/opal/regexp.rb +1 -1
  34. data/opal/opal/runtime.js +14 -4
  35. data/opal/opal/string.rb +21 -4
  36. data/opal/opal/time.rb +27 -0
  37. data/spec/core/array/allocate_spec.rb +7 -1
  38. data/spec/core/array/append_spec.rb +18 -3
  39. data/spec/core/array/array_spec.rb +7 -0
  40. data/spec/core/array/assoc_spec.rb +23 -8
  41. data/spec/core/array/at_spec.rb +23 -3
  42. data/spec/core/array/choice_spec.rb +20 -0
  43. data/spec/core/array/clear_spec.rb +45 -4
  44. data/spec/core/array/combination_spec.rb +55 -0
  45. data/spec/core/array/compact_spec.rb +72 -1
  46. data/spec/core/array/constructor_spec.rb +13 -2
  47. data/spec/core/array/count_spec.rb +15 -7
  48. data/spec/core/array/delete_at_spec.rb +44 -1
  49. data/spec/core/array/delete_if_spec.rb +52 -2
  50. data/spec/core/array/delete_spec.rb +83 -2
  51. data/spec/core/array/drop_spec.rb +24 -16
  52. data/spec/core/array/drop_while_spec.rb +17 -0
  53. data/spec/core/array/each_index_spec.rb +11 -1
  54. data/spec/core/array/each_spec.rb +20 -2
  55. data/spec/core/array/empty_spec.rb +4 -1
  56. data/spec/core/array/eql_spec.rb +14 -0
  57. data/spec/core/array/fetch_spec.rb +31 -2
  58. data/spec/core/array/find_index_spec.rb +8 -0
  59. data/spec/core/array/first_spec.rb +45 -8
  60. data/spec/core/array/fixtures/classes.rb +538 -0
  61. data/spec/core/array/flatten_spec.rb +200 -7
  62. data/spec/core/array/frozen_spec.rb +32 -0
  63. data/spec/core/array/include_spec.rb +16 -1
  64. data/spec/core/array/index_spec.rb +5 -25
  65. data/spec/core/array/insert_spec.rb +37 -3
  66. data/spec/core/array/inspect_spec.rb +6 -12
  67. data/spec/core/array/intersection_spec.rb +55 -4
  68. data/spec/core/array/join_spec.rb +29 -4
  69. data/spec/core/array/keep_if_spec.rb +13 -6
  70. data/spec/core/array/last_spec.rb +35 -1
  71. data/spec/core/array/length_spec.rb +7 -4
  72. data/spec/core/array/map_spec.rb +9 -47
  73. data/spec/core/array/minus_spec.rb +68 -4
  74. data/spec/core/array/multiply_spec.rb +138 -6
  75. data/spec/core/array/new_spec.rb +92 -3
  76. data/spec/core/array/ntimes_spec.rb +26 -0
  77. data/spec/core/array/plus_spec.rb +48 -2
  78. data/spec/core/array/pop_spec.rb +159 -39
  79. data/spec/core/array/push_spec.rb +29 -1
  80. data/spec/core/array/rassoc_spec.rb +31 -2
  81. data/spec/core/array/reject_spec.rb +89 -2
  82. data/spec/core/array/replace_spec.rb +7 -29
  83. data/spec/core/array/reverse_each_spec.rb +25 -1
  84. data/spec/core/array/reverse_spec.rb +53 -1
  85. data/spec/core/array/rindex_spec.rb +55 -5
  86. data/spec/core/array/select_spec.rb +35 -8
  87. data/spec/core/array/shared/collect.rb +0 -0
  88. data/spec/core/array/shared/enumeratorize.rb +12 -0
  89. data/spec/core/array/shared/eql.rb +95 -0
  90. data/spec/core/array/shared/index.rb +37 -0
  91. data/spec/core/array/shared/inspect.rb +3 -0
  92. data/spec/core/array/shared/join.rb +7 -0
  93. data/spec/core/array/shared/keep_if.rb +0 -0
  94. data/spec/core/array/shared/length.rb +0 -0
  95. data/spec/core/array/shared/replace.rb +0 -0
  96. data/spec/core/array/shared/slice.rb +0 -0
  97. data/spec/core/array/shift_spec.rb +132 -23
  98. data/spec/core/array/shuffle_spec.rb +82 -6
  99. data/spec/core/array/size_spec.rb +7 -4
  100. data/spec/core/array/slice_spec.rb +132 -1
  101. data/spec/core/array/sort_spec.rb +263 -14
  102. data/spec/core/array/take_spec.rb +24 -16
  103. data/spec/core/array/take_while_spec.rb +14 -10
  104. data/spec/core/array/to_a_spec.rb +18 -1
  105. data/spec/core/array/to_ary_spec.rb +15 -1
  106. data/spec/core/array/try_convert_spec.rb +39 -2
  107. data/spec/core/array/uniq_spec.rb +148 -3
  108. data/spec/core/array/unshift_spec.rb +36 -1
  109. data/spec/core/array/zip_spec.rb +36 -1
  110. data/spec/core/class/new_spec.rb +8 -6
  111. data/spec/core/enumerable/all_spec.rb +37 -9
  112. data/spec/core/enumerable/any_spec.rb +45 -7
  113. data/spec/core/enumerable/collect_spec.rb +4 -1
  114. data/spec/core/enumerable/count_spec.rb +4 -1
  115. data/spec/core/enumerable/detect_spec.rb +2 -2
  116. data/spec/core/enumerable/drop_spec.rb +4 -1
  117. data/spec/core/enumerable/drop_while_spec.rb +4 -1
  118. data/spec/core/enumerable/each_slice_spec.rb +2 -1
  119. data/spec/core/enumerable/each_with_index_spec.rb +4 -1
  120. data/spec/core/enumerable/each_with_object_spec.rb +4 -1
  121. data/spec/core/enumerable/entries_spec.rb +4 -1
  122. data/spec/core/enumerable/find_all_spec.rb +4 -1
  123. data/spec/core/enumerable/find_index_spec.rb +4 -1
  124. data/spec/core/enumerable/find_spec.rb +5 -2
  125. data/spec/core/enumerable/first_spec.rb +4 -1
  126. data/spec/core/enumerable/fixtures/classes.rb +198 -2
  127. data/spec/core/enumerable/grep_spec.rb +4 -1
  128. data/spec/core/enumerable/take_spec.rb +4 -1
  129. data/spec/core/enumerable/to_a_spec.rb +4 -1
  130. data/spec/core/false/and_spec.rb +11 -0
  131. data/spec/core/false/inspect_spec.rb +7 -0
  132. data/spec/core/false/or_spec.rb +11 -0
  133. data/spec/core/false/to_s_spec.rb +7 -0
  134. data/spec/core/false/xor_spec.rb +11 -0
  135. data/spec/core/kernel/rand_spec.rb +5 -5
  136. data/spec/core/module/const_get_spec.rb +4 -4
  137. data/spec/core/module/fixtures/classes.rb +434 -0
  138. data/spec/core/module/method_defined_spec.rb +49 -0
  139. data/spec/core/module/module_function_spec.rb +28 -0
  140. data/spec/core/nil/and_spec.rb +3 -1
  141. data/spec/core/nil/dup_spec.rb +7 -0
  142. data/spec/core/nil/inspect_spec.rb +3 -1
  143. data/spec/core/nil/nil_spec.rb +3 -1
  144. data/spec/core/nil/or_spec.rb +4 -2
  145. data/spec/core/nil/to_a_spec.rb +3 -1
  146. data/spec/core/nil/to_f_spec.rb +3 -1
  147. data/spec/core/nil/to_i_spec.rb +3 -1
  148. data/spec/core/nil/to_s_spec.rb +3 -1
  149. data/spec/core/nil/xor_spec.rb +4 -2
  150. data/spec/core/string/element_reference_spec.rb +14 -1
  151. data/spec/core/string/fixtures/classes.rb +0 -0
  152. data/spec/core/true/and_spec.rb +11 -0
  153. data/spec/core/true/inspect_spec.rb +7 -0
  154. data/spec/core/true/or_spec.rb +11 -0
  155. data/spec/core/true/to_s_spec.rb +7 -0
  156. data/spec/core/true/xor_spec.rb +11 -0
  157. data/spec/{core → core_ext}/array/element_reference_spec.rb +0 -0
  158. data/spec/{core → core_ext}/array/equal_value_spec.rb +0 -0
  159. data/spec/{core → core_ext}/array/fill_spec.rb +0 -0
  160. data/spec/{core → core_ext}/array/reduce_spec.rb +0 -0
  161. data/spec/core_ext/basic_object/send_spec.rb +3 -3
  162. data/spec/{core → core_ext}/boolean/singleton_class_spec.rb +0 -0
  163. data/spec/{core → core_ext}/boolean/to_json_spec.rb +0 -0
  164. data/spec/core_ext/class/_inherited_spec.rb +3 -3
  165. data/spec/core_ext/class/proc_methods_spec.rb +2 -2
  166. data/spec/core_ext/class/singleton_methods_spec.rb +8 -8
  167. data/spec/core_ext/method_missing_spec.rb +3 -3
  168. data/spec/core_ext/native/method_missing_spec.rb +3 -2
  169. data/spec/core_ext/native/to_native_spec.rb +3 -2
  170. data/spec/{core → core_ext}/nil/to_json_spec.rb +0 -0
  171. data/spec/date.rb +0 -0
  172. data/spec/fileutils.rb +0 -0
  173. data/spec/filters/ancestors.rb +4 -0
  174. data/spec/filters/array_delete.rb +3 -0
  175. data/spec/filters/array_fetch.rb +3 -0
  176. data/spec/filters/array_first.rb +3 -0
  177. data/spec/filters/array_flatten.rb +14 -0
  178. data/spec/filters/array_intersection.rb +5 -0
  179. data/spec/filters/array_join.rb +6 -0
  180. data/spec/filters/array_subclasses.rb +4 -0
  181. data/spec/filters/block_args.rb +3 -0
  182. data/spec/filters/coerce_integer.rb +9 -0
  183. data/spec/filters/frozen.rb +4 -0
  184. data/spec/filters/mocks.rb +3 -0
  185. data/spec/filters/should_receive.rb +4 -0
  186. data/spec/filters/tainted.rb +7 -0
  187. data/spec/fixtures/class.rb +124 -0
  188. data/spec/fixtures/class_variables.rb +0 -0
  189. data/spec/fixtures/constants.rb +0 -0
  190. data/spec/grammar/alias_spec.rb +1 -1
  191. data/spec/grammar/def_spec.rb +1 -0
  192. data/spec/grammar/lvar_spec.rb +1 -2
  193. data/spec/grammar/nth_ref_spec.rb +13 -0
  194. data/spec/grammar/sclass_spec.rb +6 -7
  195. data/spec/grammar/str_spec.rb +4 -4
  196. data/spec/grammar/string_spec.rb +8 -0
  197. data/spec/grammar/xstr_spec.rb +4 -4
  198. data/spec/iconv.rb +0 -0
  199. data/spec/language/alias_spec.rb +140 -3
  200. data/spec/language/and_spec.rb +14 -7
  201. data/spec/language/array_spec.rb +57 -5
  202. data/spec/language/block_spec.rb +466 -49
  203. data/spec/language/break_spec.rb +294 -44
  204. data/spec/language/case_spec.rb +151 -3
  205. data/spec/language/class_spec.rb +196 -0
  206. data/spec/language/class_variable_spec.rb +56 -0
  207. data/spec/language/def_spec.rb +507 -4
  208. data/spec/language/defined_spec.rb +19 -7
  209. data/spec/language/ensure_spec.rb +26 -39
  210. data/spec/language/execution_spec.rb +15 -0
  211. data/spec/language/fixtures/array.rb +11 -0
  212. data/spec/language/fixtures/block.rb +57 -0
  213. data/spec/language/fixtures/break.rb +240 -0
  214. data/spec/language/fixtures/ensure.rb +72 -0
  215. data/spec/language/fixtures/literal_lambda.rb +7 -0
  216. data/spec/language/fixtures/metaclass.rb +33 -0
  217. data/spec/language/fixtures/module.rb +24 -0
  218. data/spec/language/fixtures/next.rb +78 -12
  219. data/spec/language/fixtures/return.rb +118 -0
  220. data/spec/language/fixtures/send.rb +110 -0
  221. data/spec/language/fixtures/send_1.9.rb +22 -0
  222. data/spec/language/fixtures/super.rb +308 -0
  223. data/spec/language/fixtures/variables.rb +58 -0
  224. data/spec/language/fixtures/yield.rb +5 -0
  225. data/spec/language/for_spec.rb +192 -0
  226. data/spec/language/hash_spec.rb +29 -5
  227. data/spec/language/if_spec.rb +90 -9
  228. data/spec/language/literal_lambda_spec.rb +1 -47
  229. data/spec/language/loop_spec.rb +39 -2
  230. data/spec/language/metaclass_spec.rb +151 -5
  231. data/spec/language/module_spec.rb +56 -0
  232. data/spec/language/next_spec.rb +370 -12
  233. data/spec/language/not_spec.rb +55 -0
  234. data/spec/language/numbers_spec.rb +56 -0
  235. data/spec/language/or_spec.rb +31 -3
  236. data/spec/language/order_spec.rb +79 -0
  237. data/spec/language/precedence_spec.rb +483 -0
  238. data/spec/language/proc_spec.rb +249 -21
  239. data/spec/language/redo_spec.rb +67 -0
  240. data/spec/language/rescue_spec.rb +121 -0
  241. data/spec/language/retry_spec.rb +56 -0
  242. data/spec/language/return_spec.rb +281 -0
  243. data/spec/language/send_spec.rb +141 -48
  244. data/spec/language/singleton_class_spec.rb +1 -1
  245. data/spec/language/string_spec.rb +11 -0
  246. data/spec/language/super_spec.rb +213 -133
  247. data/spec/language/symbol_spec.rb +2 -1
  248. data/spec/language/undef_spec.rb +3 -1
  249. data/spec/language/unless_spec.rb +6 -2
  250. data/spec/language/until_spec.rb +102 -3
  251. data/spec/language/variables_spec.rb +1212 -16
  252. data/spec/language/versions/array_1.9.rb +39 -0
  253. data/spec/language/versions/case_1.9.rb +20 -0
  254. data/spec/language/versions/hash_1.9.rb +18 -0
  255. data/spec/language/versions/literal_lambda_1.9.rb +143 -0
  256. data/spec/language/versions/not_1.9.rb +22 -0
  257. data/spec/language/versions/send_1.9.rb +241 -0
  258. data/spec/language/versions/symbol_1.9.rb +15 -0
  259. data/spec/language/versions/variables_1.9.rb +8 -0
  260. data/spec/language/while_spec.rb +70 -5
  261. data/spec/language/yield_spec.rb +32 -6
  262. data/spec/mspec/guards/block_device.rb +0 -0
  263. data/spec/mspec/guards/endian.rb +0 -0
  264. data/spec/mspec/helpers/environment.rb +0 -0
  265. data/spec/mspec/helpers/language_version.rb +0 -0
  266. data/spec/mspec/helpers/tmp.rb +0 -0
  267. data/spec/ospec/filter.rb +32 -0
  268. data/spec/ospec/main.rb.erb +18 -0
  269. data/spec/ospec/phantom.rb +97 -0
  270. data/spec/ospec/runner.rb +95 -0
  271. data/spec/ospec/sprockets.js +40 -0
  272. data/spec/pp.rb +3 -0
  273. data/spec/rbconfig.rb +5 -0
  274. data/spec/spec_helper.rb +53 -26
  275. data/spec/yaml.rb +0 -0
  276. metadata +275 -31
  277. data/config.ru +0 -8
  278. data/lib/opal/processor.rb +0 -47
  279. data/lib/opal/scope.rb +0 -236
  280. data/lib/opal/server.rb +0 -94
  281. data/spec/core/boolean/and_spec.rb +0 -17
  282. data/spec/core/boolean/inspect_spec.rb +0 -9
  283. data/spec/core/boolean/or_spec.rb +0 -17
  284. data/spec/core/boolean/to_s_spec.rb +0 -9
  285. data/spec/core/boolean/xor_spec.rb +0 -17
@@ -0,0 +1,56 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The retry statement" do
4
+ pending "re-executes the closest block" do
5
+ retry_first = true
6
+ retry_second = true
7
+ results = []
8
+ begin
9
+ results << 1
10
+ raise
11
+ rescue
12
+ results << 2
13
+ if retry_first
14
+ results << 3
15
+ retry_first = false
16
+ retry
17
+ end
18
+ begin
19
+ results << 4
20
+ raise
21
+ rescue
22
+ results << 5
23
+ if retry_second
24
+ results << 6
25
+ retry_second = false
26
+ retry
27
+ end
28
+ end
29
+ end
30
+
31
+ results.should == [1, 2, 3, 1, 2, 4, 5, 6, 4, 5]
32
+ end
33
+
34
+ ruby_version_is "1.9" do
35
+ it "raises a SyntaxError when used outside of a begin statement" do
36
+ lambda { eval 'retry' }.should raise_error(SyntaxError)
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "The retry keyword inside a begin block's rescue block" do
42
+ pending "causes the begin block to be executed again" do
43
+ counter = 0
44
+
45
+ begin
46
+ counter += 1
47
+ raise "An exception"
48
+ rescue
49
+ retry unless counter == 7
50
+ end
51
+
52
+ counter.should == 7
53
+ end
54
+ end
55
+
56
+ # language_version __FILE__, "retry"
@@ -0,0 +1,281 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/return', __FILE__)
3
+
4
+ describe "The return keyword" do
5
+ it "returns any object directly" do
6
+ def r; return 1; end
7
+ r().should == 1
8
+ end
9
+
10
+ it "returns an single element array directly" do
11
+ def r; return [1]; end
12
+ r().should == [1]
13
+ end
14
+
15
+ it "returns an multi element array directly" do
16
+ def r; return [1,2]; end
17
+ r().should == [1,2]
18
+ end
19
+
20
+ it "returns nil by default" do
21
+ def r; return; end
22
+ r().should be_nil
23
+ end
24
+
25
+ describe "in a Thread" do
26
+ ruby_version_is "" ... "1.9" do
27
+ it "raises a ThreadError if used to exit a thread" do
28
+ lambda { Thread.new { return }.join }.should raise_error(ThreadError)
29
+ end
30
+ end
31
+
32
+ ruby_version_is "1.9" do
33
+ pending "raises a LocalJumpError if used to exit a thread" do
34
+ lambda { Thread.new { return }.join }.should raise_error(LocalJumpError)
35
+ end
36
+ end
37
+ end
38
+
39
+ describe "when passed a splat" do
40
+ ruby_version_is "" ... "1.9" do
41
+ it "returns nil when the ary is empty" do
42
+ def r; ary = []; return *ary; end
43
+ r.should be_nil
44
+ end
45
+ end
46
+
47
+ ruby_version_is "1.9" do
48
+ pending "returns [] when the ary is empty" do
49
+ def r; ary = []; return *ary; end
50
+ r.should == []
51
+ end
52
+ end
53
+
54
+ ruby_version_is "" ... "1.9" do
55
+ it "returns the first element when the array is size of 1" do
56
+ def r; ary = [1]; return *ary; end
57
+ r.should == 1
58
+ end
59
+ end
60
+
61
+ ruby_version_is "1.9" do
62
+ it "returns the array when the array is size of 1" do
63
+ def r; ary = [1]; return *ary; end
64
+ r.should == [1]
65
+ end
66
+ end
67
+
68
+ it "returns the whole array when size is greater than 1" do
69
+ def r; ary = [1,2]; return *ary; end
70
+ r.should == [1,2]
71
+
72
+ def r; ary = [1,2,3]; return *ary; end
73
+ r.should == [1,2,3]
74
+ end
75
+
76
+ ruby_version_is "" ... "1.9" do
77
+ it "returns a non-array when used as a splat" do
78
+ def r; value = 1; return *value; end
79
+ r.should == 1
80
+ end
81
+ end
82
+
83
+ ruby_version_is "1.9" do
84
+ pending "returns an array when used as a splat" do
85
+ def r; value = 1; return *value; end
86
+ r.should == [1]
87
+ end
88
+ end
89
+
90
+
91
+ pending "calls 'to_a' on the splatted value first" do
92
+ def r
93
+ obj = Object.new
94
+ def obj.to_a
95
+ [1,2]
96
+ end
97
+
98
+ return *obj
99
+ end
100
+
101
+ r().should == [1,2]
102
+ end
103
+
104
+ ruby_version_is "" ... "1.9" do
105
+ it "calls 'to_ary' on the splatted value first" do
106
+ def r
107
+ obj = Object.new
108
+ def obj.to_ary
109
+ [1,2]
110
+ end
111
+
112
+ return *obj
113
+ end
114
+
115
+ r().should == [1,2]
116
+ end
117
+ end
118
+ end
119
+
120
+ describe "within a begin" do
121
+ before :each do
122
+ ScratchPad.record []
123
+ end
124
+
125
+ it "executes ensure before returning" do
126
+ def f()
127
+ begin
128
+ ScratchPad << :begin
129
+ return :begin
130
+ ScratchPad << :after_begin
131
+ ensure
132
+ ScratchPad << :ensure
133
+ end
134
+ ScratchPad << :function
135
+ end
136
+ f().should == :begin
137
+ ScratchPad.recorded.should == [:begin, :ensure]
138
+ end
139
+
140
+ it "returns last value returned in ensure" do
141
+ def f()
142
+ begin
143
+ ScratchPad << :begin
144
+ return :begin
145
+ ScratchPad << :after_begin
146
+ ensure
147
+ ScratchPad << :ensure
148
+ return :ensure
149
+ ScratchPad << :after_ensure
150
+ end
151
+ ScratchPad << :function
152
+ end
153
+ f().should == :ensure
154
+ ScratchPad.recorded.should == [:begin, :ensure]
155
+ end
156
+
157
+ pending "executes nested ensures before returning" do
158
+ # def f()
159
+ # begin
160
+ # begin
161
+ # ScratchPad << :inner_begin
162
+ # return :inner_begin
163
+ # ScratchPad << :after_inner_begin
164
+ # ensure
165
+ # ScratchPad << :inner_ensure
166
+ # end
167
+ # ScratchPad << :outer_begin
168
+ # return :outer_begin
169
+ # ScratchPad << :after_outer_begin
170
+ # ensure
171
+ # # ScratchPad << :outer_ensure
172
+ # end
173
+ # ScratchPad << :function
174
+ # end
175
+ # f().should == :inner_begin
176
+ # ScratchPad.recorded.should == [:inner_begin, :inner_ensure, :outer_ensure]
177
+ end
178
+
179
+ pending "returns last value returned in nested ensures" do
180
+ # def f()
181
+ # begin
182
+ # begin
183
+ # ScratchPad << :inner_begin
184
+ # return :inner_begin
185
+ # ScratchPad << :after_inner_begin
186
+ # ensure
187
+ # ScratchPad << :inner_ensure
188
+ # return :inner_ensure
189
+ # ScratchPad << :after_inner_ensure
190
+ # end
191
+ # ScratchPad << :outer_begin
192
+ # return :outer_begin
193
+ # ScratchPad << :after_outer_begin
194
+ # ensure
195
+ # ScratchPad << :outer_ensure
196
+ # return :outer_ensure
197
+ # ScratchPad << :after_outer_ensure
198
+ # end
199
+ # ScratchPad << :function
200
+ # end
201
+ # f().should == :outer_ensure
202
+ # ScratchPad.recorded.should == [:inner_begin, :inner_ensure, :outer_ensure]
203
+ end
204
+
205
+ it "executes the ensure clause when begin/ensure are inside a lambda" do
206
+ lambda do
207
+ begin
208
+ return
209
+ ensure
210
+ ScratchPad.recorded << :ensure
211
+ end
212
+ end.call
213
+ ScratchPad.recorded.should == [:ensure]
214
+ end
215
+ end
216
+
217
+ describe "within a block" do
218
+ before :each do
219
+ ScratchPad.clear
220
+ end
221
+
222
+ ruby_version_is "" ... "1.9" do
223
+ it "raises a LocalJumpError if there is no lexicaly enclosing method" do
224
+ def f; yield; end
225
+ lambda { f { return 5 } }.should raise_error(LocalJumpError)
226
+ end
227
+ end
228
+
229
+ it "causes lambda to return nil if invoked without any arguments" do
230
+ lambda { return; 456 }.call.should be_nil
231
+ end
232
+
233
+ it "causes lambda to return nil if invoked with an empty expression" do
234
+ lambda { return (); 456 }.call.should be_nil
235
+ end
236
+
237
+ it "causes lambda to return the value passed to return" do
238
+ lambda { return 123; 456 }.call.should == 123
239
+ end
240
+
241
+ pending "causes the method that lexically encloses the block to return" do
242
+ ReturnSpecs::Blocks.new.enclosing_method.should == :return_value
243
+ ScratchPad.recorded.should == :before_return
244
+ end
245
+
246
+ pending "returns from the lexically enclosing method even in case of chained calls" do
247
+ ReturnSpecs::NestedCalls.new.enclosing_method.should == :return_value
248
+ ScratchPad.recorded.should == :before_return
249
+ end
250
+
251
+ pending "returns from the lexically enclosing method even in case of chained calls(in yield)" do
252
+ ReturnSpecs::NestedBlocks.new.enclosing_method.should == :return_value
253
+ ScratchPad.recorded.should == :before_return
254
+ end
255
+
256
+ pending "causes the method to return even when the immediate parent has already returned" do
257
+ ReturnSpecs::SavedInnerBlock.new.start.should == :return_value
258
+ ScratchPad.recorded.should == :before_return
259
+ end
260
+
261
+ end
262
+
263
+ describe "within two blocks" do
264
+ pending "causes the method that lexically encloses the block to return" do
265
+ def f
266
+ 1.times { 1.times {return true}; false}; false
267
+ end
268
+ f.should be_true
269
+ end
270
+ end
271
+
272
+ describe "within define_method" do
273
+ pending "goes through the method via a closure" do
274
+ ReturnSpecs::ThroughDefineMethod.new.outer.should == :good
275
+ end
276
+
277
+ it "stops at the method when the return is used directly" do
278
+ ReturnSpecs::DefineMethod.new.outer.should == :good
279
+ end
280
+ end
281
+ end
@@ -1,39 +1,16 @@
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
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+ require File.expand_path('../fixtures/send', __FILE__)
3
+ require 'language/fixtures/send'
4
+
5
+ # Why so many fixed arg tests? JRuby and I assume other Ruby impls have
6
+ # separate call paths for simple fixed arity methods. Testing up to five
7
+ # will verify special and generic arity code paths for all impls.
8
+ #
9
+ # Method naming conventions:
10
+ # M - Manditory Args
11
+ # O - Optional Arg
12
+ # R - Rest Arg
13
+ # Q - Post Manditory Args (1.9)
37
14
 
38
15
  specs = LangSendSpecs
39
16
 
@@ -43,16 +20,27 @@ describe "Invoking a method" do
43
20
  specs.fooM0.should == 100
44
21
  end
45
22
 
23
+ it "raises ArgumentError if the method has a positive arity" do
24
+ lambda {
25
+ specs.fooM1
26
+ }.should raise_error(ArgumentError)
27
+ end
46
28
  end
47
29
 
48
30
  describe "with only manditory arguments" do
49
- it "requires exactly thr same number of passed values" do
31
+ it "requires exactly the same number of passed values" do
50
32
  specs.fooM1(1).should == [1]
51
33
  specs.fooM2(1,2).should == [1,2]
52
34
  specs.fooM3(1,2,3).should == [1,2,3]
53
35
  specs.fooM4(1,2,3,4).should == [1,2,3,4]
54
36
  specs.fooM5(1,2,3,4,5).should == [1,2,3,4,5]
55
37
  end
38
+
39
+ it "raises ArgumentError if the methods arity doesn't match" do
40
+ lambda {
41
+ specs.fooM1(1,2)
42
+ }.should raise_error(ArgumentError)
43
+ end
56
44
  end
57
45
 
58
46
  describe "with optional arguments" do
@@ -63,11 +51,29 @@ describe "Invoking a method" do
63
51
  it "uses the passed argument if available" do
64
52
  specs.fooM0O1(2).should == [2]
65
53
  end
54
+
55
+ pending "raises ArgumentError if extra arguments are passed" do
56
+ lambda {
57
+ specs.fooM0O1(2,3)
58
+ }.should raise_error(ArgumentError)
59
+ end
66
60
  end
67
61
 
68
62
  describe "with manditory and optional arguments" do
69
63
  it "uses the passed values in left to right order" do
70
- specs.fooM1O1(2).should == [2, 1]
64
+ specs.fooM1O1(2).should == [2,1]
65
+ end
66
+
67
+ it "raises an ArgumentError if there are no values for the manditory args" do
68
+ lambda {
69
+ specs.fooM1O1
70
+ }.should raise_error(ArgumentError)
71
+ end
72
+
73
+ pending "raises an ArgumentError if too many values are passed" do
74
+ lambda {
75
+ specs.fooM1O1(1,2,3)
76
+ }.should raise_error(ArgumentError)
71
77
  end
72
78
  end
73
79
 
@@ -83,7 +89,7 @@ describe "Invoking a method" do
83
89
  end
84
90
  end
85
91
 
86
- it "with a block makes it available to yield" do
92
+ pending "with a block makes it available to yield" do
87
93
  specs.oneb(10) { 200 }.should == [10,200]
88
94
  end
89
95
 
@@ -93,7 +99,7 @@ describe "Invoking a method" do
93
99
  prc.call.should == "hello"
94
100
  end
95
101
 
96
- it "with an object as a block used 'to_proc' for coercion" do
102
+ pending "with an object as a block uses 'to_proc' for coercion" do
97
103
  o = LangSendSpecs::ToProc.new(:from_to_proc)
98
104
 
99
105
  specs.makeproc(&o).call.should == :from_to_proc
@@ -101,6 +107,12 @@ describe "Invoking a method" do
101
107
  specs.yield_now(&o).should == :from_to_proc
102
108
  end
103
109
 
110
+ pending "raises a SyntaxError with both a literal block and an object as block" do
111
+ lambda {
112
+ eval "specs.oneb(10, &l){ 42 }"
113
+ }.should raise_error(SyntaxError)
114
+ end
115
+
104
116
  it "with same names as existing variables is ok" do
105
117
  foobar = 100
106
118
 
@@ -115,20 +127,101 @@ describe "Invoking a method" do
115
127
  specs.fooM3(*a).should == [1,2,3]
116
128
  end
117
129
 
118
- it "without parantheses works" do
130
+ it "without parentheses works" do
119
131
  (specs.fooM3 1,2,3).should == [1,2,3]
120
132
  end
121
133
 
122
- it "passes literal hashes without curly braces as the last parameter" do
123
- specs.fooM3('abc', 456, 'rbx' => 'cool', 'specs' => 'fail sometimes', 'oh' => 'weh').should == ['abc', 456, {'rbx' => 'cool', 'specs' => 'fail sometimes', 'oh' => 'weh'}]
134
+ it "with a space separating method name and parenthesis treats expression in parenthesis as first argument" do
135
+ specs.weird_parens().should == "55"
124
136
  end
125
137
 
126
- it "passes a literal hash without curly braces or parens" do
127
- (specs.fooM3 'abc', 456, 'rbx' => 'cool', 'specs' => 'fail sometimes', 'oh' => 'weh').should == ['abc', 456, {'rbx' => 'cool', 'specs' => 'fail sometimes', 'oh' => 'weh'}]
138
+ ruby_version_is "" ... "1.9" do
139
+ describe "allows []=" do
140
+ before :each do
141
+ @obj = LangSendSpecs::AttrSet.new
142
+ end
143
+
144
+ it "with *args in the [] expanded to individual arguments" do
145
+ ary = [2,3]
146
+ (@obj[1, *ary] = 4).should == 4
147
+ @obj.result.should == [1,2,3,4]
148
+ end
149
+
150
+ it "with multiple *args" do
151
+ ary = [2,3]
152
+ post = [4,5]
153
+ (@obj[1, *ary] = *post).should == [4,5]
154
+ @obj.result.should == [1,2,3,[4,5]]
155
+ end
156
+
157
+ it "with multiple *args and unwraps the last splat" do
158
+ ary = [2,3]
159
+ post = [4]
160
+ (@obj[1, *ary] = *post).should == 4
161
+ @obj.result.should == [1,2,3,4]
162
+ end
163
+
164
+ it "with a *args and multiple rhs args" do
165
+ ary = [2,3]
166
+ (@obj[1, *ary] = 4, 5).should == [4,5]
167
+ @obj.result.should == [1,2,3,[4,5]]
168
+ end
169
+ end
170
+ end
171
+
172
+ pending "passes literal hashes without curly braces as the last parameter" do
173
+ #specs.fooM3('abc', 456, 'rbx' => 'cool',
174
+ # 'specs' => 'fail sometimes', 'oh' => 'weh').should == \
175
+ # ['abc', 456, {'rbx' => 'cool', 'specs' => 'fail sometimes', 'oh' => 'weh'}]
176
+ end
177
+
178
+ pending "passes a literal hash without curly braces or parens" do
179
+ #(specs.fooM3 'abc', 456, 'rbx' => 'cool',
180
+ # 'specs' => 'fail sometimes', 'oh' => 'weh').should == \
181
+ # ['abc', 456, { 'rbx' => 'cool', 'specs' => 'fail sometimes', 'oh' => 'weh'}]
128
182
  end
129
183
 
130
184
  it "allows to literal hashes without curly braces as the only parameter" do
131
- specs.fooM1(:rbx => :cool, :specs => :fail_sometimes).should == [{ :rbx => :cool, :specs => :fail_sometimes }]
132
- (specs.fooM1 :rbx => :cool, :specs => :fail_sometimes).should == [{ :rbx => :cool, :specs => :fail_sometimes }]
185
+ specs.fooM1(:rbx => :cool, :specs => :fail_sometimes).should ==
186
+ [{ :rbx => :cool, :specs => :fail_sometimes }]
187
+
188
+ (specs.fooM1 :rbx => :cool, :specs => :fail_sometimes).should ==
189
+ [{ :rbx => :cool, :specs => :fail_sometimes }]
133
190
  end
134
- end
191
+
192
+ describe "when the method is not available" do
193
+ it "invokes method_missing" do
194
+ o = LangSendSpecs::MethodMissing.new
195
+ o.not_there(1,2)
196
+ o.message.should == :not_there
197
+ o.args.should == [1,2]
198
+ end
199
+ end
200
+
201
+ end
202
+
203
+ describe "Invoking a private setter method" do
204
+ describe "permits self as a receiver" do
205
+ it "for normal assignment" do
206
+ receiver = LangSendSpecs::PrivateSetter.new
207
+ receiver.call_self_foo_equals(42)
208
+ receiver.foo.should == 42
209
+ end
210
+
211
+ pending "for multiple assignment" do
212
+ receiver = LangSendSpecs::PrivateSetter.new
213
+ receiver.call_self_foo_equals_masgn(42)
214
+ receiver.foo.should == 42
215
+ end
216
+ end
217
+ end
218
+
219
+ describe "Invoking a private getter method" do
220
+ pending "does not permit self as a receiver" do
221
+ receiver = LangSendSpecs::PrivateGetter.new
222
+ lambda { receiver.call_self_foo }.should raise_error(NoMethodError)
223
+ lambda { receiver.call_self_foo_or_equals(6) }.should raise_error(NoMethodError)
224
+ end
225
+ end
226
+
227
+ # language_version __FILE__, "send"