Brick

 view release on metacpan or  search on metacpan

t/use_cases/phone_number.t  view on Meta::CPAN

for production code, you might want this step in there so you can turn
it on for debugging.

=cut

diag "\nExplaining zip code profile:\n",
	$profile->explain if $ENV{DEBUG};

=head2 Get some input data

The input is a hash reference. The field names are the keys and their
values are the hash values.

=cut

my $Input = {
	phone        => 5551234,     # fine, plenty of characters
	space_phone  => "555 1234",  # fine, after munging
	dot_phone    => "555.1234",  # fine, after munging
	short_phone  => 555123,      # Oops, not enough characters
	long_phone   => 8005551212,  # Oops, too many (for this test)
	letter_phone => 'abcdefg',   # Oops, non-digits
	};

=head2 Validate the data with apply()

This isn't a necessary step, but it's nice to know that the profile
makes sense before you actually try to use it. Even if you don't do it
for production code, you might want this step in there so you can turn
it on for debugging.

=cut

my $result = $brick->apply( $profile, $Input );

=head2 Check the results

This isn't a necessary step, but it's nice to know that the profile
makes sense before you actually try to use it. Even if you don't do it
for production code, you might want this step in there so you can turn
it on for debugging.

=cut

#print STDERR Data::Dumper->Dump( [$result], [qw(result)] ) ; #if $ENV{DEBUG};
use Data::Dumper;

diag "\n" if $ENV{DEBUG};

foreach my $index ( 0 .. $#$result )
	{
	my $entry = $result->[$index];

	diag '-----' . $entry->get_label .  "----------------------------\n"
		if $ENV{DEBUG};

	do { diag "\tpassed\n\n" if $ENV{DEBUG}; next } if $entry->get_result;

	my @data = ( $entry->get_messages );
	my @errors = ();
	my $iterations = 0;
	while( my $error = shift @data )
		{
		last if $iterations++ > 20; # debugging guard against infinity
#		print STDERR "Iteration $iterations\n";
		if( $error->{handler} =~ m/^__/ )
			{
			push @data, @{ $error->{errors} };
			next;
			}

		push @errors, $error;
		}

	#print STDERR Data::Dumper->Dump( [\@errors], [qw(errors)] ) ; #if $ENV{DEBUG};

	#print STDERR "$entry->[0] checked by $entry->[1] which returned:\n\t$message\n";

	next unless eval {  @{ $entry->get_messages->{errors} } > 0 };

	foreach my $error ( @errors )
		{
		diag "$error->{handler}: $error->{message}\n" if $ENV{DEBUG};
		}

	diag "\n" if $ENV{DEBUG};
	}

# the first three results should pass
foreach my $row ( splice @$result, 0, 3 )
	{
	is( $row->get_result, 1, $row->get_label . ' passes' );  # the name
	}

# the rest of the results should fail
foreach my $row ( @$result )
	{
	is( $row->get_result, 0, $row->get_label . ' fails (as expected)' );
	}



( run in 2.000 seconds using v1.01-cache-2.11-cpan-302cb4679cc )