File: 220_magic.t

package info (click to toggle)
libunicode-utf8-perl 0.62-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 492 kB
  • sloc: perl: 2,365; sh: 6; makefile: 3
file content (89 lines) | stat: -rw-r--r-- 2,173 bytes parent folder | download | duplicates (4)
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!perl
use strict;
use warnings;

use Test::More;

BEGIN {
    eval 'use Variable::Magic qw[cast wizard];';
    plan skip_all => 'Variable::Magic is required for this test' if $@;
    plan tests => 13;
}

BEGIN {
    use_ok('Unicode::UTF8', qw[ decode_utf8
                                encode_utf8 ]);
}

my ($string_get,
    $string_set,
    $octets_get,
    $octets_set);

my $w_string = wizard get => sub { $string_get++ },
                      set => sub { $string_set++ };

my $w_octets = wizard get => sub { $octets_get++ },
                      set => sub { $octets_set++ };


($string_get, $string_set, $octets_get, $octets_set) = (0, 0, 0, 0);

{
    my $octets = "\x80\x80\x80";
    my $string;

    cast $octets, $w_octets;
    cast $string, $w_string;
    {
        no warnings 'utf8';
        $string = decode_utf8($octets);
    }

    is($octets_get, 1, 'decode_utf8() $octets GET magic');
    is($octets_set, 0, 'decode_utf8() $octets SET magic');
    is($string_get, 0, 'decode_utf8() $string GET magic');
    is($string_set, 1, 'decode_utf8() $string SET magic');
}

($string_get, $string_set, $octets_get, $octets_set) = (0, 0, 0, 0);

{
    my $octets = "Foo \xE2\x98\xBA";
    my $string;

    utf8::upgrade($octets);

    cast $octets, $w_octets;
    cast $string, $w_string;
    {
        no warnings 'utf8';
        $string = decode_utf8($octets);
    }

    is($octets_get, 1, 'decode_utf8(upgraded) $octets GET magic');
    is($octets_set, 0, 'decode_utf8(upgraded) $octets SET magic');
    is($string_get, 0, 'decode_utf8(upgraded) $string GET magic');
    is($string_set, 1, 'decode_utf8(upgraded) $string SET magic');
}

($string_get, $string_set, $octets_get, $octets_set) = (0, 0, 0, 0);

{
    my $string = "\x{110000}\x{110000}\x{110000}";
    my $octets;

    cast $octets, $w_octets;
    cast $string, $w_string;
    {
        no warnings 'utf8';
        $octets = encode_utf8($string);
    }

    is($octets_get, 0, 'encode_utf8() $octets GET magic');
    is($octets_set, 1, 'encode_utf8() $octets SET magic');
    is($string_get, 1, 'encode_utf8() $string GET magic');
    is($string_set, 0, 'encode_utf8() $string SET magic');
}