diff options
author | Paul "LeoNerd" Evans <leonerd@leonerd.org.uk> | 2022-03-14 11:13:15 +0000 |
---|---|---|
committer | Paul Evans <leonerd@leonerd.org.uk> | 2022-03-14 14:17:41 +0000 |
commit | 10bccff2c8e43fb1dd67e3bdb834a159fd1de574 (patch) | |
tree | d3c930f0b4d56648a13d370f8f4b8de7e3275888 /lib/builtin.t | |
parent | 2302ea7b9721ad8468f04645825a700b19a9d477 (diff) | |
download | perl-10bccff2c8e43fb1dd67e3bdb834a159fd1de574.tar.gz |
An initial implementation of builtin::indexed
* Implementation, unit tests, documentation
Diffstat (limited to 'lib/builtin.t')
-rw-r--r-- | lib/builtin.t | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lib/builtin.t b/lib/builtin.t index b56e9b7516..f95e69f139 100644 --- a/lib/builtin.t +++ b/lib/builtin.t @@ -176,4 +176,47 @@ package FetchStoreCounter { cmp_ok($val, $_, !1, "false is equivalent to !1 by $_") for qw( eq == ); } +# indexed +{ + use builtin qw( indexed ); + + # We don't have Test::More's is_deeply here + + ok(eq_array([indexed], [] ), + 'indexed on empty list'); + + ok(eq_array([indexed "A"], [0, "A"] ), + 'indexed on singleton list'); + + ok(eq_array([indexed "X" .. "Z"], [0, "X", 1, "Y", 2, "Z"] ), + 'indexed on 3-item list'); + + my @orig = (1..3); + $_++ for indexed @orig; + ok(eq_array(\@orig, [1 .. 3]), 'indexed copies values, does not alias'); + + { + no warnings 'experimental::for_list'; + + my $ok = 1; + foreach my ($len, $s) (indexed "", "x", "xx") { + length($s) == $len or undef $ok; + } + ok($ok, 'indexed operates nicely with multivar foreach'); + } + + { + my %hash = indexed "a" .. "e"; + ok(eq_hash(\%hash, { 0 => "a", 1 => "b", 2 => "c", 3 => "d", 4 => "e" }), + 'indexed can be used to create hashes'); + } + + { + no warnings 'scalar'; + + my $count = indexed 'i', 'ii', 'iii', 'iv'; + is($count, 8, 'indexed in scalar context yields size of list it would return'); + } +} + done_testing(); |