Data-MultiValuedHash

 view release on metacpan or  search on metacpan

t/Data-MultiValuedHash.t  view on Meta::CPAN


	$did = serialize( scalar( $mvh->delete( 'Name' ) ) );
	$should = "[ 'John', 'Glenn', ], ";
	result( $did eq $should, "delete( 'Name' ) returns '$did'" );
	
	$did = serialize( scalar( $mvh->fetch( 'Name' ) ) );
	$should = "undef, ";
	result( $did eq $should, "fetch( 'Name' ) returns '$did'" );

	$did = serialize( scalar( $mvh->delete( 'Name' ) ) );
	$should = "undef, ";
	result( $did eq $should, "delete( 'Name' ) returns '$did'" );

	# now we do delete_all()

	$did = serialize( scalar( $mvh->delete_all() ) );
	$should = "{ '' => [ ], 'Siblings' => [ ], 'age' => [ '18', ], 'color' => [ ], 'pets' => [ 'Dog', 'Hamster', ], 'songs' => [ 'this', 'that', 'and the other', ], }, ";
	result( $did eq $should, "delete_all() returns '$did'" );

	$did = serialize( scalar( $mvh->fetch_all() ) );
	$should = "{ }, ";
	result( $did eq $should, "fetch_all() returns '$did'" );

	$did = serialize( scalar( $mvh->delete_all() ) );
	$should = "{ }, ";
	result( $did eq $should, "delete_all() returns '$did'" );
}

######################################################################
# testing batch_new()

{
	message( "testing batch_new()" );

	my ($mvh, $did, $should);

	my @sources = (
		'',
		[],
		{},
		{ name => 'Don Smith' },
		Data::MultiValuedHash->new( 1, {
			Name => 'John',
			age => 17,
			color => 'green',
			Siblings => ['Laura', 'Andrew', 'Julia'],
			pets => ['Cat', 'Bird'],
		} ),
		{
			visible_title => "What's your name?",
			type => 'textfield',
			name => 'name',
		}, {
			visible_title => "What's the combination?",
			type => 'checkbox_group',
			name => 'words',
			'values' => ['eenie', 'meenie', 'minie', 'moe'],
			default => ['eenie', 'minie'],
		}, {
			visible_title => "What's your favorite colour?",
			type => 'popup_menu',
			name => 'color',
			'values' => ['red', 'green', 'blue', 'chartreuse'],
		}, {
			type => 'submit', 
		},	
	);
	result( @sources == 9, "there are @{[scalar(@sources)]} source records" );

	my @mvh_list = Data::MultiValuedHash->batch_new( 0, \@sources );
	result( @mvh_list == 9, "there are @{[scalar(@mvh_list)]} new objects" );
	
	my @expected = (
		"{ }, ",
		"{ }, ",
		"{ }, ",
		"{ 'name' => [ 'Don Smith', ], }, ",
		"{ 'age' => [ '17', ], 'color' => [ 'green', ], 'name' => [ 'John', ], 'pets' => [ 'Cat', 'Bird', ], 'siblings' => [ 'Laura', 'Andrew', 'Julia', ], }, ",
		"{ 'name' => [ 'name', ], 'type' => [ 'textfield', ], 'visible_title' => [ 'What's your name?', ], }, ",
		"{ 'default' => [ 'eenie', 'minie', ], 'name' => [ 'words', ], 'type' => [ 'checkbox_group', ], 'values' => [ 'eenie', 'meenie', 'minie', 'moe', ], 'visible_title' => [ 'What's the combination?', ], }, ",
		"{ 'name' => [ 'color', ], 'type' => [ 'popup_menu', ], 'values' => [ 'red', 'green', 'blue', 'chartreuse', ], 'visible_title' => [ 'What's your favorite colour?', ], }, ",
		"{ 'type' => [ 'submit', ], }, ",
	);

	foreach my $i (0..$#mvh_list) {
		$mvh = $mvh_list[$i];
		
		$did = UNIVERSAL::isa( $mvh, "Data::MultiValuedHash" );
		result( $did, "new object $i is an MVH object" );

		if( $did ) {
			$did = $mvh->ignores_case();
			result( $did == 0, "mvh$i->ignores_case() returns '$did'" );

			$did = serialize( scalar( $mvh->fetch_all() ) );
			$should = $expected[$i];
			result( $did eq $should, "mvh$i->fetch_all() returns '$did'" );
		}
	}
}

######################################################################

message( "DONE TESTING Data::MultiValuedHash" );

######################################################################

1;



( run in 0.801 second using v1.01-cache-2.11-cpan-364913b4093 )