Ruby generator support for while loops
This commit is contained in:
parent
9925b2a9c9
commit
d8fbaf75d8
|
@ -117,6 +117,25 @@ end
|
|||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Processes a while statement node.
|
||||
#
|
||||
# @param [Oga::Ruby::Node] ast
|
||||
# @return [String]
|
||||
#
|
||||
def on_while(ast)
|
||||
cond, body = *ast
|
||||
|
||||
cond_str = process(cond)
|
||||
body_str = process(body)
|
||||
|
||||
<<-EOF
|
||||
while #{cond_str}
|
||||
#{body_str}
|
||||
end
|
||||
EOF
|
||||
end
|
||||
|
||||
##
|
||||
# Processes a method call node.
|
||||
#
|
||||
|
|
|
@ -114,6 +114,18 @@ module Oga
|
|||
Node.new(:if, [self, yield])
|
||||
end
|
||||
|
||||
##
|
||||
# Wraps the current node in a `while` statement.
|
||||
#
|
||||
# The body of this statement is set to the return value of the supplied
|
||||
# block.
|
||||
#
|
||||
# @return [Oga::Ruby::Node]
|
||||
#
|
||||
def while_true
|
||||
Node.new(:while, [self, yield])
|
||||
end
|
||||
|
||||
##
|
||||
# Adds an "else" statement to the current node.
|
||||
#
|
||||
|
|
|
@ -87,6 +87,20 @@ end
|
|||
end
|
||||
end
|
||||
|
||||
describe '#on_while' do
|
||||
it 'returns a String' do
|
||||
statement = Oga::Ruby::Node.new(:lit, %w{foo}).while_true do
|
||||
Oga::Ruby::Node.new(:lit, %w{bar})
|
||||
end
|
||||
|
||||
@generator.on_while(statement).should == <<-EOF
|
||||
while foo
|
||||
bar
|
||||
end
|
||||
EOF
|
||||
end
|
||||
end
|
||||
|
||||
describe '#on_send' do
|
||||
describe 'without arguments' do
|
||||
it 'returns a String' do
|
||||
|
|
|
@ -89,7 +89,7 @@ describe Oga::Ruby::Node do
|
|||
end
|
||||
|
||||
describe '#if_true' do
|
||||
it 'returns an if-statement Node' do
|
||||
it 'returns an if statement Node' do
|
||||
condition = described_class.new(:lit, %w{number})
|
||||
body = described_class.new(:lit, %w{10})
|
||||
statement = condition.if_true { body }
|
||||
|
@ -99,6 +99,17 @@ describe Oga::Ruby::Node do
|
|||
end
|
||||
end
|
||||
|
||||
describe '#while_true' do
|
||||
it 'returns a while statement Node' do
|
||||
condition = described_class.new(:lit, %w{number})
|
||||
body = described_class.new(:lit, %w{10})
|
||||
statement = condition.while_true { body }
|
||||
|
||||
statement.type.should == :while
|
||||
statement.to_a.should == [condition, body]
|
||||
end
|
||||
end
|
||||
|
||||
describe '#else' do
|
||||
it 'returns an if-statement with an else clause' do
|
||||
condition = described_class.new(:lit, %w{number})
|
||||
|
|
Loading…
Reference in New Issue