summaryrefslogtreecommitdiffstats
path: root/test/util/subclass_loader.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-06 19:03:05 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2007-03-06 19:03:05 +0000
commit46d344b9daa24047b60183cc94509d306b6b562a (patch)
tree3c11eaad696ba3d6e6dd40bd7b9e7d1a4a71af85 /test/util/subclass_loader.rb
parent68233706a9ff05be8fa8ab3ab7198cd0918517d6 (diff)
downloadpuppet-46d344b9daa24047b60183cc94509d306b6b562a.tar.gz
puppet-46d344b9daa24047b60183cc94509d306b6b562a.tar.xz
puppet-46d344b9daa24047b60183cc94509d306b6b562a.zip
Merging the webserver_portability branch from version 2182 to version 2258.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@2259 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'test/util/subclass_loader.rb')
-rwxr-xr-xtest/util/subclass_loader.rb100
1 files changed, 100 insertions, 0 deletions
diff --git a/test/util/subclass_loader.rb b/test/util/subclass_loader.rb
new file mode 100755
index 000000000..5c8174cfa
--- /dev/null
+++ b/test/util/subclass_loader.rb
@@ -0,0 +1,100 @@
+#!/usr/bin/env ruby
+
+$:.unshift("../lib") if __FILE__ =~ /\.rb$/
+
+require 'puppettest'
+require 'puppet/util/subclass_loader'
+
+class TestPuppetUtilSubclassLoader < Test::Unit::TestCase
+ include PuppetTest
+
+ class LoadTest
+ extend Puppet::Util::SubclassLoader
+ handle_subclasses :faker, "puppet/fakeloaders"
+ end
+
+ def mk_subclass(name, path, parent)
+ # Make a fake client
+ unless defined? @basedir
+ @basedir ||= tempfile()
+ $: << @basedir
+ cleanup { $:.delete(@basedir) if $:.include?(@basedir) }
+ end
+
+ libdir = File.join([@basedir, path.split(File::SEPARATOR)].flatten)
+ FileUtils.mkdir_p(libdir)
+
+ file = File.join(libdir, "#{name}.rb")
+ File.open(file, "w") do |f|
+ f.puts %{class #{parent}::#{name.to_s.capitalize} < #{parent}; end}
+ end
+ end
+
+ def test_subclass_loading
+ # Make sure we don't get a failure but that we also get nothing back
+ assert_nothing_raised do
+ assert_nil(LoadTest.faker(:fake),
+ "Got something back from a missing subclass")
+ assert_nil(LoadTest.fake,
+ "Got something back from missing subclass method")
+ end
+ # Make a fake client
+ mk_subclass("fake", "puppet/fakeloaders", "TestPuppetUtilSubclassLoader::LoadTest")
+
+
+ fake = nil
+ assert_nothing_raised do
+ fake = LoadTest.faker(:fake)
+ end
+ assert_nothing_raised do
+ assert_equal(fake, LoadTest.fake,
+ "Did not get subclass back from main method")
+ end
+ assert(fake, "did not load subclass")
+
+ # Now make sure the subclass behaves correctly
+ assert_equal(:Fake, fake.name, "name was not calculated correctly")
+ end
+
+ def test_multiple_subclasses
+ sub1 = Class.new(LoadTest)
+ Object.const_set("Sub1", sub1)
+ sub2 = Class.new(sub1)
+ Object.const_set("Sub2", sub2)
+ assert_equal(sub2, LoadTest.sub2, "did not get subclass of subclass")
+ end
+
+ # I started out using a class variable to mark the loader,
+ # but it's shared among all classes that include this module,
+ # so it didn't work. This is testing whether I get the behaviour
+ # that I want.
+ def test_multiple_classes_using_module
+ other = Class.new do
+ extend Puppet::Util::SubclassLoader
+ handle_subclasses :other, "puppet/other"
+ end
+ Object.const_set("OtherLoader", other)
+
+ mk_subclass("multipletest", "puppet/other", "OtherLoader")
+ mk_subclass("multipletest", "puppet/fakeloaders", "TestPuppetUtilSubclassLoader::LoadTest")
+ #system("find %s" % @basedir)
+ #puts File.read(File.join(@basedir, "puppet/fakeloaders/multipletest.rb"))
+ #puts File.read(File.join(@basedir, "puppet/other/multipletest.rb"))
+
+ othersub = mainsub = nil
+ assert_nothing_raised("Could not look up other sub") do
+ othersub = OtherLoader.other(:multipletest)
+ end
+ assert_nothing_raised("Could not look up main sub") do
+ mainsub = LoadTest.faker(:multipletest)
+ end
+ assert(othersub, "did not get other sub")
+ assert(mainsub, "did not get main sub")
+ assert(othersub.ancestors.include?(OtherLoader),
+ "othersub is not a subclass of otherloader")
+ assert(mainsub.ancestors.include?(LoadTest),
+ "mainsub is not a subclass of loadtest")
+ end
+end
+
+# $Id$