diff options
| author | Brice Figureau <brice-puppet@daysofwonder.com> | 2009-07-28 19:11:03 +0200 |
|---|---|---|
| committer | James Turnbull <james@lovedthanlost.net> | 2009-08-01 11:15:28 +1000 |
| commit | 17e62b1ec806815abea909291df1e591a825c375 (patch) | |
| tree | 477ef33e88077c8e46d7184f31a106a0b850425a /spec/unit/parser | |
| parent | 4f9545f2f6cb377eff126c9d52b421ab405aa677 (diff) | |
| download | puppet-17e62b1ec806815abea909291df1e591a825c375.tar.gz puppet-17e62b1ec806815abea909291df1e591a825c375.tar.xz puppet-17e62b1ec806815abea909291df1e591a825c375.zip | |
Add AST::Regex, an AST leaf node representing a regex
Add a regex rule (unused for the moment) to the parser.
Signed-off-by: Brice Figureau <brice-puppet@daysofwonder.com>
Diffstat (limited to 'spec/unit/parser')
| -rwxr-xr-x | spec/unit/parser/ast/leaf.rb | 121 |
1 files changed, 120 insertions, 1 deletions
diff --git a/spec/unit/parser/ast/leaf.rb b/spec/unit/parser/ast/leaf.rb index 5ca7bc675..8315b8066 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 |
