App-Test-Generator

 view release on metacpan or  search on metacpan

t/function.t  view on Meta::CPAN


	# Property checks must appear in the generated code
	like($code, qr/\$result >= 0/, 'property checks present');

	# Trial count must be emitted with the property
	like($code, qr/trials\s*=>\s*100/, 'trial count emitted');

	# should_die property checks $died not property_checks
	my $die_props = [{
		name            => 'die_prop',
		generator_spec  => '$x <- Int',
		call_code       => 'dangerous($x)',
		property_checks => '',
		should_die      => 1,
		should_warn     => 0,
		trials          => 50,
	}];
	my $die_code = $fn->($die_props);
	like($die_code, qr/\$died/, 'should_die uses $died check');

	done_testing();
};

# ==================================================================
# _load_schema (integration — requires a real temp file)
# --------------------------------------------------
# Tests for the schema file loader
# ==================================================================
subtest '_load_schema' => sub {
	my $fn = \&App::Test::Generator::_load_schema;

	# Undef file path croaks with a Usage message
	throws_ok { $fn->(undef) } qr/Usage/, 'undef path croaks';

	# Empty file path croaks before touching the filesystem
	throws_ok { $fn->($EMPTY_STRING) } qr/empty filename/, 'empty path croaks';

	# Non-existent file path croaks with a filesystem error
	throws_ok { $fn->('/no/such/file.yml') } qr/./, 'missing file croaks';

	# A valid YAML file loads successfully and returns a hashref
	my ($fh, $tmpfile) = tempfile(SUFFIX => '.yml', UNLINK => 1);
	print {$fh} "function: test\nmodule: Foo\n";
	close $fh;

	my $schema = $fn->($tmpfile);
	is(ref($schema), 'HASH', 'valid YAML returns hashref');
	is($schema->{function}, 'test', 'function key loaded correctly');
	is($schema->{module},   'Foo',  'module key loaded correctly');

	# The _source key must be injected with the originating file path
	ok(exists $schema->{_source}, '_source key injected');
	is($schema->{_source}, $tmpfile, '_source contains the file path');

	done_testing();
};

# ==================================================================
# generate (smoke tests)
# --------------------------------------------------
# End-to-end tests verifying that generate() produces
# a file that compiles and contains expected markers
# ==================================================================
subtest 'generate smoke' => sub {
	my $dir = tempdir(CLEANUP => 1);

	# Write a minimal valid schema to a temp file
	my ($schema_fh, $schema_file) = tempfile(
		DIR    => $dir,
		SUFFIX => '.yml',
		UNLINK => 1,
	);
	print {$schema_fh} <<'YAML';
function: test_func
module: builtin

input:
  type: string

output:
  type: string
YAML
	close $schema_fh;

	# Generate the test file and confirm it is non-empty
	my ($out_fh, $out_file) = tempfile(
		DIR    => $dir,
		SUFFIX => '.t',
		UNLINK => 1,
	);
	close $out_fh;

	lives_ok {
		App::Test::Generator->generate($schema_file, $out_file)
	} 'generate() lives for valid schema';

	ok(-s $out_file, 'output file is non-empty');

	# The generated file must compile cleanly under the current perl
	my $result = system($^X, '-c', $out_file);
	is($result, 0, 'generated file compiles');

	# Check for essential structural markers in the generated output
	open my $fh, '<', $out_file or die $!;
	my $content = do { local $/; <$fh> };
	close $fh;
	like($content, qr/diag\(/,       'generated file has diag line');
	like($content, qr/test_func/,    'function name present in output');
	like($content, qr/done_testing/, 'done_testing present in output');

	done_testing();
};

subtest 'generate croaks for missing schema file' => sub {
	# A non-existent schema file must croak rather than silently
	# producing empty or broken output
	throws_ok {
		App::Test::Generator->generate('/no/such/schema.yml', '/tmp/out.t')
	} qr/./, 'missing schema file croaks';

	done_testing();



( run in 0.582 second using v1.01-cache-2.11-cpan-e1769b4cff6 )