On Jul 27, 2009, at 12:21 AM, Brice Figureau wrote:

>
> On Sun, 2009-07-26 at 21:19 -0700, Luke Kanies wrote:
>> Small comment below (not worth re-reviewing); otherwise, +1.
>>
>> On Jul 26, 2009, at 9:03 AM, Brice Figureau wrote:
>>
>>>
>>> Add a regex rule (unused for the moment) to the parser.
>>>
>>> Signed-off-by: Brice Figureau <[email protected]>
>>> ---
>>> lib/puppet/parser/ast/leaf.rb |   42 ++++++++++++++
>>> lib/puppet/parser/grammar.ra  |    4 ++
>>> lib/puppet/parser/parser.rb   |   85 ++++++++++++++++-------------
>>> spec/unit/parser/ast/leaf.rb  |  121 ++++++++++++++++++++++++++++++ 
>>> ++
>>> ++++++++-
>>> 4 files changed, 213 insertions(+), 39 deletions(-)
>>>
>>> diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/
>>> leaf.rb
>>> index d089380..de5876e 100644
>>> --- a/lib/puppet/parser/ast/leaf.rb
>>> +++ b/lib/puppet/parser/ast/leaf.rb
>>> @@ -10,6 +10,15 @@ class Puppet::Parser::AST
>>>            return @value
>>>        end
>>>
>>> +        # evaluate ourselves, and match
>>> +        def evaluate_match(value, scope, options = {})
>>> +            obj = self.safeevaluate(scope)
>>> +            if ! options[:sensitive] && obj.respond_to?(:downcase)
>>> +                obj = obj.downcase
>>> +            end
>>> +            obj == value
>>> +        end
>>> +
>>>        def to_s
>>>            return @value.to_s unless @value.nil?
>>>        end
>>> @@ -99,4 +108,37 @@ class Puppet::Parser::AST
>>>            end
>>>        end
>>>    end
>>> +
>>> +    class Regex < AST::Leaf
>>> +        def initialize(hash)
>>> +            super
>>> +            @value = Regexp.new(@value) unless @value.is_a?(Regexp)
>>> +        end
>>> +
>>> +        # we're returning self here to wrap the regexp and to be
>>> used in places
>>> +        # where a string would have been used, without modifying
>>> any client code.
>>> +        # For instance, in many places we have the following code
>>> snippet:
>>> +        #  val = @val.safeevaluate(@scope)
>>> +        #  if val.match(otherval)
>>> +        #      ...
>>> +        #  end
>>> +        # this way, we don't have to modify this test specifically
>>> for handling
>>> +        # regexes.
>>> +        def evaluate(scope)
>>> +            return self
>>> +        end
>>> +
>>> +        def evaluate_match(value, scope, options = {})
>>> +            value = value.is_a?(String) ? value : value.to_s
>>> +
>>> +            if matched = @value.match(value)
>>> +                scope.ephemeral_from(matched, options[:file],
>>> options[:line])
>>> +            end
>>> +            matched
>>> +        end
>>> +
>>> +        def to_s
>>> +            return @value.source
>>
>> This would probably be better as @value.inspect.  That gives you
>> the //, otherwise you have to add it yourself.
>
> I think inspect is runtime dependant so I'm not sure I'll get the
> correct result with JRuby (or anything else). Since this method is  
> used
> for the node regex stuff, I think using source is the correct way
> (albeit I should add the //).

Makes sense, but I agree on the //'s.

>
>>>
>>> +        end
>>> +    end
>>> end
>>> diff --git a/lib/puppet/parser/grammar.ra b/lib/puppet/parser/
>>> grammar.ra
>>> index bb75ecf..68acf35 100644
>>> --- a/lib/puppet/parser/grammar.ra
>>> +++ b/lib/puppet/parser/grammar.ra
>>> @@ -765,6 +765,10 @@ comma:        FARROW
>>> endcomma:     # nothing
>>>            | COMMA { result = nil }
>>>
>>> +regex:        REGEX {
>>> +    result = ast AST::Regex, :value => val[0][:value]
>>> +}
>>> +
>>> end
>>> ---- header ----
>>> require 'puppet'
>>> diff --git a/lib/puppet/parser/parser.rb b/lib/puppet/parser/ 
>>> parser.rb
>>> index 6f4895a..ef08d6e 100644
>>> --- a/lib/puppet/parser/parser.rb
>>> +++ b/lib/puppet/parser/parser.rb
>>> <Patch elided>
>>> diff --git a/spec/unit/parser/ast/leaf.rb b/spec/unit/parser/ast/
>>> leaf.rb
>>> index 5ca7bc6..8315b80 100755
>>> --- a/spec/unit/parser/ast/leaf.rb
>>> +++ b/spec/unit/parser/ast/leaf.rb
>>> @@ -3,6 +3,41 @@
>>> require File.dirname(__FILE__) + '/../../../spec_helper'
>>>
>>> describe Puppet::Parser::AST::Leaf do
>>> +    before :each do
>>> +        @scope = stub 'scope'
>>> +    end
>>> +
>>> +    it "should have a evaluate_match method" do
>>> +        Puppet::Parser::AST::Leaf.new(:value => "value").should
>>> respond_to(:evaluate_match)
>>> +    end
>>> +
>>> +    describe "when evaluate_match is called" do
>>> +        before :each do
>>> +            @value = stub 'value'
>>> +            @leaf = Puppet::Parser::AST::Leaf.new(:value => @value)
>>> +        end
>>> +
>>> +        it "should evaluate itself" do
>>> +            @leaf.expects(:safeevaluate).with(@scope)
>>> +
>>> +            @leaf.evaluate_match("value", @scope)
>>> +        end
>>> +
>>> +        it "should match values by equality" do
>>> +            @leaf.stubs(:safeevaluate).with(@scope).returns(@value)
>>> +            @value.expects(:==).with("value")
>>> +
>>> +            @leaf.evaluate_match("value", @scope)
>>> +        end
>>> +
>>> +        it "should downcase the evaluated value if wanted" do
>>> +            @leaf.stubs(:safeevaluate).with(@scope).returns(@value)
>>> +            @value.expects(:downcase).returns("value")
>>> +
>>> +            @leaf.evaluate_match("value", @scope, :insensitive =>
>>> true)
>>> +        end
>>> +    end
>>> +
>>>    describe "when converting to string" do
>>>        it "should transform its value to string" do
>>>            value = stub 'value', :is_a? => true
>>> @@ -21,7 +56,7 @@ describe Puppet::Parser::AST::FlatString do
>>>    end
>>> end
>>>
>>> -describe Puppet::Parser::AST::FlatString do
>>> +describe Puppet::Parser::AST::String do
>>>    describe "when converting to string" do
>>>        it "should transform its value to a quoted string" do
>>>            value = stub 'value', :is_a? => true, :to_s => "ab"
>>> @@ -29,3 +64,87 @@ describe Puppet::Parser::AST::FlatString do
>>>        end
>>>    end
>>> end
>>> +
>>> +describe Puppet::Parser::AST::Regex do
>>> +    before :each do
>>> +        @scope = stub 'scope'
>>> +    end
>>> +
>>> +    describe "when initializing" do
>>> +        it "should create a Regexp with its content when value is
>>> not a Regexp" do
>>> +            Regexp.expects(:new).with("/ab/")
>>> +
>>> +            Puppet::Parser::AST::Regex.new :value => "/ab/"
>>> +        end
>>> +
>>> +        it "should not create a Regexp with its content when value
>>> is a Regexp" do
>>> +            value = Regexp.new("/ab/")
>>> +            Regexp.expects(:new).with("/ab/").never
>>> +
>>> +            Puppet::Parser::AST::Regex.new :value => value
>>> +        end
>>> +    end
>>> +
>>> +    describe "when evaluating" do
>>> +        it "should return self" do
>>> +            val = Puppet::Parser::AST::Regex.new :value => "/ab/"
>>> +
>>> +            val.evaluate(@scope).should === val
>>> +        end
>>> +    end
>>> +
>>> +    describe "when evaluate_match" do
>>> +        before :each do
>>> +            @value = stub 'regex'
>>> +            @value.stubs(:match).with("value").returns(true)
>>> +            Regexp.stubs(:new).returns(@value)
>>> +            @regex = Puppet::Parser::AST::Regex.new :value => "/ 
>>> ab/"
>>> +        end
>>> +
>>> +        it "should issue the regexp match" do
>>> +            @value.expects(:match).with("value")
>>> +
>>> +            @regex.evaluate_match("value", @scope)
>>> +        end
>>> +
>>> +        it "should set ephemeral scope vars if there is a match" do
>>> +            @scope.expects(:ephemeral_from).with(true, nil, nil)
>>> +
>>> +            @regex.evaluate_match("value", @scope)
>>> +        end
>>> +
>>> +        it "should return the match to the caller" do
>>> +            @value.stubs(:match).with("value").returns(:match)
>>> +            @scope.stubs(:ephemeral_from)
>>> +
>>> +            @regex.evaluate_match("value", @scope)
>>> +        end
>>> +    end
>>> +
>>> +    it "should return the regex source with to_s" do
>>> +        regex = stub 'regex'
>>> +        Regexp.stubs(:new).returns(regex)
>>> +
>>> +        val = Puppet::Parser::AST::Regex.new :value => "/ab/"
>>> +
>>> +        regex.expects(:source)
>>> +
>>> +        val.to_s
>>> +    end
>>> +end
>>> +
>>> +describe Puppet::Parser::AST::HostName do
>>> +    before :each do
>>> +        @scope = stub 'scope'
>>> +        @value = stub 'value', :is_a? => true, :=~ => true
>>> +        @host = Puppet::Parser::AST::HostName.new( :value =>  
>>> @value)
>>> +    end
>>> +
>>> +    it "should raise an error if hostname is not valid" do
>>> +        lambda { Puppet::Parser::AST::HostName.new( :value => "not
>>> an hostname!" ) }.should raise_error
>>> +    end
>>> +
>>> +    it "should evaluate to its value" do
>>> +        @host.evaluate(@scope).should == @value
>>> +    end
>>> +end
>>> -- 
>>> 1.6.0.2
>
> -- 
> Brice Figureau
> My Blog: http://www.masterzen.fr/
>
>
> >


-- 
Men never do evil so completely and cheerfully as when they do it from a
religious conviction. --Blaise Pascal
---------------------------------------------------------------------
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