forked from ryan-allen/lumberjack
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlumberjack_test.rb
More file actions
324 lines (283 loc) · 9.01 KB
/
Copy pathlumberjack_test.rb
File metadata and controls
324 lines (283 loc) · 9.01 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
%w(test/unit lumberjack).each { |f| require f }
class Family
attr_accessor :name, :members, :heritage
def initialize(name = nil, args = {})
@name = name
args.each { |k,v| send "#{k}=", v }
end
end
class Person
attr_accessor :given_name, :age
def initialize(name = nil, age = nil)
@given_name, @age = name, age
end
end
class Showroom < Array
end
class Vehicle
class Heavy < Vehicle
class ReallyHeavy < Heavy
end
end
attr_accessor :name, :wheels, :person
def initialize(args = {:name => 'A Car, ya mum'})
@name = args[:name]
@wheels = SetOfWheels.new
end
end
class SetOfWheels < Array
end
class Wheel
attr_accessor :wear
def initialize(args)
@wear = args[:wear]
end
end
# tree = Lumberjack.construct do # create a list
# family do # new Family
# name 'Allen' # name = on instance of Family, scope :instance
# members do # assign a list to members =, scope :list
# person 'Tim', 58 # << Person.new('Tim', 58)
# person 'Jan', 52
# person 'Ryan', 25
# person 'Bridget' do
# age 22
# end
# person do
# name 'Becca'
# age 20
# end
# end
# end
# end
class LumberjackTest < Test::Unit::TestCase
def test_construct_returns_an_empty_list
assert_equal [], Lumberjack.construct
end
def test_can_create_a_single_class
tree = Lumberjack.construct do
family {} # api change w/ scoping requires a block to be passed, otherwise can't tell if you're
# trying to resolve a nested scope
end
assert 1, tree.length
assert_kind_of Family, tree.first
end
def test_can_create_a_single_class_passing_in_args
tree = Lumberjack.construct do
family 'Allen', :heritage => :mixed
end
assert 1, tree.length
assert_kind_of Family, tree.first
assert_equal 'Allen', tree.first.name
assert_equal :mixed, tree.first.heritage
end
def test_can_create_two_classes_passing_in_args
tree = Lumberjack.construct do
family 'Allen', :heritage => [:english, :irish]
family 'Ta\'eed', :heritage => [:iranian, :english]
end
assert 2, tree.length
assert_kind_of Family, tree[0]
assert_equal 'Allen', tree[0].name
assert_equal [:english, :irish], tree[0].heritage
assert_kind_of Family, tree[1]
assert_equal 'Ta\'eed', tree[1].name
assert_equal [:iranian, :english], tree[1].heritage
end
def test_can_set_instance_members_with_block
tree = Lumberjack.construct do
family do
name 'Allen'
heritage [:english, :irish]
end
end
assert 1, tree.length
assert_kind_of Family, tree[0]
assert_equal 'Allen', tree[0].name
assert_equal [:english, :irish], tree[0].heritage
end
def test_can_used_mixed_constructor_and_instance_members_in_blocke
tree = Lumberjack.construct do
family 'Allen' do
heritage [:english, :irish]
end
end
assert 1, tree.length
assert_kind_of Family, tree[0]
assert_equal 'Allen', tree[0].name
assert_equal [:english, :irish], tree[0].heritage
end
def test_create_list_in_scoped_instance_if_block_with_no_args
tree = Lumberjack.construct do
family 'Allen' do
heritage [:english, :irish]
members do # working from here
person 'Tim', 58
person 'Jan', 54
person 'Ryan' do
age 24
end
end
end
end
assert 1, tree.length
assert_kind_of Family, tree[0]
assert_equal 'Allen', tree[0].name
assert_equal [:english, :irish], tree[0].heritage
assert_equal 3, tree[0].members.length
assert_equal 'Tim', tree[0].members[0].given_name
assert_equal 58, tree[0].members[0].age
assert_equal 'Jan', tree[0].members[1].given_name
assert_equal 54, tree[0].members[1].age
assert_equal 'Ryan', tree[0].members[2].given_name
assert_equal 24, tree[0].members[2].age
end
def test_can_take_generate_arrays_with_comma_semantics_and_tell_the_difference
tree = Lumberjack.construct do
family 'Allen' do
heritage :english, :irish
end
end
assert_equal [:english, :irish], tree[0].heritage
end
def test_will_push_element_onto_object_if_list_accessor_is_already_initialized
vehicles = Lumberjack.construct do
vehicle :name => 'unicycle' do
wheels do
wheel :wear => 'bald'
end
end
end
assert_kind_of SetOfWheels, vehicles[0].wheels
end
def test_can_set_initial_context_to_something_else_besdies_an_array
showroom = Lumberjack.construct Showroom.new do
vehicle :name => 'a FERRARRI!!!1'
vehicle :name => 'a MASERATI!!!1'
vehicle :name => 'a PORCHE OMG!!!'
end
assert_kind_of Showroom, showroom
assert_equal 3, showroom.length
end
# biggest hack ever, use a ! to isntanciate a class to an accessor, must be
# inferable by the accessor name, such a large hack, but we need it for
# production, and i'm sure other people will need it, so lets leave this
# gaping flaw of lumberjack for the time being till we can think of something
# more nice and appropriate :/ :D
def test_can_create_instance_of_class_via_bang_method
cars = Lumberjack.construct do
vehicle :name => 'Prius (are owned by rich hippies)' do
person! 'Ryan' do # i so put my foot in here, i'm not a rich hippy!
age 25
end
end
end
assert_kind_of Person, cars[0].person
assert_equal 'Ryan', cars[0].person.given_name
assert_equal 25, cars[0].person.age
end
def test_can_create_list_of_primitives # not sure this is valid useage (of course it is you big dummy ryan from the past!)
tree = Lumberjack.construct do
array [:one, :two, :three]
array [:four, :five, :six]
end
assert_equal [[:one, :two, :three], [:four, :five, :six]], tree
end
def test_we_got_backslashes_that_resolve_scope_or_something
cars = Lumberjack.construct do
vehicle :name => 'Normal Car'
# unfortunatley we need to use parantehseshtheses here :(
vehicle/heavy(:name => 'Heavy Car')
vehicle/heavy/really_heavy(:name => 'Really Heavy Heavy Car')
end
assert_kind_of Vehicle, cars[0]
assert_kind_of Vehicle::Heavy, cars[1]
assert_kind_of Vehicle::Heavy::ReallyHeavy, cars[2]
end
def test_we_can_load_in_other_files
family = Lumberjack.construct do
family 'Barton' do
heritage [:dutch, :mongrel_aussie]
members do
load_tree_file 'examples/people.rb'
end
end
end
assert 1, family.length
assert_kind_of Family, family.first
assert_equal 'Barton', family.first.name
assert_equal [:dutch, :mongrel_aussie], family.first.heritage
assert_equal 5, family.first.members.size
assert_equal 'John S', family.first.members.first.given_name
assert_equal 50, family.first.members.first.age
assert_equal 'Ethan', family.first.members.last.given_name
assert_equal 10, family.first.members.last.age
end
def test_we_can_share_branches_that_are_defined
families = Lumberjack.construct do
shared_branch :kids do
person 'Jack', 11
person 'Jill', 10
end
family "Dad's new family" do
members do
person 'Dad', 45
graft_branch :kids
end
end
family "Mum's new family" do
members do
person 'Mum', 40
person 'Red-headed step-child', 8
graft_branch :kids
end
end
end
assert 2, families.length
assert_kind_of Family, families[0]
assert_kind_of Family, families[1]
assert 3, families[0].members.size
assert families[0].members.any? {|m| m.given_name == 'Jack'}
assert families[0].members.any? {|m| m.given_name == 'Jill'}
assert 4, families[1].members.size
assert families[1].members.any? {|m| m.given_name == 'Jack'}
assert families[1].members.any? {|m| m.given_name == 'Jill'}
end
def test_we_can_remove_twigs_with_prune
families = Lumberjack.construct do
shared_branch :kids do
person 'Jack', 12
person 'Jane', 10
person 'Bob', 10
end
shared_branch :update_kids do
prune :age, 10
person 'Will', 11
end
family "Dad's family" do
members do
person 'Dad', 45
person 'Mum', 49
prune :given_name, 'Mum'
person 'Mum', 26
graft_branch :kids
graft_branch :update_kids
end
end
end
assert families[0].members.any? {|m| m.given_name == 'Will' && m.age == 11}
assert families[0].members.any? {|m| m.given_name == 'Jack' && m.age == 12}
assert families[0].members.any? {|m| m.given_name == 'Mum' && m.age == 26}
assert families[0].members.any? {|m| m.given_name == 'Dad' && m.age == 45}
end
def test_we_cant_share_branches_that_are_undefined
# TODO: why does this output funny stuff
assert_raise RuntimeError do
Lumberjack.construct do
family 'wont work' do
graft_branch :non_existant
end
end
end
end
end