/
12_Tree_Simple_exceptions_test.t
404 lines (311 loc) · 13.5 KB
/
12_Tree_Simple_exceptions_test.t
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use v6;
use Test;
plan 50;
BEGIN
{
@*INC.push('lib');
@*INC.push('blib');
}
use Tree::Simple;
## ----------------------------------------------------------------------------
## Exception Tests for Tree::Simple
## ----------------------------------------------------------------------------
class Bad { has $.node = 'Fail'};
my $BAD_OBJECT = Bad.new();
my $TEST_SUB_TREE = Tree::Simple.new("test");
# -----------------------------------------------
# exceptions for new
# -----------------------------------------------
# not giving a proper argument for parent
dies_ok( { Tree::Simple.new("test", 0) });
# not giving a proper argument for parent
dies_ok ( {
Tree::Simple.new("test", []);
} );
# not giving a proper argument for parent
dies_ok( {
Tree::Simple.new("test", $BAD_OBJECT);
} ) ; # qr/^Insufficient Arguments \:/, '... this should die';
# -----------------------------------------------
my $tree = Tree::Simple.new($Tree::Simple::ROOT);
# -----------------------------------------------
# exceptions for setNodeValue
# -----------------------------------------------
# not giving an argument for setNodeValue
dies_ok( {
$tree.setNodeValue();
} ) ; # qr/^Insufficient Arguments \: must supply a value for node/, '... this should die';
# -----------------------------------------------
# exceptions for addChild
# -----------------------------------------------
# not giving an argument for addChild
dies_ok( {
$tree.addChild();
} ) ; # qr/^Insufficient Arguments : no tree\(s\) to insert/, '... this should die';
# # giving an bad argument for addChild
dies_ok( {
$tree.addChild("fail");
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# # giving an bad argument for addChild
dies_ok( {
$tree.addChild([]);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# # giving an bad object argument for addChild
dies_ok( {
$tree.addChild($BAD_OBJECT);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for insertChild
# -----------------------------------------------
#todo this will pass not because it validates the arguments but because their is no alias yet for InsertChild
# giving no index argument for insertChild
dies_ok( {
$tree.insertChild();
} ) ; # qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die';
# giving an out of bounds index argument for insertChild
dies_ok( {
$tree.insertChild(5);
} ) ; # qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die';
# giving an good index argument but no tree argument for insertChild
dies_ok( {
$tree.insertChild(0);
} ) ; # qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die';
# giving an good index argument but an undefined tree argument for insertChild
dies_ok( {
$tree.insertChild(0, Mu);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object tree argument for insertChild
dies_ok( {
$tree.insertChild(0, "Fail");
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# # giving an good index argument but a non-object-ref tree argument for insertChild
dies_ok( {
$tree.insertChild(0, []);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# # giving an good index argument but a bad object tree argument for insertChild
dies_ok( {
$tree.insertChild(0, $BAD_OBJECT);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for insertChildAt
# -----------------------------------------------
# giving no index argument for insertChildAt
dies_ok( {
$tree.insertChildAt();
} ) ; # qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die';
# giving an out of bounds index argument for insertChildAt
dies_ok( {
$tree.insertChildAt(5);
} ) ; # qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die';
# giving an good index argument but no tree argument for insertChildAt
dies_ok( {
$tree.insertChildAt(0);
} ) ; # qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die';
# giving an good index argument but an undefined tree argument for insertChildAt
dies_ok( {
$tree.insertChildAt(0, Mu);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object tree argument for insertChildAt
dies_ok( {
$tree.insertChildAt(0, "Fail");
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# # giving an good index argument but a non-object-ref tree argument for insertChildAt
dies_ok( {
$tree.insertChildAt(0, []);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# # giving an good index argument but a bad object tree argument for insertChildAt
dies_ok( {
$tree.insertChildAt(0, $BAD_OBJECT);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for insertChildren
# -----------------------------------------------
# NOTE:
# even though insertChild and insertChildren are
# implemented in the same function, it makes sense
# to future-proof our tests by checking it anyway
# this will help to save us the trouble later on
#todo this will pass not because it validates the arguments but because their is no alias yet for insertChildren
# giving no index argument for insertChild
dies_ok( {
$tree.insertChildren();
} ) ; # qr/^Insufficient Arguments \: Cannot insert child without index/, '... this should die';
# giving an out of bounds index argument for insertChild
dies_ok( {
$tree.insertChildren(5);
} ) ; # qr/^Index Out of Bounds \: got \(5\) expected no more than \(0\)/, '... this should die';
# giving an good index argument but no tree argument for insertChild
dies_ok( {
$tree.insertChildren(0);
} ) ; # qr/^Insufficient Arguments \: no tree\(s\) to insert/, '... this should die';
# giving an good index argument but a Mu tree argument for insertChild
dies_ok( {
$tree.insertChildren(0, Mu);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object tree argument for insertChild
dies_ok( {
$tree.insertChildren(0, "Fail");
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a non-object-ref tree argument for insertChild
dies_ok( {
$tree.insertChildren(0, []);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# giving an good index argument but a bad object tree argument for insertChild
dies_ok( {
$tree.insertChildren(0, $BAD_OBJECT);
} ) ; # qr/^Insufficient Arguments \: Child must be a Tree\:\:Simple object/, '... this should die';
# -----------------------------------------------
# exceptions for removeChildAt
# -----------------------------------------------
# giving no index argument for removeChildAt
dies_ok( {
$tree.removeChildAt();
} ) ; # qr/^Insufficient Arguments \: Cannot remove child without index/, '... this should die';
# attempt to remove a child when there are none
dies_ok( {
$tree.removeChildAt(5);
} ) ; # qr/^Illegal Operation \: There are no children to remove/, '... this should die';
# add a child now
$tree.addChild($TEST_SUB_TREE);
# giving no index argument for removeChildAt
dies_ok( {
$tree.removeChildAt(5);
} ) ; # qr/^Index Out of Bounds \: got \(5\) expected no more than \(1\)/, '... this should die';
is($tree.removeChildAt(0), $TEST_SUB_TREE, '... these should be the same');
# -----------------------------------------------
# exceptions for removeChild
# -----------------------------------------------
# giving no index argument for removeChild
dies_ok( {
$tree.removeChild();
} ) ; # qr/^Insufficient Arguments \: /, '... this should die';
# giving bad ref argument
dies_ok( {
$tree.removeChild([]);
} ) ; # qr/^Insufficient Arguments \: /, '... this should die';
# giving bad object argument
dies_ok( {
$tree.removeChild($BAD_OBJECT);
} ) ; # qr/^Insufficient Arguments \: /, '... this should die';
# giving bad object argument
dies_ok( {
$tree.removeChild($TEST_SUB_TREE);
} ) ; # qr/^Child Not Found \: /, '... this should die';
# -----------------------------------------------
# exceptions for *Sibling methods
# -----------------------------------------------
# attempting to add sibling to root trees
dies_ok( {
$tree.addSibling($TEST_SUB_TREE);
} ) ; # qr/^Insufficient Arguments \: cannot add a sibling to a ROOT tree/, '... this should die';
# attempting to add siblings to root trees
dies_ok( {
$tree.addSiblings($TEST_SUB_TREE);
} ) ; # qr/^Insufficient Arguments \: cannot add siblings to a ROOT tree/, '... this should die';
# attempting to insert sibling to root trees
dies_ok( {
$tree.insertSibling(0, $TEST_SUB_TREE);
} ) ; # qr/^Insufficient Arguments \: cannot insert sibling\(s\) to a ROOT tree/, '... this should die';
# attempting to insert sibling to root trees
dies_ok( {
$tree.insertSiblings(0, $TEST_SUB_TREE);
} ) ; # qr/^Insufficient Arguments \: cannot insert sibling\(s\) to a ROOT tree/, '... this should die';
# -----------------------------------------------
# exceptions for getChild
# -----------------------------------------------
# # not giving an index to the getChild method
dies_ok( {
$tree.getChild();
} ) ; # qr/^Insufficient Arguments \: Cannot get child without index/, '... this should die';
# -----------------------------------------------
# exceptions for getSibling
# -----------------------------------------------
# trying to get siblings of a root tree
dies_ok( {
$tree.getSibling();
} ) ; # qr/^Insufficient Arguments \: cannot get siblings from a ROOT tree/, '... this should die';
# trying to get siblings of a root tree
dies_ok( {
$tree.getAllSiblings();
} ) ; # qr/^Insufficient Arguments \: cannot get siblings from a ROOT tree/, '... this should die';
# -----------------------------------------------
# exceptions for traverse
# -----------------------------------------------
# passing no args to traverse
dies_ok( {
$tree.traverse();
} ) ; # qr/^Insufficient Arguments \: Cannot traverse without traversal function/, '... this should die';
# passing non-ref arg to traverse
dies_ok( {
$tree.traverse("Fail");
} ) ; # qr/^Incorrect Object Type \: traversal function is not a function/, '... this should die';
# passing non-code-ref arg to traverse
dies_ok( {
$tree.traverse($BAD_OBJECT);
} ) ; # qr/^Incorrect Object Type \: traversal function is not a function/, '... this should die';
# passing second non-ref arg to traverse
dies_ok( {
$tree.traverse(sub {}, "Fail");
} ) ; # qr/^Incorrect Object Type \: post traversal function is not a function/, '... this should die';
# passing second non-code-ref arg to traverse
dies_ok( {
$tree.traverse(sub {}, $BAD_OBJECT);
} ) ; # qr/^Incorrect Object Type \: post traversal function is not a function/, '... this should die';
# -----------------------------------------------
# exceptions for accept
# -----------------------------------------------
#todo have not implemented vistor class or methods
# passing no args to accept
#dies_ok( {
# $tree.accept();
#} ) ; # qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# # passing non-ref arg to accept
#dies_ok( {
# $tree.accept("Fail");
#} ) ; # qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# # passing non-object-ref arg to accept
#dies_ok( {
# $tree.accept([]);
#} ) ; # qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# # passing non-Tree::Simple::Visitor arg to accept
#dies_ok( {
# $tree.accept($BAD_OBJECT);
#} ) ; # qr/^Insufficient Arguments \: You must supply a valid Visitor object/, '... this should die';
# {
# package TestPackage;
# sub visit {}
# };
# # passing non-Tree::Simple::Visitor arg to accept
# lives_ok {
# $tree.accept(bless({}, "TestPackage"));
#} ) ; # '... but, this should live';
# # -----------------------------------------------
# # exceptions for setParent
# # -----------------------------------------------
#todo may remove setParent completely
# # if no parent is given
#dies_ok( {
# $tree.setParent();
#} ) ; # qr/^Insufficient Arguments/, '... this should croak';
# # if the parent that is given is not an object
#dies_ok( {
# $tree.setParent("Test");
#} ) ; # qr/^Insufficient Arguments/, '... this should croak';
# # if the parent that is given is a ref but not an object
#dies_ok( {
# $tree.setParent([]);
#} ) ; # qr/^Insufficient Arguments/, '... this should croak';
# # and if the parent that is given is an object but
# # is not a Tree::Simple object
#dies_ok( {
# $tree.setParent($BAD_OBJECT);
#} ) ; # qr/^Insufficient Arguments/, '... this should croak';
# -----------------------------------------------
# exceptions for setUID
# -----------------------------------------------
dies_ok( {
$tree.setUID();
} ) ; # qr/^Insufficient Arguments/, '... this should croak';
## ----------------------------------------------------------------------------
## end Exception Tests for Tree::Simple
## ----------------------------------------------------------------------------