Additional tests for first_sibling and last_sibling.
Aran Deltac [Thu, 23 Mar 2006 18:05:37 +0000 (18:05 +0000)]
lib/DBIx/Class/Positioned.pm
t/run/26positioned.tl

index d3b7cf7..02d4aac 100644 (file)
@@ -115,12 +115,14 @@ sub siblings {
 
   my $sibling = $employee->first_sibling();
 
-Returns the first sibling object.
+Returns the first sibling object, or 0 if the first sibling 
+is this sibliing.
 
 =cut
 
 sub first_sibling {
     my( $self ) = @_;
+    return 0 if ($self->get_column($self->position_column())==1);
     return ($self->result_source->resultset->search(
         {
             $self->position_column => 1,
@@ -133,13 +135,15 @@ sub first_sibling {
 
   my $sibling = $employee->last_sibling();
 
-Return the last sibling.
+Return the last sibling, or 0 if the last sibling is this 
+sibling.
 
 =cut
 
 sub last_sibling {
     my( $self ) = @_;
     my $count = $self->result_source->resultset->search({$self->_collection_clause()})->count();
+    return 0 if ($self->get_column($self->position_column())==$count);
     return ($self->result_source->resultset->search(
         {
             $self->position_column => $count,
index c73e257..720b173 100644 (file)
@@ -2,7 +2,7 @@
 
 sub run_tests {
 
-    plan tests => 56;
+    plan tests => 66;
     my $schema = shift;
 
     my $employees = $schema->resultset('Employee::Positioned');
@@ -44,14 +44,20 @@ sub run_tests {
         if ($position==1) {
             ok( !$employee->previous_sibling(), 'no previous sibling' );
             ok( $employee->next_sibling(), 'next sibling' );
+            ok( !$employee->first_sibling(), 'no first sibling' );
+            ok( $employee->last_sibling(), 'last sibling' );
         }
         elsif ($position==$employees->count()) {
             ok( $employee->previous_sibling(), 'previous sibling' );
             ok( !$employee->next_sibling(), 'no next sibling' );
+            ok( $employee->first_sibling(), 'first sibling' );
+            ok( !$employee->last_sibling(), 'no last sibling' );
         }
         else {
             ok( $employee->previous_sibling(), 'previous sibling' );
             ok( $employee->next_sibling(), 'next sibling' );
+            ok( $employee->first_sibling(), 'first sibling' );
+            ok( $employee->last_sibling(), 'last sibling' );
         }
 
     }