Skip to content

Commit

Permalink
Reformat messages to fit 78 char limit, add documentation, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusramberg committed Feb 23, 2013
1 parent 171cb51 commit 1bee554
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 24 deletions.
8 changes: 4 additions & 4 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -61,15 +61,15 @@ sub app_url {

# DEPRECATED in Rainbow!
sub build_form_tx {
warn_deprecated
"Mojo::UserAgent->build_form_tx is DEPRECATED in favor of Mojo::UserAgent->build_tx!!!";
warn_deprecated "Mojo::UserAgent->build_form_tx is DEPRECATED "
. "in favor of Mojo::UserAgent->build_tx!!!";
shift->transactor->form(@_);
}

# DEPRECATED in Rainbow!
sub build_json_tx {
warn_deprecated
"Mojo::UserAgent->build_json_tx is DEPRECATED in favor of Mojo::UserAgent->build_tx!!!";
warn_deprecated "Mojo::UserAgent->build_json_tx is DEPRECATED "
. "in favor of Mojo::UserAgent->build_tx!!!";
shift->transactor->json(@_);
}

Expand Down
18 changes: 13 additions & 5 deletions lib/Mojo/Util.pm
Expand Up @@ -330,11 +330,6 @@ sub xml_escape {
return $string;
}

sub warn_deprecated {
my $deprecation=shift;
local $Carp::CarpLevel=1;
$ENV{MOJO_FATAL_DEPRECATIONS} ? croak($deprecation) : carp($deprecation);
}

sub xor_encode {
my ($input, $key) = @_;
Expand Down Expand Up @@ -399,6 +394,12 @@ sub _hmac {
return unpack 'H*', $hash->($opad . $hash->($ipad . $string));
}

sub warn_deprecated {
my $deprecation = shift;
local $Carp::CarpLevel = 1;
$ENV{MOJO_FATAL_DEPRECATIONS} ? croak($deprecation) : carp($deprecation);
}

1;

=head1 NAME
Expand Down Expand Up @@ -627,6 +628,13 @@ C<^A-Za-z0-9\-._~>.
Decode percent encoded characters in string.
=head2 warn_deprecated
warn_deprecated "Mojo::foo DEPRECATED in favor of Mojo::bar";
Warn about deprecated feature from perspective of caller. Set environment
variable MOJO_FATAL_DEPRECATIONS to make deprecations fatal.
=head2 xml_escape
my $escaped = xml_escape $string;
Expand Down
7 changes: 3 additions & 4 deletions lib/Mojolicious.pm
Expand Up @@ -122,10 +122,9 @@ sub dispatch {

# DEPRECATED in Rainbow!
if ($plugins->has_subscribers('after_static_dispatch')) {
warn_deprecated
<<EOF and $plugins->emit_hook_reverse(after_static_dispatch => $c);
after_static_dispatch hook is DEPRECATED in favor of before_routes!!!
EOF
warn_deprecated "after_static_dispatch hook is DEPRECATED "
. "in favor of before_routes!!!"
and $plugins->emit_hook_reverse(after_static_dispatch => $c);
}

# Routes
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Commands.pm
Expand Up @@ -106,8 +106,8 @@ sub run {

# DEPRECATED in Rainbow!
sub start {
warn_deprecated
"Mojolicious::Commands->start is DEPRECATED in favor of Mojolicious::Commands->start_app!!!";
warn_deprecated "Mojolicious::Commands->start is DEPRECATED "
. "in favor of Mojolicious::Commands->start_app!!!";
my $self = shift;
return $self->start_app($ENV{MOJO_APP} => @_) if $ENV{MOJO_APP};
return $self->new->app->start(@_);
Expand Down
4 changes: 2 additions & 2 deletions lib/Mojolicious/Routes.pm
Expand Up @@ -81,8 +81,8 @@ sub lookup {

# DEPRECATED in Rainbow!
sub namespace {
warn_deprecated
"Mojolicious::Routes->namespace is DEPRECATED in favor of Mojolicious::Routes->namespaces!";
warn_deprecated "Mojolicious::Routes->namespace is DEPRECATED "
. "in favor of Mojolicious::Routes->namespaces!";
my $self = shift;
return $self->namespaces->[0] unless @_;
$self->namespaces->[0] = shift;
Expand Down
12 changes: 6 additions & 6 deletions lib/Test/Mojo.pm
Expand Up @@ -220,17 +220,17 @@ sub post_ok { shift->_request_ok(post => @_) }

# DEPRECATED in Rainbow!
sub post_form_ok {
warn_deprecated
"Test::Mojo->post_form_ok is DEPRECATED in favor of Test::Mojo->post_ok!!!";
warn_deprecated "Test::Mojo->post_form_ok is DEPRECATED "
. "in favor of Test::Mojo->post_ok!!!";
my ($self, $url) = (shift, shift);
my $tx = $self->tx($self->ua->post_form($url, @_))->tx;
return $self->_test('ok', $tx->is_finished, encode('UTF-8', "post $url"));
}

# DEPRECATED in Rainbow!
sub post_json_ok {
warn_deprecated
"Test::Mojo->post_json_ok is DEPRECATED in favor of Test::Mojo->post_ok!!!";
warn_deprecated "Test::Mojo->post_json_ok is DEPRECATED "
. "in favor of Test::Mojo->post_ok!!!";
my ($self, $url) = (shift, shift);
my $tx = $self->tx($self->ua->post_json($url, @_))->tx;
return $self->_test('ok', $tx->is_finished, encode('UTF-8', "post $url"));
Expand Down Expand Up @@ -375,8 +375,8 @@ sub _wait {

# DEPRECATED in Rainbow!
my $new = $self->{new} //= $wait;
warn_deprecated
"Testing WebSocket messages without Test::Mojo->message_ok is DEPRECATED!!!"
warn_deprecated "Testing WebSocket messages without "
. "Test::Mojo->message_ok is DEPRECATED!!!"
unless $new;
return $self->message if $new && !$wait;

Expand Down
23 changes: 22 additions & 1 deletion t/mojo/util.t
Expand Up @@ -10,7 +10,7 @@ use Mojo::Util
qw(decode encode get_line hmac_md5_sum hmac_sha1_sum html_unescape),
qw(md5_bytes md5_sum monkey_patch punycode_decode punycode_encode quote),
qw(squish trim unquote secure_compare sha1_bytes sha1_sum slurp spurt),
qw(url_escape url_unescape xml_escape xor_encode);
qw(url_escape url_unescape warn_deprecated xml_escape xor_encode);

# camelize
is camelize('foo_bar_baz'), 'FooBarBaz', 'right camelized result';
Expand Down Expand Up @@ -400,4 +400,25 @@ is MojoMonkeyTest::yin(), 'yin', 'right result';
ok !!MojoMonkeyTest->can('yang'), 'function "yang" exists';
is MojoMonkeyTest::yang(), 'yang', 'right result';

# warn_deprecated

{
my ($warn, $die) = @_;
local $SIG{__WARN__} = sub { $warn = shift; };
local $SIG{__DIE__} = sub { $die = shift; return undef; };
warn_deprecated("This warns from caller");
local $ENV{MOJO_FATAL_DEPRECATIONS} = 1;
eval { warn_deprecated("This dies from caller"); };
like(
$warn,
qr/This warns from caller at t\/mojo\/util.t line \d+/,
"warn message is right deprecation"
);
like(
$die,
qr/This dies from caller at t\/mojo\/util.t line \d+/,
"die message is right for deprecation"
);
}

done_testing();

0 comments on commit 1bee554

Please sign in to comment.