summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatt Robinson <matt@puppetlabs.com>2010-07-19 14:57:23 -0700
committerMarkus Roberts <Markus@reality.com>2010-07-19 19:56:06 -0700
commit539d71635132bd5f772a550b7bfff530e8b59b68 (patch)
tree1f1ae2297bf9b13f62473f918f3ae4d172326fa9
parentd2da1d45aaf44de7fd0648b3bab48887838549d8 (diff)
downloadpuppet-539d71635132bd5f772a550b7bfff530e8b59b68.tar.gz
puppet-539d71635132bd5f772a550b7bfff530e8b59b68.tar.xz
puppet-539d71635132bd5f772a550b7bfff530e8b59b68.zip
[#4287] Fix the undefined evaluate_match error when comparing functions
Ticket #4238 introduced a problem that a function couldn't compare to another value until after it was evaluated, and AST::Function didn't have the evaluate_match method. This change moves that method from AST::Leaf to AST. The special casing necessary for doing comparisons between AST objects feels messy and could probably be encapsulated better. I've created ticket #4291 to remind us to refactor this at some point. Paired with: Nick Lewis Signed-off-by: Matt Robinson <matt@puppetlabs.com>
-rw-r--r--lib/puppet/parser/ast.rb14
-rw-r--r--lib/puppet/parser/ast/leaf.rb14
-rwxr-xr-xspec/unit/parser/ast/leaf_spec.rb57
-rw-r--r--spec/unit/parser/ast_spec.rb70
4 files changed, 84 insertions, 71 deletions
diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb
index 2773a240e..54e034acb 100644
--- a/lib/puppet/parser/ast.rb
+++ b/lib/puppet/parser/ast.rb
@@ -87,6 +87,20 @@ class Puppet::Parser::AST
def initialize(args)
set_options(args)
end
+
+ # evaluate ourselves, and match
+ def evaluate_match(value, scope)
+ obj = self.safeevaluate(scope)
+
+ obj = obj.downcase if obj.respond_to?(:downcase)
+ value = value.downcase if value.respond_to?(:downcase)
+
+ obj = Puppet::Parser::Scope.number?(obj) || obj
+ value = Puppet::Parser::Scope.number?(value) || value
+
+ # "" == undef for case/selector/if
+ obj == value or (obj == "" and value == :undef)
+ end
end
# And include all of the AST subclasses.
diff --git a/lib/puppet/parser/ast/leaf.rb b/lib/puppet/parser/ast/leaf.rb
index 49f430278..db9788f50 100644
--- a/lib/puppet/parser/ast/leaf.rb
+++ b/lib/puppet/parser/ast/leaf.rb
@@ -10,20 +10,6 @@ class Puppet::Parser::AST
@value
end
- # evaluate ourselves, and match
- def evaluate_match(value, scope)
- obj = self.safeevaluate(scope)
-
- obj = obj.downcase if obj.respond_to?(:downcase)
- value = value.downcase if value.respond_to?(:downcase)
-
- obj = Puppet::Parser::Scope.number?(obj) || obj
- value = Puppet::Parser::Scope.number?(value) || value
-
- # "" == undef for case/selector/if
- obj == value or (obj == "" and value == :undef)
- end
-
def match(value)
@value == value
end
diff --git a/spec/unit/parser/ast/leaf_spec.rb b/spec/unit/parser/ast/leaf_spec.rb
index d21cbf573..6729cd278 100755
--- a/spec/unit/parser/ast/leaf_spec.rb
+++ b/spec/unit/parser/ast/leaf_spec.rb
@@ -13,63 +13,6 @@ describe Puppet::Parser::AST::Leaf do
Puppet::Parser::AST::Leaf.new(:value => "value").should respond_to(:evaluate_match)
end
- describe "when evaluate_match is called" do
- it "should evaluate itself" do
- @leaf.expects(:safeevaluate).with(@scope)
-
- @leaf.evaluate_match("value", @scope)
- end
-
- it "should match values by equality" do
- @value.stubs(:==).returns(false)
- @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)
- end
-
- it "should convert values to number" do
- @leaf.stubs(:safeevaluate).with(@scope).returns(@value)
- Puppet::Parser::Scope.expects(:number?).with(@value).returns(2)
- Puppet::Parser::Scope.expects(:number?).with("23").returns(23)
-
- @leaf.evaluate_match("23", @scope)
- end
-
- it "should compare 'numberized' values" do
- @leaf.stubs(:safeevaluate).with(@scope).returns(@value)
- two = stub_everything 'two'
- one = stub_everything 'one'
-
- Puppet::Parser::Scope.stubs(:number?).with(@value).returns(one)
- Puppet::Parser::Scope.stubs(:number?).with("2").returns(two)
-
- one.expects(:==).with(two)
-
- @leaf.evaluate_match("2", @scope)
- end
-
- it "should match undef if value is an empty string" do
- @leaf.stubs(:safeevaluate).with(@scope).returns("")
-
- @leaf.evaluate_match(:undef, @scope).should be_true
- end
-
- it "should downcase the parameter value if wanted" do
- parameter = stub 'parameter'
- parameter.expects(:downcase).returns("value")
-
- @leaf.evaluate_match(parameter, @scope)
- end
- end
-
describe "when converting to string" do
it "should transform its value to string" do
value = stub 'value', :is_a? => true
diff --git a/spec/unit/parser/ast_spec.rb b/spec/unit/parser/ast_spec.rb
index b743cea2e..29dce2b9c 100644
--- a/spec/unit/parser/ast_spec.rb
+++ b/spec/unit/parser/ast_spec.rb
@@ -39,3 +39,73 @@ describe Puppet::Parser::AST do
end
end
+
+describe 'AST Generic Child' do
+ before :each do
+ @value = stub 'value'
+ class Evaluateable < Puppet::Parser::AST
+ attr_accessor :value
+ def safeevaluate(*options)
+ return value
+ end
+ end
+ @evaluateable = Evaluateable.new(:value => @value)
+ @scope = stubs 'scope'
+ end
+
+ describe "when evaluate_match is called" do
+ it "should evaluate itself" do
+ @evaluateable.expects(:safeevaluate).with(@scope)
+
+ @evaluateable.evaluate_match("value", @scope)
+ end
+
+ it "should match values by equality" do
+ @value.expects(:==).with("value").returns(true)
+
+ @evaluateable.evaluate_match("value", @scope)
+ end
+
+ it "should downcase the evaluated value if wanted" do
+ @value.expects(:downcase).returns("value")
+
+ @evaluateable.evaluate_match("value", @scope)
+ end
+
+ it "should convert values to number" do
+ Puppet::Parser::Scope.expects(:number?).with(@value).returns(2)
+ Puppet::Parser::Scope.expects(:number?).with("23").returns(23)
+
+ @evaluateable.evaluate_match("23", @scope)
+ end
+
+ it "should compare 'numberized' values" do
+ two = stub_everything 'two'
+ one = stub_everything 'one'
+
+ Puppet::Parser::Scope.stubs(:number?).with(@value).returns(one)
+ Puppet::Parser::Scope.stubs(:number?).with("2").returns(two)
+
+ one.expects(:==).with(two)
+
+ @evaluateable.evaluate_match("2", @scope)
+ end
+
+ it "should match undef if value is an empty string" do
+ @evaluateable.value = ''
+ @evaluateable.evaluate_match(:undef, @scope).should be_true
+ end
+
+ it "should downcase the parameter value if wanted" do
+ parameter = stub 'parameter'
+ parameter.expects(:downcase).returns("value")
+
+ @evaluateable.evaluate_match(parameter, @scope)
+ end
+
+ it "should not match '' if value is undef" do
+ @evaluateable.value = :undef
+ @evaluateable.evaluate_match('', @scope).should be_false
+ end
+ end
+end