1 ENV["RAILS_ENV"] = "test"
2 require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
5 class RubyExtensionsTest < Test::Unit::TestCase
8 # create the module to be used for config testing
9 eval "module TestModule end"
13 # remove the TestModule constant from our scope
14 self.class.class_eval { remove_const :TestModule }
22 def test_config_no_arguments
23 assert_raise(RuntimeError) { TestModule.config }
26 def test_config_array_arguments
27 TestModule.config :monkey, 123
28 assert_equal(123, TestModule.config(:monkey))
31 def test_config_hash_arguments
32 TestModule.config :monkey => 123, :donkey => 456
33 assert_equal(123, TestModule.config(:monkey))
34 assert_equal(456, TestModule.config(:donkey))
37 def test_config_can_store_hash
38 TestModule.config :hash, :key1 => 'val1', :key2 => 'val2'
39 assert_equal({:key1 => 'val1', :key2 => 'val2'}, TestModule.config(:hash))
42 def test_config_cant_overwrite_existing_config_values
43 TestModule.config :monkey, 123
44 assert_equal(123, TestModule.config(:monkey))
45 TestModule.config :monkey, 456
46 assert_equal(123, TestModule.config(:monkey))
48 TestModule.config :monkey => 456
49 assert_equal(123, TestModule.config(:monkey))
51 # in this case, the resulting Hash only has {:baboon => "goodbye!"} - that's Ruby, users beware.
52 TestModule.config :baboon => "hello", :baboon => "goodbye!"
53 assert_equal("goodbye!", TestModule.config(:baboon))
56 def test_config_force_new_value
57 TestModule.config :monkey, 123
58 TestModule.config :man, 321
59 assert_equal(123, TestModule.config(:monkey))
60 assert_equal(321, TestModule.config(:man))
61 TestModule.config :monkey, 456, :force
62 assert_equal(456, TestModule.config(:monkey))
63 TestModule.config :monkey => 456, :man => 654, :force => true
64 assert_equal(456, TestModule.config(:monkey))
65 assert_equal(654, TestModule.config(:man))
66 TestModule.config :monkey => 789, :man => 987, :force => false
67 assert_equal(456, TestModule.config(:monkey))
68 assert_equal(654, TestModule.config(:man))
70 TestModule.config :hash, :key1 => 'val1', :key2 => 'val2'
71 assert_equal({:key1 => 'val1', :key2 => 'val2'}, TestModule.config(:hash))
72 TestModule.config :hash => {:key1 => 'val3', :key2 => 'val4'}, :force => true
73 assert_equal({:key1 => 'val3', :key2 => 'val4'}, TestModule.config(:hash))
76 # this test is somewhat redundant, but it might be an idea to havbe it explictly anyway
77 def test_config_get_values
78 TestModule.config :monkey, 123
79 assert_equal(123, TestModule.config(:monkey))
82 def test_config_get_multiple_values
83 TestModule.config :monkey, 123
84 TestModule.config :donkey, 456
85 assert_equal([123, 456], TestModule.config([:monkey, :donkey]))
90 # Module.default_constant
93 def test_default_constant_set
94 TestModule.default_constant :Monkey, 123
95 assert_equal(123, TestModule::Monkey)
96 TestModule.default_constant "Hello", 456
97 assert_equal(456, TestModule::Hello)
100 def test_default_constant_cannot_set_again
101 TestModule.default_constant :Monkey, 789
102 assert_equal(789, TestModule::Monkey)
103 TestModule.default_constant :Monkey, 456
104 assert_equal(789, TestModule::Monkey)
107 def test_default_constant_bad_arguments
108 # constant names must be Captialized
109 assert_raise(NameError) { TestModule.default_constant :lowercase_name, 123 }
111 # constant names should be given as Strings or Symbols
112 assert_raise(RuntimeError) { TestModule.default_constant 123, 456 }
113 assert_raise(RuntimeError) { TestModule.default_constant Object.new, 456 }