Make sure that an enum's inline env works when unioned with another enum (each env has a unique var)
__PACKAGE__->meta->add_attribute('inline_environment' => (
init_arg => 'inline_environment',
- accessor => 'inline_environment',
+ accessor => '_inline_environment',
default => sub { {} },
));
return $self->inlined->( $self, @_ );
}
+sub inline_environment {
+ my $self = shift;
+
+ if ( $self->has_parent && $self->constraint == $null_constraint ) {
+ return $self->parent->inline_environment;
+ }
+
+ return $self->_inline_environment;
+}
+
sub assert_valid {
my ($self, $value) = @_;
accessor => 'values',
));
+__PACKAGE__->meta->add_attribute('_inline_var_name' => (
+ accessor => '_inline_var_name',
+));
+
my $inliner = sub {
my $self = shift;
my $val = shift;
return 'defined(' . $val . ') '
. '&& !ref(' . $val . ') '
- . '&& $enums{' . $val . '}';
+ . '&& $' . $self->_inline_var_name . '{' . $val . '}';
};
+# a quadrillion enums ought to be enough for any app
+my $var_suffix = '000000000000000000';
+
sub new {
my ( $class, %args ) = @_;
my %values = map { $_ => 1 } @{ $args{values} };
$args{constraint} = sub { $values{ $_[0] } };
- $args{inline_environment} = { '%enums' => \%values };
+
+ my $var_name = 'enums' . $var_suffix++;;
+ $args{_inline_var_name} = $var_name;
+ $args{inline_environment} = { '%' . $var_name => \%values };
my $self = $class->_new(\%args);
@{ $self->type_constraints };
};
+sub inline_environment {
+ my $self = shift;
+
+ return { map { %{ $_->inline_environment } }
+ @{ $self->type_constraints } };
+}
+
sub equals {
my ( $self, $type_or_name ) = @_;
}
{
+ enum 'Enum1' => 'a', 'b';
+ enum 'Enum2' => 'x', 'y';
+
+ subtype 'EnumUnion', as 'Enum1 | Enum2';
+
+ test_constraint(
+ 'EnumUnion', {
+ accept => [qw( a b x y )],
+ reject => [
+ $ZERO,
+ $ONE,
+ $INT,
+ $NEG_INT,
+ $NUM,
+ $NEG_NUM,
+ $EMPTY_STRING,
+ $STRING,
+ $NUM_IN_STRING,
+ $INT_WITH_NL1,
+ $INT_WITH_NL2,
+ $SCALAR_REF,
+ $SCALAR_REF_REF,
+ $ARRAY_REF,
+ $HASH_REF,
+ $CODE_REF,
+ $GLOB,
+ $GLOB_REF,
+ $FH,
+ $FH_OBJECT,
+ $REGEX,
+ $REGEX_OBJ,
+ $FAKE_REGEX,
+ $OBJECT,
+ $UNDEF,
+ ],
+ }
+ );
+}
+
+{
package DoesRole;
use Moose;