File Coverage

blib/lib/Tree/Visualize/GraphViz/Layouts/Simple/Tree.pm
Criterion Covered Total %
statement 30 30 100.0
branch 3 6 50.0
condition 2 6 33.3
subroutine 8 8 100.0
pod 1 1 100.0
total 44 51 86.2


line stmt bran cond sub pod time code
1              
2             package Tree::Visualize::GraphViz::Layouts::Simple::Tree;
3              
4 2     2   2988 use strict;
  2         8  
  2         65  
5 2     2   10 use warnings;
  2         4  
  2         51  
6              
7 2     2   11 use Tree::Visualize::Exceptions;
  2         4  
  2         97  
8              
9             our $VERSION = '0.01';
10              
11 2     2   29 use base qw(Tree::Visualize::Layout::ILayout);
  2         3  
  2         1027  
12              
13             sub draw {
14 1     1 1 3 my ($self, $tree) = @_;
15 1 50 33     18 (defined($tree) && ref($tree) && UNIVERSAL::isa($tree, "Tree::Simple"))
      33        
16             || throw Tree::Visualize::IncorrectObjectType "argument must be Tree::Simple instance";
17 1         5 return $self->_draw($tree);
18             }
19            
20             sub _draw {
21 1     1   3 my ($self, $tree) = @_;
22 1         2 my $output = "digraph test {\n";
23 1         6 $output .= "node_" . $tree->getUID() . " [ label = \"" . $tree->getNodeValue() . "\" ];\n";
24 1         12 $output .= $self->_draw_tree($tree);
25 1         2 $output .= "}\n";
26 1         9 return $output;
27             }
28              
29             sub _draw_tree {
30 1     1   2 my ($self, $tree) = @_;
31 1         2 my $output = "";
32             $tree->traverse(sub {
33 25     25   383 my ($tree) = @_;
34 25         56 my $tree_id = $tree->getUID();
35 25 50       117 my $parent_id = $tree->getParent()->getUID() unless $tree->isRoot();
36 25         314 $output .= "node_$tree_id [ label = \"" . $tree->getNodeValue() . "\" ];\n";
37 25 50       176 $output .= "node_${parent_id} -> node_${tree_id};\n" if $parent_id;
38 1         13 });
39 1         24 return $output;
40             }
41              
42              
43              
44             1;
45              
46             __END__