File: 010_basic.t

package info (click to toggle)
liburl-encode-perl 0.03-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: perl: 1,586; makefile: 2
file content (67 lines) | stat: -rwxr-xr-x 1,738 bytes parent folder | download | duplicates (3)
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
#!perl

use strict;
use warnings;

use Test::More;

BEGIN {
    use_ok('URL::Encode::PP', qw[ url_encode
                                  url_encode_utf8
                                  url_decode
                                  url_decode_utf8 ]);
}

sub UNRESERVED () {  "0123456789"
                   . "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                   . "abcdefghijklmnopqrstuvwxyz"
                   . "_.~-" }

my @tests = (
    [ "",         "",     "empty string" ],
    [ "\x{00E5}", "%E5",  "U+00E5 in native encoding" ],
    [ UNRESERVED, UNRESERVED, "unreserved characters" ],
    [ " ", "+", "U+0020 SPACE" ]
);

for my $ord (0x00..0x1F, 0x21..0xFF) {
    my $chr = pack 'C', $ord;
    next unless index(UNRESERVED, $chr) < 0;
    my $enc = sprintf('%%%.2X', $ord);
    push @tests, [ $chr, $enc, sprintf("ordinal %d", $ord) ];
}

foreach my $test (@tests) {
    my ($expected, $encoded, $name) = @$test;
    is(url_decode($encoded), $expected, "url_decode(): $name");
}

foreach my $test (@tests) {
    my ($octets, $expected, $name) = @$test;
    is(url_encode($octets), $expected, "url_encode(): $name");
}

{
    use utf8;
    my $dec = 'blåbär är gött!';
    my $enc = 'bl%E5b%E4r+%E4r+g%F6tt%21';
    is(url_encode($dec), $enc, 'url_encode: native string');
    is(url_decode($enc), $dec, 'url_decode: native string');
}

{
    use utf8;
    my $dec = 'blåbär är gött!';
    my $enc = 'bl%C3%A5b%C3%A4r+%C3%A4r+g%C3%B6tt%21';
    is(url_encode_utf8($dec), $enc, 'url_encode_utf8: UTF-8 string');
    is(url_decode_utf8($enc), $dec, 'url_decode_utf8: UTF-8 string');
}

{
    my $enc = '%AE%Ae%aE';
    my $dec = "\xAE\xAE\xAE";
    is(url_decode($enc), $dec, 'mixed hexadecimal case');
}

done_testing();