From 0dcee637d3792ed95fa2640f1f8b59ce9dc03a61 Mon Sep 17 00:00:00 2001 From: Yorick Peterse Date: Tue, 18 Aug 2015 16:51:01 +0200 Subject: [PATCH] Added Ruby::Node#if_false --- lib/oga/ruby/node.rb | 9 +++++++++ spec/oga/ruby/node_spec.rb | 15 +++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/oga/ruby/node.rb b/lib/oga/ruby/node.rb index 5927392..682b686 100644 --- a/lib/oga/ruby/node.rb +++ b/lib/oga/ruby/node.rb @@ -148,6 +148,15 @@ module Oga Node.new(:if, [self, yield]) end + ## + # Wraps the current node in an `if !...` statement. + # + # @see [#if_true] + # + def if_false + self.not.if_true { yield } + end + ## # Wraps the current node in a `while` statement. # diff --git a/spec/oga/ruby/node_spec.rb b/spec/oga/ruby/node_spec.rb index 1e12176..8f29617 100644 --- a/spec/oga/ruby/node_spec.rb +++ b/spec/oga/ruby/node_spec.rb @@ -133,6 +133,21 @@ describe Oga::Ruby::Node do end end + describe '#if_false' 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_false { body } + + statement.type.should == :if + + statement.to_a[0].type.should == :send + + statement.to_a[0].to_a.should == [condition, '!'] + statement.to_a[1].should == body + end + end + describe '#while_true' do it 'returns a while statement Node' do condition = described_class.new(:lit, %w{number})