summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2015-09-20 09:33:58 +0200
committerYorhel <git@yorhel.nl>2015-09-20 09:33:58 +0200
commitd53385db68107234e9c06f82387f6251ae9fc2dc (patch)
treeac296acca01546180c7ee3a1880581261c29be5a /t
parent4223f4a6f345de8df0dd311ee624baee69f8202d (diff)
kv_validate(): Allow templates to set more fields
The documentation has always implied that fields like required, default, rmwhitespace, multi, mincount and maxcount could be set in a template, but that was never supported. Now it does what you expect it to do.
Diffstat (limited to 't')
-rw-r--r--t/kv_validate.t27
1 files changed, 27 insertions, 0 deletions
diff --git a/t/kv_validate.t b/t/kv_validate.t
index 612f423..d04c73f 100644
--- a/t/kv_validate.t
+++ b/t/kv_validate.t
@@ -163,6 +163,9 @@ BEGIN{@tests=(
$templates{hex} = { regex => qr/^[0-9a-f]+$/i };
$templates{crc32} = { template => 'hex', minlength => 8, maxlength => 8 };
$templates{prefix} = { func => sub { $_[0] =~ /^$_[1]{prefix}/ }, inherit => ['prefix'] };
+ $templates{bool} = { required => 0, default => 0, func => sub { $_[0] = $_[0]?1:0 } };
+ $templates{rawtuple} = { rmwhitespace => 0, template => 'tuple' };
+ $templates{tuple} = { multi => 1, mincount => 2, maxcount => 2 };
()},
{ param => 'crc', template => 'hex' },
[ crc => '12345678' ],
@@ -188,6 +191,30 @@ BEGIN{@tests=(
[ x => 'hullo' ],
{ x => 'hullo', _err => [[ 'x', 'template', 'prefix' ]] },
+ { param => 'issexy', template => 'bool' },
+ [],
+ { issexy => 0 },
+
+ { param => 'issexy', required => 1, template => 'bool' },
+ [],
+ { issexy => undef, _err => [['issexy', 'required', 1]] },
+
+ { param => 'issexy', template => 'bool' },
+ [ issexy => 'HI IM SEXY!' ],
+ { issexy => 1 },
+
+ { param => 'tuple', template => 'rawtuple' },
+ [ tuple => ' so much space ', tuple => ' more space ' ],
+ { tuple => [' so much space ', ' more space ' ]},
+
+ { param => 'tuple', template => 'rawtuple' },
+ [ tuple => 1 ],
+ { tuple => [1], _err => [['tuple', 'mincount', 2]] }, # This error reporting is confusing
+
+ { param => 'tuple', template => 'rawtuple' },
+ [ tuple => 1, tuple => 2, tuple => 3 ],
+ { tuple => [1,2,3], _err => [['tuple', 'maxcount', 2]] }, # Likewise
+
# num / int / uint templates
{ param => 'age', template => 'num' },
[ age => 0 ],