changed properties and the name of hyperchad to selectricity
[selectricity-live] / vendor / plugins / engines / test / ruby_extensions_test.rb
1 ENV["RAILS_ENV"] = "test"
2 require File.expand_path(File.dirname(__FILE__) + '/../../../../config/environment')
3 require 'test_help'
4
5 class RubyExtensionsTest < Test::Unit::TestCase
6
7   def setup
8     # create the module to be used for config testing
9     eval "module TestModule end"
10   end  
11
12   def teardown
13     # remove the TestModule constant from our scope
14     self.class.class_eval { remove_const :TestModule }
15   end
16
17
18   #
19   # Module.config
20   #
21
22   def test_config_no_arguments
23     assert_raise(RuntimeError) { TestModule.config }
24   end
25   
26   def test_config_array_arguments
27     TestModule.config :monkey, 123
28     assert_equal(123, TestModule.config(:monkey))
29   end
30   
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))
35   end
36
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))
40   end
41     
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))
47     
48     TestModule.config :monkey => 456
49     assert_equal(123, TestModule.config(:monkey))    
50     
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))    
54   end
55   
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))
69     
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))           
74   end
75   
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))
80   end
81   
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]))
86   end
87   
88   
89   #
90   # Module.default_constant
91   #
92   
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)
98   end
99
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)
105   end
106
107   def test_default_constant_bad_arguments
108     # constant names must be Captialized
109     assert_raise(NameError) { TestModule.default_constant :lowercase_name, 123 }
110     
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 }
114   end
115 end

Benjamin Mako Hill || Want to submit a patch?