Skip to content
Browse files

- Fixed attr_* generators in cases where the body isn't idiomatic. (r…

…obertfeldt)

[git-p4: depot-paths = "//src/ruby2ruby/dev/": change = 8298]
  • Loading branch information...
1 parent 65ec59d commit 1e001e3a2728925b998425b031a655eb104a5261 @zenspider zenspider committed Mar 8, 2013
Showing with 59 additions and 4 deletions.
  1. +23 −4 lib/ruby2ruby.rb
  2. +36 −0 test/test_ruby2ruby.rb
View
27 lib/ruby2ruby.rb
@@ -315,15 +315,34 @@ def process_defn(exp)
type1 = exp[1].first
type2 = exp[2].first rescue nil
- if type1 == :args and [:ivar, :attrset].include? type2 then
- name = exp.shift
+ if type1 == :args and [:ivar, :iasgn, :attrset].include? type2 then
+ name = exp.first # don't shift in case we pass through
case type2
when :ivar then
- exp.clear
- return "attr_reader #{name.inspect}"
+ ivar_name = exp.ivar.last
+
+ meth_name = ivar_name.to_s[1..-1].to_sym
+ expected = s(meth_name, s(:args), s(:ivar, ivar_name))
+
+ if exp == expected then
+ exp.clear
+ return "attr_reader #{name.inspect}"
+ end
when :attrset then
+ # TODO: deprecate? this is a PT relic
exp.clear
return "attr_writer :#{name.to_s[0..-2]}"
+ when :iasgn then
+ ivar_name = exp.iasgn[1]
+ meth_name = "#{ivar_name.to_s[1..-1]}=".to_sym
+ arg_name = exp.args.last
+ expected = s(meth_name, s(:args, arg_name),
+ s(:iasgn, ivar_name, s(:lvar, arg_name)))
+
+ if exp == expected then
+ exp.clear
+ return "attr_writer :#{name.to_s[0..-2]}"
+ end
else
raise "Unknown defn type: #{exp.inspect}"
end
View
36 test/test_ruby2ruby.rb
@@ -80,6 +80,42 @@ def test_and_alias
util_compare inn, out
end
+ def test_attr_reader_diff
+ inn = s(:defn, :same, s(:args), s(:ivar, :@diff))
+ out = "def same\n @diff\nend"
+ util_compare inn, out
+ end
+
+ def test_attr_reader_same
+ inn = s(:defn, :same, s(:args), s(:ivar, :@same))
+ out = "attr_reader :same"
+ util_compare inn, out
+ end
+
+ def test_attr_reader_same_name_diff_body
+ inn = s(:defn, :same, s(:args), s(:not, s(:ivar, :@same)))
+ out = "def same\n (not @same)\nend"
+ util_compare inn, out
+ end
+
+ def test_attr_writer_diff
+ inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@diff, s(:lvar, :o)))
+ out = "def same=(o)\n @diff = o\nend"
+ util_compare inn, out
+ end
+
+ def test_attr_writer_same_name_diff_body
+ inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@same, s(:lit, 42)))
+ out = "def same=(o)\n @same = 42\nend"
+ util_compare inn, out
+ end
+
+ def test_attr_writer_same
+ inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@same , s(:lvar, :o)))
+ out = "attr_writer :same"
+ util_compare inn, out
+ end
+
def test_dregx_slash
inn = util_thingy(:dregx)
out = '/a"b#{(1 + 1)}c"d\/e/'

0 comments on commit 1e001e3

Please sign in to comment.
Something went wrong with that request. Please try again.