From: <pau...@us...> - 2006-09-12 18:43:15
|
Revision: 908 http://svn.sourceforge.net/everydevel/?rev=908&view=rev Author: paul_the_nomad Date: 2006-09-12 11:42:51 -0700 (Tue, 12 Sep 2006) Log Message: ----------- Unit tests for Util.t, Security.t and NodeCache.t Modified Paths: -------------- trunk/ebase/t/NodeCache.t trunk/ebase/t/Security.t trunk/ebase/t/Util.t Added Paths: ----------- trunk/ebase/lib/Everything/Test/NodeCache.pm trunk/ebase/lib/Everything/Test/Security.pm trunk/ebase/lib/Everything/Test/Util.pm trunk/ebase/lib/Everything/Test.pm trunk/ebase/t/Node/extendednode.t Added: trunk/ebase/lib/Everything/Test/NodeCache.pm =================================================================== --- trunk/ebase/lib/Everything/Test/NodeCache.pm (rev 0) +++ trunk/ebase/lib/Everything/Test/NodeCache.pm 2006-09-12 18:42:51 UTC (rev 908) @@ -0,0 +1,177 @@ +package Everything::Test::NodeCache; + +use Test::More; +use Test::MockObject; +use Scalar::Util qw/blessed/; +use base 'Everything::Test'; +use strict; +use warnings; + + +sub startup :Test(startup => +0) { + my $self = shift; + $self->SUPER; + my $class = $self->{class}; + my $file; + ($file = $class) =~ s/::/\//g; + + $file .= '.pm'; + + require $file; + $class->import; + $self->{mock} = Test::MockObject->new; +} + + +sub test_set_cache_size : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'setCacheSize' ); + +} + +sub test_get_cache_size : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'getCacheSize' ); + +} + +sub test_get_cached_node_by_name : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'getCachedNodeByName' ); +} + +sub test_get_cached_node_by_id : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'getCachedNodeById' ); + +} + +sub test_cache_node : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'cacheNode' ); + +} + +sub test_remove_node : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'removeNode' ); + +} + +sub test_flush_cache : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'flushCache' ); +} + +sub test_flush_cache_global : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'flushCacheGlobal' ); +} + +sub test_dump_cache : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'dumpCache' ); +} + +sub test_purge_cache : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'purgeCache' ); +} + +sub test_remove_node_from_hash : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'removeNodeFromHash' ); +} + +sub test_get_global_version : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'getGlobalVersion' ); +} + +sub test_is_same_version : Test(9) +{ + my $self = shift; + my $package = $self->{class}; + my $mock = $self->{mock}; + can_ok( $package, 'isSameVersion' ); + is( Everything::NodeCache::isSameVersion(), undef, + 'isSameVersion() should return undef without node' ); + + $mock->{version}{12} = 1; + $mock->{verified}{11} = 1; + $mock->{typeVerified}{10} = 1; + + my $node = { + type => { node_id => 10 }, + node_id => 11, + }; + + $node->{type}{node_id} = 11; + ok( Everything::NodeCache::isSameVersion( $mock, $node ), '... true if node type is verified' ); + + $node->{node_id} = 11; + ok( Everything::NodeCache::isSameVersion( $mock, $node ), '... true if node id is verified' ); + + $node->{node_id} = 13; + ok( ! Everything::NodeCache::isSameVersion( $mock, $node ), + '... false unless node version is verified' ); + + $node->{node_id} = 12; + $mock->set_series( getGlobalVersion => undef, 2, 1 ); + ok( ! Everything::NodeCache::isSameVersion( $mock, $node ), + '... false unless node has global version' ); + ok( ! Everything::NodeCache::isSameVersion( $mock, $node ), '... false unless global version matches' ); + ok( Everything::NodeCache::isSameVersion( $mock, $node ), '... true if global version matches' ); + ok( $mock->{verified}{12}, '... setting verified flag' ); + + +} + +sub test_increment_global_version : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'incrementGlobalVersion' ); + +} + +sub test_reset_cache : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'resetCache' ); +} + +sub test_cache_method : Test(1) +{ + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'cacheMethod' ); + +} + +1; Property changes on: trunk/ebase/lib/Everything/Test/NodeCache.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/ebase/lib/Everything/Test/Security.pm =================================================================== --- trunk/ebase/lib/Everything/Test/Security.pm (rev 0) +++ trunk/ebase/lib/Everything/Test/Security.pm 2006-09-12 18:42:51 UTC (rev 908) @@ -0,0 +1,48 @@ +package Everything::Test::Security; + +use base 'Everything::Test'; +use Test::More; + + +sub test_inherit_permissions :Test(6) { + + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'inheritPermissions' ); + is( Everything::Security::inheritPermissions( '----', 'rwxd' ), + '----', + 'inheritPermissions() should not modify uninheritable permissions' ); + is( Everything::Security::inheritPermissions( 'i-i-', 'rwxd' ), + 'r-x-', '... and should inherit the inheritable' ); + + my @le; + local *Everything::logErrors; + *Everything::logErrors = sub { push @le, [@_] }; + + ok( ! Everything::Security::inheritPermissions( '---', '----' ), + '... should fail with permission length mismatch' ); + is( @le, 1, '... logging a warning' ); + like( $le[0][0], qr/permission length mismatch/i, '... and warn about it' ); + +} + +sub test_check_permissions :Test(7) { + + my $self = shift; + my $package = $self->{class}; + can_ok( $package, 'checkPermissions' ); + + @le = (); + ok( !Everything::Security::checkPermissions('rwx-'), + 'check() should return false unless modes are passed' ); + is( @le, 0, '... and should not warn' ); + + ok( Everything::Security::checkPermissions( 'rwxd', 'rw' ), + '... should return true if op is permitted' ); + ok( ! Everything::Security::checkPermissions( 'rwxd', 'rwxdc' ), '... and false if op is prohibited' ); + ok( ! Everything::Security::checkPermissions( '', 'r' ), '... and false if no perms are present' ); + ok( ! Everything::Security::checkPermissions( 'i', '' ), '... and false if no modes are present' ); + +} + +1; Property changes on: trunk/ebase/lib/Everything/Test/Security.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/ebase/lib/Everything/Test/Util.pm =================================================================== --- trunk/ebase/lib/Everything/Test/Util.pm (rev 0) +++ trunk/ebase/lib/Everything/Test/Util.pm 2006-09-12 18:42:51 UTC (rev 908) @@ -0,0 +1,46 @@ +package Everything::Test::Util; + +use base 'Everything::Test'; +use Test::More; +use strict; + +sub startup :Test(startup => +0) { + my $self = shift; + $self->SUPER; + my $class = $self->{class}; + my $file; + ($file = $class) =~ s/::/\//g; + + $file .= '.pm'; + + require $file; + $class->import; + +} + +sub test_escape :Test(4){ + + my $self = shift; + can_ok( $self->{class}, 'escape' ); + my $encoded = escape('abc|@# _123'); + $self->{encoded} = $encoded; + like( $encoded, qr/^abc.+_123$/, 'escape() should not modify alphanumerics' ); + my @encs = $encoded =~ m/%([a-fA-F\d]{2})/g; + is( scalar @encs, 4, '... should encode all nonalphanumerics in string' ); + is( join( '', map { chr( hex($_) ) } @encs ), + '|@# ', '... using ord() and hex()' ); + + + +} + +sub test_unescape :Test(2) { + my $self = shift; + my $encoded = $self->{encoded}; + can_ok( $self->{class}, 'unescape' ); + is( unescape($encoded), 'abc|@# _123', 'unescape() should reverse escape()' ); + + +} + +1; Property changes on: trunk/ebase/lib/Everything/Test/Util.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/ebase/lib/Everything/Test.pm =================================================================== --- trunk/ebase/lib/Everything/Test.pm (rev 0) +++ trunk/ebase/lib/Everything/Test.pm 2006-09-12 18:42:51 UTC (rev 908) @@ -0,0 +1,29 @@ +package Everything::Test; + +use Scalar::Util 'blessed'; +use SUPER; +use Test::More; + +use base 'Test::Class'; + + + +sub module_class +{ + my $self = shift; + my $name = blessed( $self ); + $name =~ s/Test:://; + return $name; +} + + +sub startup :Test( startup => 1 ) +{ + my $self = shift; + my $module = $self->module_class(); + use_ok( $module ) or exit; + $self->{class} = $self->module_class; + +} + +1; Property changes on: trunk/ebase/lib/Everything/Test.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/ebase/t/Node/extendednode.t =================================================================== --- trunk/ebase/t/Node/extendednode.t (rev 0) +++ trunk/ebase/t/Node/extendednode.t 2006-09-12 18:42:51 UTC (rev 908) @@ -0,0 +1,4 @@ +#! perl + +use Everything::Node::Test::extendednode; +Everything::Node::Test::extendednode->runtests(); Property changes on: trunk/ebase/t/Node/extendednode.t ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Modified: trunk/ebase/t/NodeCache.t =================================================================== --- trunk/ebase/t/NodeCache.t 2006-07-20 11:33:52 UTC (rev 907) +++ trunk/ebase/t/NodeCache.t 2006-09-12 18:42:51 UTC (rev 908) @@ -1,84 +1,7 @@ #!/usr/bin/perl -w use strict; +use Everything::Test::NodeCache; -BEGIN -{ - chdir 't' if -d 't'; - unshift @INC, '../blib/lib', 'lib/', '..'; -} +Everything::Test::NodeCache->runtests; -use vars qw( $AUTOLOAD $errors ); - -use Test::MockObject; -use Test::More tests => 25; - -my $package = 'Everything::NodeCache'; - -use_ok($package) or diag "Compile error\n", exit; - -sub AUTOLOAD -{ - return if $AUTOLOAD =~ /DESTROY$/; - - no strict 'refs'; - $AUTOLOAD =~ s/^main:://; - - my $sub = "${package}::$AUTOLOAD"; - if ( defined &{$sub} ) - { - *{$AUTOLOAD} = \&{$sub}; - goto &{$sub}; - } -} - -my $mock = Test::MockObject->new(); - -can_ok( $package, 'isSameVersion' ); -is( isSameVersion(), undef, - 'isSameVersion() should return undef without node' ); - -$mock->{version}{12} = 1; -$mock->{verified}{11} = 1; -$mock->{typeVerified}{10} = 1; - -my $node = { - type => { node_id => 10 }, - node_id => 11, -}; - -$node->{type}{node_id} = 11; -ok( isSameVersion( $mock, $node ), '... true if node type is verified' ); - -$node->{node_id} = 11; -ok( isSameVersion( $mock, $node ), '... true if node id is verified' ); - -$node->{node_id} = 13; -ok( !isSameVersion( $mock, $node ), - '... false unless node version is verified' ); - -$node->{node_id} = 12; -$mock->set_series( getGlobalVersion => undef, 2, 1 ); -ok( !isSameVersion( $mock, $node ), - '... false unless node has global version' ); -ok( !isSameVersion( $mock, $node ), '... false unless global version matches' ); -ok( isSameVersion( $mock, $node ), '... true if global version matches' ); -ok( $mock->{verified}{12}, '... setting verified flag' ); - -#stubbing out possible tests: - -can_ok( $package, 'setCacheSize' ); -can_ok( $package, 'getCacheSize' ); -can_ok( $package, 'cacheNode' ); -can_ok( $package, 'removeNode' ); -can_ok( $package, 'getCachedNodeById' ); -can_ok( $package, 'getCachedNodeByName' ); -can_ok( $package, 'dumpCache' ); -can_ok( $package, 'flushCache' ); -can_ok( $package, 'flushCacheGlobal' ); -can_ok( $package, 'purgeCache' ); -can_ok( $package, 'removeNodeFromHash' ); -can_ok( $package, 'getGlobalVersion' ); -can_ok( $package, 'incrementGlobalVersion' ); -can_ok( $package, 'resetCache' ); -can_ok( $package, 'cacheMethod' ); Modified: trunk/ebase/t/Security.t =================================================================== --- trunk/ebase/t/Security.t 2006-07-20 11:33:52 UTC (rev 907) +++ trunk/ebase/t/Security.t 2006-09-12 18:42:51 UTC (rev 908) @@ -1,63 +1,9 @@ #!/usr/bin/perl -w use strict; -use vars qw( $AUTOLOAD ); +use Everything::Test::Security; -BEGIN -{ - chdir 't' if -d 't'; - use lib '../blib/lib', '..', 'lib/'; -} +Everything::Test::Security->runtests; -use Test::More tests => 14; +exit 0; -my $package = 'Everything::Security'; - -sub AUTOLOAD -{ - return if $AUTOLOAD =~ /DESTROY$/; - - no strict 'refs'; - $AUTOLOAD =~ s/^main:://; - - my $sub = "${package}::$AUTOLOAD"; - - if ( defined &{$sub} ) - { - *{$AUTOLOAD} = \&{$sub}; - goto &{$sub}; - } -} - -use_ok($package); - -# inheritPermissions() -can_ok( $package, 'inheritPermissions' ); -is( inheritPermissions( '----', 'rwxd' ), - '----', - 'inheritPermissions() should not modify uninheritable permissions' ); -is( inheritPermissions( 'i-i-', 'rwxd' ), - 'r-x-', '... and should inherit the inheritable' ); - -my @le; -local *Everything::logErrors; -*Everything::logErrors = sub { push @le, [@_] }; - -ok( !inheritPermissions( '---', '----' ), - '... should fail with permission length mismatch' ); -is( @le, 1, '... logging a warning' ); -like( $le[0][0], qr/permission length mismatch/i, '... and warn about it' ); - -# checkPermissions() -can_ok( $package, 'checkPermissions' ); - -@le = (); -ok( !checkPermissions('rwx-'), - 'check() should return false unless modes are passed' ); -is( @le, 0, '... and should not warn' ); - -ok( checkPermissions( 'rwxd', 'rw' ), - '... should return true if op is permitted' ); -ok( !checkPermissions( 'rwxd', 'rwxdc' ), '... and false if op is prohibited' ); -ok( !checkPermissions( '', 'r' ), '... and false if no perms are present' ); -ok( !checkPermissions( 'i', '' ), '... and false if no modes are present' ); Modified: trunk/ebase/t/Util.t =================================================================== --- trunk/ebase/t/Util.t 2006-07-20 11:33:52 UTC (rev 907) +++ trunk/ebase/t/Util.t 2006-09-12 18:42:51 UTC (rev 908) @@ -1,24 +1,6 @@ #!/usr/bin/perl -w use strict; +use Everything::Test::Util; -BEGIN -{ - chdir 't' if -d 't'; - use lib '../blib/lib', '../lib', '..'; -} - -use Test::More tests => 7; - -use_ok('Everything::Util'); - -can_ok( 'main', 'escape' ); -my $encoded = escape('abc|@# _123'); -like( $encoded, qr/^abc.+_123$/, 'escape() should not modify alphanumerics' ); -my @encs = $encoded =~ m/%([a-fA-F\d]{2})/g; -is( scalar @encs, 4, '... should encode all nonalphanumerics in string' ); -is( join( '', map { chr( hex($_) ) } @encs ), - '|@# ', '... using ord() and hex()' ); - -can_ok( 'main', 'unescape' ); -is( unescape($encoded), 'abc|@# _123', 'unescape() should reverse escape()' ); +Everything::Test::Util->runtests; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |