Follow mg_obj, which points to an SV.
[p5sagit/Devel-Size.git] / t / magic.t
CommitLineData
72e2658d 1#!/usr/bin/perl -w
2
3use strict;
b7130948 4use Test::More tests => 11;
72e2658d 5use Devel::Size ':all';
6require Tie::Scalar;
7
8{
9 my $string = 'Perl Rules';
10 my $before_size = total_size($string);
11 is($string =~ /Perl/g, 1, 'It had better match');
12 cmp_ok($before_size, '>', length $string,
13 'Our string has a non-zero length');
14 cmp_ok(total_size($string), '>', $before_size,
15 'size increases due to magic');
16}
17
18{
19 my $string = 'Perl Rules';
20 my $before_size = total_size($string);
21 formline $string;
22 my $compiled_size = total_size($string);
23 cmp_ok($before_size, '>', length $string,
24 'Our string has a non-zero length');
25 cmp_ok($compiled_size, '>', $before_size,
26 'size increases due to magic (and the compiled state)');
27 # Not fully sure why (didn't go grovelling) but need to use a temporary to
28 # avoid the magic being copied.
29 $string = '' . $string;
30 my $after_size = total_size($string);
31 cmp_ok($after_size, '>', $before_size, 'Still larger than initial size');
32 cmp_ok($after_size, '<', $compiled_size, 'size decreases due to unmagic');
33}
b7130948 34
35{
36 my $string = 'Perl Rules';
37 my $before_size = total_size($string);
38 cmp_ok($before_size, '>', length $string,
39 'Our string has a non-zero length');
40 tie $string, 'Tie::StdScalar';
41 my $after_size = total_size($string);
42 cmp_ok($after_size, '>', $before_size, 'size increases due to magic');
43 is($string, undef, 'No value yet');
44 # This is defineately cheating, in that we're poking inside the
45 # implementation of Tie::StdScalar, but if we just write to $string, the way
46 # magic works, the (nice long) value is first written to the regular scalar,
47 # then picked up by the magic. So it grows, which defeats the purpose of the
48 # test.
49 ${tied $string} = 'X' x 1024;
50 cmp_ok(total_size($string), '>', $after_size + 1024,
51 'the magic object is counted');
52}