$:.unshift File.join(File.dirname(__FILE__), '..', '..', 'lib') require 'test/unit' require 'glue/configuration' # Hack: To make compatible with rake test. class Dummy setting :root_dir, :default => '/home/gmosx', :doc => 'The root directory for the app' setting :conn_count, :default => 5, :type => Fixnum end class Another setting :max_age, :default => 4, :doc => 'Maximum allowed age' end class Configuration def self.clear_all_settings @@owners = Hash.new end end class TC_Configuration < Test::Unit::TestCase # :nodoc: all class Internal setting :radius, :default => 4 end def setup # gmosx: A hack to make compatible with rake. All code in this # method is not needed if you only run this test (or in your # real aplications of course). Configuration.clear_all_settings Dummy.class_eval do setting :root_dir, :default => '/home/gmosx', :doc => 'The root directory for the app' setting :conn_count, :default => 5, :type => Fixnum end Another.class_eval do setting :max_age, :default => 4, :doc => 'Maximum allowed age' end Internal.class_eval do setting :radius, :default => 4 end end def test_all assert_equal 4, Configuration.settings.size assert_equal 4, Configuration.all.size assert_equal 2, Configuration.settings(Dummy).size assert_equal 1, Configuration.settings(Another).size assert_equal Fixnum, Configuration.settings(Dummy)[:conn_count].type assert_equal String, Configuration[Dummy][:root_dir].type assert_equal '/home/gmosx', Dummy.root_dir assert_equal 4, Another.max_age Dummy.root_dir = '/changed/dir' assert_equal '/changed/dir', Dummy.root_dir assert_equal '/changed/dir', Configuration[Dummy][:root_dir].value Another.max_age = 99 assert_equal 99, Configuration[Another][:max_age].value # test reverse setting. Configuration[Another][:max_age].value = 69 assert_equal 69, Configuration[Another][:max_age].value assert_equal 69, Another.max_age # setup Configuration.setup( Dummy => { :root_dir => '/gmosx/2', :conn_count => 12 }, Another => { :max_age => 2 }, TC_Configuration::Internal => { :radius => 99 } ) assert_equal '/gmosx/2', Dummy.root_dir assert_equal '/gmosx/2', Configuration[Dummy][:root_dir].value assert_equal 2, Another.max_age Configuration.parse <<-end_val Another: max_age: 5 TC_Configuration.Internal: radius: 99 Dummy: root_dir: /gmosx/99 conn_count: 33 end_val assert_equal '/gmosx/99', Dummy.root_dir assert_equal '/gmosx/99', Configuration[Dummy][:root_dir].value assert_equal 5, Another.max_age # cooler ;-) assert_equal String, Configuration.Dummy[:root_dir].type assert_equal String, Configuration.Dummy.root_dir.type # alias assert_equal String, Settings.Dummy.root_dir.type assert_equal 5, Settings.Another.max_age.value # Handle namespace assert_equal 99, Internal.radius assert_equal 99, TC_Configuration::Internal.radius end end