-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_properties_spec.rb
189 lines (128 loc) · 5.27 KB
/
dynamic_properties_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
require "rspec"
require_relative "../dynamic_properties"
module Berico
class BasicDynoClass
include DynamicProperties
end
describe DynamicProperties do
context "Utility Methods" do
context "create_name_parser" do
it "should return the identity lambda when supplied an empty hash" do
dp = BasicDynoClass.new
name_parser = dp.create_name_parser({})
expected = "method_name"
name_parser.call(expected).should == expected
end
it "should return the prefix lambda when supplied the prefix key" do
dp = BasicDynoClass.new
prefix = "blah_"
name_parser = dp.create_name_parser({:prefix => prefix})
expected = "method_name"
name_parser.call(prefix + expected).should == expected
end
it "should return the suffix lambda when supplied the suffix key" do
dp = BasicDynoClass.new
suffix = "_blah"
name_parser = dp.create_name_parser({:suffix => suffix})
expected = "method_name"
name_parser.call(expected + suffix).should == expected
end
it "should return the regex lambda when supplied the match key" do
dp = BasicDynoClass.new
regex = /([a-z]{3}[0-9]{3})/
name_parser = dp.create_name_parser({:matcher => regex})
expected = "abc123"
name_parser.call(expected).should == expected
end
end
context "configure" do
it "should merge a set of initial properties if provided on the configuration object" do
class TestInitialPropertyClass
include DynamicProperties
def initialize
configure({ :properties => { "name" => "Richard Clayton", :age => 30 }})
end
end
test_object = TestInitialPropertyClass.new
test_object.configured.should == true
test_object.properties.length.should == 2
test_object.name.should == "Richard Clayton"
test_object.age.should == 30
end
it "should use a prefix for properties if the class is configured to use one" do
class PrefixDynoClass
include DynamicProperties
def initialize
configure({ :prefix => "p_" })
end
end
prefix_object = PrefixDynoClass.new
expected = "Richard Clayton"
prefix_object.p_name = expected
prefix_object.properties.length.should == 1
prefix_object.p_name.should == expected
end
it "should use a suffix for properties if the class is configured to use one" do
class SuffixDynoClass
include DynamicProperties
def initialize
self.configure({ :suffix => "_prop" })
end
end
suffix_object = SuffixDynoClass.new
expected = "Richard Clayton"
suffix_object.name_prop = expected
suffix_object.properties.length.should == 1
suffix_object.name_prop.should == expected
end
it "should use a regex for properties if the class is configured to use one" do
class MatcherDynoClass
include DynamicProperties
def initialize
self.configure({ :matcher => /p_(.*)_prop/ })
end
end
regex_object = MatcherDynoClass.new
expected = "Richard Clayton"
regex_object.p_name_prop = expected
regex_object.properties.length.should == 1
regex_object.p_name_prop.should == expected
end
end
context "monkey_patch" do
it "should create a new accessor and mutator for a property the first time it goes missing" do
richard_clayton = "Richard Clayton"
dp = BasicDynoClass.new
dp.name = richard_clayton
dp.methods.include?(:name).should == true
dp.methods.include?(:name=).should == true
dp.properties.length.should == 1
dp.properties["name"].should == richard_clayton
dp.name.should == richard_clayton
joe_black = "Joe Black"
dp.name = joe_black
dp.properties.length.should == 1
dp.properties["name"].should == joe_black
dp.name.should == joe_black
end
end
context "Validation Functions" do
it "should return whether a configuration hash is valid" do
dp = BasicDynoClass.new
dp.config_valid?({ :prefix => "prefix" }).should == true
dp.config_valid?({ :prefix => 22 }).should_not == true
dp.config_valid?({ :suffix => "suffix" }).should == true
dp.config_valid?({ :suffix => true }).should_not == true
dp.config_valid?({ :matcher => /abcdefg/ }).should == true
dp.config_valid?({ :matcher => "matcher" }).should_not == true
end
it "should validate a key's existence and value's type within a hash" do
dp = BasicDynoClass.new
dp.valid_for_key?({ :prefix => "prefix" }, :prefix, String).should == true
dp.valid_for_key?({ :suffix => "suffix" }, :prefix, String).should_not == true
dp.valid_for_key?({ :prefix => 22 }, :prefix, String).should_not == true
end
end
end
end
end