PHP-Strings

 view release on metacpan or  search on metacpan

t/str_word_count.t  view on Meta::CPAN

#!/usr/bin/perl
use strict;
use warnings FATAL => 'all';
use Test::More tests => 40;
use Test::Differences;
use lib 't';
use TestPHP;
BEGIN { use_ok 'PHP::Strings', ':str_word_count' };

my $php = find_php;

sub test_0
{
    my ( $input, $expected, $comment ) = @_;

    is( str_word_count( $input ) => $expected, $comment );

    SKIP: {
        skip "No PHP found", 1 unless $php;
        my $answer = read_php( sprintf q{<?= str_word_count( "%s" ) ?>},
            $input
        );
        is( $answer => $expected, "$comment - in PHP" );
    }
}

sub test_1
{
    my ( $input, $expected, $comment ) = @_;

    my @words = str_word_count( $input, 1 );
    is ( scalar @words => scalar @$expected, "$comment - sizes" );
    ok( eq_array( \@words, $expected ), "$comment - arrays match" );

    SKIP: {
        skip "No PHP found", 1 unless $php;
        my $answer = read_php( sprintf q{<?= join( " ", str_word_count( "%s", 1 ) ) ?>},
            $input
        );
        is( $answer => "@$expected", "$comment - in PHP" );
    }
}

sub test_2
{
    my ( $input, $expected, $comment ) = @_;

    my %words = str_word_count( $input, 2 );
    is ( scalar keys %words => scalar keys %$expected, "$comment - sizes" );
    ok( eq_hash( \%words, $expected ), "$comment - hashes match" );

    SKIP: {
        skip "No PHP found", 1 unless $php;
        my $answer = read_php( sprintf <<'EOP',
<?
            $words = str_word_count( "%s", 2 );
            foreach( $words as $i => $word ) {
                print "$i = $word\n";
            }
?>
EOP
            $input
        );
        eq_or_diff( $answer => join( '', map {
                    "$_ = $expected->{$_}\n"



( run in 1.454 second using v1.01-cache-2.11-cpan-f56aa216473 )