6.23. Multiple expected values

Someone asked me this question:

Q: Is it possible instead of is(foo(), 42) to have a fixed set of expected values ?

A: With is there is no such option but you have a number of solutions anyway.

Example 6-16. examples/intro/multiple_choice.t

#!/usr/bin/perl
use strict;
use warnings;

# run this script several times to see the (random) error message

use Test::More tests => 2;

like( foo(), qr/^(23|42|68)$/ );
ok( grep { foo() eq $_ } ( 23, 42, 68 ), "name" );

sub foo {
    return ( ( 23, 42, 68, 100, 200 )[ rand(5) ] );
}
$ perl multiple_choice.t 
not ok 1
#     Failed test (multiple_choice.t at line 4)
#                   '100'
#     doesn't match '(?-xism:^(23|42|68)$)'
not ok 2
#     Failed test (multiple_choice.t at line 5)
1..2
# Looks like you failed 2 tests of 2.

$ perl multiple_choice.t 
ok 1
ok 2
1..2