use Mojo::Base -strict;

use utf8;

# "Homer, we're going to ask you a few simple yes or no questions.
#  Do you understand?
#  Yes. *lie dectector blows up*"
use Test::More tests => 44;

use File::Spec::Functions qw(catfile splitdir);
use File::Temp 'tempdir';
use FindBin;
use Mojo::ByteStream 'b';

# camelize
is b('foo_bar_baz')->camelize, 'FooBarBaz', 'right camelized result';

# decamelize
is b('FooBarBaz')->decamelize, 'foo_bar_baz', 'right decamelized 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_escape
is b("foo bar'<baz>")->html_escape, 'foo bar&#39;&LT;baz&GT;',
  'right html escaped result';

# html_unescape
is b('&#x3c;foo&#x3E;bar&lt;baz&gt;&#x26;&#34;')->html_unescape,
  "<foo>bar<baz>&\"", 'right html unescaped result';

# xml_escape
is b(qq{la<f>\nbar"baz"'yada\n'&lt;la})->xml_escape,
  "la&lt;f&gt;\nbar&quot;baz&quot;&#39;yada\n&#39;&amp;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_md5_sum
is b('Hi there')->hmac_md5_sum(1234567890),
  'e3b5fab1b3f5b9d1fe391d09fce7b2ae', 'right hmac md5 checksum';

# hmac_sha1_sum
is b('Hi there')->hmac_sha1_sum(1234567890),
  '4fd7160f392dc54308608cae6587e137c62c2e39', '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';

# 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(',')->each],   [1, 2, 3, 4, 5], 'right elements';
is_deeply [$stream->split(qr/,/)->each], [1, 2, 3, 4, 5], 'right elements';
is_deeply [b('54321')->split('')->each], [5, 4, 3, 2, 1], 'right elements';
is_deeply [b('')->split('')->each],    [], 'no elements';
is_deeply [b('')->split(',')->each],   [], 'no elements';
is_deeply [b('')->split(qr/,/)->each], [], '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);
my $stdout = *STDOUT;
*STDOUT = $handle;
b(1, 2, 3)->say;
*STDOUT = $stdout;
is $buffer, "test\n123\n", 'right output';

# slurp
my $file = catfile splitdir($FindBin::Bin), qw(templates exception.mt);
$stream = b($file)->slurp;
is $stream, "test\n% die;\n123\n", 'right content';
$stream = b($file)->slurp->split("\n")->grep(qr/die/)->join('');
is $stream, '% die;', 'right content';

# spurt
my $dir = tempdir CLEANUP => 1;
$file = catfile $dir, 'test.txt';
is b("just\nworks!")->spurt($file)->quote, qq{"just\nworks!"}, 'right result';
is b($file)->slurp, "just\nworks!", 'successful roundtrip';
