App-Test-Generator

 view release on metacpan or  search on metacpan

t/SchemaExtractor_function.t  view on Meta::CPAN

		if($block) {
			my $result = $e->_extract_schema_hash_from_block($block);
			ok(1, '_extract_schema_hash_from_block completed without crash');
		} else {
			ok(1, 'no block found — skipping extraction test');
		}
	}
};

subtest '_extract_schema_hash_from_block() returns undef for undef input' => sub {
	my $e      = _extractor();
	my $result = $e->_extract_schema_hash_from_block(undef);
	ok(!defined $result, 'undef input -> undef returned');
};

subtest '_extract_schema_hash_from_block() returns undef for non-PPI input' => sub {
	my $e      = _extractor();
	my $result = $e->_extract_schema_hash_from_block('not a ppi node');
	ok(!defined $result, 'non-PPI input -> undef returned');
};

# ==================================================================
# _extract_pvs_schema
# ==================================================================

subtest '_extract_pvs_schema() returns undef for code with no validate_strict' => sub {
	my $e = _extractor();
	my $result = $e->_extract_pvs_schema('sub foo { return 1; }');
	ok(!defined $result, 'no validate_strict -> undef');
};

subtest '_extract_pvs_schema() returns undef for empty code' => sub {
	my $e      = _extractor();
	my $result = $e->_extract_pvs_schema('');
	ok(!defined $result, 'empty code -> undef');
};

subtest '_extract_pvs_schema() detects validate_strict call in method body' => sub {
	my $e = _extractor();
	my $code = <<'CODE';
sub foo {
	my $params = Params::Validate::Strict::validate_strict(
		args   => \@_,
		schema => {
			name => { type => 'string', optional => 0 },
			age  => { type => 'integer', optional => 1 },
		}
	);
}
CODE
	my $result = $e->_extract_pvs_schema($code);
	# Either returns a schema hashref or undef — just check no crash
	ok(1, '_extract_pvs_schema completed without crash on validate_strict code');
	if(defined $result) {
		is(ref($result), 'HASH', 'returned value is a hashref when defined');
	}
};

subtest '_extract_pvs_schema() returns hashref with input key when schema detected' => sub {
	my $e = _extractor();
	# Use the bare function name form that the extractor looks for
	my $code = <<'CODE';
sub my_method {
	my $params = validate_strict({
		name => { type => 'string', optional => 0 }
	});
}
CODE
	my $result = $e->_extract_pvs_schema($code);
	if(defined $result) {
		is(ref($result), 'HASH', 'returns hashref');
		ok(exists $result->{input} || exists $result->{style},
			'has input or style key');
	} else {
		ok(1, 'returned undef — schema format not parseable by extractor');
	}
};

# ==================================================================
# _extract_pv_schema
# ==================================================================

subtest '_extract_pv_schema() returns undef for code with no validate call' => sub {
	my $e      = _extractor();
	my $result = $e->_extract_pv_schema('sub foo { return 1; }');
	ok(!defined $result, 'no validate -> undef');
};

subtest '_extract_pv_schema() returns undef for empty code' => sub {
	my $e      = _extractor();
	my $result = $e->_extract_pv_schema('');
	ok(!defined $result, 'empty code -> undef');
};

subtest '_extract_pv_schema() detects Params::Validate validate call' => sub {
	my $e    = _extractor();
	my $code = <<'CODE';
sub foo {
	my %args = validate(\@_, {
		name => { type => SCALAR },
		age  => { type => SCALAR, optional => 1 },
	});
}
CODE
	my $result = $e->_extract_pv_schema($code);
	ok(1, '_extract_pv_schema completed without crash on validate code');
	if(defined $result) {
		is(ref($result), 'HASH', 'returns hashref when schema detected');
	}
};

subtest '_extract_pv_schema() handles fully-qualified Params::Validate::validate' => sub {
	my $e    = _extractor();
	my $code = <<'CODE';
sub foo {
	my %args = Params::Validate::validate(\@_, {
		x => { type => SCALAR },
	});
}
CODE
	my $result = $e->_extract_pv_schema($code);



( run in 0.794 second using v1.01-cache-2.11-cpan-6aa56a78535 )