PHP-Strings

 view release on metacpan or  search on metacpan

t/strtr.t  view on Meta::CPAN

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

my $php = find_php;

sub test_tr
{
    my ( $input, $to, $from, $expected, $comment ) = @_;

    is( strtr( $input, $to, $from ) => $expected, $comment );

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

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

    is( strtr( $input, $pairs ) => $expected, $comment );

    SKIP: {
        skip "No PHP found", 1 unless $php;
        my $array = '';
        $array .= qq,\$pairs["$_"] = "$pairs->{$_}";\n, for keys %$pairs;
        my $answer = read_php( sprintf q{<?
            $pairs = array();
            %s
            echo strtr( "%s", $pairs );
            ?>},
            $array, $input,
        );
        is( $answer => $expected, "$comment - in PHP" );
    }
}

# Good inputs, simple tr
{
    test_tr( "Hello", "e", "f", "Hfllo", "Simple" );
    test_tr( "Hello", "eloH", "xyzw", "wxyyz", "Multiple characters" );
    test_tr( "Hello", "elo", "xyzw", "Hxyyz", "Ignore excess \$to" );
    test_tr( "Hello", "eloH", "xyz", "Hxyyz", "Ignore excess \$from" );
}

# Good inputs, paired tr
{
    test_pairs( "hi all, I said hello", {
            "hello" => "hi", "hi" => "hello"
        }, "hello all, I said hi", "Pairs from docs" );
    test_pairs( "longest match of keywords goes first", {qw(
            long wumpus longest xyzzy
            )}, "xyzzy match of keywords goes first", "Longest pair" );
}



( run in 1.810 second using v1.01-cache-2.11-cpan-39bf76dae61 )