Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Now passing all tests in 20_.* Fixed up depth attribute to accept onl…

…y two class constants. Hopefully in time use a subset attribute
  • Loading branch information...
commit 79b496f43233a3124ab1ede7b36acc3dccd1feb6 1 parent d35c434
@Takadonet authored
Showing with 72 additions and 72 deletions.
  1. +29 −29 lib/Tree/Simple/Visitor.pm
  2. +43 −43 t/20_Tree_Simple_Visitor_test.t
View
58 lib/Tree/Simple/Visitor.pm
@@ -2,11 +2,11 @@ class Tree::Simple::Visitor{
use Tree::Simple;
# class constants
-our $RECURSIVE = 'RECUSRIVE';
+our $RECURSIVE = 'RECURSIVE';
our $CHILDREN_ONLY = 'CHILDREN_ONLY';
-#RECURSIVE CHILDREN_ONLY or an integer
-subset DEPTH of Mu where { $_ eq 'RECURSIVE' || $_ eq 'CHILDREN_ONLY' or $_ ~~ Int};
+#RECURSIVE CHILDREN_ONLY
+subset DEPTH of Mu where { $_ eq 'RECURSIVE' || $_ eq 'CHILDREN_ONLY'};
has $.depth is rw ;
@@ -20,11 +20,12 @@ multi method new(){
}
multi method new(Code $func) {
- self.bless(*, depth => 0,filter_fcn => $func, include_trunk=>Bool::True);
+ self.bless(*, depth => 'RECURSIVE',filter_fcn => $func, include_trunk=>Bool::True);
}
-#check to see if $depth is a integer or RECURSIVE or CHILDREN_ONLY
+#check to see if $depth is RECURSIVE or CHILDREN_ONLY
multi method new(Code $func,$depth) {
+ die 'Not legal value for depth.' if $depth ne 'RECURSIVE' and $depth ne 'CHILDREN_ONLY';
self.bless(*, depth => $depth,filter_fcn => $func, include_trunk=>Bool::True);
}
@@ -84,12 +85,11 @@ method setNodeFilter(Code $filter_fcn) {
self.filter_fcn = $filter_fcn;
}
-# # resultscn methods
+# resultscn methods
-# sub setResults {
-# my ($self, @results) = @_;
-# $self->{results} = \@results;
-# }
+method setResults(@results) {
+ @.results= @results;
+}
method getResults() {
return @.results;
@@ -137,33 +137,33 @@ Tree::Simple::Visitor - Visitor object for Tree::Simple objects
use Tree::Simple::Visitor;
# create a visitor instance
- my $visitor = Tree::Simple::Visitor->new();
+ my $visitor = Tree::Simple::Visitor.new();
# create a tree to visit
- my $tree = Tree::Simple->new(Tree::Simple->ROOT)
- ->addChildren(
- Tree::Simple->new("1.0"),
- Tree::Simple->new("2.0")
- ->addChild(
- Tree::Simple->new("2.1.0")
+ my $tree = Tree::Simple.new($Tree::Simple::ROOT)
+ .addChildren(
+ Tree::Simple.new("1.0"),
+ Tree::Simple.new("2.0")
+ .addChild(
+ Tree::Simple.new("2.1.0")
),
- Tree::Simple->new("3.0")
+ Tree::Simple.new("3.0")
);
# by default this will collect all the
# node values in depth-first order into
# our results
- $tree->accept($visitor);
+ $tree.accept($visitor);
# get our results and print them
- 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"
# for more complex node objects, you can specify
# a node filter which will be used to extract the
# information desired from each node
- $visitor->setNodeFilter(sub {
+ $visitor.setNodeFilter(sub {
my ($t) = @_;
- return $t->getNodeValue()->description();
+ return $t.getNodeValue().description();
});
# NOTE: this object has changed, but it still remains
@@ -177,21 +177,21 @@ This object has been revised into what I think is more intelligent approach to V
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 $visitor = Tree::Simple::Visitor.new(sub {
my ($tree) = @_;
- push @accumlator, $tree->getNodeValue();
+ push @accumlator, $tree.getNodeValue();
},
- Tree::Simple::Visitor->RECURSIVE);
+ $Tree::Simple::Visitor::RECURSIVE);
- $tree->accept($visitor);
+ $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();
- $tree->accept($visitor);
- print join ", ", $visitor->getResults(); # prints "1.0, 2.0, 2.1.0, 3.0"
+ my $visitor = Tree::Simple::Visitor.new();
+ $tree.accept($visitor);
+ print join ", ", $visitor.getResults(); # prints "1.0, 2.0, 2.1.0, 3.0"
This object is still pretty much a wrapper around the Tree::Simple C<traverse> method, and can be thought of as a depth-first traversal Visitor object.
View
86 t/20_Tree_Simple_Visitor_test.t
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 25;
+plan 35;
BEGIN
{
@*INC.push('lib');
@@ -66,23 +66,21 @@ dies_ok ({
# compatability
# -----------------------------------------------
-# # and that our RECURSIVE constant is properly defined
-# can_ok("Tree::Simple::Visitor", 'RECURSIVE');
-# # and that our CHILDREN_ONLY constant is properly defined
-# can_ok("Tree::Simple::Visitor", 'CHILDREN_ONLY');
+# and that our RECURSIVE constant is properly defined
+is($Tree::Simple::Visitor::RECURSIVE, 'RECURSIVE');
+# and that our CHILDREN_ONLY constant is properly defined
+is($Tree::Simple::Visitor::CHILDREN_ONLY, 'CHILDREN_ONLY');
# no depth
my $visitor1 = Tree::Simple::Visitor.new($SIMPLE_SUB);
ok($visitor1 ~~ Tree::Simple::Visitor);
# children only
-#todo replace with class constant instead of text
-my $visitor2 = Tree::Simple::Visitor.new($SIMPLE_SUB, 'CHILDREN_ONLY');
+my $visitor2 = Tree::Simple::Visitor.new($SIMPLE_SUB, $Tree::Simple::Visitor::CHILDREN_ONLY);
ok($visitor2 ~~ Tree::Simple::Visitor);
# recursive
-#todo replace with class constant instead of text
-my $visitor3 = Tree::Simple::Visitor.new($SIMPLE_SUB, 'RECURSIVE');
+my $visitor3 = Tree::Simple::Visitor.new($SIMPLE_SUB, $Tree::Simple::Visitor::RECURSIVE);
ok($visitor3 ~~ Tree::Simple::Visitor);
# -----------------------------------------------
@@ -95,54 +93,58 @@ dies_ok ({
} ,'Insufficient Arguments : Depth arguement must be either RECURSIVE or CHILDREN_ONLY');
# we pass a bad depth (numeric)
-#dies_ok ({
-#my $test = Tree::Simple::Visitor.new($SIMPLE_SUB, 100);
+dies_ok ({
+my $test = Tree::Simple::Visitor.new($SIMPLE_SUB, 100);
+},'Insufficient Arguments : Depth arguement must be either RECURSIVE or CHILDREN_ONLY');
-# },'Insufficient Arguments : Depth arguement must be either RECURSIVE or CHILDREN_ONLY');
+# we pass a non-ref func argument
+dies_ok ({
+ my $test = Tree::Simple::Visitor.new("Fail");
+} ,"Insufficient Arguments : filter function argument must be a subroutine reference");
-# # we pass a non-ref func argument
-# throws_ok {
-# my $test = Tree::Simple::Visitor->new("Fail");
-# } qr/Insufficient Arguments \: filter function argument must be a subroutine reference/,
-# '... we are expecting this error';
# # we pass a non-code-ref func arguement
-# throws_ok {
-# my $test = Tree::Simple::Visitor->new([]);
-# } qr/Insufficient Arguments \: filter function argument must be a subroutine reference/,
-# '... we are expecting this error';
+dies_ok ({
+ my $test = Tree::Simple::Visitor.new([]);
+},"Insufficient Arguments : filter function argument must be a subroutine reference");
+
+
# -----------------------------------------------
# test other exceptions
# -----------------------------------------------
-# # and make sure we can call the visit method
+# and make sure we can call the visit method
ok($visitor1.can('visit'));
# test no arg
-# throws_ok {
-# $visitor1->visit();
-# } qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
+dies_ok ( {
+ $visitor1.visit();
+} , "Insufficient Arguments : You must supply a valid Tree::Simple object");
+
# '... we are expecting this error';
-# # test non-ref arg
-# throws_ok {
-# $visitor1->visit("Fail");
-# } qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
+# test non-ref arg
+dies_ok ( {
+ $visitor1.visit("Fail");
+}, "Insufficient Arguments : You must supply a valid Tree::Simple object");
+
# '... we are expecting this error';
-# # test non-object ref arg
-# throws_ok {
-# $visitor1->visit([]);
-# } qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
+# test non-object ref arg
+dies_ok ( {
+ $visitor1.visit([]);
+ } ,"Insufficient Arguments : You must supply a valid Tree::Simple object");
# '... we are expecting this error';
+
+class BAD {};
+
+my $BAD_OBJECT = BAD.new();
-# my $BAD_OBJECT = bless({}, "Test");
-
-# # test non-Tree::Simple object arg
-# throws_ok {
-# $visitor1->visit($BAD_OBJECT);
-# } qr/Insufficient Arguments \: You must supply a valid Tree\:\:Simple object/,
+# test non-Tree::Simple object arg
+dies_ok ( {
+ $visitor1.visit($BAD_OBJECT);
+} ,"Insufficient Arguments \: You must supply a valid Tree\:\:Simple object");
# '... we are expecting this error';
@@ -173,11 +175,9 @@ lives_ok( {
}, '.. this passes fine');
# and pass the visitor2 to accept
-#todo figure out why it fails when it does not even die!
-#lives_ok ({
+lives_ok( {
$tree1.accept($visitor2);
-#}, '.. this passes fine');
-
+}, '.. this passes fine');
# and pass the visitor3 to accept
lives_ok( {
Please sign in to comment.
Something went wrong with that request. Please try again.