opal 0.5.5 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +40 -9
  3. data/CHANGELOG.md +349 -0
  4. data/Gemfile +7 -8
  5. data/README.md +25 -3
  6. data/Rakefile +4 -2
  7. data/bin/opal +1 -1
  8. data/examples/rack/Gemfile +3 -0
  9. data/examples/rack/app/application.rb +13 -0
  10. data/examples/rack/app/user.rb +21 -0
  11. data/examples/rack/config.ru +7 -0
  12. data/examples/rack/index.html.erb +10 -0
  13. data/examples/sinatra/Gemfile +4 -0
  14. data/examples/sinatra/app/application.rb +7 -0
  15. data/examples/sinatra/config.ru +21 -0
  16. data/lib/mspec/opal/rake_task.rb +29 -8
  17. data/lib/mspec/opal/runner.rb +5 -4
  18. data/lib/opal.rb +1 -0
  19. data/lib/opal/builder.rb +0 -28
  20. data/lib/opal/cli.rb +0 -14
  21. data/lib/opal/compiler.rb +12 -11
  22. data/lib/opal/fragment.rb +8 -1
  23. data/lib/opal/nodes/array.rb +1 -1
  24. data/lib/opal/nodes/base.rb +4 -0
  25. data/lib/opal/nodes/call.rb +6 -2
  26. data/lib/opal/nodes/call_special.rb +1 -1
  27. data/lib/opal/nodes/class.rb +2 -2
  28. data/lib/opal/nodes/constants.rb +3 -1
  29. data/lib/opal/nodes/helpers.rb +23 -14
  30. data/lib/opal/nodes/if.rb +16 -9
  31. data/lib/opal/nodes/literal.rb +37 -5
  32. data/lib/opal/nodes/logic.rb +7 -1
  33. data/lib/opal/nodes/module.rb +2 -2
  34. data/lib/opal/nodes/scope.rb +13 -2
  35. data/lib/opal/nodes/top.rb +9 -0
  36. data/lib/opal/nodes/variables.rb +5 -2
  37. data/lib/opal/parser.rb +306 -71
  38. data/lib/opal/parser/grammar.rb +2667 -2775
  39. data/lib/opal/parser/grammar.y +177 -233
  40. data/lib/opal/parser/lexer.rb +511 -427
  41. data/lib/opal/parser/sexp.rb +15 -3
  42. data/lib/opal/source_map.rb +8 -4
  43. data/lib/opal/sprockets.rb +4 -0
  44. data/lib/opal/sprockets/cache_key_fix.rb +17 -0
  45. data/lib/opal/sprockets/environment.rb +21 -0
  46. data/lib/opal/sprockets/erb.rb +30 -0
  47. data/lib/opal/sprockets/processor.rb +127 -0
  48. data/lib/opal/sprockets/server.rb +166 -0
  49. data/lib/opal/util.rb +29 -0
  50. data/lib/opal/version.rb +1 -1
  51. data/opal.gemspec +1 -1
  52. data/opal/corelib/array.rb +106 -187
  53. data/opal/corelib/array/inheritance.rb +113 -0
  54. data/opal/corelib/basic_object.rb +6 -2
  55. data/opal/corelib/boolean.rb +4 -0
  56. data/opal/corelib/class.rb +2 -0
  57. data/opal/corelib/complex.rb +3 -0
  58. data/opal/corelib/enumerable.rb +75 -8
  59. data/opal/corelib/enumerator.rb +2 -0
  60. data/opal/corelib/error.rb +23 -23
  61. data/opal/corelib/hash.rb +5 -5
  62. data/opal/corelib/helpers.rb +51 -16
  63. data/opal/corelib/io.rb +7 -24
  64. data/opal/corelib/kernel.rb +23 -11
  65. data/opal/corelib/module.rb +44 -47
  66. data/opal/corelib/nil_class.rb +4 -0
  67. data/opal/corelib/numeric.rb +101 -15
  68. data/opal/corelib/range.rb +2 -0
  69. data/opal/corelib/rational.rb +3 -0
  70. data/opal/corelib/regexp.rb +36 -17
  71. data/opal/corelib/runtime.js +22 -7
  72. data/opal/corelib/string.rb +213 -110
  73. data/opal/corelib/string/inheritance.rb +78 -0
  74. data/opal/corelib/struct.rb +8 -0
  75. data/opal/corelib/time.rb +54 -42
  76. data/opal/corelib/variables.rb +24 -0
  77. data/opal/opal.rb +5 -27
  78. data/spec/cli/compiler_spec.rb +136 -0
  79. data/spec/cli/dependency_resolver_spec.rb +40 -0
  80. data/spec/cli/lexer_spec.rb +110 -0
  81. data/spec/cli/parser/alias_spec.rb +26 -0
  82. data/spec/cli/parser/and_spec.rb +13 -0
  83. data/spec/cli/parser/attrasgn_spec.rb +28 -0
  84. data/spec/cli/parser/begin_spec.rb +42 -0
  85. data/spec/cli/parser/block_spec.rb +12 -0
  86. data/spec/cli/parser/break_spec.rb +17 -0
  87. data/spec/cli/parser/call_spec.rb +139 -0
  88. data/spec/cli/parser/class_spec.rb +35 -0
  89. data/spec/cli/parser/comments_spec.rb +11 -0
  90. data/spec/cli/parser/def_spec.rb +61 -0
  91. data/spec/cli/parser/if_spec.rb +26 -0
  92. data/spec/cli/parser/iter_spec.rb +59 -0
  93. data/spec/cli/parser/lambda_spec.rb +64 -0
  94. data/spec/cli/parser/literal_spec.rb +113 -0
  95. data/spec/cli/parser/masgn_spec.rb +37 -0
  96. data/spec/cli/parser/module_spec.rb +27 -0
  97. data/spec/cli/parser/not_spec.rb +27 -0
  98. data/spec/cli/parser/op_asgn1_spec.rb +23 -0
  99. data/spec/cli/parser/op_asgn2_spec.rb +23 -0
  100. data/spec/cli/parser/or_spec.rb +13 -0
  101. data/spec/cli/parser/return_spec.rb +17 -0
  102. data/spec/cli/parser/sclass_spec.rb +21 -0
  103. data/spec/cli/parser/string_spec.rb +269 -0
  104. data/spec/cli/parser/super_spec.rb +20 -0
  105. data/spec/cli/parser/undef_spec.rb +15 -0
  106. data/spec/cli/parser/unless_spec.rb +13 -0
  107. data/spec/cli/parser/variables_spec.rb +92 -0
  108. data/spec/cli/parser/while_spec.rb +15 -0
  109. data/spec/cli/parser/yield_spec.rb +20 -0
  110. data/spec/cli/spec_helper.rb +31 -11
  111. data/spec/opal/core/array/set_range_to_array_spec.rb +7 -0
  112. data/spec/opal/core/date_spec.rb +122 -0
  113. data/spec/opal/core/language/predefined_spec.rb +1 -1
  114. data/spec/opal/core/runtime/operator_call_spec.rb +13 -0
  115. data/spec/opal/core/runtime/truthy_spec.rb +23 -0
  116. data/spec/opal/filters/bugs/array.rb +96 -87
  117. data/spec/opal/filters/bugs/basic_object.rb +9 -0
  118. data/spec/opal/filters/bugs/class.rb +16 -0
  119. data/spec/opal/filters/bugs/enumerable.rb +54 -0
  120. data/spec/opal/filters/bugs/language.rb +37 -3
  121. data/spec/opal/filters/bugs/math.rb +93 -0
  122. data/spec/opal/filters/bugs/nil.rb +7 -0
  123. data/spec/opal/filters/bugs/numeric.rb +19 -0
  124. data/spec/opal/filters/bugs/opal.rb +12 -0
  125. data/spec/opal/filters/bugs/regexp.rb +0 -2
  126. data/spec/opal/filters/bugs/string.rb +317 -19
  127. data/spec/opal/filters/bugs/struct.rb +29 -0
  128. data/spec/opal/filters/bugs/time.rb +130 -9
  129. data/spec/opal/filters/unsupported/encoding.rb +52 -4
  130. data/spec/opal/filters/unsupported/enumerator.rb +0 -3
  131. data/spec/opal/filters/unsupported/integer_size.rb +7 -0
  132. data/spec/opal/filters/unsupported/method_added.rb +10 -0
  133. data/spec/opal/filters/unsupported/mutable_strings.rb +299 -1
  134. data/spec/opal/filters/unsupported/private_constants.rb +30 -0
  135. data/spec/opal/filters/unsupported/private_methods.rb +16 -0
  136. data/spec/opal/filters/unsupported/random.rb +4 -0
  137. data/spec/opal/filters/unsupported/tainted.rb +53 -0
  138. data/spec/opal/filters/unsupported/trusted.rb +5 -0
  139. data/spec/opal/rubyspecs +167 -234
  140. data/spec/opal/spec_helper.rb +3 -0
  141. data/spec/opal/stdlib/promise/error_spec.rb +15 -0
  142. data/spec/opal/stdlib/promise/rescue_spec.rb +35 -0
  143. data/spec/opal/stdlib/promise/then_spec.rb +54 -0
  144. data/spec/opal/stdlib/promise/trace_spec.rb +35 -0
  145. data/spec/opal/stdlib/promise/value_spec.rb +15 -0
  146. data/spec/opal/stdlib/promise/when_spec.rb +34 -0
  147. data/stdlib/base64.rb +152 -0
  148. data/stdlib/date.rb +82 -49
  149. data/{opal/corelib → stdlib}/encoding.rb +3 -1
  150. data/stdlib/erb.rb +0 -1
  151. data/stdlib/json.rb +10 -26
  152. data/stdlib/math.rb +370 -0
  153. data/stdlib/native.rb +40 -33
  154. data/stdlib/opal-parser.rb +7 -4
  155. data/stdlib/promise.rb +292 -0
  156. data/stdlib/strscan.rb +1 -1
  157. data/stdlib/template.rb +1 -3
  158. data/stdlib/time.rb +9 -0
  159. metadata +143 -204
  160. data/doc/compiler.md +0 -42
  161. data/doc/compiler_options.md +0 -5
  162. data/doc/examples/node_http_server.rb +0 -49
  163. data/doc/external_libraries.md +0 -9
  164. data/doc/generated_javascript.md +0 -272
  165. data/doc/home.md +0 -17
  166. data/doc/method_missing.md +0 -58
  167. data/doc/static_applications.md +0 -60
  168. data/doc/using_ruby_from_javascript.md +0 -18
  169. data/doc/using_sprockets.md +0 -65
  170. data/spec/opal/core/numeric/abs_spec.rb +0 -12
  171. data/spec/opal/core/numeric/downto_spec.rb +0 -19
  172. data/spec/opal/core/numeric/equal_value_spec.rb +0 -9
  173. data/spec/opal/core/numeric/even_spec.rb +0 -21
  174. data/spec/opal/core/numeric/magnitude_spec.rb +0 -12
  175. data/spec/opal/core/numeric/odd_spec.rb +0 -21
  176. data/spec/opal/core/string/chop_spec.rb +0 -10
  177. data/spec/opal/core/string/chr_spec.rb +0 -13
  178. data/spec/opal/core/string/clone_spec.rb +0 -8
  179. data/spec/opal/core/string/comparison_spec.rb +0 -13
  180. data/spec/opal/core/string/dup_spec.rb +0 -8
  181. data/spec/opal/core/string/element_reference_spec.rb +0 -96
  182. data/spec/opal/core/string/fixtures/classes.rb +0 -49
  183. data/spec/opal/core/string/format_spec.rb +0 -9
  184. data/spec/opal/core/string/freeze_spec.rb +0 -15
  185. data/spec/opal/core/string/gsub_spec.rb +0 -31
  186. data/spec/opal/core/string/lines_spec.rb +0 -9
  187. data/spec/opal/core/string/ljust_spec.rb +0 -32
  188. data/spec/opal/core/string/lstrip_spec.rb +0 -7
  189. data/spec/opal/core/string/match_spec.rb +0 -49
  190. data/spec/opal/core/string/next_spec.rb +0 -10
  191. data/spec/opal/core/string/ord_spec.rb +0 -9
  192. data/spec/opal/core/string/partition_spec.rb +0 -10
  193. data/spec/opal/core/string/rindex_spec.rb +0 -50
  194. data/spec/opal/core/string/rjust_spec.rb +0 -32
  195. data/spec/opal/core/string/rstrip_spec.rb +0 -7
  196. data/spec/opal/core/string/scan_spec.rb +0 -66
  197. data/spec/opal/core/string/slice_spec.rb +0 -74
  198. data/spec/opal/core/string/split_spec.rb +0 -5
  199. data/spec/opal/core/string/strip_spec.rb +0 -6
  200. data/spec/opal/core/string/sub_spec.rb +0 -38
  201. data/spec/opal/core/string/succ_spec.rb +0 -10
  202. data/spec/opal/core/string/sum_spec.rb +0 -5
  203. data/spec/opal/core/string/to_f_spec.rb +0 -14
  204. data/spec/opal/core/string/to_i_spec.rb +0 -25
  205. data/spec/opal/core/string/tr_s_spec.rb +0 -31
  206. data/spec/opal/core/string/tr_spec.rb +0 -31
  207. data/spec/opal/filters/bugs/parser.rb +0 -10
  208. data/spec/opal/filters/unsupported/immutable_strings.rb +0 -24
  209. data/spec/opal/filters/unsupported/string_subclasses.rb +0 -8
  210. data/spec/opal/parser/alias_spec.rb +0 -26
  211. data/spec/opal/parser/and_spec.rb +0 -13
  212. data/spec/opal/parser/array_spec.rb +0 -22
  213. data/spec/opal/parser/attrasgn_spec.rb +0 -28
  214. data/spec/opal/parser/begin_spec.rb +0 -42
  215. data/spec/opal/parser/block_spec.rb +0 -12
  216. data/spec/opal/parser/break_spec.rb +0 -17
  217. data/spec/opal/parser/call_spec.rb +0 -131
  218. data/spec/opal/parser/class_spec.rb +0 -35
  219. data/spec/opal/parser/const_spec.rb +0 -13
  220. data/spec/opal/parser/cvar_spec.rb +0 -11
  221. data/spec/opal/parser/def_spec.rb +0 -61
  222. data/spec/opal/parser/false_spec.rb +0 -17
  223. data/spec/opal/parser/file_spec.rb +0 -7
  224. data/spec/opal/parser/gvar_spec.rb +0 -13
  225. data/spec/opal/parser/hash_spec.rb +0 -17
  226. data/spec/opal/parser/heredoc_spec.rb +0 -30
  227. data/spec/opal/parser/iasgn_spec.rb +0 -9
  228. data/spec/opal/parser/if_spec.rb +0 -26
  229. data/spec/opal/parser/int_spec.rb +0 -13
  230. data/spec/opal/parser/iter_spec.rb +0 -59
  231. data/spec/opal/parser/ivar_spec.rb +0 -9
  232. data/spec/opal/parser/lambda_spec.rb +0 -64
  233. data/spec/opal/parser/lasgn_spec.rb +0 -8
  234. data/spec/opal/parser/line_spec.rb +0 -8
  235. data/spec/opal/parser/lvar_spec.rb +0 -38
  236. data/spec/opal/parser/masgn_spec.rb +0 -37
  237. data/spec/opal/parser/module_spec.rb +0 -27
  238. data/spec/opal/parser/nil_spec.rb +0 -17
  239. data/spec/opal/parser/not_spec.rb +0 -27
  240. data/spec/opal/parser/nth_ref_spec.rb +0 -13
  241. data/spec/opal/parser/op_asgn1_spec.rb +0 -23
  242. data/spec/opal/parser/op_asgn2_spec.rb +0 -23
  243. data/spec/opal/parser/or_spec.rb +0 -13
  244. data/spec/opal/parser/parse_spec.rb +0 -66
  245. data/spec/opal/parser/regexp_spec.rb +0 -16
  246. data/spec/opal/parser/return_spec.rb +0 -17
  247. data/spec/opal/parser/sclass_spec.rb +0 -21
  248. data/spec/opal/parser/self_spec.rb +0 -17
  249. data/spec/opal/parser/str_spec.rb +0 -107
  250. data/spec/opal/parser/string_spec.rb +0 -8
  251. data/spec/opal/parser/super_spec.rb +0 -20
  252. data/spec/opal/parser/true_spec.rb +0 -17
  253. data/spec/opal/parser/undef_spec.rb +0 -15
  254. data/spec/opal/parser/unless_spec.rb +0 -13
  255. data/spec/opal/parser/while_spec.rb +0 -15
  256. data/spec/opal/parser/xstr_spec.rb +0 -116
  257. data/spec/opal/parser/yield_spec.rb +0 -20
