App-Test-Generator
view release on metacpan or search on metacpan
lib/App/Test/Generator/SchemaExtractor.pm view on Meta::CPAN
"returns self", "method chaining").
=head3 Error Return Conventions
Analyzes how methods signal errors:
B<Pattern Detection:>
=over 4
=item * C<undef_on_error> - Explicit C<return undef if/unless condition>
=item * C<implicit_undef> - Bare C<return if/unless condition>
=item * C<empty_list> - C<return ()> for list context errors
=item * C<zero_on_error> - Returns 0/false for boolean error indication
=item * C<exception_handling> - Uses eval blocks with error checking
=back
B<Example Analysis:>
sub fetch_user {
my ($self, $id) = @_;
return undef unless $id; # undef_on_error
return undef if $id < 0; # undef_on_error
return $self->{users}{$id};
}
Results in:
_error_return: 'undef'
_success_failure_pattern: 1
_error_handling: {
undef_on_error: ['$id', '$id < 0']
}
B<Success/Failure Pattern:>
Methods that return different types for success vs. failure are flagged with
C<_success_failure_pattern>. Common patterns:
=over 4
=item * Returns value on success, undef on failure
=item * Returns true on success, false on failure
=item * Returns data on success, empty list on failure
=back
=head3 Success Indicator Detection
Methods that always return true (typically for side effects):
sub update_status {
my ($self, $status) = @_;
$self->{status} = $status;
return 1; # Success indicator
}
Sets C<_success_indicator> flag when method consistently returns 1.
=head3 Schema Output
Enhanced return analysis adds these fields to method schemas:
output:
type: boolean # Inferred return type
_context_aware: 1 # Uses wantarray
_list_context:
type: array
_scalar_context:
type: integer
_returns_self: 1 # Returns $self
_void_context: 1 # No meaningful return
_success_indicator: 1 # Always returns true
_error_return: undef # How errors are signaled
_success_failure_pattern: 1 # Mixed return types
_error_handling: # Detailed error patterns
undef_on_error: [...]
exception_handling: 1
This comprehensive analysis enables:
=over 4
=item * Better test generation (testing both contexts, error paths)
=item * Documentation generation (clear error conventions)
=item * API design validation (consistent error handling)
=item * Contract specification (precise return behavior)
=back
=head2 Example
For a method like:
sub connect {
my ($self, $host, $port, $ssl, $file, $content) = @_;
die if $file && $content; # mutually exclusive
die unless $host || $file; # required group
die "Port requires host" if $port && !$host; # dependency
die if $ssl && $port != 443; # value constraint
# ... connection logic
}
The extractor generates:
relationships:
- type: mutually_exclusive
( run in 0.634 second using v1.01-cache-2.11-cpan-df04353d9ac )