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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../lib/puppettest'
require 'puppet'
require 'puppettest'
class TestPuppetUtilClassGen < Test::Unit::TestCase
include PuppetTest
class FakeBase
class << self
attr_accessor :name
end
end
class GenTest
class << self
include Puppet::Util::ClassGen
end
end
def testclasses(name)
sub = Class.new(GenTest) do @name = "base#{name.to_s}" end
self.class.const_set("Base#{name.to_s}", sub)
klass = Class.new(FakeBase) do @name = "gen#{name.to_s}"end
return sub, klass
end
def test_handleclassconst
sub, klass = testclasses("const")
const = nil
assert_nothing_raised do
const = sub.send(:handleclassconst, klass, klass.name, {})
end
# make sure the constant is set
assert(defined?(Baseconst::Genconst), "const was not defined")
assert_equal(Baseconst::Genconst.object_id, klass.object_id)
# Now make sure don't replace by default
newklass = Class.new(FakeBase) do @name = klass.name end
assert_raise(Puppet::ConstantAlreadyDefined) do
const = sub.send(:handleclassconst, newklass, klass.name, {})
end
assert_equal(Baseconst::Genconst.object_id, klass.object_id)
# Now make sure we can replace it
assert_nothing_raised do
const = sub.send(:handleclassconst, newklass, klass.name, :overwrite => true)
end
assert_equal(Baseconst::Genconst.object_id, newklass.object_id)
# Now make sure we can choose our own constant
assert_nothing_raised do
const = sub.send(
:handleclassconst, newklass, klass.name,
:constant => "Fooness")
end
assert(defined?(Baseconst::Fooness), "Specified constant was not defined")
# And make sure prefixes work
assert_nothing_raised do
const = sub.send(
:handleclassconst, newklass, klass.name,
:prefix => "Test")
end
assert(defined?(Baseconst::TestGenconst), "prefix was not used")
end
def test_initclass_preinit
sub, klass = testclasses("preinit")
class << klass
attr_accessor :set
def preinit
@set = true
end
end
assert(!klass.set, "Class was already initialized")
assert_nothing_raised do sub.send(:initclass, klass, {}) end
assert(klass.set, "Class was not initialized")
end
def test_initclass_initvars
sub, klass = testclasses("initvars")
class << klass
attr_accessor :set
def initvars
@set = true
end
end
assert(!klass.set, "Class was already initialized")
assert_nothing_raised do sub.send(:initclass, klass, {}) end
assert(klass.set, "Class was not initialized")
end
def test_initclass_attributes
sub, klass = testclasses("attributes")
class << klass
attr_accessor :one, :two, :three
end
assert(!klass.one, "'one' was already set")
assert_nothing_raised do sub.send(
:initclass, klass,
:attributes => {:one => :a, :two => :b}) end
assert_equal(:a, klass.one, "Class was initialized incorrectly")
assert_equal(:b, klass.two, "Class was initialized incorrectly")
assert_nil(klass.three, "Class was initialized incorrectly")
end
def test_initclass_include_and_extend
sub, klass = testclasses("include_and_extend")
incl = Module.new do
attr_accessor :included
end
self.class.const_set("Incl", incl)
ext = Module.new do
attr_accessor :extended
end
self.class.const_set("Ext", ext)
assert(! klass.respond_to?(:extended), "Class already responds to extended")
assert(! klass.new.respond_to?(:included), "Class already responds to included")
assert_nothing_raised do sub.send(
:initclass, klass,
:include => incl, :extend => ext)
end
assert(klass.respond_to?(:extended), "Class did not get extended")
assert(klass.new.respond_to?(:included), "Class did not include")
end
def test_genclass
hash = {}
array = []
name = "yayness"
klass = nil
assert_nothing_raised {
klass = GenTest.genclass(name, :array => array, :hash => hash, :parent => FakeBase) do
class << self
attr_accessor :name
end
end
}
assert(klass.respond_to?(:name=), "Class did not execute block")
assert(
hash.include?(klass.name),
"Class did not get added to hash")
assert(
array.include?(klass),
"Class did not get added to array")
assert_equal(klass.superclass, FakeBase, "Parent class was wrong")
end
# Make sure we call a preinithook, if there is one.
def test_inithooks
newclass = Class.new(FakeBase) do
class << self
attr_accessor :preinited, :postinited
end
def self.preinit
self.preinited = true
end
def self.postinit
self.postinited = true
end
end
klass = nil
assert_nothing_raised {
klass = GenTest.genclass(:funtest, :parent => newclass)
}
assert(klass.preinited, "prehook did not get called")
assert(klass.postinited, "posthook did not get called")
end
def test_modulegen
hash = {}
array = []
name = "modness"
mod = nil
assert_nothing_raised {
mod = GenTest.genmodule(name, :array => array, :hash => hash) do
class << self
attr_accessor :yaytest
end
@yaytest = true
end
}
assert(mod.respond_to?(:yaytest), "Class did not execute block")
assert_instance_of(Module, mod)
assert(hash.include?(mod.name), "Class did not get added to hash")
assert(array.include?(mod), "Class did not get added to array")
end
def test_genconst_string
const = nil
assert_nothing_raised do
const = GenTest.send(:genconst_string, :testing, :prefix => "Yayness")
end
assert_equal("YaynessTesting", const)
end
end
|