6 use Test::More tests => 13;
7 use Time::HiRes qw/gettimeofday/;
10 my @fudge_t = ( 0, 0 );
13 *Time::HiRes::gettimeofday = sub () { return @fudge_t };
16 BEGIN { use_ok("Catalyst::Stats") };
19 my $stats = Catalyst::Stats->new;
20 is (ref($stats), "Catalyst::Stats", "new");
22 is_deeply([ $stats->created ], [0, 0], "created time");
24 my @expected; # level, string, time
27 ok($stats->profile("single comment arg"), "profile");
28 push(@expected, [ 0, "- single comment arg", 1, 0 ]);
31 $stats->profile(comment => "hash comment arg");
32 push(@expected, [ 0, "- hash comment arg", 2, 0 ]);
35 $stats->profile(begin => "block", comment => "start block");
36 push(@expected, [ 0, "block - start block", 4, 1 ]);
40 $stats->profile("inside block");
41 push(@expected, [ 1, "- inside block", 1, 0 ]);
44 my $uid = $stats->profile(begin => "nested block", uid => "boo");
45 push(@expected, [ 1, "nested block", 0.7, 1 ]);
46 is ($uid, "boo", "set UID");
50 $stats->profile("this shouldn't appear");
54 $stats->profile(begin => "double nested block 1");
55 push(@expected, [ 2, "double nested block 1", 0.2, 1 ]);
57 $stats->profile(comment => "attach to uid", parent => $uid);
60 $stats->profile(begin => "badly nested block 1");
61 push(@expected, [ 3, "badly nested block 1", 0.35, 1 ]);
64 $stats->profile(comment => "interleave 1");
65 push(@expected, [ 4, "- interleave 1", 0.05, 0 ]);
67 $fudge_t[1] = 400000; # end double nested block time
68 $stats->profile(end => "double nested block 1");
71 $stats->profile(comment => "interleave 2");
72 push(@expected, [ 4, "- interleave 2", 0.2, 0 ]);
75 $stats->profile(begin => "begin with no end");
76 push(@expected, [ 4, "begin with no end", 0.05, 1 ]);
78 $fudge_t[1] = 600000; # end badly nested block time
79 $stats->profile(end => "badly nested block 1");
81 $fudge_t[1] = 800000; # end nested block time
82 $stats->profile(end => "nested block");
84 $fudge_t[0] = 14; # end block time
86 $stats->profile(end => "block", comment => "end block");
88 push(@expected, [ 2, "- attach to uid", 0.1, 0 ]);
91 my @report = $stats->report;
92 is_deeply(\@report, \@expected, "report");
94 # print scalar($stats->report);
96 is ($stats->elapsed, 14, "elapsed");
99 # COMPATABILITY METHODS
103 my $stats = Catalyst::Stats->new;
104 my $root = $stats->{tree};
105 my $uid = $root->getUID;
107 my $visitor = Tree::Simple::Visitor::FindByUID->new;
108 $visitor->includeTrunk(1); # needed for this test
109 $visitor->searchForUID($uid);
110 $stats->accept($visitor);
111 is( $visitor->getResult, $root, '[COMPAT] accept()' );
117 my $stats = Catalyst::Stats->new;
118 my $node = Tree::Simple->new(
126 $stats->addChild( $node );
128 my $actual = $stats->{ tree }->{ _children }->[ 0 ];
129 is( $actual, $node, '[COMPAT] addChild()' );
130 is( $actual->getNodeValue->{ elapsed }, 10, '[COMPAT] addChild(), data munged' );
135 my $stats = Catalyst::Stats->new;
142 $stats->setNodeValue( $stat );
144 is_deeply( $stats->{tree}->getNodeValue, { action => 'test', elapsed => 10, comment => '' } , '[COMPAT] setNodeValue(), data munged' );
149 my $stats = Catalyst::Stats->new;
150 my $expected = $stats->{tree}->getNodeValue->{t};
151 is_deeply( $stats->getNodeValue, $expected, '[COMPAT] getNodeValue()' );
156 my $stats = Catalyst::Stats->new;
157 $stats->{tree}->addChild( Tree::Simple->new( { foo => 'bar' } ) );
159 $stats->traverse( sub { push @value, shift->getNodeValue->{ foo }; } );
161 is_deeply( \@value, [ 'bar' ], '[COMPAT] traverse()' );