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,52 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ class BuildSourceTest < Opal::Builder
4
+ attr_reader :files
5
+
6
+ def initialize
7
+ @files = []
8
+ end
9
+
10
+ def build_file(base, source)
11
+ @files << [base, source]
12
+ end
13
+ end
14
+
15
+ build_source_dir = File.expand_path '../fixtures/build_source', __FILE__
16
+
17
+ describe "Builder#build_source" do
18
+ it "should only build ruby sources" do
19
+ Dir.chdir(build_source_dir) do
20
+ b = BuildSourceTest.new
21
+ b.build_source '.', 'foo'
22
+
23
+ b.files.size.should == 2
24
+ b.files.include?(['foo', 'a.rb']).should be_true
25
+ b.files.include?(['foo', 'b.rb']).should be_true
26
+ end
27
+ end
28
+
29
+ it "should recursively go into directories to find ruby sources" do
30
+ Dir.chdir(build_source_dir) do
31
+ b = BuildSourceTest.new
32
+ b.build_source '.', 'bar'
33
+
34
+ b.files.size.should == 3
35
+ b.files.include?(['bar', 'a.rb']).should be_true
36
+ b.files.include?(['bar/wow', 'b.rb']).should be_true
37
+ b.files.include?(['bar/wow/cow', 'c.rb']).should be_true
38
+ end
39
+ end
40
+
41
+ it "should be able to handle top level files" do
42
+ Dir.chdir(build_source_dir) do
43
+ b = BuildSourceTest.new
44
+ b.build_source '.', 'adam.rb'
45
+ b.files.should == [['.', 'adam.rb']]
46
+
47
+ b2 = BuildSourceTest.new
48
+ b2.build_source '.', 'charles.js'
49
+ b2.files.should == []
50
+ end
51
+ end
52
+ end
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
@@ -0,0 +1,50 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Builder#output_path" do
4
+ describe "with an output dir" do
5
+ it "should return output dir joined with source when base is '.'" do
6
+ b = Opal::Builder.new '', :out => 'build'
7
+
8
+ b.output_path('.', 'foo.rb').should == 'build/foo.js'
9
+ b.output_path('.', 'bar.rb').should == 'build/bar.js'
10
+ end
11
+
12
+ it "returns the parts joined but with first part of base removed" do
13
+ b = Opal::Builder.new '', :out => 'build'
14
+
15
+ b.output_path('lib', 'foo.rb').should == 'build/foo.js'
16
+ b.output_path('lib/foo', 'bar.rb').should == 'build/foo/bar.js'
17
+ b.output_path('lib/foo/bar', 'baz.rb').should == 'build/foo/bar/baz.js'
18
+ end
19
+ end
20
+
21
+ describe "without an output dir" do
22
+ it "should return base and source joined with .js extname" do
23
+ b = Opal::Builder.new '', :out => ''
24
+ b.output_path('.', 'foo.rb').should == 'foo.js'
25
+ b.output_path('lib', 'foo.rb').should == 'lib/foo.js'
26
+ b.output_path('lib/foo', 'bar.rb').should == 'lib/foo/bar.js'
27
+ end
28
+
29
+ it "supports '.' as output dir" do
30
+ b = Opal::Builder.new '', :out => '.'
31
+ b.output_path('.', 'foo.rb').should == 'foo.js'
32
+ b.output_path('lib', 'foo.rb').should == 'lib/foo.js'
33
+ b.output_path('lib/foo', 'bar.rb').should == 'lib/foo/bar.js'
34
+ end
35
+
36
+ it "supports nil as output dir" do
37
+ b = Opal::Builder.new '', :out => nil
38
+ b.output_path('.', 'foo.rb').should == 'foo.js'
39
+ b.output_path('lib', 'foo.rb').should == 'lib/foo.js'
40
+ b.output_path('lib/foo', 'bar.rb').should == 'lib/foo/bar.js'
41
+ end
42
+
43
+ it "supports false as output dir" do
44
+ b = Opal::Builder.new '', :out => false
45
+ b.output_path('.', 'foo.rb').should == 'foo.js'
46
+ b.output_path('lib', 'foo.rb').should == 'lib/foo.js'
47
+ b.output_path('lib/foo', 'bar.rb').should == 'lib/foo/bar.js'
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The alias keyword" do
4
+ describe "with fitem" do
5
+ it "should return an s(:alias) with s(:lit)" do
6
+ opal_parse("alias a b").should == [:alias, [:lit, :a], [:lit, :b]]
7
+ opal_parse("alias == equals").should == [:alias, [:lit, :==], [:lit, :equals]]
8
+ end
9
+
10
+ it "should accept symbols as names" do
11
+ opal_parse("alias :foo :bar").should == [:alias, [:lit, :foo], [:lit, :bar]]
12
+ end
13
+ end
14
+
15
+ describe "with gvar" do
16
+ it "should return a s(:valias) with two gvars as arguments" do
17
+ opal_parse("alias $foo $bar").should == [:valias, :$foo, :$bar]
18
+ end
19
+ end
20
+
21
+ describe "with gvar and nth ref" do
22
+ it "should return a s(:valias) with two values as arguments" do
23
+ opal_parse("alias $foo $1").should == [:valias, :$foo, :"$1"]
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The and statement" do
4
+ it "should always return s(:and)" do
5
+ opal_parse("1 and 2").should == [:and, [:lit, 1], [:lit, 2]]
6
+ end
7
+ end
8
+
9
+ describe "The && expression" do
10
+ it "should always return s(:and)" do
11
+ opal_parse("1 && 2").should == [:and, [:lit, 1], [:lit, 2]]
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Arrays" do
4
+ it "should parse empty arrays as s(:array)" do
5
+ opal_parse("[]").should == [:array]
6
+ end
7
+
8
+ it "should append regular args onto end of array sexp" do
9
+ opal_parse("[1]").should == [:array, [:lit, 1]]
10
+ opal_parse("[1, 2]").should == [:array, [:lit, 1], [:lit, 2]]
11
+ opal_parse("[1, 2, 3]").should == [:array, [:lit, 1], [:lit, 2], [:lit, 3]]
12
+ end
13
+
14
+ it "should return a single item s(:array) with given splat if no norm args" do
15
+ opal_parse("[*1]").should == [:array, [:splat, [:lit, 1]]]
16
+ end
17
+
18
+ it "should allow splats combined with any number of norm args" do
19
+ opal_parse("[1, *2]").should == [:array, [:lit, 1], [:splat, [:lit, 2]]]
20
+ opal_parse("[1, 2, *3]").should == [:array, [:lit, 1], [:lit, 2], [:splat, [:lit, 3]]]
21
+ end
22
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Attribute assignments" do
4
+ it "should return a s(:attrasgn) for simple assignments" do
5
+ opal_parse('self.foo = 1').should == [:attrasgn, [:self], :foo=, [:arglist, [:lit, 1]]]
6
+ opal_parse('bar.foo = 1').should == [:attrasgn, [:call, nil, :bar, [:arglist]], :foo=, [:arglist, [:lit, 1]]]
7
+ opal_parse('@bar.foo = 1').should == [:attrasgn, [:ivar, :@bar], :foo=, [:arglist, [:lit, 1]]]
8
+ end
9
+
10
+ it "accepts both '.' and '::' for method call operators" do
11
+ opal_parse('self.foo = 1').should == [:attrasgn, [:self], :foo=, [:arglist, [:lit, 1]]]
12
+ opal_parse('self::foo = 1').should == [:attrasgn, [:self], :foo=, [:arglist, [:lit, 1]]]
13
+ end
14
+
15
+ it "can accept a constant as assignable name when using '.'" do
16
+ opal_parse('self.FOO = 1').should == [:attrasgn, [:self], :FOO=, [:arglist, [:lit, 1]]]
17
+ end
18
+
19
+ describe "when setting element reference" do
20
+ it "uses []= as the method call" do
21
+ opal_parse('self[1] = 2').should == [:attrasgn, [:self], :[]=, [:arglist, [:lit, 1], [:lit, 2]]]
22
+ end
23
+
24
+ it "supports multiple arguments inside brackets" do
25
+ opal_parse('self[1, 2] = 3').should == [:attrasgn, [:self], :[]=, [:arglist, [:lit, 1], [:lit, 2], [:lit, 3]]]
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,38 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The begin keyword" do
4
+ it "should be removed when used without a resuce or enusre body" do
5
+ opal_parse('begin; 1; end').should == [:lit, 1]
6
+ opal_parse('begin; 1; 2; end').should == [:block, [:lit, 1], [:lit, 2]]
7
+ end
8
+
9
+ describe "with 'rescue' bodies" do
10
+ it "should create a s(:rescue) sexp" do
11
+ opal_parse('begin; 1; rescue; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array], [:lit, 2]]]
12
+ end
13
+
14
+ it "allows multiple rescue bodies" do
15
+ opal_parse('begin 1; rescue; 2; rescue; 3; end').should == [:rescue, [:lit, 1], [:resbody, [:array], [:lit, 2]], [:resbody, [:array], [:lit, 3]]]
16
+ end
17
+
18
+ it "accepts a list of classes used to match against the exception" do
19
+ opal_parse('begin 1; rescue Klass; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass]], [:lit, 2]]]
20
+ end
21
+
22
+ it "accepts an identifier to assign exception to" do
23
+ opal_parse('begin 1; rescue => a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:lasgn, :a, [:gvar, :$!]]], [:lit, 2]]]
24
+ opal_parse('begin 1; rescue Klass => a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass],[:lasgn, :a, [:gvar, :$!]]], [:lit, 2]]]
25
+ end
26
+
27
+ it "accepts an ivar to assign exception to" do
28
+ opal_parse('begin 1; rescue => @a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:iasgn, :@a, [:gvar, :$!]]], [:lit, 2]]]
29
+ opal_parse('begin 1; rescue Klass => @a; 2; end').should == [:rescue, [:lit, 1], [:resbody, [:array, [:const, :Klass],[:iasgn, :@a, [:gvar, :$!]]], [:lit, 2]]]
30
+ end
31
+ end
32
+
33
+ describe "with an 'ensure' block" do
34
+ it "should propagate into single s(:ensure) statement when no rescue block given" do
35
+ opal_parse('begin; 1; ensure; 2; end').should == [:ensure, [:lit, 1], [:lit, 2]]
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,12 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Block statements" do
4
+ it "should return the direct expression if only one expresssion in block" do
5
+ opal_parse("42").should == [:lit, 42]
6
+ end
7
+
8
+ it "should return an s(:block) with all expressions appended for > 1 expression" do
9
+ opal_parse("42; 43").should == [:block, [:lit, 42], [:lit, 43]]
10
+ opal_parse("42; 43\n44").should == [:block, [:lit, 42], [:lit, 43], [:lit, 44]]
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The break keyword" do
4
+ it "should return s(:break) when given no args" do
5
+ opal_parse("break").should == [:break]
6
+ end
7
+
8
+ it "returns s(:break) with a single arg not wrapped in s(:array)" do
9
+ opal_parse("break 1").should == [:break, [:lit, 1]]
10
+ opal_parse("break *1").should == [:break, [:splat, [:lit, 1]]]
11
+ end
12
+
13
+ it "returns s(:break) with an s(:array) for args size > 1" do
14
+ opal_parse("break 1, 2").should == [:break, [:array, [:lit, 1], [:lit, 2]]]
15
+ opal_parse("break 1, *2").should == [:break, [:array, [:lit, 1], [:splat, [:lit, 2]]]]
16
+ end
17
+ end
@@ -0,0 +1,58 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Method calls" do
4
+ it "should use 'nil' for calls without a receiver" do
5
+ opal_parse("foo").should == [:call, nil, :foo, [:arglist]]
6
+ opal_parse("foo()").should == [:call, nil, :foo, [:arglist]]
7
+ end
8
+
9
+ it "should always have an arglist when not passed any arguments" do
10
+ opal_parse("foo").should == [:call, nil, :foo, [:arglist]]
11
+ opal_parse("self.foo").should == [:call, [:self], :foo, [:arglist]]
12
+ opal_parse("foo()").should == [:call, nil, :foo, [:arglist]]
13
+ opal_parse("self.foo()").should == [:call, [:self], :foo, [:arglist]]
14
+ end
15
+
16
+ it "appends all arguments onto arglist" do
17
+ opal_parse("foo 1").should == [:call, nil, :foo, [:arglist, [:lit, 1]]]
18
+ opal_parse("foo 1, 2").should == [:call, nil, :foo, [:arglist, [:lit, 1], [:lit, 2]]]
19
+ opal_parse("foo 1, *2").should == [:call, nil, :foo, [:arglist, [:lit, 1], [:splat, [:lit, 2]]]]
20
+ end
21
+ end
22
+
23
+ describe "Operator calls" do
24
+ it "should become regular calls with operator as method" do
25
+ opal_parse("1 + 2").should == [:call, [:lit, 1], :+, [:arglist, [:lit, 2]]]
26
+ opal_parse("1 - 2").should == [:call, [:lit, 1], :-, [:arglist, [:lit, 2]]]
27
+ opal_parse("1 / 2").should == [:call, [:lit, 1], :/, [:arglist, [:lit, 2]]]
28
+ opal_parse("1 * 2").should == [:call, [:lit, 1], :*, [:arglist, [:lit, 2]]]
29
+ opal_parse("1 % 2").should == [:call, [:lit, 1], :%, [:arglist, [:lit, 2]]]
30
+ opal_parse("1 ** 2").should == [:call, [:lit, 1], :**, [:arglist, [:lit, 2]]]
31
+
32
+ opal_parse("+self").should == [:call, [:self], :+@, [:arglist]]
33
+ opal_parse("-self").should == [:call, [:self], :-@, [:arglist]]
34
+
35
+ opal_parse("1 | 2").should == [:call, [:lit, 1], :|, [:arglist, [:lit, 2]]]
36
+ opal_parse("1 ^ 2").should == [:call, [:lit, 1], :^, [:arglist, [:lit, 2]]]
37
+ opal_parse("1 & 2").should == [:call, [:lit, 1], :&, [:arglist, [:lit, 2]]]
38
+ opal_parse("1 <=> 2").should == [:call, [:lit, 1], :<=>, [:arglist, [:lit, 2]]]
39
+
40
+ opal_parse("1 < 2").should == [:call, [:lit, 1], :<, [:arglist, [:lit, 2]]]
41
+ opal_parse("1 <= 2").should == [:call, [:lit, 1], :<=, [:arglist, [:lit, 2]]]
42
+ opal_parse("1 > 2").should == [:call, [:lit, 1], :>, [:arglist, [:lit, 2]]]
43
+ opal_parse("1 >= 2").should == [:call, [:lit, 1], :>=, [:arglist, [:lit, 2]]]
44
+
45
+ opal_parse("1 == 2").should == [:call, [:lit, 1], :==, [:arglist, [:lit, 2]]]
46
+ opal_parse("1 === 2").should == [:call, [:lit, 1], :===, [:arglist, [:lit, 2]]]
47
+ opal_parse("1 =~ 2").should == [:call, [:lit, 1], :=~, [:arglist, [:lit, 2]]]
48
+
49
+ opal_parse("~1").should == [:call, [:lit, 1], :~, [:arglist]]
50
+ opal_parse("1 << 2").should == [:call, [:lit, 1], :<<, [:arglist, [:lit, 2]]]
51
+ opal_parse("1 >> 2").should == [:call, [:lit, 1], :>>, [:arglist, [:lit, 2]]]
52
+ end
53
+
54
+ it "optimizes +@ and -@ on numerics" do
55
+ opal_parse("+1").should == [:lit, 1]
56
+ opal_parse("-1").should == [:lit, -1]
57
+ end
58
+ end
@@ -0,0 +1,35 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The class keyword" do
4
+ it "returns a plain s(:scope) when given an empty body" do
5
+ opal_parse('class A; end').should == [:class, :A, nil, [:scope]]
6
+ end
7
+
8
+ it "does not place single expressions into a s(:block)" do
9
+ opal_parse('class A; 1; end').should == [:class, :A, nil, [:scope, [:lit, 1]]]
10
+ end
11
+
12
+ it "adds multiple body expressions into a s(:block)" do
13
+ opal_parse('class A; 1; 2; end').should == [:class, :A, nil, [:scope, [:block, [:lit, 1], [:lit, 2]]]]
14
+ end
15
+
16
+ it "uses nil as a placeholder when no superclass is given" do
17
+ opal_parse('class A; end')[2].should == nil
18
+ end
19
+
20
+ it "reflects the given superclass" do
21
+ opal_parse('class A < B; end')[2].should == [:const, :B]
22
+ end
23
+
24
+ it "should accept just a constant for the class name" do
25
+ opal_parse('class A; end')[1].should == :A
26
+ end
27
+
28
+ it "should accept a prefix constant for the class name" do
29
+ opal_parse('class ::A; end')[1].should == [:colon3, :A]
30
+ end
31
+
32
+ it "should accept a nested constant for the class name" do
33
+ opal_parse('class A::B; end')[1].should == [:colon2, [:const, :A], :B]
34
+ end
35
+ end
@@ -0,0 +1,13 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Constants" do
4
+ it "should always become a s(:const)" do
5
+ opal_parse("FOO").should == [:const, :FOO]
6
+ opal_parse("BAR").should == [:const, :BAR]
7
+ end
8
+
9
+ it "should be returned as s(:cdecl) on assignment" do
10
+ opal_parse("FOO = 1").should == [:cdecl, :FOO, [:lit, 1]]
11
+ opal_parse("FOO = BAR").should == [:cdecl, :FOO, [:const, :BAR]]
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Class variables" do
4
+ it "should always be returned as s(:cvar)" do
5
+ opal_parse("@@foo").should == [:cvar, :@@foo]
6
+ end
7
+
8
+ it "should always be converted to s(:cvdecl) on assignment" do
9
+ opal_parse("@@foo = 100").should == [:cvdecl, :@@foo, [:lit, 100]]
10
+ end
11
+ end
@@ -0,0 +1,60 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The def keyword" do
4
+ describe "for normal definitions" do
5
+ it "should return s(:defn)" do
6
+ opal_parse("def a; end").should == [:defn, :a, [:args], [:scope, [:block, [:nil]]]]
7
+ end
8
+
9
+ it "adds s(:nil) on an empty body" do
10
+ opal_parse("def foo; end").last.should == [:scope, [:block, [:nil]]]
11
+ end
12
+ end
13
+
14
+ describe "for singleton definitions" do
15
+ it "should return s(:defs)" do
16
+ opal_parse("def self.a; end").should == [:defs, [:self], :a, [:args], [:scope, [:block]]]
17
+ end
18
+
19
+ it "does not add s(:nil) on an empty body" do
20
+ opal_parse("def self.foo; end").last.should == [:scope, [:block]]
21
+ end
22
+ end
23
+
24
+ describe "with normal args" do
25
+ it "should list all args" do
26
+ opal_parse("def foo(a); end")[2].should == [:args, :a]
27
+ opal_parse("def foo(a, b); end")[2].should == [:args, :a, :b]
28
+ opal_parse("def foo(a, b, c); end")[2].should == [:args, :a, :b, :c]
29
+ end
30
+ end
31
+
32
+ describe "with opt args" do
33
+ it "should list all opt args as well as block with each lasgn" do
34
+ opal_parse("def foo(a = 1); end")[2].should == [:args, :a, [:block, [:lasgn, :a, [:lit, 1]]]]
35
+ opal_parse("def foo(a = 1, b = 2); end")[2].should == [:args, :a, :b, [:block, [:lasgn, :a, [:lit, 1]], [:lasgn, :b, [:lit, 2]]]]
36
+ end
37
+
38
+ it "should list lasgn block after all other args" do
39
+ opal_parse("def foo(a, b = 1); end")[2].should == [:args, :a, :b, [:block, [:lasgn, :b, [:lit, 1]]]]
40
+ opal_parse("def foo(b = 1, *c); end")[2].should == [:args, :b, :"*c", [:block, [:lasgn, :b, [:lit, 1]]]]
41
+ opal_parse("def foo(b = 1, &block); end")[2].should == [:args, :b, :"&block", [:block, [:lasgn, :b, [:lit, 1]]]]
42
+ end
43
+ end
44
+
45
+ describe "with rest args" do
46
+ it "should list rest args in place as a symbol with '*' prefix" do
47
+ opal_parse("def foo(*a); end")[2].should == [:args, :"*a"]
48
+ end
49
+
50
+ it "uses '*' as an arg name for rest args without a name" do
51
+ opal_parse("def foo(*); end")[2].should == [:args, :"*"]
52
+ end
53
+ end
54
+
55
+ describe "with block arg" do
56
+ it "should list block argument with the '&' prefix" do
57
+ opal_parse("def foo(&a); end")[2].should == [:args, :"&a"]
58
+ end
59
+ end
60
+ end