From 04aa8f654644edbd2eb1690eca821cb2ea4fba63 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Mon, 27 Jul 2015 00:28:07 +0200 Subject: [PATCH] Ruby generator support for "begin" blocks --- lib/oga/ruby/generator.rb | 16 ++++++++++++++++ lib/oga/ruby/node.rb | 9 +++++++++ spec/oga/ruby/generator_spec.rb | 13 +++++++++++++ spec/oga/ruby/node_spec.rb | 10 ++++++++++ 4 files changed, 48 insertions(+) diff --git a/lib/oga/ruby/generator.rb b/lib/oga/ruby/generator.rb index 1946961..c15aae6 100644 --- a/lib/oga/ruby/generator.rb +++ b/lib/oga/ruby/generator.rb @@ -52,6 +52,22 @@ module Oga "#{var_names.join(', ')} = #{val_str}" end + ## + # Processes a `begin` node. + # + # @param [Oga::Ruby::Node] ast + # @return [String] + # + def on_begin(ast) + body = process(ast.to_a[0]) + + <<-EOF +begin + #{body} +end + EOF + end + ## # Processes an equality node. # diff --git a/lib/oga/ruby/node.rb b/lib/oga/ruby/node.rb index 7e2c0dd..bf11e0e 100644 --- a/lib/oga/ruby/node.rb +++ b/lib/oga/ruby/node.rb @@ -104,6 +104,15 @@ module Oga Node.new(:block, [self, args, yield]) end + ## + # Wraps the current node in a `begin` node. + # + # @return [Oga::Ruby::Node] + # + def wrap + Node.new(:begin, [self]) + end + ## # Wraps the current node in an if statement node. # diff --git a/spec/oga/ruby/generator_spec.rb b/spec/oga/ruby/generator_spec.rb index ca046c4..7d531c7 100644 --- a/spec/oga/ruby/generator_spec.rb +++ b/spec/oga/ruby/generator_spec.rb @@ -37,6 +37,19 @@ describe Oga::Ruby::Generator do end end + describe '#on_begin' do + it 'returns a String' do + number = Oga::Ruby::Node.new(:lit, %w{10}) + node = Oga::Ruby::Node.new(:begin, [number]) + + @generator.on_begin(node).should == <<-EOF +begin + 10 +end + EOF + end + end + describe '#on_eq' do it 'returns a String' do var = Oga::Ruby::Node.new(:lit, %w{number}) diff --git a/spec/oga/ruby/node_spec.rb b/spec/oga/ruby/node_spec.rb index 9eca407..c13dd20 100644 --- a/spec/oga/ruby/node_spec.rb +++ b/spec/oga/ruby/node_spec.rb @@ -88,6 +88,16 @@ describe Oga::Ruby::Node do end end + describe '#wrap' do + it 'returns a begin Node' do + number = described_class.new(:lit, %w{10}) + wrapped = number.wrap + + wrapped.type.should == :begin + wrapped.to_a.should == [number] + end + end + describe '#if_true' do it 'returns an if statement Node' do condition = described_class.new(:lit, %w{number})