Context-Singleton

 view release on metacpan or  search on metacpan

t/Context-Singleton-Frame.t  view on Meta::CPAN

	};

	test_method "returns undef if depth is greater then frame depth" => do {
		my $root = $CLASS->new;
		my $child = $root->new->new;

		with_depth => 1,
		object => $root,
		expect => undef,
	};

	test_method "returns expected frame" => do {
		my $root = $CLASS->new;
		my $expect = $root->new;         # depth 1
		my $child = $expect->new->new;

		with_depth => 1,
		object => $child,
		expect => $expect,
	};

	return;
};

describe_method proclaim   => [qw[ rule value ]] => as {
	shared->frame_class = 'Sample::Context::Singleton::Frame::__::Basic';

	plan tests => 7;

	test_method_proclaim "should proclaim() rule without known builder" => do {
		object      => build_frame,
		with_rule   => 'unknown',
		with_value  => 'foo',
	};

	test_method_proclaim "should proclaim() rule with known builder" => do {
		object      => build_frame,
		with_rule   => 'constant',
		with_value  => 'foo',
	};

	test_method_proclaim "should throw when rule is already proclaim()-ed" => do {
		object      => build_frame (some_rule => 'bar'),
		with_rule   => 'some_rule',
		with_value  => 'foo',
		throws      => 'Context::Singleton::Exception::Deduced',
	};

	test_method_proclaim "should throw when rule is already deduce()-ed" => do {
		my $object = build_frame;
		$object->deduce ('constant');

		object      => $object,
		with_rule   => 'constant',
		with_value  => 'foo',
		throws      => 'Context::Singleton::Exception::Deduced',
	};

	test_method_proclaim "should throw when rule is already deduce()-ed as dependency" => do {
		my $object = build_frame;
		$object->deduce ('cascaded');

		object      => $object,
		with_rule   => 'constant',
		with_value  => 'foo',
		throws      => 'Context::Singleton::Exception::Deduced',
	};

	test_method_proclaim "should proclaim() rule with known builder after its deduce() failed" => do {
		my $object = build_frame;
		$object->try_deduce ('with_deps');

		object      => $object,
		with_rule   => 'with_deps',
		with_value  => 'foo',
	};

	test_method_proclaim "should proclaim() rule if proclaim()-ed in parent frame" => do {
		my $parent = build_frame (some_rule => 'bar');

		object      => $parent->new,
		with_rule   => 'some_rule',
		with_value  => 'foo',
	};

	return;
};

describe_method is_deduced => [qw[ rule ]]       => as {
	shared->frame_class = 'Sample::Context::Singleton::Frame::__::Basic';

	plan tests => 8;

	should_not_be_deduced   'empty frame should not have any value deduced' => do {
		my $object = build_frame;

		object      => $object,
		with_rule   => 'Key',
	};

	should_not_be_deduced   'empty inherited frame should not have any value deduced' => do {
		my $object = build_frame build_frame;

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deduced       'predefined value should be deduced' => do {
		my $object = build_frame Key => 'Value';

		object      => $object,
		with_rule   => 'Key',
	};

	should_not_be_deduced   'inherited value should not be deduced' => do {
		my $object = build_frame build_frame Key => 'Value';

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deduced       'after proclaim value should be deduced' => do {
		my $object = build_frame;
		$object->proclaim (Key => 'Value');

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deduced       'after deduce() should be deduced' => do {
		my $object = build_frame;
		$object->deduce ('constant');

		object     => $object,
		with_rule  => 'constant',
	};

	should_be_deduced       'after cascaded deduce() should be deduced' => do {
		my $object = build_frame;
		$object->deduce ('cascaded');

		object     => $object,
		with_rule  => 'constant',
	};

	should_not_be_deduced   'after unsuccessful cascaded deduce() should not be deduced' => do {
		my $object = build_frame;
		$object->try_deduce ('with_multi_deps');

		object     => $object,
		with_rule  => 'constant',
	};

	return;
};

describe_method is_deducible => [qw[ rule ]]     => as {
	shared->frame_class = 'Sample::Context::Singleton::Frame::__::Basic';

	plan tests => 7;

	should_not_be_deducible 'empty frame should not have any value deducible' => do {
		my $object = build_frame;

		object      => $object,
		with_rule   => 'Key',
	};

	should_not_be_deducible 'empty inherited frame should not have any value deducible' => do {
		my $object = build_frame build_frame;

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deducible     'predefined value should be deducible' => do {
		my $object = build_frame Key => 'Value';

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deducible     'inherited value should be deducible' => do {
		my $object = build_frame build_frame Key => 'Value';

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deducible     'after proclaim value should be deducible' => do {
		my $object = build_frame;
		$object->proclaim (Key => 'Value');

		object      => $object,
		with_rule   => 'Key',
	};

	should_be_deducible     'after deduce() should be deducible' => do {
		my $object = build_frame;
		$object->deduce ('constant');

		object      => $object,
		with_rule   => 'constant',
	};

	should_be_deducible     'after cascaded deduce() should be deducible' => do {
		my $object = build_frame;
		$object->deduce ('cascaded');

		object      => $object,
		with_rule   => 'constant',
	};

	return;
};

describe_method 'deduce' => [qw[ rule ]]         => as {
	shared->frame_class = 'Sample::Context::Singleton::Frame::__::Basic';

	plan tests => 12;

	should_not_deduce   'should not deduce value without builder' => do {
		my $object = build_frame;

		object      => $object,
		with_rule   => 'unknown',
	};

	should_deduce       'should deduce value without dependencies' => do {
		my $object = build_frame;

		object      => $object,
		with_rule   => 'constant',
		expect      => 'value-42',
	};

	should_deduce       'should deduce value with dependencies with default values' => do {
		my $object = build_frame;

		object      => $object,
		with_rule   => 'with_default',
		expect      => 'with_default:some:value-42',
	};

	should_deduce       'should deduce value with dependencies and default values redefined' => do {
		my $object = build_frame (
			constant => 24,
			unknown  => 'another-value',
		);

		object      => $object,
		with_rule   => 'with_default',
		expect      => 'with_default:another-value:24',
	};

	should_deduce       'should deduce value with dependencies and default values redefined in parent' => do {
		my $parent = build_frame (constant => 24);
		my $object = build_frame ($parent);

		object      => $object,
		with_rule   => 'with_default',
		expect      => 'with_default:some:24',
	};

	should_deduce       'should deduce value with dependencies redefined in current frame' => do {
		my $parent = build_frame (constant => 24);
		my $object = build_frame ($parent, constant => 242);



( run in 0.538 second using v1.01-cache-2.11-cpan-39bf76dae61 )