1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    require './test.pl';
6    set_up_inc('../lib');
7    skip_all_if_miniperl("no dynamic loading on miniperl, no Tie::Hash::NamedCapture");
8}
9
10# Do a basic test on all the tied methods of Tie::Hash::NamedCapture
11
12plan(tests => 37);
13
14# PL_curpm->paren_names can be a null pointer. See that this succeeds anyway.
15'x' =~ /(.)/;
16() = %+;
17pass( 'still alive' );
18
19"hlagh" =~ /
20    (?<a>.)
21    (?<b>.)
22    (?<a>.)
23    .*
24    (?<e>$)
25/x;
26
27# FETCH
28is($+{a}, "h", "FETCH");
29is($+{b}, "l", "FETCH");
30is($-{a}[0], "h", "FETCH");
31is($-{a}[1], "a", "FETCH");
32
33# STORE
34eval { $+{a} = "yon" };
35like($@, qr/read-only/, "STORE");
36
37# DELETE
38eval { delete $+{a} };
39like($@, qr/read-only/, "DELETE");
40
41# CLEAR
42eval { %+ = () };
43like($@, qr/read-only/, "CLEAR");
44
45# EXISTS
46ok(exists $+{e}, "EXISTS");
47ok(!exists $+{d}, "EXISTS");
48
49# FIRSTKEY/NEXTKEY
50is(join('|', sort keys %+), "a|b|e", "FIRSTKEY/NEXTKEY");
51
52# SCALAR
53is(scalar(%+), 3, "SCALAR");
54is(scalar(%-), 3, "SCALAR");
55
56# Abuse all methods with undef as the first argument (RT #71828 and then some):
57
58is(Tie::Hash::NamedCapture::FETCH(undef, undef), undef, 'FETCH with undef');
59eval {Tie::Hash::NamedCapture::STORE(undef, undef, undef)};
60like($@, qr/Modification of a read-only value attempted/, 'STORE with undef');
61eval {Tie::Hash::NamedCapture::DELETE(undef, undef)};
62like($@, , qr/Modification of a read-only value attempted/,
63     'DELETE with undef');
64eval {Tie::Hash::NamedCapture::CLEAR(undef)};
65like($@, qr/Modification of a read-only value attempted/, 'CLEAR with undef');
66is(Tie::Hash::NamedCapture::EXISTS(undef, undef), undef, 'EXISTS with undef');
67is(Tie::Hash::NamedCapture::FIRSTKEY(undef), undef, 'FIRSTKEY with undef');
68is(Tie::Hash::NamedCapture::NEXTKEY(undef, undef), undef, 'NEXTKEY with undef');
69is(Tie::Hash::NamedCapture::SCALAR(undef), undef, 'SCALAR with undef');
70
71my $obj = tied %+;
72foreach ([FETCH => '$key'],
73	 [STORE => '$key, $value'],
74	 [DELETE => '$key'],
75	 [CLEAR => ''],
76	 [EXISTS => '$key'],
77	 [FIRSTKEY => ''],
78	 [NEXTKEY => '$lastkey'],
79	 [SCALAR => ''],
80	) {
81    my ($method, $error) = @$_;
82
83    is(eval {$obj->$method(0..3); 1}, undef, "$method with undef");
84    like($@, qr/Usage: Tie::Hash::NamedCapture::$method\(\Q$error\E\)/,
85	 "usage method for $method");
86}
87