=pod

=head1 NAME

unit/string/append.t - tests the C<append> method

=cut

use Test2::V0;
use Scalar::Util 'blessed';
use Sub::HandlesVia::XS;

subtest "Basic" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_1" => {
		str_source  => Sub::HandlesVia::XS::STR_SRC_INVOCANT,
	} );

	my $x = "foo";
	is( Local::test_1($x, "bar"), "foobar" );
	is( $x, "foobar" );
	
	my $y = "baz";
	is( Local::test_1($x, $y), "foobarbaz" );
	is( $x, "foobarbaz" );
	is( $y, "baz" );
};

subtest "Scalarref invocant" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_2" => {
		str_source  => Sub::HandlesVia::XS::STR_SRC_DEREF_SCALAR,
	} );

	my $x = "foo";
	my $x_ref = \$x;
	is( Local::test_2($x_ref, "bar"), "foobar" );
	is( $x, "foobar" );
	
	my $y = "baz";
	is( Local::test_2($x_ref, $y), "foobarbaz" );
	is( $x, "foobarbaz" );
	ok !blessed $x_ref;
};

subtest "Blessed scalarref invocant" => sub {
	my $x = "foo";
	my $x_ref = bless( \$x, "Local::Test2" );
	is( Local::test_2($x_ref, "bar"), "foobar" );
	is( $x, "foobar" );
	isa_ok( $x_ref, 'Local::Test2' );
	
	my $y = "baz";
	is( Local::test_2($x_ref, $y), "foobarbaz" );
	is( $x, "foobarbaz" );
	isa_ok( $x_ref, 'Local::Test2' );
};

subtest "Arrayref invocant" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_3" => {
		str_source         => Sub::HandlesVia::XS::STR_SRC_DEREF_ARRAY,
		str_source_index   => 2,
	} );

	my $x = [undef, undef, "foo"];
	is( Local::test_3($x, "bar"), "foobar" );
	is( $x, [undef, undef, "foobar"] );
	
	my $y = "baz";
	is( Local::test_3($x, $y), "foobarbaz" );
	is( $x, [undef, undef, "foobarbaz"] );
	ok !blessed $x;
};

subtest "Blessed arrayref invocant" => sub {
	my $x = bless( [undef, undef, "foo"], 'Local::Test3' );
	is( Local::test_3($x, "bar"), "foobar" );
	is( $x, bless( [undef, undef, "foobar"], 'Local::Test3' ) );
	isa_ok( $x, 'Local::Test3' );
	
	my $y = "baz";
	is( Local::test_3($x, $y), "foobarbaz" );
	is( $x, [undef, undef, "foobarbaz"] );
	isa_ok( $x, 'Local::Test3' );
};

subtest "Hashref invocant" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_4" => {
		str_source         => Sub::HandlesVia::XS::STR_SRC_DEREF_HASH,
		str_source_string  => "xyz",
	} );

	my $x = { xyz => "foo" };
	is( Local::test_4($x, "bar"), "foobar" );
	is( $x, { xyz => "foobar" } );
	
	my $y = "baz";
	is( Local::test_4($x, $y), "foobarbaz" );
	is( $x, { xyz => "foobarbaz" } );
	ok !blessed $x;
};

subtest "Hashref invocant, initially missing key" => sub {
	my $x = {};
	my $w = warning {
		is( Local::test_4($x, "bar"), "bar" );
	};
	is( $x, { xyz => "bar" } );
	
	like $w, qr/uninitialized value/;
	
	my $y = "baz";
	is( Local::test_4($x, $y), "barbaz" );
	is( $x, { xyz => "barbaz" } );
	ok !blessed $x;
};

subtest "Blessed hashref invocant" => sub {
	my $x = bless( { xyz => "foo" }, 'Local::Test4' );
	is( Local::test_4($x, "bar"), "foobar" );
	is( $x, bless( { xyz => "foobar" }, 'Local::Test4' ) );
	
	my $y = "baz";
	is( Local::test_4($x, $y), "foobarbaz" );
	is( $x, bless( { xyz => "foobarbaz" }, 'Local::Test4' ) );
	isa_ok( $x, 'Local::Test4' );
};

my ( $GETS, $SETS );

{
	package Local::Test5;
	# Avoid storing $self->xyz as $self->{xyz}
	sub new {
		my ( $class, %arg ) = @_;
		return bless [ whatever => $arg{xyz} ], $class;
	}
	sub xyz {
		( @_ == 1 ) ? ( $GETS++  ) : ( $SETS++ );
		( @_ == 1 ) ? ( $_[0][1] ) : ( $_[0][1] = $_[1] );
	}
}

subtest "Object invocant" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_5" => {
		str_source         => Sub::HandlesVia::XS::STR_SRC_CALL_METHOD,
		str_source_string  => "xyz",
	} );

	my $x = Local::Test5->new( xyz => "foo" );
	is( Local::test_5($x, "bar"), "foobar" );
	is( $x->xyz, "foobar" );
	
	my $y = "baz";
	is( Local::test_5($x, $y), "foobarbaz" );
	is( $x->xyz, "foobarbaz" );
	isa_ok( $x, 'Local::Test5' );
	
	is( $GETS, 4 );
	is( $SETS, 2 );
};

sub Local::Test6::_builder {
	shift->{xyz} = 'foo';
}

subtest "Blessed hashref invocant with fallback builder" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_6" => {
		str_source           => Sub::HandlesVia::XS::STR_SRC_DEREF_HASH,
		str_source_string    => "xyz",
		str_source_fallback  => "_builder",
	} );
	
	my $x = bless( {}, 'Local::Test6' );
	is( Local::test_6($x, "bar"), "foobar" );
	is( $x, bless( { xyz => "foobar" }, 'Local::Test6' ) );
	
	my $y = "baz";
	is( Local::test_6($x, $y), "foobarbaz" );
	is( $x, bless( { xyz => "foobarbaz" }, 'Local::Test6' ) );
	isa_ok( $x, 'Local::Test6' );
};

subtest "With curried SVs and interesting return patterns" => sub {
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_7" => {
		str_source             => Sub::HandlesVia::XS::STR_SRC_INVOCANT,
		curried_sv             => "x",
		method_return_pattern  => Sub::HandlesVia::XS::SHOULD_RETURN_UNDEF,
	} );
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_8" => {
		str_source             => Sub::HandlesVia::XS::STR_SRC_INVOCANT,
		curried_sv             => "y",
		method_return_pattern  => Sub::HandlesVia::XS::SHOULD_RETURN_FALSE,
	} );
	Sub::HandlesVia::XS::INSTALL_shvxs_string_append( "Local::test_9" => {
		str_source             => Sub::HandlesVia::XS::STR_SRC_INVOCANT,
		curried_sv             => "z",
		method_return_pattern  => Sub::HandlesVia::XS::SHOULD_RETURN_TRUE,
	} );

	my $x = "";
	is( Local::test_7( $x ), undef );
	is( Local::test_8( $x ), !!0 );
	is( Local::test_9( $x ), !!1 );
	is( Local::test_9( $x ), !!1 );
	is( Local::test_8( $x ), !!0 );
	is( $x, "xyzzy" );
};

done_testing;
