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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
use Mojo::Base -strict;
use Test::More;
use FindBin;
use Mojo::ByteStream 'b';
# Tap into method chain
is b('test')->tap(sub { $$_ .= '1' })->camelize, 'Test1', 'right result';
# camelize
is b('foo_bar_baz')->camelize, 'FooBarBaz', 'right camelized result';
# decamelize
is b('FooBarBaz')->decamelize, 'foo_bar_baz', 'right decamelized result';
# unindent
is b(" test\n 123\n 456\n")->unindent, "test\n 123\n456\n",
'right unindented result';
# b64_encode
is b('foobar$%^&3217')->b64_encode, "Zm9vYmFyJCVeJjMyMTc=\n",
'right Base64 encoded result';
# b64_decode
is b("Zm9vYmFyJCVeJjMyMTc=\n")->b64_decode, 'foobar$%^&3217',
'right Base64 decoded result';
# url_escape
is b('business;23')->url_escape, 'business%3B23', 'right URL escaped result';
# url_unescape
is b('foo%C3%9F%C4%80bar%E2%98%BA')->url_unescape->decode,
"foo\x{df}\x{0100}bar\x{263a}", 'right URL unescaped result';
is b('foo%C3%9F%C4%80bar%E2%98%BA')->url_unescape->decode('UTF-8'),
"foo\x{df}\x{0100}bar\x{263a}", 'right URL unescaped result';
# html_unescape
is b('<foo>bar<baz>&"')->html_unescape,
"<foo>bar<baz>&\"", 'right HTML unescaped result';
# xml_escape
is b(qq{la<f>\nbar"baz"'yada\n'<la})->xml_escape,
"la<f>\nbar"baz"'yada\n'&lt;la",
'right XML escaped result';
# punycode_encode
is b('bücher')->punycode_encode, 'bcher-kva', 'right punycode encoded result';
# punycode_decode
is b('bcher-kva')->punycode_decode, 'bücher', 'right punycode decoded result';
# quote
is b('foo; 23 "bar')->quote, '"foo; 23 \"bar"', 'right quoted result';
# unquote
is b('"foo 23 \"bar"')->unquote, 'foo 23 "bar', 'right unquoted result';
# trim
is b(' la la la ')->trim, 'la la la', 'right trimmed result';
# md5_bytes
is unpack('H*', b('foo bar baz ♥')->encode->md5_bytes),
'a740aeb6e066f158cbf19fd92e890d2d', 'right binary md5 checksum';
is unpack('H*', b('foo bar baz ♥')->encode('UTF-8')->md5_bytes),
'a740aeb6e066f158cbf19fd92e890d2d', 'right binary md5 checksum';
# md5_sum
is b('foo bar baz')->md5_sum, 'ab07acbb1e496801937adfa772424bf7',
'right md5 checksum';
# sha1_bytes
is unpack('H*', b('foo bar baz')->sha1_bytes),
'c7567e8b39e2428e38bf9c9226ac68de4c67dc39', 'right binary sha1 checksum';
# sha1_sum
is b('foo bar baz')->sha1_sum, 'c7567e8b39e2428e38bf9c9226ac68de4c67dc39',
'right sha1 checksum';
# hmac_sha1_sum
is b('Hi there')->hmac_sha1_sum('abc1234567890'),
'5344f37e1948dd3ffb07243a4d9201a227abd6e1', 'right hmac sha1 checksum';
# secure_compare
ok b('hello')->secure_compare('hello'), 'values are equal';
ok !b('hell')->secure_compare('hello'), 'values are not equal';
# xor_encode
is b('hello')->xor_encode('foo'), "\x0e\x0a\x03\x0a\x00", 'right result';
is b("\x0e\x0a\x03\x0a\x00")->xor_encode('foo'), 'hello', 'right result';
# Nested bytestreams
my $stream = b(b('test'));
ok !ref $stream->to_string, 'nested bytestream stringified';
$stream = Mojo::ByteStream->new(Mojo::ByteStream->new('test'));
ok !ref $stream->to_string, 'nested bytestream stringified';
# split
$stream = b('1,2,3,4,5');
is_deeply $stream->split(',')->to_array, [1, 2, 3, 4, 5], 'right elements';
is_deeply $stream->split(qr/,/)->to_array, [1, 2, 3, 4, 5], 'right elements';
is_deeply b('54321')->split('')->to_array, [5, 4, 3, 2, 1], 'right elements';
is_deeply b('')->split('')->to_array, [], 'no elements';
is_deeply b('')->split(',')->to_array, [], 'no elements';
is_deeply b('')->split(qr/,/)->to_array, [], 'no elements';
$stream = b('1/2/3');
is $stream->split('/')->map(sub { $_->quote })->join(', '), '"1", "2", "3"',
'right result';
is $stream->split('/')->map(sub { shift->quote })->join(', '), '"1", "2", "3"',
'right result';
# length
is b('foo bar baz')->size, 11, 'size is 11';
# "0"
$stream = b('0');
is $stream->size, 1, 'size is 1';
is $stream->to_string, '0', 'right content';
# clone
$stream = b('foo');
my $clone = $stream->clone;
isnt $stream->b64_encode->to_string, 'foo', 'original changed';
is $clone->to_string, 'foo', 'clone did not change';
# say and autojoin
my $buffer = '';
open my $handle, '>', \$buffer;
b('te', 'st')->say($handle);
{
local *STDOUT = $handle;
b(1, 2, 3)->say->quote->say;
}
is $buffer, "test\n123\n\"123\"\n", 'right output';
# term_escape
is b("\t\b\r\n\f")->term_escape, "\\x09\\x08\\x0d\n\\x0c", 'right result';
done_testing();
|