Clay-UI
view release on metacpan or search on metacpan
lib/Clay/UI/_validate.pm view on Meta::CPAN
);
# The largest user sizing-group id. Clay::UI::Grid packs its own ids
# above it (grid id << the bits of this range), so the two never meet.
use constant USER_GROUP_ID_MAX => (1 << 20) - 1;
sub _fail ($name, $message) {
croak_ui "Clay::UI: '$name' $message";
}
# A plain (non-reference) number that is neither NaN nor infinite.
sub is_finite_number ($value) {
return defined $value && !ref $value && looks_like_number($value)
&& $value == $value && $value != 9**9**9 && $value != -9**9**9;
}
# A child, row or column index: a non-negative integer in plain decimal
# digits without leading zeros (so '01' and '1' are never two indices),
# defined and not a reference.
sub is_index ($value) {
return defined $value && !ref $value && $value =~ /\A(?:0|[1-9][0-9]*)\z/ ? 1 : 0;
lib/Clay/UI/_validate.pm view on Meta::CPAN
name the attribute and the snake_case path inside it, e.g.
C<Clay::UI: 'layout.padding.left' expected an integer in 0..65535, got '-5'>,
and list the known keys for an unknown one.
The other validators hold Clay::UI's own rules: element ids must not
start with C<anon:>, user sizing-group ids must stay at or below
C<USER_GROUP_ID_MAX> (C<2**20 - 1>; L<Clay::UI::Grid> packs its own ids
above it), text must be a defined string, and C<border_width> may also
be a single number.
C<is_finite_number> is true for a plain number that is neither NaN nor
infinite. C<copy_value> deep-copies hashes and arrays (blessed ones
into plain data) and keeps scalars and objects with overloading as they
are.
This module is internal. The API is not part of the public contract.
=cut
lib/Clay/XS.pm view on Meta::CPAN
=item *
wrong reference types, at any nesting level;
=item *
non-numeric numbers;
=item *
numbers that are not finite as a C C<float>: NaN, infinities, and
values beyond about 3.4e38 (the C<max> of a sizing axis may be C<+Inf>
or larger, meaning no maximum);
=item *
integers outside the C field's range, for example negative padding or
an enum value Clay does not define.
=back
lib/Clay/XS/Structs.pod view on Meta::CPAN
=head2 Value types and ranges
Every value is checked when it crosses into Clay, at any nesting depth.
The field descriptions below use these types:
=over 4
=item float
Any number that is finite as a C C<float> (absolute value up to about
3.4e38). NaN, infinities and non-numeric strings croak. Unless a key's
entry gives a range, signs are not checked: Clay accepts negative values
and computes with them. The one exception to finiteness is the C<max> of
a sizing axis, which may also be C<+Inf> (see L</sizing>).
=item U16
An integer in C<0 .. 65535>. Values outside C<0 .. 65535> and
non-integers (C<1.5>) croak; C<1.0> is accepted. Padding, C<childGap>,
C<lineGap>, border widths, C<fontId>, C<fontSize>, C<letterSpacing> and
C<lineHeight> are U16 and range-checked this way.
t/21-ui-validation.t view on Meta::CPAN
is( $box->layout, { child_gap => 4 }, 'changing the returned hash does not either' );
my $blessed = bless { r => 1, g => 2, b => 3, a => 255 }, 'Some::Colour';
$box->background_color($blessed);
$blessed->{r} = 999;
is( $box->background_color, { r => 1, g => 2, b => 3, a => 255 }, 'a blessed hash is copied as plain data' );
};
subtest 'viewport sizes must be positive finite numbers' => sub {
my $ui = make_ui(Clay::UI::Test::Box->new(id => 'root'));
like( dies { $ui->width('inf') }, qr/width must be a positive finite number/, 'an infinite width dies' );
like( dies { $ui->height('nan') }, qr/height must be a positive finite number/, 'a NaN height dies' );
like( dies { $ui->width(10, 20) }, qr/width takes one value/, 'one value at a time' );
is( [ $ui->width, $ui->height ], [ 100, 100 ], 'the size is unchanged' );
ok( lives { $ui->height(50) }, 'a valid size still works' );
like( dies { Clay::UI->new(width => 'inf', height => 1, root => Clay::UI::Test::Box->new) },
qr/'width' and 'height' must be positive finite numbers/, 'also at construction' );
};
subtest 'a non-finite pointer dies in Clay::UI and is not remembered' => sub {
my $ui = make_ui(Clay::UI::Test::Box->new(id => 'root'));
$ui->render(pointer_state => { x => 1, y => 1 });
like( dies { $ui->render(pointer_state => { x => 'nan', y => 2 }) }, qr/pointer_state 'x' must be a finite number/,
'a NaN position dies' );
like( dies { $ui->render(scroll_delta => [ 'inf', 0 ]) }, qr/'scroll_delta' must be/, 'so does an infinite delta' );
ok( lives { $ui->render }, 'the next render reuses the last good pointer' );
};
subtest 'render arguments are validated' => sub {
my $ui = make_ui(Clay::UI::Test::Box->new(id => 'root'));
like( dies { $ui->render(pointer => {}) }, qr/unknown argument\(s\): pointer/, 'unknown argument' );
like( dies { $ui->render(pointer_state => [1, 2]) }, qr/'pointer_state' must be a hashref/, 'pointer_state type' );
like( dies { $ui->render(pointer_state => { x => 1, y => 2, pressed => 1 }) },
qr/unknown pointer_state key\(s\): pressed/, 'unknown pointer_state key' );
( run in 0.948 second using v1.01-cache-2.11-cpan-60884a8df76 )