summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-04-26 05:39:58 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-04-26 05:39:58 +0000
commit46ce36b175962ce89e06af4863d2c9dc50f2a02f (patch)
treeaeff2dacfd81caddce54666e2d155e07b6ad09e8 /test
parentccc4d95dd28164f6f10763a758db85db0d48984c (diff)
Creating a simplistic, generic function framework in the parser, so it is now very easy to add new functions. There is a pretty crappy, hardwired distinction between functions that return values and those that do not, but I do not see a good way around it right now. Functions are also currently responsible for handling their own arity, although I have plans for fixing that.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1134 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test')
-rwxr-xr-xtest/language/ast.rb54
-rwxr-xr-xtest/language/scope.rb59
-rwxr-xr-xtest/language/snippets.rb14
-rw-r--r--test/puppettest.rb15
4 files changed, 142 insertions, 0 deletions
diff --git a/test/language/ast.rb b/test/language/ast.rb
index d87cf033c..ac9b53dad 100755
--- a/test/language/ast.rb
+++ b/test/language/ast.rb
@@ -701,4 +701,58 @@ class TestAST < Test::Unit::TestCase
"Node's long name got set")
assert(scope.classlist.include?("node"), "Node's name did not get set")
end
+
+ def test_functions
+ assert_raise(Puppet::ParseError) do
+ Puppet::Parser::AST::Function.new(
+ :name => "fakefunction",
+ :arguments => AST::ASTArray.new(
+ :children => [nameobj("avalue")]
+ )
+ )
+ end
+
+ assert_nothing_raised do
+ Puppet::Parser::Functions.newfunction(:fakefunction, :rvalue) do |input|
+ return "output %s" % input[0]
+ end
+ end
+
+ func = nil
+ assert_nothing_raised do
+ func = Puppet::Parser::AST::Function.new(
+ :name => "fakefunction",
+ :ftype => :rvalue,
+ :arguments => AST::ASTArray.new(
+ :children => [nameobj("avalue")]
+ )
+ )
+ end
+
+ scope = Puppet::Parser::Scope.new()
+ val = nil
+ assert_nothing_raised do
+ val = func.evaluate(:scope => scope)
+ end
+
+ assert_equal("output avalue", val)
+ end
+
+ def test_taggedfunction
+ scope = Puppet::Parser::Scope.new()
+
+ tag = "yayness"
+ scope.setclass(tag.object_id, tag)
+
+ {"yayness" => true, "booness" => false}.each do |tag, retval|
+ func = taggedobj(tag, :rvalue)
+
+ val = nil
+ assert_nothing_raised do
+ val = func.evaluate(:scope => scope)
+ end
+
+ assert_equal(retval, val, "'tagged' returned %s for %s" % [val, tag])
+ end
+ end
end
diff --git a/test/language/scope.rb b/test/language/scope.rb
index 48689cd2e..8e0068c79 100755
--- a/test/language/scope.rb
+++ b/test/language/scope.rb
@@ -563,4 +563,63 @@ class TestScope < Test::Unit::TestCase
end
end
+
+ def test_tagfunction
+ scope = Puppet::Parser::Scope.new()
+
+ assert_nothing_raised {
+ scope.function_tag(["yayness", "booness"])
+ }
+
+ assert(scope.classlist.include?("yayness"), "tag 'yayness' did not get set")
+ assert(scope.classlist.include?("booness"), "tag 'booness' did not get set")
+
+ # Now verify that the 'tagged' function works correctly
+ assert(scope.function_tagged("yayness"),
+ "tagged function incorrectly returned false")
+ assert(scope.function_tagged("booness"),
+ "tagged function incorrectly returned false")
+
+ assert(! scope.function_tagged("funtest"),
+ "tagged function incorrectly returned true")
+ end
+
+ def test_includefunction
+ scope = Puppet::Parser::Scope.new()
+
+ one = tempfile()
+ two = tempfile()
+
+ children = []
+
+ children << classobj("one", :code => AST::ASTArray.new(
+ :children => [
+ fileobj(one, "owner" => "root")
+ ]
+ ))
+
+ children << classobj("two", :code => AST::ASTArray.new(
+ :children => [
+ fileobj(two, "owner" => "root")
+ ]
+ ))
+
+ top = AST::ASTArray.new(:children => children)
+
+ top.evaluate(:scope => scope)
+
+ assert_nothing_raised {
+ scope.function_include(["one", "two"])
+ }
+
+
+ assert(scope.classlist.include?("one"), "tag 'one' did not get set")
+ assert(scope.classlist.include?("two"), "tag 'two' did not get set")
+
+ # Now verify that the 'tagged' function works correctly
+ assert(scope.function_tagged("one"),
+ "tagged function incorrectly returned false")
+ assert(scope.function_tagged("two"),
+ "tagged function incorrectly returned false")
+ end
end
diff --git a/test/language/snippets.rb b/test/language/snippets.rb
index 536fab4fd..f541b4eeb 100755
--- a/test/language/snippets.rb
+++ b/test/language/snippets.rb
@@ -433,6 +433,20 @@ class TestSnippets < Test::Unit::TestCase
@@tmpfiles << "/tmp/settestingness"
end
+ # Make sure that set tags are correctly in place, yo.
+ def snippet_tagged(trans)
+ tags = {"testing" => true, "yayness" => false,
+ "both" => false, "bothtrue" => true}
+
+ tags.each do |tag, retval|
+ @@tmpfiles << "/tmp/tagged#{tag}true"
+ @@tmpfiles << "/tmp/tagged#{tag}false"
+
+ assert(FileTest.exists?("/tmp/tagged#{tag}#{retval.to_s}"),
+ "'tagged' did not return %s with %s" % [retval, tag])
+ end
+ end
+
def snippet_emptyclass(trans)
# There's nothing to check other than that it works
end
diff --git a/test/puppettest.rb b/test/puppettest.rb
index ffd6d34f1..733f1ae71 100644
--- a/test/puppettest.rb
+++ b/test/puppettest.rb
@@ -923,6 +923,21 @@ module ParserTesting
)
}
end
+
+ def taggedobj(name, ftype = :statement)
+ func = nil
+ assert_nothing_raised do
+ func = Puppet::Parser::AST::Function.new(
+ :name => "tagged",
+ :ftype => ftype,
+ :arguments => AST::ASTArray.new(
+ :children => [nameobj(name)]
+ )
+ )
+ end
+
+ return func
+ end
end
class PuppetTestSuite