File: 011-class.t

package info (click to toggle)
libdata-printer-perl 1.002001-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 748 kB
  • sloc: perl: 4,364; makefile: 7; sh: 1
file content (574 lines) | stat: -rw-r--r-- 14,498 bytes parent folder | download
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
use strict;
use warnings;

package Bar;
sub bar    { 'Bar' }
sub borg   { }
sub _moo   { }
1;

package Foo;
our @ISA = qw(Bar);
sub new    { bless { test => 42 }, shift }
sub foo    { }
sub baz    { }
sub borg   { $_[0]->{borg} = $_[1]; }
sub _other { }
1;

package Baz;
sub bar { 'Baz' }
1;

package Meep;
our @ISA = qw(Foo Baz);
1;

package ParentLess;
sub new    { bless {}, shift }
1;

package FooArray;
sub new    { bless [], shift }
sub foo    { }
1;

package FooScalar;
sub new    { my $val = 42; bless \$val, shift }
sub foo    { }
1;

package FooCode;
sub new    { my $ref = sub {}; bless $ref, shift }
sub foo    { }
1;

package ICanHazStringOverload;
use overload
  '""' => sub { 'le string of le object' };
sub new { bless {}, shift };
1;

package ICanHazNumberOverload;
use overload
  '0+' => sub { 42 };
sub new { bless {}, shift };
1;

package ChildOfOverload;
our @ISA = ('ICanHazNumberOverload');
sub new { bless {}, shift };
1;

package UnrelatedOverload;
use overload '<' => sub {}, '+' => sub {};
sub new { bless {}, shift };
1;

package ICanHazStringMethodOne;
sub new { bless {}, shift };
sub as_string { 'number one!' }
sub stringify { 'second!' };
1;

package ICanHazStringMethodTwo;
sub new { bless {}, shift };
sub stringify { 'second!' };
1;


package main;
use Test::More tests => 38;
use Data::Printer::Object;
use Data::Printer::Common;

my $ddp = Data::Printer::Object->new( colored => 0 );

# first we try some very weird edge cases
# https://github.com/garu/Data-Printer/issues/105
my $weird = bless {}, 'HASH';

is(
    $ddp->parse($weird),
'HASH  {
    public methods (0)
    private methods (0)
    internals: {}
}', 'empty "HASH" object'
);

$weird = bless [], 'ARRAY';
is(
    $ddp->parse($weird),
'ARRAY  {
    public methods (0)
    private methods (0)
    internals: []
}', 'empty "ARRAY" object'
);

$weird = bless {}, "0";
is(
    $ddp->parse($weird),
'0  {
    public methods (0)
    private methods (0)
    internals: {}
}', 'empty "0" object'
);

# okay, now back to testing "proper" objects :)
my $object = Foo->new;

is(
    $ddp->parse($object),
'Foo  {
    parents: Bar
    public methods (5):
        baz, borg, foo, new
        Bar:
            bar
    private methods (1): _other
    internals: {
        test   42
    }
}',
    'testing objects'
);

$ddp = Data::Printer::Object->new( colored => 0, class => { linear_isa => 1 } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    linear @ISA: Foo, Bar
    public methods (5):
        baz, borg, foo, new
        Bar:
            bar
    private methods (1): _other
    internals: {
        test   42
    }
}', 'testing objects, forcing linear @ISA' );

$ddp = Data::Printer::Object->new( colored => 0, class => { parents => 0 } );
is( $ddp->parse($object), 'Foo  {
    public methods (5):
        baz, borg, foo, new
        Bar:
            bar
    private methods (1): _other
    internals: {
        test   42
    }
}', 'testing objects (parents => 0)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_methods => 'none' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    internals: {
        test   42
    }
}', 'testing objects (no methods)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_methods => 'public' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (5):
        baz, borg, foo, new
        Bar:
            bar
    internals: {
        test   42
    }
}', 'testing objects (only public methods)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_methods => 'private' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (1):
        Bar:
            bar
    private methods (1): _other
    internals: {
        test   42
    }
}', 'testing objects (show_methods private, show_inherited public)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_methods => 'private', inherited => 'private' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    private methods (2):
        _other
        Bar:
            _moo
    internals: {
        test   42
    }
}', 'testing objects (only private methods)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_methods => 'private', inherited => 'none' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    private methods (1): _other
    internals: {
        test   42
    }
}', 'testing objects (only public private methods)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_methods => 'all', inherited => 'all' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (5):
        baz, borg, foo, new
        Bar:
            bar
    private methods (2):
        _other
        Bar:
            _moo
    internals: {
        test   42
    }
}', 'testing objects (explicitly asking for all methods)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { internals => 0 } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (5):
        baz, borg, foo, new
        Bar:
            bar
    private methods (1): _other
}', 'testing objects (no internals)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'none' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (4): baz, borg, foo, new
    private methods (1): _other
    internals: {
        test   42
    }
}', 'testing objects (inherited => "none")' );

