From: <pau...@us...> - 2006-09-30 12:49:12
|
Revision: 916 http://svn.sourceforge.net/everydevel/?rev=916&view=rev Author: paul_the_nomad Date: 2006-09-30 05:48:23 -0700 (Sat, 30 Sep 2006) Log Message: ----------- Moving more tests for FormObjects Modified Paths: -------------- trunk/ebase/lib/Everything/HTML/FormObject/Test/ListMenu.pm trunk/ebase/t/HTML/FormObject/NodetypeMenu.t trunk/ebase/t/HTML/FormObject/TypeMenu.t Added Paths: ----------- trunk/ebase/lib/Everything/HTML/FormObject/Test/FormMenu.pm trunk/ebase/lib/Everything/HTML/FormObject/Test/NodetypeMenu.pm trunk/ebase/lib/Everything/HTML/FormObject/Test/TypeMenu.pm trunk/ebase/t/HTML/FormObject/FormMenu.t Added: trunk/ebase/lib/Everything/HTML/FormObject/Test/FormMenu.pm =================================================================== --- trunk/ebase/lib/Everything/HTML/FormObject/Test/FormMenu.pm (rev 0) +++ trunk/ebase/lib/Everything/HTML/FormObject/Test/FormMenu.pm 2006-09-30 12:48:23 UTC (rev 916) @@ -0,0 +1,152 @@ +package Everything::HTML::FormObject::Test::FormMenu; + +use base 'Everything::HTML::Test::FormObject'; +use Test::More; +use Scalar::Util qw/blessed/; +use base 'Test::Class'; +use strict; +use warnings; + + + +sub setup_globals { + my $self = shift; + $self->SUPER; + no strict 'refs'; + *{ $self->package_under_test(__PACKAGE__) . '::DB' } = \$self->{mock}; + use strict 'refs'; + +} + +sub test_get_values_array : Test(3) +{ + my $self = shift; + can_ok($self->{class}, 'getValuesArray') || return 'getValuesArray not implemented.'; + my $instance = $self->{instance}; + is_deeply ($instance->getValuesArray, [], '...should return and empty array ref if no values.'); + + my $values = [qw/one two/]; + $instance->{VALUES} = $values; + is_deeply ($instance->getValuesArray, $values, '...should return the VALUES attribute if exists.'); + + +} + +sub test_get_labels_hash : Test(3) +{ + my $self = shift; + can_ok($self->{class}, 'getLabelsHash') || return 'getLabelsHash not implemented.'; + my $instance = $self->{instance}; + is_deeply ($instance->getLabelsHash, {}, '...should return and empty stringif no values.'); + + my $values = {one => 'two'}; + $instance->{LABELS} = $values; + is_deeply ($instance->getLabelsHash, $values, '...should return the LABELS attribute if exists.'); + + +} + +sub test_clear_menu : Test(3) +{ + my $self = shift; + can_ok($self->{class}, 'clearMenu') || return 'clearMenu not implemented.'; + my $instance = $self->{instance}; + $instance->{VALUES} = [qw/one two three/]; + $instance->{LABELS} = {four => 'five'}; + $instance->clearMenu; + is_deeply($instance->{VALUES}, [], '...should clear VALUES array ref'); + is_deeply($instance->{LABELS}, {}, '...should clear LABELS hash ref'); + +} + +sub test_sort_menu : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'sortMenu') || return 'sortMenu not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_remove_items : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'removeItems') || return 'removeItems not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_add_type : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'addType') || return 'addType not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_add_group : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'addGroup') || return 'addGroup not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_add_hash : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'addHash') || return 'addHash not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_add_array : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'addArray') || return 'addArray not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_add_labels : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'addLabels') || return 'addLabels not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_gen_popup_menu : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'genPopupMenu') || return 'genPopupMenu not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_gen_list_menu : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'genListMenu') || return 'genListMenu not implemented.'; + my $instance = $self->{instance}; + + +} + +sub test_gen_object : Test(1) +{ + my $self = shift; + can_ok($self->{class}, 'genObject') || return 'genObject not implemented.'; + my $instance = $self->{instance}; + + +} + +1; Property changes on: trunk/ebase/lib/Everything/HTML/FormObject/Test/FormMenu.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Modified: trunk/ebase/lib/Everything/HTML/FormObject/Test/ListMenu.pm =================================================================== --- trunk/ebase/lib/Everything/HTML/FormObject/Test/ListMenu.pm 2006-09-30 09:59:58 UTC (rev 915) +++ trunk/ebase/lib/Everything/HTML/FormObject/Test/ListMenu.pm 2006-09-30 12:48:23 UTC (rev 916) @@ -1,6 +1,6 @@ package Everything::HTML::FormObject::Test::ListMenu; -use base 'Everything::HTML::Test::FormObject'; +use base 'Everything::HTML::FormObject::Test::FormMenu'; use Test::More; use Test::MockObject; use Test::MockObject::Extends; @@ -9,15 +9,6 @@ use warnings; use strict; -sub setup_globals { - my $self = shift; - $self->SUPER; - no strict 'refs'; - *{'Everything::HTML::FormObject::FormMenu::DB'} = \$self->{mock}; - use strict 'refs'; - -} - sub test_cgi_update : Test(11) { my $self = shift; my $instance = Test::MockObject::Extends->new( $self->{instance} ); Added: trunk/ebase/lib/Everything/HTML/FormObject/Test/NodetypeMenu.pm =================================================================== --- trunk/ebase/lib/Everything/HTML/FormObject/Test/NodetypeMenu.pm (rev 0) +++ trunk/ebase/lib/Everything/HTML/FormObject/Test/NodetypeMenu.pm 2006-09-30 12:48:23 UTC (rev 916) @@ -0,0 +1,268 @@ +package Everything::HTML::FormObject::Test::NodetypeMenu; + +use base 'Everything::HTML::FormObject::Test::TypeMenu'; +use Test::MockObject::Extends; +use Test::MockObject; +use Test::More; +use Scalar::Util qw/blessed/; +use base 'Test::Class'; +use strict; +use SUPER; +use warnings; + +sub setup_globals { + my $self = shift; + $self->SUPER; + no strict 'refs'; + *{ $self->package_under_test(__PACKAGE__) . '::DB' } = \$self->{mock}; + use strict 'refs'; + +} + +sub test_gen_object : Test(10) { + my $self = shift; + my $instance = Test::MockObject::Extends->new( $self->{instance} ); + my $mock = Test::MockObject->new; + + my ( %gpa, @gpa ); + $mock->fake_module( $self->{class}, + getParamArray => + sub { push @gpa, \@_; return @gpa{qw( q bn f n ou U no i it )} } ); + + my @go; + $mock->fake_module( + 'Everything::HTML::FormObject::TypeMenu', + genObject => sub { push @go, \@_; return 'html' } + ); + + @gpa{qw( q bn f n ou U no i )} = ( + 'query', 'bindNode', 'field', 'name', + 'omitutil', 'USER', 'none', 'inherit' + ); + + my $result = $instance->genObject( 1, 2, 3 ); + is( @gpa, 1, 'genObject() should call getParamArray' ); + is( + $gpa[0][0], + 'query, bindNode, field, name, omitutil, ' + . 'USER, none, inherit, inherittxt', + '... requesting the appropriate arguments' + ); + is_deeply( + [ @{ $gpa[0] }[ 1 .. 3 ] ], + [ 1, 2, 3 ], + '... with the method arguments' + ); + unlike( join( ' ', @{ $gpa[0] } ), + qr/$mock/, '... but not the object itself' ); + is( @go, 1, '... should call SUPER::genObject()' ); + is_deeply( + [ @{ $go[0] } ], + [ + $instance, 'query', 'bindNode', 'field', + 'name', 'nodetype', 'AUTO', 'USER', + 'c', 'none', 'inherit' + ], + '... passing ten correct args' + ); + is( $instance->{omitutil}, 'omitutil', + '... should set $$this{omitutil} to $omitutil' ); + + @gpa{qw(ou U)} = ( undef, undef ); + + $instance->genObject(); + is( $instance->{omitutil}, 0, '... should default $omitutil to 0' ); + is( ${ $go[1] }[7], -1, '... should default $USER to -1' ); + + is( $result, 'html', '... should return result of SUPER::genObject()' ); +} + +sub test_add_types : Test(22) { + my $self = shift; + my $instance = Test::MockObject::Extends->new( $self->{instance} ); + my $mock = $self->{mock}; + + my ( %types, @hTA ); + for ( 'a', 'b', 'c' ) { + $types{$_} = Test::MockObject->new; + $types{$_}->mock( 'hasTypeAccess', sub { push @hTA, $_[0]; 1 } ); + $types{$_}->set_true('derivesFrom'); + $types{$_}->{title} = $_; + } + + $mock->set_list( 'getAllTypes', $types{c}, $types{a}, $types{b} ); + + $instance->set_always( 'createTree', + [ { label => 'l1', value => 'v1' }, { label => 'l2', value => 'v2' } ] + ); + $instance->set_true('addHash'); + $instance->set_true('addArray'); + $instance->set_true('addLabels'); + + my $result = $instance->addTypes( 't', 'U', 'p', 'n', 'i' ); + + my ( $method, $args ) = $instance->next_call; + is( $method, 'addHash', + 'addTypes() should call addHash() if $none defined' ); + is_deeply( + [ @$args[ 1, 2 ] ], + [ { 'None' => 'n' }, 1 ], + '... passing {"None" => $none}, 1' + ); + + ( $method, $args ) = $instance->next_call; + is( $method, 'addHash', '... should call addHash() if $inherit defined' ); + is_deeply( + [ @$args[ 1, 2 ] ], + [ { 'Inherit' => 'i' }, 1 ], + '... passing {"Inherit" => $inherit}, 1' + ); + + ( $method, $args ) = $mock->next_call; + is( $method, 'getAllTypes', '... should call $DB->getAllTypes' ); + + is_deeply( + [@hTA], + [ @types{qw(a b c)} ], + '... should sort returned types by title' + ); + + my $type = Test::MockObject->new; + $type->set_series( 'hasTypeAccess', 1, 1, 1, 0 ); + $type->set_series( 'derivesFrom', 0, 1, 1, 0 ); + $type->{title} = 'title'; + $mock->set_always( 'getAllTypes', $type ); + + $instance->{omitutil} = 1; + $instance->clear; + + $instance->addTypes( 't', 'U', 'p', undef, undef ); + + ( $method, $args ) = $type->next_call; + is( $method, 'hasTypeAccess', '... should check hasTypeAccess() for type' ); + is_deeply( [ @$args[ 1 .. 2 ] ], [ 'U', 'c' ], + '... passing $USER and "c"' ); + + ( $method, $args ) = $type->next_call; + is( $method, 'derivesFrom', + '... should check derivesFrom() if $this->{omitutil}' ); + is( $args->[1], 'utility', '... passing "utility"' ); + + ( $method, $args ) = $instance->next_call; + isnt( $method, 'addHash', + '... should not call addHash() when no $none or $inherit' ); + is( $method, 'createTree', '... should call createTree()' ); + is_deeply( $$args[1], [$type], + '... passing it $TYPE if hasTypeAccess() and not derivesFrom()' ); + + $instance->clear; + + $instance->addTypes( 't', 'U', 'p', undef, undef ); + ( $method, $args ) = $instance->next_call; + is_deeply( [ @{ $$args[1] } ], + [], '... not passing it $TYPE if derivesFrom() and $omitutil' ); + + $instance->{omitutil} = 0; + $instance->clear; + + $instance->addTypes( 't', 'U', 'p', undef, undef ); + + ( $method, $args ) = $instance->next_call; + is_deeply( $$args[1], [$type], + '... passing it $TYPE if hasTypeAccess() and not $omitutil' ); + + $instance->clear; + + $instance->addTypes( 't', 'U', 'p', undef, undef ); + + ( $method, $args ) = $instance->next_call; + is_deeply( [ @{ $$args[1] } ], + [], '... not passing it $TYPE if not hasTypeAccess()' ); + + ( $method, $args ) = $instance->next_call; + is( $method, 'addArray', '... should call addArray()' ); + is_deeply( + $$args[1], + [ 'v1', 'v2' ], + '... passing it ref to array of menu values' + ); + + ( $method, $args ) = $instance->next_call; + is( $method, 'addLabels', '... should call addLabels()' ); + is_deeply( + $$args[1], + { l2 => 'v2', l1 => 'v1' }, + '... passing it ref to hash of all menu label/value pairs' + ); + is( $$args[2], 1, '... and passing it 1' ); + + is( $result, 1, '... should return 1' ); +} + +sub test_create_tree : Test(6) { + + my $self = shift; + my $instance = Test::MockObject::Extends->new( $self->{instance} ); + + no strict 'refs'; + my $create_tree_code = *{ $self->{class} . '::createTree' }{CODE}; + use strict 'refs'; + my $mock = Test::MockObject->new; + $instance->mock( + 'createTree', + sub { + $create_tree_code->(@_); + return [ { label => 'v1' }, { label => 'v2' } ]; + } + ); + + my $types = [ + { extends_nodetype => 0, title => 'zero', node_id => 1 }, + { extends_nodetype => 1, title => 'one1', node_id => 2 }, + { extends_nodetype => 1, title => 'one2', node_id => 3 }, + ]; + + $instance->createTree( $types, 1 ); + + my ( $method, $args ) = $instance->next_call; + ( $method, $args ) = $instance->next_call; + is( $method, 'createTree', 'createTree() should call createTree()' ); + is_deeply( + $args, + [ $instance, $types, 2 ], + '... passing it $types, node_id' + ); + + ( $method, $args ) = $instance->next_call; + is_deeply( + [ $method, @$args ], + [ 'createTree', $instance, $types, 3 ], + '... for each $type with extends_nodetype matching $current' + ); + + ( $method, $args ) = $mock->next_call; + ok( !$method, '... but no more' ); + + my $called = 0; + $instance->clear; + + $instance->set_always( 'createTree', + [ { label => 'v1' }, { label => 'v2' } ] ); + + my $result = $create_tree_code->( $instance, $types, undef ); + + ( $method, $args ) = $instance->next_call; + is( $$args[2], 1, '... $current defaults to 0' ); + + is_deeply( + $result, + [ + { label => ' + zero', value => 1 }, + { label => ' - -v1' }, + { label => ' - -v2' } + ], + '... should return correct nodetype tree' + ); +} + +1; Property changes on: trunk/ebase/lib/Everything/HTML/FormObject/Test/NodetypeMenu.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/ebase/lib/Everything/HTML/FormObject/Test/TypeMenu.pm =================================================================== --- trunk/ebase/lib/Everything/HTML/FormObject/Test/TypeMenu.pm (rev 0) +++ trunk/ebase/lib/Everything/HTML/FormObject/Test/TypeMenu.pm 2006-09-30 12:48:23 UTC (rev 916) @@ -0,0 +1,127 @@ +package Everything::HTML::FormObject::Test::TypeMenu; + +use base 'Everything::HTML::FormObject::Test::FormMenu'; +use Test::MockObject::Extends; +use Test::More; +use Scalar::Util qw/blessed/; +use base 'Test::Class'; +use strict; +use warnings; + +sub test_add_types : Test(12) { + my $self = shift; + my $instance = Test::MockObject::Extends->new( $self->{instance} ); + my $node = $self->{node}; + $instance->set_true( 'addHash', 'addType' ); + my $result = $instance->addTypes( 't', 'U', 'p', 'n', 'i', 'it' ); + + my ( $method, $args ) = $instance->next_call; + is( $method, 'addHash', + 'addTypes() should call addHash() if defined $none' ); + + ( $method, $args ) = $instance->next_call; + is( $method, 'addHash', + '... should call addHash() again if defined $inherit' ); + is( ${ $args->[1] }{'inherit (it)'}, + 'i', + '... $label should be set to "inherit ($inherittxt)" if $inherittxt' ); + + ( $method, $args ) = $instance->next_call; + is( $method, 'addType', '... should call addType()' ); + is( $args->[2], 'U', '... should use provided $USER' ); + is( $args->[3], 'p', '... and $perm' ); + is( $result, 1, '... should return 1' ); + + $instance->clear; + $instance->addTypes( 't', '', '', 'n' ); + ( $method, $args ) = $instance->next_call; + is( ${ $args->[1] }{None}, + 'n', '... skip an addHash() if $inherit undefined' ); + + ( $method, $args ) = $instance->next_call; + + is( $args->[2], -1, '... $USER defaults to -1' ); + is( $args->[3], 'r', '... and $perm to "r"' ); + + $instance->clear; + $instance->addTypes( 't', 'U', 'p', undef, 'i' ); + ( $method, $args ) = $instance->next_call; + is_deeply( + $args->[1], + { inherit => 'i' }, + '... $label should be set to "inherit" if no $inherittxt' + ); + ( $method, $args ) = $instance->next_call; + is( $method, 'addType', '... skip and addHash() if $none undefined' ); + +} + +sub test_gen_object : Test(16) { + my $self = shift; + my $node = $self->{node}; + my $instance = Test::MockObject::Extends->new( $self->{instance} ); + + $instance->set_true('addTypes'); + $instance->set_always( 'genPopupMenu', "a" ); + my @params; + *Everything::HTML::FormObject::TypeMenu::getParamArray = sub { + push @params, "@_"; + shift; + @_; + }; + + my ( $genObject_name, $genObject_args ); + $node->fake_module( + 'Everything::HTML::FormObject', + genObject => sub { + my $node = shift; + $genObject_name = 'genObject'; + $genObject_args = [@_]; + return 'html'; + } + ); + + my $result = + $instance->genObject( 'q', 'bN', 'f', 'n', 't', 'd', 'U', 'p', 'n', 'i', + 'it' ); + + is( + $params[0], + 'query, bindNode, field, name, type, default, USER, perm, ' + . 'none, inherit, inherittxt q bN f n t d U p n i it', + 'genObject() should call getParamArray() with @_' + ); + is( $genObject_name, 'genObject', '... should call SUPER::genObject()' ); + + my ( $method, $args ) = $instance->next_call; + is( $method, 'addTypes', '... should call addTypes()' ); + is( $args->[1], 't', '... should use provided $type' ); + is( $args->[2], 'U', '... should use provided $USER' ); + is( $args->[3], 'p', '... should use provided $perm' ); + + ( $method, $args ) = $instance->next_call; + is( $method, 'genPopupMenu', '... should call genPopupMenu()' ); + is( $args->[2], 'n', '... should use provided $name' ); + is( $args->[3], undef, '... $default becomes undef if true' ); + is( $result, "html\na", + '... returning concatenation of SUPER() and genPopupMenu() calls' ); + + $instance->clear; + $instance->genObject( 'q', { f => 'field' }, 'f' ); + ( $method, $args ) = $instance->next_call; + is( $args->[1], 'nodetype', '... $type should default to "nodetype"' ); + is( $args->[2], '-1', '... $USER should default to -1' ); + is( $args->[3], 'r', '... $perm should default to "r"' ); + + ( $method, $args ) = $instance->next_call; + is( $args->[2], 'f', '... $name should default to $field' ); + is( $args->[3], 'field', + '... with no default value, should bind to provided node field' ); + $instance->clear; + $instance->genObject( 'q', '', 'field', '', '', 'AUTO' ); + $args = [ $instance->call_args(-1) ]; + is( $args->[3], undef, + '... default value should be undef if "AUTO" and lacking bound node' ); +} + +1; Property changes on: trunk/ebase/lib/Everything/HTML/FormObject/Test/TypeMenu.pm ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Added: trunk/ebase/t/HTML/FormObject/FormMenu.t =================================================================== --- trunk/ebase/t/HTML/FormObject/FormMenu.t (rev 0) +++ trunk/ebase/t/HTML/FormObject/FormMenu.t 2006-09-30 12:48:23 UTC (rev 916) @@ -0,0 +1,5 @@ +#!/usr/bin/perl + +use Everything::HTML::FormObject::Test::FormMenu; + +Everything::HTML::FormObject::Test::FormMenu->runtests; Property changes on: trunk/ebase/t/HTML/FormObject/FormMenu.t ___________________________________________________________________ Name: svn:mime-type + text/plain Name: svn:eol-style + native Modified: trunk/ebase/t/HTML/FormObject/NodetypeMenu.t =================================================================== --- trunk/ebase/t/HTML/FormObject/NodetypeMenu.t 2006-09-30 09:59:58 UTC (rev 915) +++ trunk/ebase/t/HTML/FormObject/NodetypeMenu.t 2006-09-30 12:48:23 UTC (rev 916) @@ -1,276 +1,4 @@ #!/usr/bin/perl -w -use strict; - -BEGIN -{ - chdir 't' if -d 't'; - unshift @INC, '../blib/lib', 'lib/', '..'; -} - -package Everything::HTML::FormObject::NodetypeMenu; -use vars qw( $DB ); - -package main; - -use vars qw( $AUTOLOAD ); -use Test::More tests => 41; -use Test::MockObject; - -my $package = 'Everything::HTML::FormObject::NodetypeMenu'; - -{ - my @imports; - my @modules = ( 'Everything', 'Everything::HTML::FormObject::TypeMenu' ); - - for (@modules) - { - Test::MockObject->fake_module( $_, - import => sub { push @imports, $_[0] } ); - } - - use_ok($package); - - for ( 0 .. $#modules ) - { - is( $imports[$_], $modules[$_], "Module should use $modules[$_]" ); - } -} - -# genObject() -{ - my $mock = Test::MockObject->new; - - my ( %gpa, @gpa ); - $mock->fake_module( $package, - getParamArray => - sub { push @gpa, \@_; return @gpa{qw( q bn f n ou U no i it )} } ); - - my @go; - $mock->fake_module( - 'Everything::HTML::FormObject::TypeMenu', - genObject => sub { push @go, \@_; return 'html' } - ); - - @gpa{qw( q bn f n ou U no i )} = ( - 'query', 'bindNode', 'field', 'name', - 'omitutil', 'USER', 'none', 'inherit' - ); - - my $result = genObject( $mock, 1, 2, 3 ); - is( @gpa, 1, 'genObject() should call getParamArray' ); - is( - $gpa[0][0], - 'query, bindNode, field, name, omitutil, ' - . 'USER, none, inherit, inherittxt', - '... requesting the appropriate arguments' - ); - is_deeply( - [ @{ $gpa[0] }[ 1 .. 3 ] ], - [ 1, 2, 3 ], - '... with the method arguments' - ); - unlike( join( ' ', @{ $gpa[0] } ), - qr/$mock/, '... but not the object itself' ); - is( @go, 1, '... should call SUPER::genObject()' ); - is_deeply( - [ @{ $go[0] } ], - [ - $mock, 'query', 'bindNode', 'field', - 'name', 'nodetype', 'AUTO', 'USER', - 'c', 'none', 'inherit' - ], - '... passing ten correct args' - ); - is( $mock->{omitutil}, 'omitutil', - '... should set $$this{omitutil} to $omitutil' ); - - @gpa{qw(ou U)} = ( undef, undef ); - - genObject($mock); - is( $mock->{omitutil}, 0, '... should default $omitutil to 0' ); - is( ${ $go[1] }[7], -1, '... should default $USER to -1' ); - - is( $result, 'html', '... should return result of SUPER::genObject()' ); -} - -# addTypes() -{ - my ( %types, @hTA ); - for ( 'a', 'b', 'c' ) - { - $types{$_} = Test::MockObject->new; - $types{$_}->mock( 'hasTypeAccess', sub { push @hTA, $_[0]; 1 } ); - $types{$_}->set_true('derivesFrom'); - $types{$_}->{title} = $_; - } - - my $db = Test::MockObject->new; - $db->set_list( 'getAllTypes', $types{c}, $types{a}, $types{b} ); - $Everything::HTML::FormObject::NodetypeMenu::DB = $db; - - my $mock = Test::MockObject->new; - $mock->set_always( 'createTree', - [ { label => 'l1', value => 'v1' }, { label => 'l2', value => 'v2' } ] - ); - $mock->set_true('addHash'); - $mock->set_true('addArray'); - $mock->set_true('addLabels'); - - my $result = addTypes( $mock, 't', 'U', 'p', 'n', 'i' ); - - my ( $method, $args ) = $mock->next_call; - is( $method, 'addHash', - 'addTypes() should call addHash() if $none defined' ); - is_deeply( - [ @$args[ 1, 2 ] ], - [ { 'None' => 'n' }, 1 ], - '... passing {"None" => $none}, 1' - ); - - ( $method, $args ) = $mock->next_call; - is( $method, 'addHash', '... should call addHash() if $inherit defined' ); - is_deeply( - [ @$args[ 1, 2 ] ], - [ { 'Inherit' => 'i' }, 1 ], - '... passing {"Inherit" => $inherit}, 1' - ); - - ( $method, $args ) = $db->next_call; - is( $method, 'getAllTypes', '... should call $DB->getAllTypes' ); - - is_deeply( - [@hTA], - [ @types{qw(a b c)} ], - '... should sort returned types by title' - ); - - my $type = Test::MockObject->new; - $type->set_series( 'hasTypeAccess', 1, 1, 1, 0 ); - $type->set_series( 'derivesFrom', 0, 1, 1, 0 ); - $type->{title} = 'title'; - $db->set_always( 'getAllTypes', $type ); - - $mock->{omitutil} = 1; - $mock->clear; - - addTypes( $mock, 't', 'U', 'p', undef, undef ); - - ( $method, $args ) = $type->next_call; - is( $method, 'hasTypeAccess', '... should check hasTypeAccess() for type' ); - is_deeply( [ @$args[ 1 .. 2 ] ], [ 'U', 'c' ], - '... passing $USER and "c"' ); - - ( $method, $args ) = $type->next_call; - is( $method, 'derivesFrom', - '... should check derivesFrom() if $this->{omitutil}' ); - is( $args->[1], 'utility', '... passing "utility"' ); - - ( $method, $args ) = $mock->next_call; - isnt( $method, 'addHash', - '... should not call addHash() when no $none or $inherit' ); - is( $method, 'createTree', '... should call createTree()' ); - is_deeply( $$args[1], [$type], - '... passing it $TYPE if hasTypeAccess() and not derivesFrom()' ); - - $mock->clear; - - addTypes( $mock, 't', 'U', 'p', undef, undef ); - ( $method, $args ) = $mock->next_call; - is_deeply( [ @{ $$args[1] } ], - [], '... not passing it $TYPE if derivesFrom() and $omitutil' ); - - $mock->{omitutil} = 0; - $mock->clear; - - addTypes( $mock, 't', 'U', 'p', undef, undef ); - - ( $method, $args ) = $mock->next_call; - is_deeply( $$args[1], [$type], - '... passing it $TYPE if hasTypeAccess() and not $omitutil' ); - - $mock->clear; - - addTypes( $mock, 't', 'U', 'p', undef, undef ); - - ( $method, $args ) = $mock->next_call; - is_deeply( [ @{ $$args[1] } ], - [], '... not passing it $TYPE if not hasTypeAccess()' ); - - ( $method, $args ) = $mock->next_call; - is( $method, 'addArray', '... should call addArray()' ); - is_deeply( - $$args[1], - [ 'v1', 'v2' ], - '... passing it ref to array of menu values' - ); - - ( $method, $args ) = $mock->next_call; - is( $method, 'addLabels', '... should call addLabels()' ); - is_deeply( - $$args[1], - { l2 => 'v2', l1 => 'v1' }, - '... passing it ref to hash of all menu label/value pairs' - ); - is( $$args[2], 1, '... and passing it 1' ); - - is( $result, 1, '... should return 1' ); -} - -# createTree() -{ - my $mock = Test::MockObject->new; - $mock->set_always( 'createTree', [ { label => 'v1' }, { label => 'v2' } ] ); - - my $types = [ - { extends_nodetype => 0, title => 'zero', node_id => 1 }, - { extends_nodetype => 1, title => 'one1', node_id => 2 }, - { extends_nodetype => 1, title => 'one2', node_id => 3 }, - ]; - - createTree( $mock, $types, 1 ); - - my ( $method, $args ) = $mock->next_call; - is( $method, 'createTree', 'createTree() should call createTree()' ); - is_deeply( $args, [ $mock, $types, 2 ], '... passing it $types, node_id' ); - - ( $method, $args ) = $mock->next_call; - is_deeply( - [ $method, @$args ], - [ 'createTree', $mock, $types, 3 ], - '... for each $type with extends_nodetype matching $current' - ); - - ( $method, $args ) = $mock->next_call; - ok( !$method, '... but no more' ); - - $mock->set_always( 'createTree', [ { label => 'v1' }, { label => 'v2' } ] ); - my $result = createTree( $mock, $types, undef ); - - ( $method, $args ) = $mock->next_call; - is( $$args[2], 1, '... $current defaults to 0' ); - - is_deeply( - $result, - [ - { label => ' + zero', value => 1 }, - { label => ' - -v1' }, - { label => ' - -v2' } - ], - '... should return correct nodetype tree' - ); -} - -sub AUTOLOAD -{ - my ($subname) = $AUTOLOAD =~ /([^:]+)$/; - - if ( my $sub = $package->can( $subname ) ) - { - $sub->(@_); - } - else - { - warn "Cannot call <$subname> in ($package)\n"; - } -} +use Everything::HTML::FormObject::Test::NodetypeMenu; +Everything::HTML::FormObject::Test::NodetypeMenu->runtests; Modified: trunk/ebase/t/HTML/FormObject/TypeMenu.t =================================================================== --- trunk/ebase/t/HTML/FormObject/TypeMenu.t 2006-09-30 09:59:58 UTC (rev 915) +++ trunk/ebase/t/HTML/FormObject/TypeMenu.t 2006-09-30 12:48:23 UTC (rev 916) @@ -1,153 +1,4 @@ -#!/usr/bin/perl -w +#!/usr/bin/perl -use strict; -use vars qw( $AUTOLOAD ); - -BEGIN -{ - chdir 't' if -d 't'; - unshift @INC, '../blib/lib', 'lib/', '..'; -} - -use FakeNode; -use Test::More tests => 32; - -$INC{'Everything.pm'} = $INC{'Everything/HTML/FormObject/FormMenu.pm'} = 1; - -{ - local ( - *Everything::import, - *Everything::HTML::FormObject::FormMenu::import, - *Everything::HTML::FormObject::TypeMenu::import - ); - - my @imports; - *Everything::import = *Everything::HTML::FormObject::FormMenu::import = - sub { - push @imports, $_[0]; - }; - - *Everything::HTML::FormObject::TypeMenu::import = sub { }; - - use_ok('Everything::HTML::FormObject::TypeMenu'); - is( scalar @imports, 2, 'TypeMenu should load two packages' ); - is( $imports[0], 'Everything', '... Everything' ); - is( - $imports[1], - 'Everything::HTML::FormObject::FormMenu', - '... and FormMenu' - ); -} - -# genObject() -{ - local ( - *Everything::HTML::FormObject::TypeMenu::getParamArray, - *Everything::HTML::FormObject::TypeMenu::SUPER::genObject - ); - - my @params; - *Everything::HTML::FormObject::TypeMenu::getParamArray = sub { - push @params, "@_"; - shift; - @_; - }; - - *Everything::HTML::FormObject::TypeMenu::SUPER::genObject = sub { - my $node = shift; - $node->genObject(@_); - return 'html'; - }; - - my $node = FakeNode->new(); - $node->{_subs}{genPopupMenu} = [ 'a', 'b', 'c', 'd' ]; - - my $result = - genObject( $node, 'q', 'bN', 'f', 'n', 't', 'd', 'U', 'p', 'n', 'i', - 'it' ); - - is( - $params[0], - 'query, bindNode, field, name, type, default, USER, perm, ' - . 'none, inherit, inherittxt q bN f n t d U p n i it', - 'genObject() should call getParamArray() with @_' - ); - is( $node->{_calls}[0][0], - 'genObject', '... should call SUPER::genObject()' ); - is( $node->{_calls}[1][0], 'addTypes', '... should call addTypes()' ); - is( $node->{_calls}[2][0], - 'genPopupMenu', '... should call genPopupMenu()' ); - is( $node->{_calls}[2][2], 'n', '... should use provided $name' ); - is( $node->{_calls}[1][1], 't', '... should use provided $type' ); - is( $node->{_calls}[1][2], 'U', '... should use provided $USER' ); - is( $node->{_calls}[1][3], 'p', '... should use provided $perm' ); - is( $node->{_calls}[2][3], undef, '... $default becomes undef if true' ); - is( $result, "html\na", - '... returning concatenation of SUPER() and genPopupMenu() calls' ); - - genObject( $node, 'q', { f => 'field' }, 'f' ); - - is( $node->{_calls}[-2][1], - 'nodetype', '... $type should default to "nodetype"' ); - is( $node->{_calls}[-2][2], '-1', '... $USER should default to -1' ); - is( $node->{_calls}[-2][3], 'r', '... $perm should default to "r"' ); - is( $node->{_calls}[-1][2], 'f', '... $name should default to $field' ); - is( $node->{_calls}[-1][3], - 'field', - '... with no default value, should bind to provided node field' ); - - genObject( $node, 'q', '', 'field', '', '', 'AUTO' ); - is( $node->{_calls}[-1][3], - undef, - '... default value should be undef if "AUTO" and lacking bound node' ); -} - -# addTypes() -{ - my $node = FakeNode->new(); - $node->{_subs} = { - addHash => [ ('H') x 9 ], - addType => [ ('T') x 9 ] - }; - - my $result = addTypes( $node, 't', 'U', 'p', 'n', 'i', 'it' ); - is( $node->{_calls}[0][0], - 'addHash', 'addTypes() should call addHash() if defined $none' ); - is( $node->{_calls}[1][0], - 'addHash', '... should call addHash() again if defined $inherit' ); - is( $node->{_calls}[2][0], 'addType', '... should call addType()' ); - is( $node->{_calls}[2][2], 'U', '... should use provided $USER' ); - is( $node->{_calls}[2][3], 'p', '... and $perm' ); - is( ${ $node->{_calls}[1][1] }{'inherit (it)'}, - 'i', - '... $label should be set to "inherit ($inherittxt)" if $inherittxt' ); - is( $result, 1, '... should return 1' ); - - addTypes( $node, 't', '', '', 'n' ); - is( $node->{_calls}[-1][2], -1, '... $USER defaults to -1' ); - is( $node->{_calls}[-1][3], 'r', '... and $perm to "r"' ); - is( ${ $node->{_calls}[-2][1] }{None}, - 'n', '... skip an addHash() if $inherit undefined' ); - - addTypes( $node, 't', 'U', 'p', undef, 'i' ); - is( $node->{_calls}[-3][0], - 'addType', '... skip and addHash() if $none undefined' ); - is( ${ $node->{_calls}[-2][1] }{inherit}, - 'i', '... $label should be set to "inherit" if no $inherittxt' ); -} - -sub AUTOLOAD -{ - return if $AUTOLOAD =~ /DESTROY$/; - - no strict 'refs'; - $AUTOLOAD =~ s/^main:://; - - my $sub = "Everything::HTML::FormObject::TypeMenu::$AUTOLOAD"; - - if ( defined &{$sub} ) - { - *{$AUTOLOAD} = \&{$sub}; - goto &{$sub}; - } -} +use Everything::HTML::FormObject::Test::TypeMenu; +Everything::HTML::FormObject::Test::TypeMenu->runtests; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |