t = SvTYPE(SvRV(val));
deref = 1;
}
+ } else if (SvROK(sv)) {
+ croak("Invalid %s reference as the localization target",
+ sv_reftype(SvRV(sv), 0));
} else {
STRLEN len, l;
const char *p = SvPV_const(sv, len), *s;
use strict;
use warnings;
-use Test::More tests => 50;
+use Test::More tests => 50 + 4;
use Scope::Upper qw/localize UP HERE/;
}
is foo(), 8, 'localize "&foo", sub { 8 } => UP [ok]';
}
+
+# Invalid
+
+sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }
+
+{
+ eval { localize \1, 0 => HERE };
+ like $@, invalid_ref('SCALAR'), 'invalid localize \1, 0 => HERE';
+}
+
+{
+ eval { localize [ ], 0 => HERE };
+ like $@, invalid_ref('ARRAY'), 'invalid localize [ ], 0 => HERE';
+}
+
+{
+ eval { localize { }, 0 => HERE };
+ like $@, invalid_ref('HASH'), 'invalid localize { }, 0 => HERE';
+}
+
+{
+ eval { localize sub { }, 0 => HERE };
+ like $@, invalid_ref('CODE'), 'invalid localize sub { }, 0 => HERE';
+}
use strict;
use warnings;
-use Test::More tests => 25 + 8;
+use Test::More tests => 25 + 12;
use Scope::Upper qw/localize_elem UP HERE/;
'localize_elem "%nonexistent", "a", 13 => HERE [end]';
}
+# Invalid
+
my $invalid_glob = qr/^Can't infer the element localization type from a glob and the value/;
my $invalid_type = qr/^Can't localize an element of something that isn't an array or a hash/;
eval { localize_elem *x, 0, *x };
like $@, $invalid_glob, 'invalid localize_elem *x, 0, *x';
}
+
+sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }
+
+{
+ eval { localize_elem \1, 0, 0 => HERE };
+ like $@, invalid_ref('SCALAR'), 'invalid localize_elem \1, 0, 0 => HERE';
+}
+
+{
+ eval { localize_elem [ ], 0, 0 => HERE };
+ like $@, invalid_ref('ARRAY'), 'invalid localize_elem [ ], 0, 0 => HERE';
+}
+
+{
+ eval { localize_elem { }, 0, 0 => HERE };
+ like $@, invalid_ref('HASH'), 'invalid localize_elem { }, 0, 0 => HERE';
+}
+
+{
+ eval { localize_elem sub { }, 0, 0 => HERE };
+ like $@, invalid_ref('CODE'), 'invalid localize_elem sub { }, 0, 0 => HERE';
+}
use strict;
use warnings;
-use Test::More tests => 44;
+use Test::More tests => 44 + 4;
use Scope::Upper qw/localize_delete UP HERE/;
}
is x(), 1, 'localize_delete *x, anything => HERE [end 1]';
is $x, 1, 'localize_delete *x, anything => HERE [end 2]';
+
+# Invalid
+
+sub invalid_ref { qr/^Invalid \Q$_[0]\E reference as the localization target/ }
+
+{
+ eval { localize_delete \1, 0 => HERE };
+ like $@, invalid_ref('SCALAR'), 'invalid localize_delete \1, 0 => HERE';
+}
+
+{
+ eval { localize_delete [ ], 0 => HERE };
+ like $@, invalid_ref('ARRAY'), 'invalid localize_delete [ ], 0 => HERE';
+}
+
+{
+ eval { localize_delete { }, 0 => HERE };
+ like $@, invalid_ref('HASH'), 'invalid localize_delete { }, 0 => HERE';
+}
+
+{
+ eval { localize_delete sub { }, 0 => HERE };
+ like $@, invalid_ref('CODE'), 'invalid localize_delete sub { }, 0 => HERE';
+}