Mojolicious

 view release on metacpan or  search on metacpan

t/mojo/util.t  view on Meta::CPAN

  $tree   = [
    ['a',  'b',   'c',   undef, 'A',    'b.c', 'D',        '/E',  'a-b', '3',   'expires', 'Thu'],
    ['07', undef, 'Aug', undef, '2008', undef, '07:07:59', undef, 'GMT', undef, 'Ab',      undef]
  ];
  is_deeply split_header($header), $tree, 'right result';
};

subtest 'split_cookie_header' => sub {
  is_deeply split_cookie_header(''), [], 'right result';
  is_deeply split_cookie_header('a=b; expires=Thu, 07 Aug 2008 07:07:59 GMT,c=d'),
    [['a', 'b', 'expires', 'Thu, 07 Aug 2008 07:07:59 GMT'], ['c', 'd']], 'right result';
  is_deeply split_cookie_header('a=b; expires=Tuesday, 09-Nov-1999 23:12:40 GMT, c=d'),
    [['a', 'b', 'expires', 'Tuesday, 09-Nov-1999 23:12:40 GMT'], ['c', 'd']], 'right result';
  is_deeply split_cookie_header('a=b; expires=Tuesday, 09-Nov-1999 23:12:40 GMT;, c=d;'),
    [['a', 'b', 'expires', 'Tuesday, 09-Nov-1999 23:12:40 GMT'], ['c', 'd']], 'right result';
  is_deeply split_cookie_header('a=b; expires=Sun,06  Nov  1994  08:49:37  UTC; path=/'),
    [['a', 'b', 'expires', 'Sun,06  Nov  1994  08:49:37  UTC', 'path', '/']], 'right result';
  is_deeply split_cookie_header('a=b ; expires = Sunday 06 Nov 94 08:49:37UTC ; path=/'),
    [['a', 'b', 'expires', 'Sunday 06 Nov 94 08:49:37UTC', 'path', '/']], 'right result';
  my $header = 'expires=Thu, 07 Aug 2008 07:07:59 GMT, a=b';
  my $tree
    = [['expires', 'Thu'], ['07', undef, 'Aug', undef, '2008', undef, '07:07:59', undef, 'GMT', undef], ['a', 'b']];
  is_deeply split_cookie_header($header), $tree, 'right result';
};

subtest 'header_params' => sub {
  is_deeply [header_params('')],             [{}, ''], 'right result';
  is_deeply [header_params('foo=b=a=r')],    [{foo => 'b=a=r'}, ''],      'right result';
  is_deeply [header_params('a=b; c, d=e')],  [{a   => 'b'},     ', d=e'], 'right result';
  is_deeply [header_params('a=b; a=c')],     [{a   => 'b'},     ''],      'right result';
  is_deeply [header_params('a=b; a="c"')],   [{a   => 'b'},     ''],      'right result';
  is_deeply [header_params('a=b; b="c=d"')], [{a   => 'b', b => 'c=d'}, ''], 'right result';
  is_deeply [header_params('a=b, c=d')],     [{a   => 'b'}, ', c=d'], 'right result';
  is_deeply [header_params(q{rel="x"; t*=UTF-8'de'a%20b})], [{rel => 'x', 't*' => "UTF-8'de'a%20b"}, ''],
    'right result';
};

subtest 'extract_usage' => sub {
  is extract_usage,                                      "extract_usage test!\n",                  'right result';
  is extract_usage(curfile->sibling('lib', 'myapp.pl')), "USAGE: myapp.pl daemon\n\n test\n123\n", 'right result';
};

=head1 SYNOPSIS

  extract_usage test!

=cut

