opal 0.5.5 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +40 -9
- data/CHANGELOG.md +349 -0
- data/Gemfile +7 -8
- data/README.md +25 -3
- data/Rakefile +4 -2
- data/bin/opal +1 -1
- data/examples/rack/Gemfile +3 -0
- data/examples/rack/app/application.rb +13 -0
- data/examples/rack/app/user.rb +21 -0
- data/examples/rack/config.ru +7 -0
- data/examples/rack/index.html.erb +10 -0
- data/examples/sinatra/Gemfile +4 -0
- data/examples/sinatra/app/application.rb +7 -0
- data/examples/sinatra/config.ru +21 -0
- data/lib/mspec/opal/rake_task.rb +29 -8
- data/lib/mspec/opal/runner.rb +5 -4
- data/lib/opal.rb +1 -0
- data/lib/opal/builder.rb +0 -28
- data/lib/opal/cli.rb +0 -14
- data/lib/opal/compiler.rb +12 -11
- data/lib/opal/fragment.rb +8 -1
- data/lib/opal/nodes/array.rb +1 -1
- data/lib/opal/nodes/base.rb +4 -0
- data/lib/opal/nodes/call.rb +6 -2
- data/lib/opal/nodes/call_special.rb +1 -1
- data/lib/opal/nodes/class.rb +2 -2
- data/lib/opal/nodes/constants.rb +3 -1
- data/lib/opal/nodes/helpers.rb +23 -14
- data/lib/opal/nodes/if.rb +16 -9
- data/lib/opal/nodes/literal.rb +37 -5
- data/lib/opal/nodes/logic.rb +7 -1
- data/lib/opal/nodes/module.rb +2 -2
- data/lib/opal/nodes/scope.rb +13 -2
- data/lib/opal/nodes/top.rb +9 -0
- data/lib/opal/nodes/variables.rb +5 -2
- data/lib/opal/parser.rb +306 -71
- data/lib/opal/parser/grammar.rb +2667 -2775
- data/lib/opal/parser/grammar.y +177 -233
- data/lib/opal/parser/lexer.rb +511 -427
- data/lib/opal/parser/sexp.rb +15 -3
- data/lib/opal/source_map.rb +8 -4
- data/lib/opal/sprockets.rb +4 -0
- data/lib/opal/sprockets/cache_key_fix.rb +17 -0
- data/lib/opal/sprockets/environment.rb +21 -0
- data/lib/opal/sprockets/erb.rb +30 -0
- data/lib/opal/sprockets/processor.rb +127 -0
- data/lib/opal/sprockets/server.rb +166 -0
- data/lib/opal/util.rb +29 -0
- data/lib/opal/version.rb +1 -1
- data/opal.gemspec +1 -1
- data/opal/corelib/array.rb +106 -187
- data/opal/corelib/array/inheritance.rb +113 -0
- data/opal/corelib/basic_object.rb +6 -2
- data/opal/corelib/boolean.rb +4 -0
- data/opal/corelib/class.rb +2 -0
- data/opal/corelib/complex.rb +3 -0
- data/opal/corelib/enumerable.rb +75 -8
- data/opal/corelib/enumerator.rb +2 -0
- data/opal/corelib/error.rb +23 -23
- data/opal/corelib/hash.rb +5 -5
- data/opal/corelib/helpers.rb +51 -16
- data/opal/corelib/io.rb +7 -24
- data/opal/corelib/kernel.rb +23 -11
- data/opal/corelib/module.rb +44 -47
- data/opal/corelib/nil_class.rb +4 -0
- data/opal/corelib/numeric.rb +101 -15
- data/opal/corelib/range.rb +2 -0
- data/opal/corelib/rational.rb +3 -0
- data/opal/corelib/regexp.rb +36 -17
- data/opal/corelib/runtime.js +22 -7
- data/opal/corelib/string.rb +213 -110
- data/opal/corelib/string/inheritance.rb +78 -0
- data/opal/corelib/struct.rb +8 -0
- data/opal/corelib/time.rb +54 -42
- data/opal/corelib/variables.rb +24 -0
- data/opal/opal.rb +5 -27
- data/spec/cli/compiler_spec.rb +136 -0
- data/spec/cli/dependency_resolver_spec.rb +40 -0
- data/spec/cli/lexer_spec.rb +110 -0
- data/spec/cli/parser/alias_spec.rb +26 -0
- data/spec/cli/parser/and_spec.rb +13 -0
- data/spec/cli/parser/attrasgn_spec.rb +28 -0
- data/spec/cli/parser/begin_spec.rb +42 -0
- data/spec/cli/parser/block_spec.rb +12 -0
- data/spec/cli/parser/break_spec.rb +17 -0
- data/spec/cli/parser/call_spec.rb +139 -0
- data/spec/cli/parser/class_spec.rb +35 -0
- data/spec/cli/parser/comments_spec.rb +11 -0
- data/spec/cli/parser/def_spec.rb +61 -0
- data/spec/cli/parser/if_spec.rb +26 -0
- data/spec/cli/parser/iter_spec.rb +59 -0
- data/spec/cli/parser/lambda_spec.rb +64 -0
- data/spec/cli/parser/literal_spec.rb +113 -0
- data/spec/cli/parser/masgn_spec.rb +37 -0
- data/spec/cli/parser/module_spec.rb +27 -0
- data/spec/cli/parser/not_spec.rb +27 -0
- data/spec/cli/parser/op_asgn1_spec.rb +23 -0
- data/spec/cli/parser/op_asgn2_spec.rb +23 -0
- data/spec/cli/parser/or_spec.rb +13 -0
- data/spec/cli/parser/return_spec.rb +17 -0
- data/spec/cli/parser/sclass_spec.rb +21 -0
- data/spec/cli/parser/string_spec.rb +269 -0
- data/spec/cli/parser/super_spec.rb +20 -0
- data/spec/cli/parser/undef_spec.rb +15 -0
- data/spec/cli/parser/unless_spec.rb +13 -0
- data/spec/cli/parser/variables_spec.rb +92 -0
- data/spec/cli/parser/while_spec.rb +15 -0
- data/spec/cli/parser/yield_spec.rb +20 -0
- data/spec/cli/spec_helper.rb +31 -11
- data/spec/opal/core/array/set_range_to_array_spec.rb +7 -0
- data/spec/opal/core/date_spec.rb +122 -0
- data/spec/opal/core/language/predefined_spec.rb +1 -1
- data/spec/opal/core/runtime/operator_call_spec.rb +13 -0
- data/spec/opal/core/runtime/truthy_spec.rb +23 -0
- data/spec/opal/filters/bugs/array.rb +96 -87
- data/spec/opal/filters/bugs/basic_object.rb +9 -0
- data/spec/opal/filters/bugs/class.rb +16 -0
- data/spec/opal/filters/bugs/enumerable.rb +54 -0
- data/spec/opal/filters/bugs/language.rb +37 -3
- data/spec/opal/filters/bugs/math.rb +93 -0
- data/spec/opal/filters/bugs/nil.rb +7 -0
- data/spec/opal/filters/bugs/numeric.rb +19 -0
- data/spec/opal/filters/bugs/opal.rb +12 -0
- data/spec/opal/filters/bugs/regexp.rb +0 -2
- data/spec/opal/filters/bugs/string.rb +317 -19
- data/spec/opal/filters/bugs/struct.rb +29 -0
- data/spec/opal/filters/bugs/time.rb +130 -9
- data/spec/opal/filters/unsupported/encoding.rb +52 -4
- data/spec/opal/filters/unsupported/enumerator.rb +0 -3
- data/spec/opal/filters/unsupported/integer_size.rb +7 -0
- data/spec/opal/filters/unsupported/method_added.rb +10 -0
- data/spec/opal/filters/unsupported/mutable_strings.rb +299 -1
- data/spec/opal/filters/unsupported/private_constants.rb +30 -0
- data/spec/opal/filters/unsupported/private_methods.rb +16 -0
- data/spec/opal/filters/unsupported/random.rb +4 -0
- data/spec/opal/filters/unsupported/tainted.rb +53 -0
- data/spec/opal/filters/unsupported/trusted.rb +5 -0
- data/spec/opal/rubyspecs +167 -234
- data/spec/opal/spec_helper.rb +3 -0
- data/spec/opal/stdlib/promise/error_spec.rb +15 -0
- data/spec/opal/stdlib/promise/rescue_spec.rb +35 -0
- data/spec/opal/stdlib/promise/then_spec.rb +54 -0
- data/spec/opal/stdlib/promise/trace_spec.rb +35 -0
- data/spec/opal/stdlib/promise/value_spec.rb +15 -0
- data/spec/opal/stdlib/promise/when_spec.rb +34 -0
- data/stdlib/base64.rb +152 -0
- data/stdlib/date.rb +82 -49
- data/{opal/corelib → stdlib}/encoding.rb +3 -1
- data/stdlib/erb.rb +0 -1
- data/stdlib/json.rb +10 -26
- data/stdlib/math.rb +370 -0
- data/stdlib/native.rb +40 -33
- data/stdlib/opal-parser.rb +7 -4
- data/stdlib/promise.rb +292 -0
- data/stdlib/strscan.rb +1 -1
- data/stdlib/template.rb +1 -3
- data/stdlib/time.rb +9 -0
- metadata +143 -204
- data/doc/compiler.md +0 -42
- data/doc/compiler_options.md +0 -5
- data/doc/examples/node_http_server.rb +0 -49
- data/doc/external_libraries.md +0 -9
- data/doc/generated_javascript.md +0 -272
- data/doc/home.md +0 -17
- data/doc/method_missing.md +0 -58
- data/doc/static_applications.md +0 -60
- data/doc/using_ruby_from_javascript.md +0 -18
- data/doc/using_sprockets.md +0 -65
- data/spec/opal/core/numeric/abs_spec.rb +0 -12
- data/spec/opal/core/numeric/downto_spec.rb +0 -19
- data/spec/opal/core/numeric/equal_value_spec.rb +0 -9
- data/spec/opal/core/numeric/even_spec.rb +0 -21
- data/spec/opal/core/numeric/magnitude_spec.rb +0 -12
- data/spec/opal/core/numeric/odd_spec.rb +0 -21
- data/spec/opal/core/string/chop_spec.rb +0 -10
- data/spec/opal/core/string/chr_spec.rb +0 -13
- data/spec/opal/core/string/clone_spec.rb +0 -8
- data/spec/opal/core/string/comparison_spec.rb +0 -13
- data/spec/opal/core/string/dup_spec.rb +0 -8
- data/spec/opal/core/string/element_reference_spec.rb +0 -96
- data/spec/opal/core/string/fixtures/classes.rb +0 -49
- data/spec/opal/core/string/format_spec.rb +0 -9
- data/spec/opal/core/string/freeze_spec.rb +0 -15
- data/spec/opal/core/string/gsub_spec.rb +0 -31
- data/spec/opal/core/string/lines_spec.rb +0 -9
- data/spec/opal/core/string/ljust_spec.rb +0 -32
- data/spec/opal/core/string/lstrip_spec.rb +0 -7
- data/spec/opal/core/string/match_spec.rb +0 -49
- data/spec/opal/core/string/next_spec.rb +0 -10
- data/spec/opal/core/string/ord_spec.rb +0 -9
- data/spec/opal/core/string/partition_spec.rb +0 -10
- data/spec/opal/core/string/rindex_spec.rb +0 -50
- data/spec/opal/core/string/rjust_spec.rb +0 -32
- data/spec/opal/core/string/rstrip_spec.rb +0 -7
- data/spec/opal/core/string/scan_spec.rb +0 -66
- data/spec/opal/core/string/slice_spec.rb +0 -74
- data/spec/opal/core/string/split_spec.rb +0 -5
- data/spec/opal/core/string/strip_spec.rb +0 -6
- data/spec/opal/core/string/sub_spec.rb +0 -38
- data/spec/opal/core/string/succ_spec.rb +0 -10
- data/spec/opal/core/string/sum_spec.rb +0 -5
- data/spec/opal/core/string/to_f_spec.rb +0 -14
- data/spec/opal/core/string/to_i_spec.rb +0 -25
- data/spec/opal/core/string/tr_s_spec.rb +0 -31
- data/spec/opal/core/string/tr_spec.rb +0 -31
- data/spec/opal/filters/bugs/parser.rb +0 -10
- data/spec/opal/filters/unsupported/immutable_strings.rb +0 -24
- data/spec/opal/filters/unsupported/string_subclasses.rb +0 -8
- data/spec/opal/parser/alias_spec.rb +0 -26
- data/spec/opal/parser/and_spec.rb +0 -13
- data/spec/opal/parser/array_spec.rb +0 -22
- data/spec/opal/parser/attrasgn_spec.rb +0 -28
- data/spec/opal/parser/begin_spec.rb +0 -42
- data/spec/opal/parser/block_spec.rb +0 -12
- data/spec/opal/parser/break_spec.rb +0 -17
- data/spec/opal/parser/call_spec.rb +0 -131
- data/spec/opal/parser/class_spec.rb +0 -35
- data/spec/opal/parser/const_spec.rb +0 -13
- data/spec/opal/parser/cvar_spec.rb +0 -11
- data/spec/opal/parser/def_spec.rb +0 -61
- data/spec/opal/parser/false_spec.rb +0 -17
- data/spec/opal/parser/file_spec.rb +0 -7
- data/spec/opal/parser/gvar_spec.rb +0 -13
- data/spec/opal/parser/hash_spec.rb +0 -17
- data/spec/opal/parser/heredoc_spec.rb +0 -30
- data/spec/opal/parser/iasgn_spec.rb +0 -9
- data/spec/opal/parser/if_spec.rb +0 -26
- data/spec/opal/parser/int_spec.rb +0 -13
- data/spec/opal/parser/iter_spec.rb +0 -59
- data/spec/opal/parser/ivar_spec.rb +0 -9
- data/spec/opal/parser/lambda_spec.rb +0 -64
- data/spec/opal/parser/lasgn_spec.rb +0 -8
- data/spec/opal/parser/line_spec.rb +0 -8
- data/spec/opal/parser/lvar_spec.rb +0 -38
- data/spec/opal/parser/masgn_spec.rb +0 -37
- data/spec/opal/parser/module_spec.rb +0 -27
- data/spec/opal/parser/nil_spec.rb +0 -17
- data/spec/opal/parser/not_spec.rb +0 -27
- data/spec/opal/parser/nth_ref_spec.rb +0 -13
- data/spec/opal/parser/op_asgn1_spec.rb +0 -23
- data/spec/opal/parser/op_asgn2_spec.rb +0 -23
- data/spec/opal/parser/or_spec.rb +0 -13
- data/spec/opal/parser/parse_spec.rb +0 -66
- data/spec/opal/parser/regexp_spec.rb +0 -16
- data/spec/opal/parser/return_spec.rb +0 -17
- data/spec/opal/parser/sclass_spec.rb +0 -21
- data/spec/opal/parser/self_spec.rb +0 -17
- data/spec/opal/parser/str_spec.rb +0 -107
- data/spec/opal/parser/string_spec.rb +0 -8
- data/spec/opal/parser/super_spec.rb +0 -20
- data/spec/opal/parser/true_spec.rb +0 -17
- data/spec/opal/parser/undef_spec.rb +0 -15
- data/spec/opal/parser/unless_spec.rb +0 -13
- data/spec/opal/parser/while_spec.rb +0 -15
- data/spec/opal/parser/xstr_spec.rb +0 -116
- data/spec/opal/parser/yield_spec.rb +0 -20
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path('../spec_helper.rb', __FILE__)
|
2
|
+
|
3
|
+
describe Opal::Nodes::CallNode::DependencyResolver do
|
4
|
+
let(:compiler) { double(:compiler, :dynamic_require_severity => :none) }
|
5
|
+
|
6
|
+
it "resolves simple strings to themselves" do
|
7
|
+
expect(resolve s(:str, 'foo')).to eq('foo')
|
8
|
+
end
|
9
|
+
|
10
|
+
context "using a dynamic segment not supported" do
|
11
|
+
it "raises a compiler error when severity is :error" do
|
12
|
+
compiler = double(:compiler, :dynamic_require_severity => :error)
|
13
|
+
expect(compiler).to receive(:error).once
|
14
|
+
expect(compiler).to receive(:dynamic_require_severity).once
|
15
|
+
described_class.new(compiler, s(:self)).resolve
|
16
|
+
end
|
17
|
+
|
18
|
+
it "produces a compiler warning when severity is :warning" do
|
19
|
+
compiler = double(:compiler, :dynamic_require_severity => :warning)
|
20
|
+
expect(compiler).to receive(:warning).once
|
21
|
+
expect(compiler).to receive(:dynamic_require_severity).once
|
22
|
+
described_class.new(compiler, s(:self)).resolve
|
23
|
+
end
|
24
|
+
|
25
|
+
it "does not produce a warning or error for other options" do
|
26
|
+
compiler = double(:compiler, :dynamic_require_severity => :foo)
|
27
|
+
expect(compiler).to_not receive(:warning)
|
28
|
+
expect(compiler).to_not receive(:error)
|
29
|
+
described_class.new(compiler, s(:self)).resolve
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def s(*parts)
|
34
|
+
Opal::Sexp.new(parts)
|
35
|
+
end
|
36
|
+
|
37
|
+
def resolve(sexp)
|
38
|
+
described_class.new(compiler, sexp).resolve
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
2
|
+
require 'opal/parser'
|
3
|
+
|
4
|
+
describe Opal::Lexer do
|
5
|
+
it "sets correct line information for each token" do
|
6
|
+
expect_lines("42").to eq([1])
|
7
|
+
expect_lines("\n3.142").to eq([2])
|
8
|
+
expect_lines("3.142\n42\n57").to eq([1, 2, 3])
|
9
|
+
end
|
10
|
+
|
11
|
+
it "increments the line count over multiple new lines" do
|
12
|
+
expect_lines("1\n\n\n2").to eq([1, 4])
|
13
|
+
expect_lines("\n\n\n3\n\n5\n\n").to eq([4, 6])
|
14
|
+
end
|
15
|
+
|
16
|
+
it "increments line numbers over =begin...=end blocks" do
|
17
|
+
expect_lines("=begin\n=end\n1").to eq([3])
|
18
|
+
expect_lines("=begin\nfoo\nbar\n=end\n42\n43").to eq([5, 6])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "sets correct column for each token" do
|
22
|
+
expect_columns("1").to eq([0])
|
23
|
+
expect_columns("1;2; 3").to eq([0, 2, 5])
|
24
|
+
expect_columns(" \t3").to eq([5])
|
25
|
+
end
|
26
|
+
|
27
|
+
it "sets the column to 0 on each new line" do
|
28
|
+
expect_columns("1\n2\n\n\n3\n 4").to eq([0, 0, 0, 1])
|
29
|
+
end
|
30
|
+
|
31
|
+
it "sets column with regard to whitespace and other tokens before it" do
|
32
|
+
expect_columns("true; false; self\n ni;").to eq([0, 7, 15, 2])
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "double-quoted strings" do
|
36
|
+
it "escape backslashes in strings" do
|
37
|
+
expect_parsed_string("\"foo\"").to eq("foo")
|
38
|
+
expect_parsed_string("\"foo\\tbar\"").to eq("foo\tbar")
|
39
|
+
expect_parsed_string("\"\\\"foo\"").to eq("\"foo")
|
40
|
+
end
|
41
|
+
|
42
|
+
it "removes new line in string directly after backslash" do
|
43
|
+
expect_parsed_string("\"foo\\\nbar\"").to eq("foobar")
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "single-quoted strings" do
|
48
|
+
it "do not support interpolation" do
|
49
|
+
expect_parsed_string("'foo\#{self}'").to eq('foo#{self}')
|
50
|
+
expect_parsed_string("'\#@bar'").to eq('#@bar')
|
51
|
+
expect_parsed_string("'\#$baz'").to eq('#$baz')
|
52
|
+
end
|
53
|
+
|
54
|
+
it "do not escape backslashed characters" do
|
55
|
+
expect_parsed_string("'foo'").to eq("foo")
|
56
|
+
expect_parsed_string("'foo\\tbar'").to eq("foo\\tbar")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "can escape \\ and \' characters" do
|
60
|
+
expect_parsed_string("'a\\\\b\\'c'").to eq("a\\b'c")
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
describe "escaped characters" do
|
65
|
+
it "can parse octal escape sequences" do
|
66
|
+
expect_parsed_string('"\\101\\103\\102"').to eq("ACB")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "can parse hex escape sequences" do
|
70
|
+
expect_parsed_string('"\\x61\\x63\\x62"').to eq('acb')
|
71
|
+
end
|
72
|
+
|
73
|
+
if defined?(::Encoding)
|
74
|
+
it "can parse simple unicode sequences" do
|
75
|
+
expect_parsed_string('"\\u1234"').to eq("\u1234")
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe "__END__ content in a source file" do
|
81
|
+
it "ignores token and following content if at start of line and followed by newline" do
|
82
|
+
expect_parsed("42\n__END__").to eq([:int, 42])
|
83
|
+
expect_parsed("42\n__END__\nFred").to eq([:int, 42])
|
84
|
+
expect_parsed(" __END__").to eq([:call, nil, :__END__, [:arglist]])
|
85
|
+
expect_parsed("__END__ ").to eq([:call, nil, :__END__, [:arglist]])
|
86
|
+
expect_parsed("__END__ 42").to eq([:call, nil, :__END__, [:arglist, [:int, 42]]])
|
87
|
+
end
|
88
|
+
|
89
|
+
it "adds any __END__ content to lexer.eof_content" do
|
90
|
+
parser = Opal::Parser.new
|
91
|
+
parser.parse("42")
|
92
|
+
expect(parser.lexer.eof_content).to eq(nil)
|
93
|
+
|
94
|
+
parser = Opal::Parser.new
|
95
|
+
parser.parse("42\n__END__\nFord\nPerfect")
|
96
|
+
expect(parser.lexer.eof_content).to eq("Ford\nPerfect")
|
97
|
+
|
98
|
+
parser = Opal::Parser.new
|
99
|
+
parser.parse("42\n__END__\n")
|
100
|
+
expect(parser.lexer.eof_content).to eq("")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "variable dereferencing ([])" do
|
105
|
+
it "handles referencing correctly on local variables in scope" do
|
106
|
+
expect_parsed("x = 0; x [0]").to eq([:block, [:lasgn, :x, [:int, 0]],
|
107
|
+
[:call, [:lvar, :x], :[], [:arglist, [:int, 0]]]])
|
108
|
+
end
|
109
|
+
end
|
110
|
+
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(:sym)" do
|
6
|
+
parsed("alias a b").should == [:alias, [:sym, :a], [:sym, :b]]
|
7
|
+
parsed("alias == equals").should == [:alias, [:sym, :==], [:sym, :equals]]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should accept symbols as names" do
|
11
|
+
parsed("alias :foo :bar").should == [:alias, [:sym, :foo], [:sym, :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
|
+
parsed("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
|
+
parsed("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
|
+
parsed("1 and 2").should == [:and, [:int, 1], [:int, 2]]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "The && expression" do
|
10
|
+
it "should always return s(:and)" do
|
11
|
+
parsed("1 && 2").should == [:and, [:int, 1], [:int, 2]]
|
12
|
+
end
|
13
|
+
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
|
+
parsed('self.foo = 1').should == [:attrasgn, [:self], :foo=, [:arglist, [:int, 1]]]
|
6
|
+
parsed('bar.foo = 1').should == [:attrasgn, [:call, nil, :bar, [:arglist]], :foo=, [:arglist, [:int, 1]]]
|
7
|
+
parsed('@bar.foo = 1').should == [:attrasgn, [:ivar, :@bar], :foo=, [:arglist, [:int, 1]]]
|
8
|
+
end
|
9
|
+
|
10
|
+
it "accepts both '.' and '::' for method call operators" do
|
11
|
+
parsed('self.foo = 1').should == [:attrasgn, [:self], :foo=, [:arglist, [:int, 1]]]
|
12
|
+
parsed('self::foo = 1').should == [:attrasgn, [:self], :foo=, [:arglist, [:int, 1]]]
|
13
|
+
end
|
14
|
+
|
15
|
+
it "can accept a constant as assignable name when using '.'" do
|
16
|
+
parsed('self.FOO = 1').should == [:attrasgn, [:self], :FOO=, [:arglist, [:int, 1]]]
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "when setting element reference" do
|
20
|
+
it "uses []= as the method call" do
|
21
|
+
parsed('self[1] = 2').should == [:attrasgn, [:self], :[]=, [:arglist, [:int, 1], [:int, 2]]]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "supports multiple arguments inside brackets" do
|
25
|
+
parsed('self[1, 2] = 3').should == [:attrasgn, [:self], :[]=, [:arglist, [:int, 1], [:int, 2], [:int, 3]]]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,42 @@
|
|
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
|
+
parsed('begin; 1; end').should == [:int, 1]
|
6
|
+
parsed('begin; 1; 2; end').should == [:block, [:int, 1], [:int, 2]]
|
7
|
+
end
|
8
|
+
|
9
|
+
describe "with 'rescue' bodies" do
|
10
|
+
it "should create a s(:rescue) sexp" do
|
11
|
+
parsed('begin; 1; rescue; 2; end').should == [:rescue, [:int, 1], [:resbody, [:array], [:int, 2]]]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "allows multiple rescue bodies" do
|
15
|
+
parsed('begin 1; rescue; 2; rescue; 3; end').should == [:rescue, [:int, 1], [:resbody, [:array], [:int, 2]], [:resbody, [:array], [:int, 3]]]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "accepts a list of classes used to match against the exception" do
|
19
|
+
parsed('begin 1; rescue Klass; 2; end').should == [:rescue, [:int, 1], [:resbody, [:array, [:const, :Klass]], [:int, 2]]]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "accepts an identifier to assign exception to" do
|
23
|
+
parsed('begin 1; rescue => a; 2; end').should == [:rescue, [:int, 1], [:resbody, [:array, [:lasgn, :a, [:gvar, :$!]]], [:int, 2]]]
|
24
|
+
parsed('begin 1; rescue Klass => a; 2; end').should == [:rescue, [:int, 1], [:resbody, [:array, [:const, :Klass],[:lasgn, :a, [:gvar, :$!]]], [:int, 2]]]
|
25
|
+
end
|
26
|
+
|
27
|
+
it "accepts an ivar to assign exception to" do
|
28
|
+
parsed('begin 1; rescue => @a; 2; end').should == [:rescue, [:int, 1], [:resbody, [:array, [:iasgn, :@a, [:gvar, :$!]]], [:int, 2]]]
|
29
|
+
parsed('begin 1; rescue Klass => @a; 2; end').should == [:rescue, [:int, 1], [:resbody, [:array, [:const, :Klass],[:iasgn, :@a, [:gvar, :$!]]], [:int, 2]]]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should parse newline right after rescue" do
|
33
|
+
parsed("begin; 1; rescue\n 2; end").should == [:rescue, [:int, 1], [:resbody, [:array], [:int, 2]]]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "with an 'ensure' block" do
|
38
|
+
it "should propagate into single s(:ensure) statement when no rescue block given" do
|
39
|
+
parsed('begin; 1; ensure; 2; end').should == [:ensure, [:int, 1], [:int, 2]]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
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
|
+
parsed("42").should == [:int, 42]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should return an s(:block) with all expressions appended for > 1 expression" do
|
9
|
+
parsed("42; 43").should == [:block, [:int, 42], [:int, 43]]
|
10
|
+
parsed("42; 43\n44").should == [:block, [:int, 42], [:int, 43], [:int, 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
|
+
parsed("break").should == [:break]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns s(:break) with a single arg not wrapped in s(:array)" do
|
9
|
+
parsed("break 1").should == [:break, [:int, 1]]
|
10
|
+
parsed("break *1").should == [:break, [:splat, [:int, 1]]]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns s(:break) with an s(:array) for args size > 1" do
|
14
|
+
parsed("break 1, 2").should == [:break, [:array, [:int, 1], [:int, 2]]]
|
15
|
+
parsed("break 1, *2").should == [:break, [:array, [:int, 1], [:splat, [:int, 2]]]]
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,139 @@
|
|
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
|
+
parsed("foo").should == [:call, nil, :foo, [:arglist]]
|
6
|
+
parsed("foo()").should == [:call, nil, :foo, [:arglist]]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should always have an arglist when not passed any arguments" do
|
10
|
+
parsed("foo").should == [:call, nil, :foo, [:arglist]]
|
11
|
+
parsed("self.foo").should == [:call, [:self], :foo, [:arglist]]
|
12
|
+
parsed("foo()").should == [:call, nil, :foo, [:arglist]]
|
13
|
+
parsed("self.foo()").should == [:call, [:self], :foo, [:arglist]]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "appends all arguments onto arglist" do
|
17
|
+
parsed("foo 1").should == [:call, nil, :foo, [:arglist, [:int, 1]]]
|
18
|
+
parsed("foo 1, 2").should == [:call, nil, :foo, [:arglist, [:int, 1], [:int, 2]]]
|
19
|
+
parsed("foo 1, *2").should == [:call, nil, :foo, [:arglist, [:int, 1], [:splat, [:int, 2]]]]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "supports leading dots on newline" do
|
23
|
+
parsed("foo\n.bar").should == [:call, [:call, nil, :foo, [:arglist]], :bar, [:arglist]]
|
24
|
+
lambda { parsed("foo\n..bar") }.should raise_error(Exception)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "Operator calls" do
|
29
|
+
it "should parse all other operators into method calls" do
|
30
|
+
parsed("1 % 2").should == [:call, [:int, 1], :%, [:arglist, [:int, 2]]]
|
31
|
+
parsed("1 ** 2").should == [:call, [:int, 1], :**, [:arglist, [:int, 2]]]
|
32
|
+
|
33
|
+
parsed("+self").should == [:call, [:self], :+@, [:arglist]]
|
34
|
+
parsed("-self").should == [:call, [:self], :-@, [:arglist]]
|
35
|
+
|
36
|
+
parsed("1 | 2").should == [:call, [:int, 1], :|, [:arglist, [:int, 2]]]
|
37
|
+
parsed("1 ^ 2").should == [:call, [:int, 1], :^, [:arglist, [:int, 2]]]
|
38
|
+
parsed("1 & 2").should == [:call, [:int, 1], :&, [:arglist, [:int, 2]]]
|
39
|
+
parsed("1 <=> 2").should == [:call, [:int, 1], :<=>, [:arglist, [:int, 2]]]
|
40
|
+
|
41
|
+
parsed("1 < 2").should == [:call, [:int, 1], :<, [:arglist, [:int, 2]]]
|
42
|
+
parsed("1 <= 2").should == [:call, [:int, 1], :<=, [:arglist, [:int, 2]]]
|
43
|
+
parsed("1 > 2").should == [:call, [:int, 1], :>, [:arglist, [:int, 2]]]
|
44
|
+
parsed("1 >= 2").should == [:call, [:int, 1], :>=, [:arglist, [:int, 2]]]
|
45
|
+
|
46
|
+
parsed("1 == 2").should == [:call, [:int, 1], :==, [:arglist, [:int, 2]]]
|
47
|
+
parsed("1 === 2").should == [:call, [:int, 1], :===, [:arglist, [:int, 2]]]
|
48
|
+
parsed("1 =~ 2").should == [:call, [:int, 1], :=~, [:arglist, [:int, 2]]]
|
49
|
+
|
50
|
+
parsed("~1").should == [:call, [:int, 1], :~, [:arglist]]
|
51
|
+
parsed("1 << 2").should == [:call, [:int, 1], :<<, [:arglist, [:int, 2]]]
|
52
|
+
parsed("1 >> 2").should == [:call, [:int, 1], :>>, [:arglist, [:int, 2]]]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "optimizes +@ and -@ on numerics" do
|
56
|
+
parsed("+1").should == [:int, 1]
|
57
|
+
parsed("-1").should == [:int, -1]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "Optional paren calls" do
|
62
|
+
it "should correctly parse - and -@" do
|
63
|
+
parsed("x - 1").should == [:call, [:call, nil, :x, [:arglist]], :-, [:arglist, [:int, 1]]]
|
64
|
+
parsed("x -1").should == [:call, nil, :x, [:arglist, [:int, -1]]]
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should correctly parse + and +@" do
|
68
|
+
parsed("x + 1").should == [:call, [:call, nil, :x, [:arglist]], :+, [:arglist, [:int, 1]]]
|
69
|
+
parsed("x +1").should == [:call, nil, :x, [:arglist, [:int, 1]]]
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should correctly parse / and regexps" do
|
73
|
+
parsed("x / 500").should == [:call, [:call, nil, :x, [:arglist]], :/, [:arglist, [:int, 500]]]
|
74
|
+
parsed("x /foo/").should == [:call, nil, :x, [:arglist, [:regexp, 'foo', nil]]]
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should parse LPAREN_ARG correctly" do
|
78
|
+
parsed("x (1).y").should == [:call, nil, :x, [:arglist, [:call, [:int, 1], :y, [:arglist]]]]
|
79
|
+
parsed("x(1).y").should == [:call, [:call, nil, :x, [:arglist, [:int, 1]]], :y, [:arglist]]
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe "Operator precedence" do
|
84
|
+
it "should be raised with parentheses" do
|
85
|
+
parsed("(1 + 2) + (3 - 4)").should == [:call,
|
86
|
+
[:paren, [:call, [:int, 1], :+, [:arglist, [:int, 2]]]],
|
87
|
+
:+,
|
88
|
+
[:arglist, [:paren, [:call, [:int, 3], :-, [:arglist, [:int, 4]]]]],
|
89
|
+
]
|
90
|
+
parsed("(1 + 2) - (3 - 4)").should == [:call,
|
91
|
+
[:paren, [:call, [:int, 1], :+, [:arglist, [:int, 2]]]],
|
92
|
+
:-,
|
93
|
+
[:arglist, [:paren, [:call, [:int, 3], :-, [:arglist, [:int, 4]]]]],
|
94
|
+
]
|
95
|
+
parsed("(1 + 2) * (3 - 4)").should == [:call,
|
96
|
+
[:paren, [:call, [:int, 1], :+, [:arglist, [:int, 2]]]],
|
97
|
+
:*,
|
98
|
+
[:arglist, [:paren, [:call, [:int, 3], :-, [:arglist, [:int, 4]]]]],
|
99
|
+
]
|
100
|
+
parsed("(1 + 2) / (3 - 4)").should == [:call,
|
101
|
+
[:paren, [:call, [:int, 1], :+, [:arglist, [:int, 2]]]],
|
102
|
+
:/,
|
103
|
+
[:arglist, [:paren, [:call, [:int, 3], :-, [:arglist, [:int, 4]]]]],
|
104
|
+
]
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
describe "Calls with keywords as method names" do
|
109
|
+
|
110
|
+
keywords = %w[class module defined? def undef end do if unless else elsif self true false
|
111
|
+
nil __LINE__ __FILE__ begin rescue ensure case when or and not return next
|
112
|
+
redo break super then while until yield alias]
|
113
|
+
|
114
|
+
it "should correctly parse the keyword as a method name when after a '.'" do
|
115
|
+
keywords.each do |kw|
|
116
|
+
parsed("self.#{kw}").should == [:call, [:self], kw.to_sym, [:arglist]]
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "Calls with operators as method names" do
|
122
|
+
operators = %w[+ - * / & ** | ^ & <=> > >= < <= << >>]
|
123
|
+
|
124
|
+
it "should correctly parse the operator as method name after '.'" do
|
125
|
+
operators.each do |op|
|
126
|
+
parsed("self.#{op}").should == [:call, [:self], op.to_sym, [:arglist]]
|
127
|
+
parsed("self.#{op}(1)").should == [:call, [:self], op.to_sym, [:arglist, [:int, 1]]]
|
128
|
+
parsed("self.#{op}(1, 2)").should == [:call, [:self], op.to_sym, [:arglist, [:int, 1], [:int, 2]]]
|
129
|
+
end
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
describe "Command calls with operators" do
|
134
|
+
it "parses operators before \n in command calls" do
|
135
|
+
[:<<, :>>, :|, :^, :&, :<=>, :==, :===, :=~, :>, :>=, :<, :<=, :<<, :>>, :%, :**].each do |mid|
|
136
|
+
parsed("self #{mid}\nself").should == [:call, [:self], mid, [:arglist, [:self]]]
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require File.expand_path('../../spec_helper', __FILE__)
|
2
|
+
|
3
|
+
describe "The class keyword" do
|
4
|
+
it "returns an empty s(:block) when given an empty body" do
|
5
|
+
parsed('class A; end').should == [:class, [:const, :A], nil, [:block]]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "does not place single expressions into a s(:block)" do
|
9
|
+
parsed('class A; 1; end').should == [:class, [:const, :A], nil, [:int, 1]]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "adds multiple body expressions into a s(:block)" do
|
13
|
+
parsed('class A; 1; 2; end').should == [:class, [:const, :A], nil, [:block, [:int, 1], [:int, 2]]]
|
14
|
+
end
|
15
|
+
|
16
|
+
it "uses nil as a placeholder when no superclass is given" do
|
17
|
+
parsed('class A; end')[2].should == nil
|
18
|
+
end
|
19
|
+
|
20
|
+
it "reflects the given superclass" do
|
21
|
+
parsed('class A < B; end')[2].should == [:const, :B]
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should accept just a constant for the class name" do
|
25
|
+
parsed('class A; end')[1].should == [:const, :A]
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept a prefix constant for the class name" do
|
29
|
+
parsed('class ::A; end')[1].should == [:colon3, :A]
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should accept a nested constant for the class name" do
|
33
|
+
parsed('class A::B; end')[1].should == [:colon2, [:const, :A], :B]
|
34
|
+
end
|
35
|
+
end
|