App-Test-Generator

 view release on metacpan or  search on metacpan

t/Planner-submodules_unit.t  view on Meta::CPAN

subtest 'Isolation::plan() unknown purity_level -> isolated_block' => sub {
	my $p      = App::Test::Generator::Planner::Isolation->new();
	my $schema = {
		net => { _analysis => { side_effects => { purity_level => 'network' } } },
	};
	my $result = $p->plan($schema, { net => 1 });
	is($result->{net}{fixture}, 'isolated_block', 'unknown -> isolated_block');
};

subtest 'Isolation::plan() env dependency passed through' => sub {
	my $p   = App::Test::Generator::Planner::Isolation->new();
	my $env = { HOME => '/tmp' };
	my $schema = {
		foo => { _analysis => { dependencies => { env => $env } } },
	};
	my $result = $p->plan($schema, { foo => 1 });
	is_deeply($result->{foo}{env}, $env, 'env hashref passed through');
};

subtest 'Isolation::plan() filesystem dependency passed through' => sub {
	my $p  = App::Test::Generator::Planner::Isolation->new();
	my $fs = { reads => ['/etc/hosts'] };
	my $schema = {
		foo => { _analysis => { dependencies => { filesystem => $fs } } },
	};
	my $result = $p->plan($schema, { foo => 1 });
	is_deeply($result->{foo}{filesystem}, $fs, 'filesystem hashref passed through');
};

subtest 'Isolation::plan() time dependency sets time flag to 1' => sub {
	my $p      = App::Test::Generator::Planner::Isolation->new();
	my $schema = {
		foo => { _analysis => { dependencies => { time => 1 } } },
	};
	my $result = $p->plan($schema, { foo => 1 });
	is($result->{foo}{time}, 1, 'time flag set');
};

subtest 'Isolation::plan() network dependency sets network flag to 1' => sub {
	my $p      = App::Test::Generator::Planner::Isolation->new();
	my $schema = {
		foo => { _analysis => { dependencies => { network => 1 } } },
	};
	my $result = $p->plan($schema, { foo => 1 });
	is($result->{foo}{network}, 1, 'network flag set');
};

subtest 'Isolation::plan() omits optional keys when dependencies absent' => sub {
	my $p      = App::Test::Generator::Planner::Isolation->new();
	my $result = $p->plan({}, { foo => 1 });
	ok(!exists $result->{foo}{env},        'env absent when no dependency');
	ok(!exists $result->{foo}{filesystem}, 'filesystem absent when no dependency');
	ok(!exists $result->{foo}{time},       'time absent when no dependency');
	ok(!exists $result->{foo}{network},    'network absent when no dependency');
};

subtest 'Isolation::plan() only plans methods present in strategy' => sub {
	my $p = new_ok('App::Test::Generator::Planner::Isolation');
	my $schema = {
		in_strategy  => { _analysis => { side_effects => { purity_level => 'pure' } } },
		not_strategy => { _analysis => { side_effects => { purity_level => 'pure' } } },
	};
	my $result = $p->plan($schema, { in_strategy => 1 });
	ok( exists $result->{in_strategy},  'in_strategy present');
	ok(!exists $result->{not_strategy}, 'not_strategy absent');
};

subtest 'Mock::plan() returns a hashref' => sub {
	my $p = App::Test::Generator::Planner::Mock->new;
	my $result = $p->plan({});
	is(ref($result), 'HASH', 'plan() returns a hashref not undef');
};

subtest 'Mock::plan() assigns mock_system for calls_external' => sub {
	my $p = App::Test::Generator::Planner::Mock->new;
	my $result = $p->plan({
		my_method => { _analysis => { side_effects => { calls_external => 1 } } }
	});
	is($result->{my_method}, 'mock_system',
		'calls_external -> exactly mock_system');
};

subtest 'Mock::plan() assigns capture_io for performs_io' => sub {
	my $p = App::Test::Generator::Planner::Mock->new;
	my $result = $p->plan({
		my_method => { _analysis => { side_effects => { performs_io => 1 } } }
	});
	is($result->{my_method}, 'capture_io', 'performs_io -> exactly capture_io');
};

subtest 'Mock::plan() assigns both strategies when both side effects present' => sub {
	my $p = App::Test::Generator::Planner::Mock->new;
	my $result = $p->plan({
		my_method => { _analysis => { side_effects => {
			calls_external => 1,
			performs_io    => 1,
		} } }
	});
	is_deeply($result->{my_method}, ['mock_system', 'capture_io'],
		'both mock_system and capture_io applied when both present');
};

subtest 'Mock::plan() omits pure methods from result' => sub {
	my $p = App::Test::Generator::Planner::Mock->new;
	my $result = $p->plan({
		pure_method => { _analysis => { side_effects => {} } }
	});
	ok(!exists $result->{pure_method}, 'pure method omitted from plan');
};

subtest 'Mock::plan() return value is defined for non-empty schema' => sub {
	my $p = App::Test::Generator::Planner::Mock->new;
	my $result = $p->plan({
		m => { _analysis => { side_effects => { calls_external => 1 } } }
	});
	ok(defined $result, 'return value is defined');
	is(ref($result), 'HASH', 'return value is a hashref not undef');
};

done_testing();



( run in 1.266 second using v1.01-cache-2.11-cpan-9581c071862 )