App-Test-Generator

 view release on metacpan or  search on metacpan

t/Mutation-BooleanNegation.t  view on Meta::CPAN

	# return if condition
	@mutants = $m->mutate(_doc('sub foo { return if $x; }'));
	is(scalar @mutants, 0, 'return if produces no mutant');

	# return while condition
	@mutants = $m->mutate(_doc('sub foo { return while $x; }'));
	is(scalar @mutants, 0, 'return while produces no mutant');

	# return until condition
	@mutants = $m->mutate(_doc('sub foo { return until $x; }'));
	is(scalar @mutants, 0, 'return until produces no mutant');

	done_testing();
};

# ==================================================================
# mutate -- chained/dereferencing return expressions are negated
# in full, not just their leading token
# --------------------------------------------------
# Regression: $ret->schild(1) only ever captured the first token of
# a multi-token return expression (e.g. just $self out of
# $self->{value}), so the transform wrapped only that leading token
# in !(...) and left the rest of the chain dangling outside the
# parens, producing the broken mutant 'return !($self)->{value};'
# (dies at runtime: "Can't use string ... as a HASH ref").
# ==================================================================
subtest 'mutate: chained return expression is negated as a whole' => sub {
	my $m = _mutation();

	my $src     = 'sub foo { return $self->{value}; }';
	my @mutants = $m->mutate(_doc($src));
	is(scalar @mutants, 1, 'one mutant for chained property return');

	my $copy = _doc($src);
	$mutants[0]->transform->($copy);
	is($copy->serialize, 'sub foo { return !($self->{value}); }',
		'whole $self->{value} expression wrapped in !(...), not just $self');

	# Same chain, but with a postfix conditional appended
	$src     = 'sub foo { return $self->{value} if $x; }';
	@mutants = $m->mutate(_doc($src));
	is(scalar @mutants, 1, 'one mutant for chained property return with postfix conditional');

	$copy = _doc($src);
	$mutants[0]->transform->($copy);
	is($copy->serialize, 'sub foo { return !($self->{value}) if $x; }',
		'whole $self->{value} expression wrapped in !(...), postfix conditional preserved');

	done_testing();
};

# ==================================================================
# NOTE: API improvement opportunity
# --------------------------------------------------
# The mutate() method currently returns a flat list which is copied
# onto the caller's stack. For large documents with many return
# statements this wastes memory. The method should return an arrayref:
#
#   return \@mutants;
#
# and callers updated to dereference:
#
#   my @mutants = @{ $mutation->mutate($doc) };
#
# This applies to all Mutation::* subclasses for consistency.
# The same change should be made to ReturnUndef, NumericBoundary,
# and ConditionalInversion at the same time.
# ==================================================================

done_testing();



( run in 0.443 second using v1.01-cache-2.11-cpan-bbe5e583499 )