On Sep 19, 2008, at 2:29 PM, Brice Figureau wrote:
>
>             # return result
>             case @operator
> -            when "and": Puppet::Parser::Scope.true?(rval) and  
> Puppet::Parser::Scope.true?(lval)
> -            when "or": Puppet::Parser::Scope.true?(rval) or  
> Puppet::Parser::Scope.true?(lval)
> +            when "==": lval == rval
> +            when "!=": lval != rval
> +            when "<":  lval < rval
> +            when ">":  lval > rval
> +            when "<=": lval <= rval
> +            when ">=": lval >= rval
>             end
>         end

This might be cleaner to just do something like:

lval.send(@operator, rval)
> +
> +    it "should fail for an unknown operator" do
> +        lambda { operator =  
> Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator  
> => "or", :rval => @two }.should raise_error
> +    end
> +
> +    it "should return true for 1 < 2" do
> +        operator =  
> Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator  
> => "<", :rval => @two
> +        operator.evaluate(@scope).should == true
> +    end
> +
> +    it "should return false for 1 > 2" do
> +        operator =  
> Puppet::Parser::AST::ComparisonOperator.new :lval => @one, :operator  
> => ">", :rval => @two
> +        operator.evaluate(@scope).should == false
> +    end

Similarly for this stuff, I would probably do some metaprogramming:

%w{< > <= >=...}.each do |oper|
   it "should return the result of using '#{oper}' to compare the left  
and right sides" do
     operator = Puppet::Parser::AST::ComparisonOperator.new :lval =>  
@one, :operator => oper, :rval => @two
     @one.expects(oper).with(@two).returns "whatever"
     operator.evaluate(@scope).should == "whatever"
   end
end

Because all of these operators follow exactly the same pattern, I  
think this is pretty safe.  Of course, Jay Fields would prefer to draw  
and quarter me for doing this, but I think it's clearer to say that  
all of the operators have the exact same pattern of behaviour than to  
have separate tests for each.

I'd accept it either way, for the record - I'm just noting how I would  
do it.


-- 
It isn't necessary to have relatives in Kansas City in order to be
unhappy. -- Groucho Marx
---------------------------------------------------------------------
Luke Kanies | http://reductivelabs.com | http://madstop.com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to