1 require File.dirname(__FILE__) + '/../spec_helper'
2 require File.dirname(__FILE__) + '/fixtures/sexp_expectations'
5 alias :old_to_sexp :to_sexp
7 old_to_sexp('test', 1, false)
12 it "converts a number to an sexp" do
13 "834234".to_sexp.should == [:lit, 834234]
16 it "converts a regexp to an sexp" do
17 "/blah/".to_sexp.should == [:regex, "blah", 0]
18 "/blah/i".to_sexp.should == [:regex, "blah", 1]
19 "/blah/u".to_sexp.should == [:regex, "blah", 64]
22 it "converts a string to an sexp" do
23 "\"hello\"".to_sexp.should == [:str, "hello"]
26 it "converts a local var to an sexp" do
27 "a = 1; a".to_sexp.should ==
28 [:block, [:lasgn, :a, [:lit, 1]], [:lvar, :a, 0]]
31 it "converts an instance variable to an sexp" do
32 "@blah".to_sexp.should == [:ivar, :@blah]
35 it "converts an instance variable assignment to an sexp" do
36 "@blah = 1".to_sexp.should == [:iasgn, :@blah, [:lit, 1]]
39 it "converts a global variable to an sexp" do
40 "$blah".to_sexp.should == [:gvar, :$blah]
43 it "converts a global variable assignment to an sexp" do
44 "$blah = 1".to_sexp.should == [:gasgn, :$blah, [:lit, 1]]
47 it "converts a symbol to an sexp" do
48 ":blah".to_sexp.should == [:lit, :blah]
51 it "converts a string expansion to an sexp" do
52 'a = 1; "hello #{a}, you rock."'.to_sexp.should ==
54 [:lasgn, :a, [:lit, 1]],
55 [:dstr, "hello ", [:evstr, [:lvar, :a, 0]],
56 [:str, ", you rock."]]]
59 it "converts a pathological string expansion to an sexp" do
60 '@thing = 5; "hello #@thing, you are crazy."'.to_sexp.should ==
62 [:iasgn, :@thing, [:lit, 5]],
63 [:dstr, "hello ", [:evstr, [:ivar, :@thing]],
64 [:str, ", you are crazy."]]]
67 it "converts a method definition without arguments to an sexp" do
68 "def name; 1; end".to_sexp.should == [:defn, :name, [:scope, [:block, [:args], [:lit, 1]], []]]
71 it "converts a method definition with arguments to an sexp" do
72 "def name(a, b); 1; end".to_sexp.should ==
74 [:scope, [:block, [:args, [:a, :b], [], nil, nil], [:lit, 1]], [:a, :b]]]
77 it "converts a class definition to an sexp" do
78 "class Blah < A::B; end".to_sexp.should ==
80 [:colon2, :Blah], [:colon2, [:const, :A], :B], [:scope, []]]
83 it "converts a heredoc to an sexp" do
87 ".to_sexp.should == [:lasgn, :a, [:str, " hello\n"]]
91 def rewrite_expected array
93 when :alias, :undef then
94 array[1..-1] = array[1..-1].map { |lit| lit.last }
101 when :class, :module then
103 name = [:colon2, name] unless Array === name and name[0] == :colon2
105 array[-1] << [] # no clue
111 array[0..-1] = [:regex, regexp.source] # FIX: add options
114 type = range.exclude_end? ? :dot3 : :dot2
115 array[0..-1] = [type, [:lit, range.begin], [:lit, range.end]] # FIX
120 until array.size >= 3 do
125 array = array.map { |item|
126 if Array === item then
127 rewrite_expected(item)
136 def rewrite_actual array
139 array[-2..-1] = array[-2..-1].reverse
141 array = array.flatten.compact.map { |o| o == true ? :* : o }
143 array[-1] = array[-1].chr.to_sym
145 array = array[2] if array.size == 3 and array[1][0] == :dasgn_curr
146 when :defn, :defs then
147 array[-1].pop # remove local vars list
149 array.pop # remove index
152 array[-1] = -array[-1]
154 array.pop # TODO: add options
160 array = [:match, [:regex, *array[1..-1]]]
163 array = array.map { |item|
164 if Array === item then
174 describe "String#to_sexp" do
175 SEXP_EXPECTATIONS.each do |node, hash|
176 it "parses :#{node}" do
177 expected = rewrite_expected(hash['ParseTree'])
178 actual = rewrite_actual(hash['Ruby'].to_sexp)
179 expected.should == actual