diff options
| author | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-04-19 18:34:03 +0000 |
|---|---|---|
| committer | luke <luke@980ebf18-57e1-0310-9a29-db15c13687c0> | 2007-04-19 18:34:03 +0000 |
| commit | f8a0e99380a9a57e67363b33fb5ad54619244257 (patch) | |
| tree | d5ea5946ced9de1f0446c2cc017d1231facd6511 /test/language/scope.rb | |
| parent | 9946249255a69431349185807f4291745b15ca7f (diff) | |
| download | puppet-f8a0e99380a9a57e67363b33fb5ad54619244257.tar.gz puppet-f8a0e99380a9a57e67363b33fb5ad54619244257.tar.xz puppet-f8a0e99380a9a57e67363b33fb5ad54619244257.zip | |
Adding the functionality requested in http://mail.madstop.com/pipermail/puppet-users/2007-April/002398.html .
You can now retrieve qualified variables by specifying the full class path.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2393 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test/language/scope.rb')
| -rwxr-xr-x | test/language/scope.rb | 90 |
1 files changed, 84 insertions, 6 deletions
diff --git a/test/language/scope.rb b/test/language/scope.rb index c2346dac5..c58719e79 100755 --- a/test/language/scope.rb +++ b/test/language/scope.rb @@ -2,10 +2,7 @@ $:.unshift("../lib").unshift("../../lib") if __FILE__ =~ /\.rb$/ -require 'puppet' -require 'puppet/parser/interpreter' -require 'puppet/parser/parser' -require 'puppet/network/client' +require 'mocha' require 'puppettest' require 'puppettest/parsertesting' require 'puppettest/resourcetesting' @@ -100,6 +97,56 @@ class TestScope < Test::Unit::TestCase } end + def test_lookupvar + interp = mkinterp + scope = mkscope :interp => interp + + # first do the plain lookups + assert_equal("", scope.lookupvar("var"), "scope did not default to string") + assert_equal("", scope.lookupvar("var", true), "scope ignored usestring setting") + assert_equal(:undefined, scope.lookupvar("var", false), "scope ignored usestring setting when false") + + # Now set the var + scope.setvar("var", "yep") + assert_equal("yep", scope.lookupvar("var"), "did not retrieve value correctly") + + # Now test the parent lookups + subscope = mkscope :interp => interp + subscope.parent = scope + assert_equal("", subscope.lookupvar("nope"), "scope did not default to string with parent") + assert_equal("", subscope.lookupvar("nope", true), "scope ignored usestring setting with parent") + assert_equal(:undefined, subscope.lookupvar("nope", false), "scope ignored usestring setting when false with parent") + + assert_equal("yep", subscope.lookupvar("var"), "did not retrieve value correctly from parent") + + # Now override the value in the subscope + subscope.setvar("var", "sub") + assert_equal("sub", subscope.lookupvar("var"), "did not retrieve overridden value correctly") + + # Make sure we punt when the var is qualified. Specify the usestring value, so we know it propagates. + scope.expects(:lookup_qualified_var).with("one::two", false).returns(:punted) + assert_equal(:punted, scope.lookupvar("one::two", false), "did not return the value of lookup_qualified_var") + end + + def test_lookup_qualified_var + interp = mkinterp + scope = mkscope :interp => interp + + scopes = {} + classes = ["", "one", "one::two", "one::two::three"].each do |name| + klass = interp.newclass(name) + klass.evaluate(:scope => scope) + scopes[name] = scope.class_scope(klass) + end + + classes.each do |name| + var = [name, "var"].join("::") + scopes[name].expects(:lookupvar).with("var", false).returns(name) + + assert_equal(name, scope.send(:lookup_qualified_var, var, false), "did not get correct value from lookupvar") + end + end + def test_declarative # set to declarative top = mkscope(:declarative => true) @@ -196,13 +243,44 @@ class TestScope < Test::Unit::TestCase end def test_strinterp - scope = mkscope() + # Make and evaluate our classes so the qualified lookups work + interp = mkinterp + klass = interp.newclass("") + scope = mkscope(:interp => interp) + klass.evaluate(:scope => scope) + klass = interp.newclass("one") + klass.evaluate(:scope => scope) + + klass = interp.newclass("one::two") + klass.evaluate(:scope => scope) + + + scope = scope.class_scope("") assert_nothing_raised { scope.setvar("test","value") } + + scopes = {"" => scope} + + %w{one one::two one::two::three}.each do |name| + klass = interp.newclass(name) + klass.evaluate(:scope => scope) + scopes[name] = scope.class_scope(klass) + scopes[name].setvar("test", "value-%s" % name.sub(/.+::/,'')) + end + + assert_equal("value", scope.lookupvar("::test"), "did not look up qualified value correctly") tests = { "string ${test}" => "string value", + "string ${one::two::three::test}" => "string value-three", + "string $one::two::three::test" => "string value-three", + "string ${one::two::test}" => "string value-two", + "string $one::two::test" => "string value-two", + "string ${one::test}" => "string value-one", + "string $one::test" => "string value-one", + "string ${::test}" => "string value", + "string $::test" => "string value", "string ${test} ${test} ${test}" => "string value value value", "string $test ${test} $test" => "string value value value", "string \\$test" => "string $test", @@ -219,7 +297,7 @@ class TestScope < Test::Unit::TestCase tests.each do |input, output| assert_nothing_raised("Failed to scan %s" % input.inspect) do assert_equal(output, scope.strinterp(input), - 'did not interpret %s correctly' % input) + 'did not interpret %s correctly' % input.inspect) end end |
