Skip to content

Commit

Permalink
Added one more test using the FilterNode. Found a bug with the accept…
Browse files Browse the repository at this point in the history
… method. Also removed example of using the 'old' way of using the object.
  • Loading branch information
Takadonet committed Dec 20, 2010
1 parent 6caa82d commit cee3d9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
27 changes: 5 additions & 22 deletions lib/Tree/Simple/Visitor.pm
Expand Up @@ -10,7 +10,7 @@ subset DEPTH of Mu where { $_ eq 'RECURSIVE' || $_ eq 'CHILDREN_ONLY'};


has $.depth is rw ; has $.depth is rw ;


has Code $.filter_fcn is rw = Mu; has Code $.filter_fcn is rw;
has Bool $.include_trunk is rw = Bool::False; has Bool $.include_trunk is rw = Bool::False;
has @.results is rw; has @.results is rw;


Expand Down Expand Up @@ -46,7 +46,7 @@ multi method includeTrunk(Bool $trunk) {
self.include_trunk = $trunk; self.include_trunk = $trunk;
} }
#if given Mu, don't die but do nothing. Do not believe it should set to Bool::True #if given Mu, don't die but do nothing. Do not believe it should set to Bool::True
# # node filter methods # node filter methods


method getNodeFilter() { method getNodeFilter() {
return self.filter_fcn; return self.filter_fcn;
Expand Down Expand Up @@ -75,11 +75,12 @@ method visit(Tree::Simple $tree) {
# get all things set up # get all things set up
my @results; my @results;
my $func; my $func;

if self.filter_fcn { if self.filter_fcn {
$func = sub (*@a) { push @results , self.filter_fcn.(@a) }; $func = sub ($a) { push @results , self.filter_fcn.($a) };
} }
else { else {
$func = sub (*@a) { push @results , @a[0].getNodeValue() }; $func = sub ($a) { push @results , $a.getNodeValue() };
} }
# always apply the function # always apply the function
# to the tree's node # to the tree's node
Expand Down Expand Up @@ -141,29 +142,11 @@ Tree::Simple::Visitor - Visitor object for Tree::Simple objects
return $t.getNodeValue().description(); return $t.getNodeValue().description();
}); });


# NOTE: this object has changed, but it still remains
# backwards compatible to the older version, see the
# DESCRIPTION section below for more details


=head1 DESCRIPTION =head1 DESCRIPTION
This object has been revised into what I think is more intelligent approach to Visitor objects. This is now a more suitable base class for building your own Visitors. It is also the base class for the visitors found in the B<Tree::Simple::VisitorFactory> distribution, which includes a number of useful pre-built Visitors. This object has been revised into what I think is more intelligent approach to Visitor objects. This is now a more suitable base class for building your own Visitors. It is also the base class for the visitors found in the B<Tree::Simple::VisitorFactory> distribution, which includes a number of useful pre-built Visitors.


While I have changed a number of things about this module, I have kept it backwards compatible to the old way of using it. So the original example code still works:

my @accumulator;
my $visitor = Tree::Simple::Visitor.new(sub {
my ($tree) = @_;
push @accumlator, $tree.getNodeValue();
},
$Tree::Simple::Visitor::RECURSIVE);

$tree.accept($visitor);

print join ", ", @accumulator; # prints "1.0, 2.0, 2.1.0, 3.0"

But is better expressed as this:

my $visitor = Tree::Simple::Visitor.new(); my $visitor = Tree::Simple::Visitor.new();
$tree.accept($visitor); $tree.accept($visitor);
print join ", ", $visitor.getResults(); # prints "1.0, 2.0, 2.1.0, 3.0" print join ", ", $visitor.getResults(); # prints "1.0, 2.0, 2.1.0, 3.0"
Expand Down
11 changes: 10 additions & 1 deletion t/test_visitor.t
Expand Up @@ -21,11 +21,20 @@ my $tree = Tree::Simple.new($Tree::Simple::ROOT).addChildren(


# by default this will collect all the # by default this will collect all the
# node values in depth-first order into # node values in depth-first order into
# our results # our results


$visitor.setNodeFilter(sub ($t) {
return $t.getNodeValue();
});


$tree.accept($visitor); $tree.accept($visitor);


# get our results and print them # get our results and print them
my @results= <1.0 2.0 2.1.0 3.0>; my @results= <1.0 2.0 2.1.0 3.0>;



is($visitor.getResults().join(', '), @results.join(', '),'Find correct children'); # prints "1.0, 2.0, 2.1.0, 3.0" is($visitor.getResults().join(', '), @results.join(', '),'Find correct children'); # prints "1.0, 2.0, 2.1.0, 3.0"



0 comments on commit cee3d9a

Please sign in to comment.