@@ -0,0 +1,11 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Multiline comments" do
4
+ it "parses multiline comments and ignores them" do
5
+ parsed("=begin\nfoo\n=end\n100").should == [:int, 100]
6
+ end
7
+
8
+ it "raises an exception if not closed before end of file" do
9
+ lambda { parsed("=begin\nfoo\nbar") }.should raise_error(Exception, /embedded document meets end of file/)
10
+ end
11
+ end
@@ -0,0 +1,61 @@
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(:def)" do
6
+ parsed("def a; end").should == [:def, nil, :a, [:args], [:block, [:nil]]]
7
+ end
8
+
9
+ it "adds s(:nil) on an empty body" do
10
+ parsed("def foo; end").last.should == [:block, [:nil]]
11
+ end
12
+ end
13
+
14
+ describe "for singleton definitions" do
15
+ it "should return s(:def)" do
16
+ parsed("def self.a; end").should == [:def, [:self], :a, [:args], [:block, [:nil]]]
17
+ end
18
+
19
+ it "adds s(:nil) on an empty body" do
20
+ parsed("def self.foo; end").last.should == [:block, [:nil]]
21
+ end
22
+ end
23
+
24
+ describe "with normal args" do
25
+ it "should list all args" do
26
+ parsed("def foo(a); end")[3].should == [:args, :a]
27
+ parsed("def foo(a, b); end")[3].should == [:args, :a, :b]
28
+ parsed("def foo(a, b, c); end")[3].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
+ parsed("def foo(a = 1); end")[3].should == [:args, :a, [:block, [:lasgn, :a, [:int, 1]]]]
35
+ parsed("def foo(a = 1, b = 2); end")[3].should == [:args, :a, :b, [:block, [:lasgn, :a, [:int, 1]], [:lasgn, :b, [:int, 2]]]]
36
+ end
37
+
38
+ it "should list lasgn block after all other args" do
39
+ parsed("def foo(a, b = 1); end")[3].should == [:args, :a, :b, [:block, [:lasgn, :b, [:int, 1]]]]
40
+ parsed("def foo(b = 1, *c); end")[3].should == [:args, :b, :"*c", [:block, [:lasgn, :b, [:int, 1]]]]
41
+ parsed("def foo(b = 1, &block); end")[3].should == [:args, :b, :"&block", [:block, [:lasgn, :b, [:int, 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
+ parsed("def foo(*a); end")[3].should == [:args, :"*a"]
48
+ end
49
+
50
+ it "uses '*' as an arg name for rest args without a name" do
51
+ parsed("def foo(*); end")[3].should == [:args, :"*"]
52
+ end
53
+ end
54
+
55
+ describe "with block arg" do
56
+ it "should list block argument with the '&' prefix" do
57
+ parsed("def foo(&a); end")[3].should == [:args, :"&a"]
58
+ end
59
+ end
60
+ end
61
+
@@ -0,0 +1,26 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The if keyword" do
4
+ it "should return an s(:if) with given truthy and falsy bodies" do
5
+ parsed("if 1; 2; else; 3; end").should == [:if, [:int, 1], [:int, 2], [:int, 3]]
6
+ end
7
+
8
+ it "uses nil as fasly body if not given else-then" do
9
+ parsed("if 1; 2; end").should == [:if, [:int, 1], [:int, 2], nil]
10
+ end
11
+
12
+ it "is treats elsif parts as sub if expressions for else body" do
13
+ parsed("if 1; 2; elsif 3; 4; else; 5; end").should == [:if, [:int, 1], [:int, 2], [:if, [:int, 3], [:int, 4], [:int, 5]]]
14
+ parsed("if 1; 2; elsif 3; 4; end").should == [:if, [:int, 1], [:int, 2], [:if, [:int, 3], [:int, 4], nil]]
15
+ end
16
+
17
+ it "returns a simple s(:if) with nil else body for prefix if statement" do
18
+ parsed("1 if 2").should == [:if, [:int, 2], [:int, 1], nil]
19
+ end
20
+ end
21
+
22
+ describe "The ternary operator" do
23
+ it "gets converted into an if statement with true and false parts" do
24
+ parsed("1 ? 2 : 3").should == [:if, [:int, 1], [:int, 2], [:int, 3]]
25
+ end
26
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Iters" do
4
+ describe "Iter on a command" do
5
+ it "the outer command call gets the iter" do
6
+ parsed("a b do; end").should == [:call, nil, :a, [:arglist, [:call, nil, :b, [:arglist]]], [:iter, nil]]
7
+ parsed("a 1, b do; end").should == [:call, nil, :a, [:arglist, [:int, 1], [:call, nil, :b, [:arglist]]], [:iter, nil]]
8
+ end
9
+ end
10
+
11
+ describe "with no args" do
12
+ it "has 'nil' as the args part of sexp" do
13
+ parsed("proc do; end")[4][1].should == nil
14
+ end
15
+ end
16
+
17
+ describe "with empty || args" do
18
+ it "should have args set to nil" do
19
+ parsed("proc do ||; end")[4][1].should == nil
20
+ end
21
+ end
22
+
23
+ describe "with normal args" do
24
+ it "adds a single s(:lasgn) for 1 norm arg" do
25
+ parsed("proc do |a|; end")[4][1].should == [:lasgn, :a]
26
+ end
27
+
28
+ it "lists multiple norm args inside a s(:masgn)" do
29
+ parsed("proc do |a, b|; end")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]]]
30
+ parsed("proc do |a, b, c|; end")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:lasgn, :c]]]
31
+ end
32
+ end
33
+
34
+ describe "with splat arg" do
35
+ it "adds a s(:masgn) for the s(:splat) even if its the only arg" do
36
+ parsed("proc do |*a|; end")[4][1].should == [:masgn, [:array, [:splat, [:lasgn, :a]]]]
37
+ parsed("proc do |a, *b|; end")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:splat, [:lasgn, :b]]]]
38
+ end
39
+ end
40
+
41
+ describe "with opt args" do
42
+ it "adds a s(:block) arg to end of s(:masgn) for each lasgn" do
43
+ parsed("proc do |a = 1|; end")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:block, [:lasgn, :a, [:int, 1]]]]]
44
+ parsed("proc do |a = 1, b = 2|; end")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:block, [:lasgn, :a, [:int, 1]], [:lasgn, :b, [:int, 2]]]]]
45
+ end
46
+
47
+ it "should add lasgn block after all other args" do
48
+ parsed("proc do |a, b = 1|; end")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:block, [:lasgn, :b, [:int, 1]]]]]
49
+ parsed("proc do |b = 1, *c|; end")[4][1].should == [:masgn, [:array, [:lasgn, :b], [:splat, [:lasgn, :c]], [:block, [:lasgn, :b, [:int, 1]]]]]
50
+ parsed("proc do |b = 1, &c|; end")[4][1].should == [:masgn, [:array, [:lasgn, :b], [:block_pass, [:lasgn, :c]], [:block, [:lasgn, :b, [:int, 1]]]]]
51
+ end
52
+ end
53
+
54
+ describe "with block arg" do
55
+ it "should add block arg with s(:block_pass) wrapping s(:lasgn) prefix" do
56
+ parsed("proc do |&a|; end")[4][1].should == [:masgn, [:array, [:block_pass, [:lasgn, :a]]]]
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,64 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Lambda literals" do
4
+ it "should parse with either do/end construct or curly braces" do
5
+ parsed("-> {}").first.should == :call
6
+ parsed("-> do; end").first.should == :call
7
+ end
8
+
9
+ it "should parse as a call to 'lambda' with the lambda body as a block" do
10
+ parsed("-> {}").should == [:call, nil, :lambda, [:arglist], [:iter, nil]]
11
+ end
12
+
13
+ describe "with no args" do
14
+ it "should accept no args" do
15
+ parsed("-> {}")[4][1].should == nil
16
+ end
17
+ end
18
+
19
+ describe "with normal args" do
20
+ it "adds a single s(:lasgn) for 1 norm arg" do
21
+ parsed("->(a) {}")[4][1].should == [:lasgn, :a]
22
+ end
23
+
24
+ it "lists multiple norm args inside a s(:masgn)" do
25
+ parsed("-> (a, b) {}")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]]]
26
+ parsed("-> (a, b, c) {}")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:lasgn, :c]]]
27
+ end
28
+ end
29
+
30
+ describe "with optional braces" do
31
+ it "parses normal args" do
32
+ parsed("-> a {}")[4][1].should == [:lasgn, :a]
33
+ parsed("-> a, b {}")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b]]]
34
+ end
35
+
36
+ it "parses splat args" do
37
+ parsed("-> *a {}")[4][1].should == [:masgn, [:array, [:splat, [:lasgn, :a]]]]
38
+ parsed("-> a, *b {}")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:splat, [:lasgn, :b]]]]
39
+ end
40
+
41
+ it "parses opt args" do
42
+ parsed("-> a = 1 {}")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:block, [:lasgn, :a, [:int, 1]]]]]
43
+ parsed("-> a = 1, b = 2 {}")[4][1].should == [:masgn, [:array, [:lasgn, :a], [:lasgn, :b], [:block, [:lasgn, :a, [:int, 1]], [:lasgn, :b, [:int, 2]]]]]
44
+ end
45
+
46
+ it "parses block args" do
47
+ parsed("-> &a {}")[4][1].should == [:masgn, [:array, [:block_pass, [:lasgn, :a]]]]
48
+ end
49
+ end
50
+
51
+ describe "with body statements" do
52
+ it "should be nil when no statements given" do
53
+ parsed("-> {}")[4][2].should == nil
54
+ end
55
+
56
+ it "should be the single sexp when given one statement" do
57
+ parsed("-> { 42 }")[4][2].should == [:int, 42]
58
+ end
59
+
60
+ it "should wrap multiple statements into a s(:block)" do
61
+ parsed("-> { 42; 3.142 }")[4][2].should == [:block, [:int, 42], [:float, 3.142]]
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,113 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe Opal::Parser do
4
+
5
+ it "parses true keyword" do
6
+ parsed("true").should == [:true]
7
+ end
8
+
9
+ it "true cannot be assigned to" do
10
+ lambda {
11
+ parsed "true = 1"
12
+ }.should raise_error(Exception)
13
+ end
14
+
15
+ it "parses false keyword" do
16
+ parsed("false").should == [:false]
17
+ end
18
+
19
+ it "false cannot be assigned to" do
20
+ lambda {
21
+ parsed "true = 1"
22
+ }.should raise_error(Exception)
23
+ end
24
+
25
+ it "parses nil keyword" do
26
+ parsed("nil").should == [:nil]
27
+ end
28
+
29
+ it "nil cannot be assigned to" do
30
+ lambda {
31
+ parsed "nil = 1"
32
+ }.should raise_error(Exception)
33
+ end
34
+
35
+ it "parses self keyword" do
36
+ parsed("self").should == [:self]
37
+ end
38
+
39
+ it "self cannot be assigned to" do
40
+ lambda {
41
+ parsed "self = 1"
42
+ }.should raise_error(Exception)
43
+ end
44
+
45
+ it "parses __FILE__ and should always return a s(:str) with given parser filename" do
46
+ parsed("__FILE__", "foo").should == [:str, "foo"]
47
+ end
48
+
49
+ it "parses __LINE__ and should always return a literal number of the current line" do
50
+ parsed("__LINE__").should == [:int, 1]
51
+ parsed("\n__LINE__").should == [:int, 2]
52
+ end
53
+
54
+ it "parses integers as a s(:int) sexp" do
55
+ parsed("32").should == [:int, 32]
56
+ end
57
+
58
+ it "parses floats as a s(:float)" do
59
+ parsed("3.142").should == [:float, 3.142]
60
+ end
61
+
62
+ describe "parsing arrays" do
63
+ it "should parse empty arrays as s(:array)" do
64
+ parsed("[]").should == [:array]
65
+ end
66
+
67
+ it "should append regular args onto end of array sexp" do
68
+ parsed("[1]").should == [:array, [:int, 1]]
69
+ parsed("[1, 2]").should == [:array, [:int, 1], [:int, 2]]
70
+ parsed("[1, 2, 3]").should == [:array, [:int, 1], [:int, 2], [:int, 3]]
71
+ end
72
+
73
+ it "should return a single item s(:array) with given splat if no norm args" do
74
+ parsed("[*1]").should == [:array, [:splat, [:int, 1]]]
75
+ end
76
+
77
+ it "should allow splats combined with any number of norm args" do
78
+ parsed("[1, *2]").should == [:array, [:int, 1], [:splat, [:int, 2]]]
79
+ parsed("[1, 2, *3]").should == [:array, [:int, 1], [:int, 2], [:splat, [:int, 3]]]
80
+ end
81
+ end
82
+
83
+ describe "parsing hashes" do
84
+ it "without any assocs should return an empty hash sexp" do
85
+ parsed("{}").should == [:hash]
86
+ end
87
+
88
+ it "adds each assoc pair as individual args onto sexp" do
89
+ parsed("{1 => 2}").should == [:hash, [:int, 1], [:int, 2]]
90
+ parsed("{1 => 2, 3 => 4}").should == [:hash, [:int, 1], [:int, 2], [:int, 3], [:int, 4]]
91
+ end
92
+
93
+ it "supports 1.9 style hash keys" do
94
+ parsed("{ a: 1 }").should == [:hash, [:sym, :a], [:int, 1]]
95
+ parsed("{ a: 1, b: 2 }").should == [:hash, [:sym, :a], [:int, 1], [:sym, :b], [:int, 2]]
96
+ end
97
+ end
98
+
99
+ describe "parsing regexps" do
100
+ it "parses a regexp" do
101
+ parsed("/lol/").should == [:regexp, 'lol', nil]
102
+ end
103
+
104
+ it "parses regexp options" do
105
+ parsed("/lol/i").should == [:regexp, 'lol', 'i']
106
+ end
107
+
108
+ it "can parse regexps using %r notation" do
109
+ parsed('%r(foo)').should == [:regexp, 'foo', nil]
110
+ parsed('%r(foo)i').should == [:regexp, 'foo', 'i']
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,37 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Masgn" do
4
+ describe "with a single lhs splat" do
5
+ it "returns a s(:masgn)" do
6
+ parsed('*a = 1, 2').first.should == :masgn
7
+ parsed('* = 1, 2').first.should == :masgn
8
+ end
9
+
10
+ it "wraps splat inside a s(:array)" do
11
+ parsed('*a = 1, 2')[1].should == [:array, [:splat, [:lasgn, :a]]]
12
+ parsed('* = 1, 2')[1].should == [:array, [:splat]]
13
+ end
14
+ end
15
+
16
+ describe "with more than 1 lhs item" do
17
+ it "returns a s(:masgn) " do
18
+ parsed('a, b = 1, 2').first.should == :masgn
19
+ end
20
+
21
+ it "collects all lhs args into an s(:array)" do
22
+ parsed('a, b = 1, 2')[1].should == [:array, [:lasgn, :a], [:lasgn, :b]]
23
+ parsed('@a, @b = 1, 2')[1].should == [:array, [:iasgn, :@a], [:iasgn, :@b]]
24
+ end
25
+
26
+ it "supports splat parts" do
27
+ parsed('a, *b = 1, 2')[1].should == [:array, [:lasgn, :a], [:splat, [:lasgn, :b]]]
28
+ parsed('@a, * = 1, 2')[1].should == [:array, [:iasgn, :@a], [:splat]]
29
+ end
30
+ end
31
+
32
+ describe "with a single rhs argument" do
33
+ it "should wrap rhs in an s(:to_ary)" do
34
+ parsed('a, b = 1')[2].should == [:to_ary, [:int, 1]]
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The module keyword" do
4
+ it "returns an empty s(:block) when given an empty body" do
5
+ parsed('module A; end').should == [:module, [:const, :A], [:block]]
6
+ end
7
+
8
+ it "does not place single expressions into a s(:block)" do
9
+ parsed('module A; 1; end').should == [:module, [:const, :A], [:int, 1]]
10
+ end
11
+
12
+ it "adds multiple body expressions into a s(:block)" do
13
+ parsed('module A; 1; 2; end').should == [:module, [:const, :A], [:block, [:int, 1], [:int, 2]]]
14
+ end
15
+
16
+ it "should accept just a constant for the module name" do
17
+ parsed('module A; end')[1].should == [:const, :A]
18
+ end
19
+
20
+ it "should accept a prefix constant for the module name" do
21
+ parsed('module ::A; end')[1].should == [:colon3, :A]
22
+ end
23
+
24
+ it "should accepts a nested constant for the module name" do
25
+ parsed('module A::B; end')[1].should == [:colon2, [:const, :A], :B]
26
+ end
27
+ end
@@ -0,0 +1,27 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "The not keyword" do
4
+ it "returns a call sexp" do
5
+ parsed("not self").should == [:call, [:self], '!'.to_sym, [:arglist]]
6
+ parsed("not 42").should == [:call, [:int, 42], '!'.to_sym, [:arglist]]
7
+ end
8
+ end
9
+
10
+ describe "The '!' expression" do
11
+ it "returns a call sexp" do
12
+ parsed("!self").should == [:call, [:self], '!'.to_sym, [:arglist]]
13
+ parsed("!42").should == [:call, [:int, 42], '!'.to_sym, [:arglist]]
14
+ end
15
+ end
16
+
17
+ describe "The '!=' expression" do
18
+ it "rewrites as !(lhs == rhs)" do
19
+ parsed("1 != 2").should == [:call, [:call, [:int, 1], :==, [:arglist, [:int, 2]]], '!'.to_sym, [:arglist]]
20
+ end
21
+ end
22
+
23
+ describe "The '!~' expression" do
24
+ it "rewrites as !(lhs =~ rhs)" do
25
+ parsed("1 !~ 2").should == [:not, [:call, [:int, 1], :=~, [:arglist, [:int, 2]]]]
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "op_asgn1" do
4
+ it "returns s(:op_asgn1)" do
5
+ parsed('self[:foo] += 1')[0].should == :op_asgn1
6
+ end
7
+
8
+ it "correctly assigns the receiver" do
9
+ parsed("self[:foo] += 1")[1].should == [:self]
10
+ end
11
+
12
+ it "returns an arglist for args inside braces" do
13
+ parsed("self[:foo] += 1")[2].should == [:arglist, [:sym, :foo]]
14
+ end
15
+
16
+ it "only uses the operator, not with '=' appended" do
17
+ parsed("self[:foo] += 1")[3].should == '+'
18
+ end
19
+
20
+ it "uses a simple sexp, not an arglist" do
21
+ parsed("self[:foo] += 1")[4].should == [:int, 1]
22
+ end
23
+ end