File: Encode-Mapper-3.t

package info (click to toggle)
libencode-arabic-perl 14.1-2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 584 kB
  • ctags: 119
  • sloc: perl: 4,515; makefile: 11
file content (63 lines) | stat: -rw-r--r-- 2,777 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#########################

use Test::More tests => 4;

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

# Insert your test code below, the Test::More module is use()ed here so read
# its man page ( perldoc Test::More ) for help writing this test script.

    use Encode::Mapper;     ############################################# Enjoy the ride ^^

    use Encode::Mapper ':others', ':silent';

    ## Types of rules for mapping the data and controlling the engine's configuration #####

    @rules = (
        'x',            'y',            # single 'x' be 'y', unless greediness prefers ..
        'xx',           'Y',            # .. double 'x' be 'Y' or other rules

        'uc(x)x',       sub { 'sorry ;)' },     # if 'x' follows 'uc(x)', be sorry, else ..

        'uc(x)',        [ '', 'X' ],            # .. alias this *engine-initial* string
        'xuc(x)',       [ '', 'xX' ],           # likewise, alias for the 'x' prefix

        'Xxx',          [ sub { $i++; '' }, 'X' ],      # count the still married 'x'
    );


    ## Constructors of the engine, i.e. one Encode::Mapper instance #######################

    $mapper_A = Encode::Mapper->compile( @rules );        # engine constructor

    Encode::Mapper->options('others' => undef, 'silent' => undef, 'complement' => ['x','y','x','z']);

    $mapper_B = Encode::Mapper->new( ['others' => sub { shift }, 'silent' => 1], @rules );

    is_deeply $mapper_A, $mapper_B, 'constructor identity';

    ## Elementary performance of the engine ###############################################

    @source = ( 'x', 'xAx', 'xBxuc(x)', 'xxx', '', 'xxC' );    # distribution of the data ..
    $source = join '', @source;                             # .. is ignored in this sense

    @result_A = ($mapper_A->process(@source), $mapper_A->recover());  # the mapping procedure
    @result_B = ($mapper_B->process($source), $mapper_B->recover());  # completely equivalent

    is_deeply \@result_A, \@result_B, 'performance identity';

    $result = join '', map { ref $_ eq 'CODE' ? $_->() : $_ } @result_A;

    is $result, 'YAYByXyC', 'expected output';
    is $i, 2, 'expected side effect';

    #@follow = $mapper->compute(@source);    # follow the engine's computation over @source
    #$dumper = $mapper->dumper();            # returns the engine as a Data::Dumper object

    ## Module's higher API implemented for convenience ####################################

    #$encoder = [ $mapper, Encode::Mapper->compile( ... ), ... ];    # reference to mappers
    #$result = Encode::Mapper->encode($source, $encoder, 'utf8');    # encode down to 'utf8'

    #$decoder = [ $mapper, Encode::Mapper->compile( ... ), ... ];    # reference to mappers
    #$result = Encode::Mapper->decode($source, $decoder, 'utf8');    # decode up from 'utf8'