subtest 'getopt' => sub {
  getopt ['--charset', 'UTF-8'], 'c|charset=s' => \my $charset;
  is $charset, 'UTF-8', 'right string';
  my $array = ['-t', 'test', '-h', '--whatever', 'Whatever!', 'stuff'];
  getopt $array, ['pass_through'], 't|test=s' => \my $test;
  is $test, 'test', 'right string';
  is_deeply $array, ['-h', '--whatever', 'Whatever!', 'stuff'], 'right structure';
  getopt $array, 'h' => \my $flag, 'w|whatever=s' => \my $whatever;
  ok $flag, 'flag has been set';
  is $whatever, 'Whatever!', 'right string';
  is_deeply $array, ['stuff'], 'right structure';
  {
    local @ARGV = ('--charset', 'UTF-16', 'test');
    getopt 'c|charset=s' => \my @charset;
    is_deeply \@charset, ['UTF-16'], 'right structure';
    is_deeply \@ARGV,    ['test'],   'right structure';
  }
};

subtest 'getopt (return value)' => sub {
  local $SIG{__WARN__} = sub { };

  my $return = getopt ['--lang', 'de'], 'l|lang=s' => \my $lang;
  is $lang, 'de', 'right result';
  ok $return, 'right return value';

  $lang   = undef;
  $return = getopt ['--lnag', 'de'], 'l|lang=s' => \$lang;
  is $lang, undef, 'right result';
  ok !$return, 'right return value';

  $lang   = undef;
  $return = getopt ['--lnag', 'de', '--lang', 'de'], 'l|lang=s' => \$lang;
  is $lang, 'de', 'right result';
  ok !$return, 'right return value';
};

subtest 'unindent' => sub {
  is unindent(" test\n  123\n 456\n"),              "test\n 123\n456\n",              'right unindented result';
  is unindent("\ttest\n\t\t123\n\t456\n"),          "test\n\t123\n456\n",             'right unindented result';
  is unindent("\t \ttest\n\t \t\t123\n\t \t456\n"), "test\n\t123\n456\n",             'right unindented result';
  is unindent("\n\n\n test\n  123\n 456\n"),        "\n\n\ntest\n 123\n456\n",        'right unindented result';
  is unindent("   test\n    123\n   456\n"),        "test\n 123\n456\n",              'right unindented result';
  is unindent("    test\n  123\n   456\n"),         "  test\n123\n 456\n",            'right unindented result';
  is unindent("test\n123\n"),                       "test\n123\n",                    'right unindented result';
  is unindent(" test\n\n 123\n"),                   "test\n\n123\n",                  'right unindented result';
  is unindent('  test'),                            'test',                           'right unindented result';
  is unindent(" te st\r\n\r\n  1 2 3\r\n 456\r\n"), "te st\r\n\r\n 1 2 3\r\n456\r\n", 'right unindented result';
};

subtest 'b64_encode' => sub {
  is b64_encode('foobar$%^&3217'), "Zm9vYmFyJCVeJjMyMTc=\n", 'right Base64 encoded result';
};

subtest 'b64_decode' => sub {
  is b64_decode("Zm9vYmFyJCVeJjMyMTc=\n"), 'foobar$%^&3217', 'right Base64 decoded result';
};

subtest 'b64_encode (UTF-8)' => sub {
  is b64_encode(encode 'UTF-8', "foo\x{df}\x{0100}bar%23\x{263a}"), "Zm9vw5/EgGJhciUyM+KYug==\n",
    'right Base64 encoded result';
};

subtest 'b64_decode (UTF-8)' => sub {
  is decode('UTF-8', b64_decode "Zm9vw5/EgGJhciUyM+KYug==\n"), "foo\x{df}\x{0100}bar%23\x{263a}",
    'right Base64 decoded result';
};

subtest 'b64_encode (custom line ending)' => sub {
  is b64_encode('foobar$%^&3217', ''), 'Zm9vYmFyJCVeJjMyMTc=', 'right Base64 encoded result';
};

subtest 'decode (invalid UTF-8)' => sub {



( run in 2.780 seconds using v1.01-cache-2.11-cpan-364913b4093 )