my $sort_class = Data::Printer::Common::_initialize_nsort();
my $has_uc_sort = $sort_class eq 'Sort::Key::Natural';

my @methods = $has_uc_sort ? (
    (defined &UNIVERSAL::DOES ? 'DOES (UNIVERSAL)' : ()),
    'VERSION (UNIVERSAL)',
    'bar (Bar)',
    'baz',
    'borg',
    'can (UNIVERSAL)',
    'foo',
    (defined &UNIVERSAL::import ? 'import (UNIVERSAL)' : ()),
    'isa (UNIVERSAL)',
    'new',
    (defined &UNIVERSAL::unimport ? 'unimport (UNIVERSAL)' : ()),
) : (
    'bar (Bar)',
    'baz',
    'borg',
    'can (UNIVERSAL)',
    (defined &UNIVERSAL::DOES ? 'DOES (UNIVERSAL)' : ()),
    'foo',
    (defined &UNIVERSAL::import ? 'import (UNIVERSAL)' : ()),
    'isa (UNIVERSAL)',
    'new',
    (defined &UNIVERSAL::unimport ? 'unimport (UNIVERSAL)' : ()),
    'VERSION (UNIVERSAL)',
);

my $n = @methods;
my $public_method_list = join ', ', @methods;

$ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'all', universal => 1, format_inheritance => 'string' } );
is( $ddp->parse($object), "Foo  {
    parents: Bar
    public methods ($n): $public_method_list
    private methods (2): _moo (Bar), _other
    internals: {
        test   42
    }
}", 'testing objects (inherited => "all", universal => 1, format_inheritance => string)' );

$ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'all', universal => 1, format_inheritance => 'lines' } );

my $universal_methods = join ', ', $has_uc_sort ? (
    (defined &UNIVERSAL::DOES ? 'DOES' : ()),
    'VERSION',
    'can',
    (defined &UNIVERSAL::import ? 'import' : ()),
    'isa',
    (defined &UNIVERSAL::unimport ? 'unimport' : ()),
) : (
    'can',
    (defined &UNIVERSAL::DOES ? 'DOES' : ()),
    (defined &UNIVERSAL::import ? 'import' : ()),
    'isa',
    (defined &UNIVERSAL::import ? 'unimport' : ()),
    'VERSION',
);

is( $ddp->parse($object), "Foo  {
    parents: Bar
    public methods ($n):
        baz, borg, foo, new
        Bar:
            bar
        UNIVERSAL:
            $universal_methods
    private methods (2):
        _other
        Bar:
            _moo
    internals: {
        test   42
    }
}", 'testing objects (inherited => "all", universal => 1, format_inheritance => "lines")' );

$ddp = Data::Printer::Object->new( colored => 0, class => { expand => 0 } );
is( $ddp->parse($object), 'Foo',
    'testing objects without expansion' );

$object->borg( Foo->new );

$ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'none' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (4): baz, borg, foo, new
    private methods (1): _other
    internals: {
        borg   Foo,
        test   42
    }
}', 'testing nested objects' );

$ddp = Data::Printer::Object->new( colored => 0, class => { expand => 'all', inherited => 'none' } );
is( $ddp->parse($object), 'Foo  {
    parents: Bar
    public methods (4): baz, borg, foo, new
    private methods (1): _other
    internals: {
        borg   Foo  {
            parents: Bar
            public methods (4): baz, borg, foo, new
            private methods (1): _other
            internals: {
                test   42
            }
        },
        test   42
    }
}', 'testing nested objects with expansion' );

my $obj_with_isa = Meep->new;

SKIP: {
    my $has_mro = Data::Printer::Common::_initialize_mro();
    skip 'MRO::Compat not available, linear ISA not reliable', 3 unless $has_mro == 1;

$ddp = Data::Printer::Object->new( colored => 0 );
is( $ddp->parse($obj_with_isa), 'Meep  {
    parents: Foo, Baz
    linear @ISA: Meep, Foo, Bar, Baz
    public methods (5):
        Bar:
            bar
        Foo:
            baz, borg, foo, new
    private methods (0)
    internals: {
        test   42
    }
}', 'testing objects with @ISA' );

$ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'none' } );
is( $ddp->parse($obj_with_isa), 'Meep  {
    parents: Foo, Baz
    linear @ISA: Meep, Foo, Bar, Baz
    public methods (0)
    private methods (0)
    internals: {
        test   42
    }
}', 'testing objects with @ISA and no inheritance' );

$ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'none', universal => 1 } );
is( $ddp->parse($obj_with_isa), 'Meep  {
    parents: Foo, Baz
    linear @ISA: Meep, Foo, Bar, Baz, UNIVERSAL
    public methods (0)
    private methods (0)
    internals: {
        test   42
    }
}', 'testing objects with @ISA and no inheritance but with universal' );
};

