App-MtAws

 view release on metacpan or  search on metacpan

t/unit/config_engine_new.t  view on Meta::CPAN

			ok $res eq 'myoption';
			cmp_deeply Context->{options}->{myoption}, { name => 'myoption', value => 42, source => 'set', seen => 1 };
		}
	};
};

describe "error" => sub {
	it "should work" => sub {
		localize sub {
			error 'myerror';
			cmp_deeply Context->{errors}, ['myerror'];
		}
	};
	it "should push errors to stack" => sub {
		localize sub {
			error 'myerror';
			error 'myerror2';
			cmp_deeply Context->{errors}, ['myerror', 'myerror2'];
		}
	};
};

describe "warning" => sub {
	it "should work" => sub {
		localize sub {
			warning 'myerror';
			cmp_deeply Context->{warnings}, ['myerror'];
		}
	};
	it "should push warnings to stack" => sub {
		localize sub {
			warning 'mywarning';
			warning 'mywarning2';
			cmp_deeply Context->{warnings}, ['mywarning', 'mywarning2'];
		}
	};
};

describe "error to message" => sub {
	sub error_to_message { &App::MtAws::ConfigEngine::error_to_message };
	
	it "should work without format" => sub {
		ok error_to_message("option is mandatory") eq "option is mandatory";
	};
	it "should work without format with params" => sub {
		ok error_to_message("option is mandatory", a => 1) eq "option is mandatory";
	};
	it "should work with one param" => sub {
		ok error_to_message("%a% is mandatory", a => 42) eq "42 is mandatory";
	};
	it "should work with two params" => sub {
		ok error_to_message("%a% and %b%", a => 1, b => 2) eq "1 and 2";
	};
	it "should work with option" => sub {
		ok error_to_message("%a% and %option b%", a => 'x', b => 'y') eq 'x and "--y"';
		ok error_to_message("%option a% and %option b%", a => 'x', b => 'y') eq '"--x" and "--y"';
		ok error_to_message("%option a% and %b%", a => 'x', b => 'y') eq '"--x" and y';
	};
	it "should work with sprintf formats" => sub {
		ok error_to_message("%a% and %04d b%", a => 'x', b => 10) eq 'x and 0010';
		ok error_to_message("%04d a% and %06d b%", a => '42', b => '24') eq '0042 and 000024';
		ok error_to_message("%04d a% and %b%", a => 42, b => 'y') eq '0042 and y';
	};
	it "should work with alpa, numbers and underscore" => sub {
		ok error_to_message("%a_42% is mandatory", a_42 => 'abc') eq "abc is mandatory";
		ok error_to_message("%option a_42% is mandatory", a_42 => 'abc') eq '"--abc" is mandatory';
		ok error_to_message("%option   a_42% is mandatory", a_42 => 'abc') eq '"--abc" is mandatory';
	};
	it "should confess is variable missing" => sub {
		ok ! defined eval { error_to_message("%a% is mandatory", b => 'abc'); 1 };
		ok ! defined eval { error_to_message("%option a% is mandatory", b => 'abc'); 1 };
		ok ! defined eval { error_to_message("%04d a% is mandatory", b => 42); 1 };
	};
	it "should not confess is there is extra variable" => sub {
		ok defined eval { error_to_message("%a% is mandatory", a => 1, b => 'abc'); 1 };
		ok defined eval { error_to_message("%option a% is mandatory", a => 1, b => 'abc'); 1 };
		ok defined eval { error_to_message("%04d a% is mandatory", a => 1, b => 'abc'); 1 };
	};
};

describe "errors_or_warnings_to_messages" => sub {
	it "should work without params when format defined" => sub {
		my $c = create_engine();
		$c->{messages}->{a} = { format => 'xyz' };
		cmp_deeply [$c->errors_or_warnings_to_messages([{format => 'a'}])], ['xyz'];
	};
	it "should work without params when format not defined" => sub {
		my $c = create_engine();
		cmp_deeply [$c->errors_or_warnings_to_messages(['xyz'])], ['xyz'];
	};
	it "should work with params when format defined" => sub {
		my $c = create_engine();
		$c->{messages}->{a} = { format => 'xyz' };
		App::MtAws::ConfigEngine->expects("error_to_message")->returns(sub{shift;{sort @_}})->once; # kinda hack, we sort hash as array
		cmp_deeply [$c->errors_or_warnings_to_messages([{ format => 'a', x => 'y'}])], [sort (format => 'a', x => 'y')];
	};
	it "should work list" => sub {
		my $c = create_engine();
		$c->{messages}->{a} = {format => 'xyz' };
		cmp_deeply [$c->errors_or_warnings_to_messages([{format => 'a'}, 'abc'])], ['xyz', 'abc'];
	};
	it "should return undef" => sub {
		my $c = create_engine();
		ok ! defined $c->errors_or_warnings_to_messages(undef);
		ok ! defined $c->errors_or_warnings_to_messages();
	};
};

describe "arrayref_or_undef" => sub {
	it "should work" => sub {
		cmp_deeply App::MtAws::ConfigEngine::arrayref_or_undef([1]), [1];
	};
	it "should work with array of undef" => sub {
		cmp_deeply App::MtAws::ConfigEngine::arrayref_or_undef([undef]), [undef];
	};
	it "should work with empty array" => sub {
		ok ! defined App::MtAws::ConfigEngine::arrayref_or_undef([]);
	};
	it "should work with undef" => sub {
		ok ! defined App::MtAws::ConfigEngine::arrayref_or_undef(undef);
	};



( run in 0.540 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )