/
config_spec.rb
144 lines (103 loc) · 3.33 KB
/
config_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
describe RSpreedly::Config do
before :each do
RSpreedly::Config.clear
RSpreedly::Config.setup do |c|
c[:one] = 1
c[:two] = 2
end
end
describe "logger" do
before :each do
Object.send(:remove_const, :RAILS_DEFAULT_LOGGER) if defined?(RAILS_DEFAULT_LOGGER)
end
it "should default to RAILS_DEFAULT_LOGGER if defined" do
RAILS_DEFAULT_LOGGER = "something"
RSpreedly::Config.reset
RSpreedly::Config.logger.should == "something"
end
it "should default to a Logger if RAILS_DEFAULT_LOGGER is not defined" do
RSpreedly::Config.reset
RSpreedly::Config.logger.should be_a(Logger)
end
end
describe "configuration" do
it "should return the configuration hash" do
RSpreedly::Config.configuration.should == {:one => 1, :two => 2}
end
end
describe "[]" do
it "should return the config option matching the key" do
RSpreedly::Config[:one].should == 1
end
it "should return nil if the key doesn't exist" do
RSpreedly::Config[:monkey].should be_nil
end
end
describe "[]=" do
it "should set the config option for the key" do
lambda{
RSpreedly::Config[:banana] = :yellow
}.should change(RSpreedly::Config, :banana).from(nil).to(:yellow)
end
end
describe "delete" do
it "should delete the config option for the key" do
lambda{
RSpreedly::Config.delete(:one)
}.should change(RSpreedly::Config, :one).from(1).to(nil)
end
it "should leave the config the same if the key doesn't exist" do
lambda{
RSpreedly::Config.delete(:test)
}.should_not change(RSpreedly::Config, :configuration)
end
end
describe "fetch" do
it "should return the config option matching the key if it exists" do
RSpreedly::Config.fetch(:one, 100).should == 1
end
it "should return the config default if the key doesn't exist" do
RSpreedly::Config.fetch(:other, 100).should == 100
end
end
describe "to_hash" do
it "should return a hash of the configuration" do
RSpreedly::Config.to_hash.should == {:one => 1, :two => 2}
end
end
describe "setup" do
it "should yield self" do
RSpreedly::Config.setup do |c|
c.should == RSpreedly::Config
end
end
it "should let you set items on the configuration object as a hash" do
lambda{
RSpreedly::Config.setup do |c|
c[:bananas] = 100
end
}.should change(RSpreedly::Config, :bananas).from(nil).to(100)
end
it "should let you set items on the configuration object as a method" do
lambda{
RSpreedly::Config.setup do |c|
c.monkeys = 100
end
}.should change(RSpreedly::Config, :monkeys).from(nil).to(100)
end
end
describe "calling a missing method" do
it "should retreive the config if the method matches a key" do
RSpreedly::Config.one.should == 1
end
it "should retreive nil if the method doesn't match a key" do
RSpreedly::Config.moo.should be_nil
end
it "should set the value of the config item matching the method name if it's an assignment" do
lambda{
RSpreedly::Config.trees = 3
}.should change(RSpreedly::Config, :trees).from(nil).to(3)
end
end
end