$ddp = Data::Printer::Object->new( colored => 0, class => { linear_isa => 0, inherited => 'none' } );
is( $ddp->parse($obj_with_isa), 'Meep  {
    parents: Foo, Baz
    public methods (0)
    private methods (0)
    internals: {
        test   42
    }
}', 'testing objects with @ISA, opting out the @ISA' );

$ddp = Data::Printer::Object->new( colored => 0 );
my $parentless = ParentLess->new;

is( $ddp->parse($parentless), 'ParentLess  {
    public methods (1): new
    private methods (0)
    internals: {}
}', 'testing parentless object' );

$ddp = Data::Printer::Object->new( colored => 0 );
my $scalar_obj = FooScalar->new;
is( $ddp->parse($scalar_obj), 'FooScalar  {
    public methods (2): foo, new
    private methods (0)
    internals: 42
}', 'testing blessed scalar' );

$ddp = Data::Printer::Object->new( colored => 0, class => { show_reftype => 1 } );
is( $ddp->parse($scalar_obj), 'FooScalar (SCALAR)  {
    public methods (2): foo, new
    private methods (0)
    internals: 42
}', 'testing blessed scalar with reftype' );

$ddp = Data::Printer::Object->new( colored => 0 );
my $array_obj = FooArray->new;
is( $ddp->parse($array_obj), 'FooArray  {
    public methods (2): foo, new
    private methods (0)
    internals: []
}', 'testing blessed array' );

$ddp = Data::Printer::Object->new( colored => 0 );
my $code_obj = FooCode->new;
is( $ddp->parse($code_obj), 'FooCode  {
    public methods (2): foo, new
    private methods (0)
    internals: sub { ... }
}', 'testing blessed code' );

$ddp = Data::Printer::Object->new( colored => 0 );
my $str_overload = ICanHazStringOverload->new;
is( $ddp->parse($str_overload),
    'le string of le object (ICanHazStringOverload)',
    'object with string overload'
);
my $num_overload = ICanHazNumberOverload->new;
is( $ddp->parse($num_overload),
    '42 (ICanHazNumberOverload)',
    'object with number overload'
);

my $child_overload = ChildOfOverload->new;
is( $ddp->parse($child_overload),
    '42 (ChildOfOverload)',
    'object with inherited overload'
);

my $unrelated = UnrelatedOverload->new;
is( $ddp->parse($unrelated), 'UnrelatedOverload  {
    public methods (1): new
    private methods (0)
    overloads: +, <
    internals: {}
}',
    'object with different overload (should not stringify - lines inheritance)'
);

$ddp = Data::Printer::Object->new( colored => 0, class => { format_inheritance => 'string' } );
is( $ddp->parse($unrelated), 'UnrelatedOverload  {
    public methods (1): new
    private methods (0)
    overloads: +, <
    internals: {}
}',
    'object with different overload (should not stringify - string inheritance)'
);

$ddp = Data::Printer::Object->new( colored => 0, class => { show_overloads => 0 } );
is( $ddp->parse($unrelated), 'UnrelatedOverload  {
    public methods (1): new
    private methods (0)
    internals: {}
}',
    'object with different overload (not showing overloads)'
);

$ddp = Data::Printer::Object->new( colored => 0 );
is( $ddp->parse( ICanHazStringMethodOne->new ),
    'number one! (ICanHazStringMethodOne)',
    'object with as_string and stringify (prefer as_string)'
);

is( $ddp->parse( ICanHazStringMethodTwo->new ),
    'second! (ICanHazStringMethodTwo)',
    'object with stringify'
);

$ddp = Data::Printer::Object->new( colored => 0, class => { stringify => 0 } );
is( $ddp->parse( ICanHazStringMethodTwo->new ),
'ICanHazStringMethodTwo  {
    public methods (2): new, stringify
    private methods (0)
    internals: {}
}',
'object with stringify => 0 expands normally'
);

my $xs_code = <<'EOXS';
package MyDDPXSClass;
use Class::XSAccessor
  constructor => 'new',
  accessors => {
    foo => 'foo',
    bar => 'bar',
    _private_from_parent => '_private_from_parent',
  };

sub meep { 42 }
sub muup { 33 }

1;

package MyDDPXSChild;
use base 'MyDDPXSClass';
use Class::XSAccessor
  accessors => { meep => 'meep', moop => 'moop', _priv => '_priv' };

1;

1;
EOXS

SKIP: {
    skip 'Class::XSAccessor not available to test XS inheritance', 1
        unless eval "$xs_code";
    package main;
    my $obj = MyDDPXSChild->new( meep => 12, bar => 'test' );
    my $ddp = Data::Printer::Object->new( colored => 0, class => { inherited => 'all' } );
    is( $ddp->parse($obj), 'MyDDPXSChild  {
    parents: MyDDPXSClass
    public methods (6):
        meep, moop
        MyDDPXSClass:
            bar, foo, muup, new
    private methods (2):
        _priv
        MyDDPXSClass:
            _private_from_parent
    internals: {
        bar    "test",
        meep   12
    }
}', 'proper introspection of XS object');
};