Permalink
Browse files

Now using a real class variable for '$ROOT'. Changed the testing to u…

…se it
  • Loading branch information...
1 parent 87c4642 commit 871d2a2e5dd09fdb19beaa369b573a294d59ee5e @Takadonet committed Dec 12, 2010
View
@@ -1,16 +1,6 @@
class Tree::Simple {
## class constants
- #should not be has should be 'our'
- has $.ROOT = "root";
-
-# # set the value of the unique id
-# ($self->{_uid}) = ("$self" =~ /\((.*?)\)$/);
-# # set the value of the node
-# $self->{_node} = $node;
-# # and set the value of _children
-# $self->{_children} = $children;
-# $self->{_height} = 1;
-# $self->{_width} = 1;
+ our $ROOT = "root";
#uid should be private however cannot init it a value within a new fcn
#will be private when submethod BUILD works
@@ -112,7 +102,7 @@ method setParent($parent) {
# (($parent eq $self->ROOT) || (blessed($parent) && $parent->isa("Tree::Simple"))))
# || die "Insufficient Arguments : parent also must be a Tree::Simple object";
$.parent = $parent;
- if ($parent eq $.ROOT) {
+ if ($parent eq $ROOT) {
$.depth = -1;
}
else {
@@ -272,7 +262,7 @@ method removeChildAt($index) {
# make sure that the removed child
# is no longer connected to the parent
# so we change its parent to ROOT
- $removed_child.setParent($.ROOT);
+ $removed_child.setParent($ROOT);
# and now we make sure that the depth
# of the removed child is aligned correctly
$removed_child.fixDepth() unless $removed_child.isLeaf();
@@ -307,7 +297,7 @@ multi method removeChild($child_to_remove) {
}
method getIndex {
- return -1 if $.parent eq $.ROOT;
+ return -1 if $.parent eq $ROOT;
my $index = 0;
for self.parent.getAllChildren() -> $sibling {
#probably stringify the object to see if they are the same. Nice short circuit as well
@@ -408,7 +398,7 @@ method isLeaf {
}
method isRoot {
- return (!defined($.parent) || $.parent eq $.ROOT);
+ return (!defined($.parent) || $.parent eq $ROOT);
}
method size() {
@@ -1,6 +1,6 @@
use v6;
use Test;
-plan 273;
+plan 275;
BEGIN
{
@*INC.push('lib');
@@ -26,16 +26,14 @@ use Tree::Simple;
# splitting them up would be a chore.
## ----------------------------------------------------------------------------
-# # check that we have a constructor
-# can_ok("Tree::Simple", 'new');
-# # and that our ROOT constant is properly defined
-# can_ok("Tree::Simple", 'ROOT');
+# and that our ROOT constant is properly defined
+is($Tree::Simple::ROOT, 'root');
# make a root for our tree
# my $tree = Tree::Simple->new("root tree", Tree::Simple->ROOT);
#see if we can do positional based as well as named based
-my $tree = Tree::Simple.new("root tree",'root');
+my $tree = Tree::Simple.new("root tree",$Tree::Simple::ROOT);
ok($tree ~~ Tree::Simple, 'Tree::Simple object');
@@ -46,7 +44,7 @@ ok($tree ~~ Tree::Simple, 'Tree::Simple object');
#can_ok($tree, '_setParent');
-my @methods= < isRoot isLeaf setNodeValue getNodeValue getDepth fixDepth getParent
+my @methods= < new isRoot isLeaf setNodeValue getNodeValue getDepth fixDepth getParent
getChildCount addChild addChildren removeChildAt removeChild
getChild getAllChildren addSibling addSiblings insertSiblings getSibling
getAllSiblings traverse accept clone cloneShallow DESTROY getUID>;
@@ -64,8 +64,8 @@ for $tree.getAllChildren() -> $sub_tree {
# now create our other tree
# which we will add $tree too
-#todo make it work by using the constant class variable
-my $parent_tree = Tree::Simple.new('root');
+
+my $parent_tree = Tree::Simple.new($Tree::Simple::ROOT);
$parent_tree.addChildren(
Tree::Simple.new("1"),
Tree::Simple.new("2")
@@ -39,8 +39,7 @@ dies_ok( {
# -----------------------------------------------
-#Todo will want to reference the class variable
-my $tree = Tree::Simple.new('root');
+my $tree = Tree::Simple.new($Tree::Simple::ROOT);
# -----------------------------------------------
# exceptions for setNodeValue
@@ -14,7 +14,7 @@ use Tree::Simple;
# This specifically tests the details of the cloning functions
## ----------------------------------------------------------------------------
-# my $tree = Tree::Simple->new(Tree::Simple->ROOT);
+# my $tree = Tree::Simple->new($Tree::Simple::ROOT);
# isa_ok($tree, 'Tree::Simple');
# my $test = "test";
@@ -44,7 +44,7 @@ use Tree::Simple;
# isa_ok($clone, 'Tree::Simple');
# # make sure all the parentage is correct
-# is($clone->getParent(), Tree::Simple->ROOT, '... the clones parent is a root');
+# is($clone->getParent(), $Tree::Simple::ROOT, '... the clones parent is a root');
# for my $child ($clone->getAllChildren()) {
# is($child->getParent(), $clone, '... the clones childrens parent should be our clone');
@@ -19,8 +19,7 @@ my $SIMPLE_SUB = sub (*@a) { "test sub" };
my $visitor = Tree::Simple::Visitor.new();
ok($visitor ~~ Tree::Simple::Visitor);
-#todo use the class variable instead of 'root'
-my $tree = Tree::Simple.new('root').addChildren(
+my $tree = Tree::Simple.new($Tree::Simple::ROOT).addChildren(
Tree::Simple.new("1").addChildren(
Tree::Simple.new("1.1"),
Tree::Simple.new("1.2").addChild(Tree::Simple.new("1.2.1")),
@@ -159,8 +158,7 @@ ok($visitor1.can('visit'));
# -----------------------------------------------
# now make a tree
-#todo need to replace with class constant
-my $tree1 = Tree::Simple.new('ROOT').addChildren(
+my $tree1 = Tree::Simple.new($Tree::Simple::ROOT).addChildren(
Tree::Simple.new("1.0"),
Tree::Simple.new("2.0"),
Tree::Simple.new("3.0"),

0 comments on commit 871d2a2

Please sign in to comment.