summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/MDV/Distribconf/MediaCFG.pm32
-rw-r--r--t/03mediacfg.t13
2 files changed, 41 insertions, 4 deletions
diff --git a/lib/MDV/Distribconf/MediaCFG.pm b/lib/MDV/Distribconf/MediaCFG.pm
index 46b3dc5..0566f3c 100644
--- a/lib/MDV/Distribconf/MediaCFG.pm
+++ b/lib/MDV/Distribconf/MediaCFG.pm
@@ -117,7 +117,21 @@ added to the config (urpmi).
=cut
-$value->{size} = {};
+$value->{size} = {
+ validation => sub {
+ my ($v) = @_;
+ if ($v =~ /^(\d+)(\w)?$/) {
+ if ($2) {
+ if (! grep { lc($2) eq $_ } qw(k m g t p)) {
+ return("wrong unit");
+ }
+ }
+ return;
+ } else {
+ return ("malformed value");
+ }
+ },
+};
=head3 size
@@ -125,7 +139,11 @@ The size of the media. The value is suffixed by the unit.
=cut
-sub valid_param {
+# valid_param($media, $var, $val)
+#
+# Return a list of errors (if any) about having such value in the config
+
+sub _valid_param {
my ($media, $var, $val) = @_[-3..-1];
if (!exists($value->{$var})) {
return ("unknow var");
@@ -141,4 +159,14 @@ sub valid_param {
return @errors;
}
+# Retun a hash containing information about $var
+
+sub _value_info {
+ my ($var) = $_[-1];
+ if (exists($value->{$var})) {
+ return $value->{$var}
+ }
+ return;
+}
+
1;
diff --git a/t/03mediacfg.t b/t/03mediacfg.t
index 0c3f58a..48d6a16 100644
--- a/t/03mediacfg.t
+++ b/t/03mediacfg.t
@@ -2,8 +2,17 @@
use strict;
use warnings;
-use Test::More tests => 2;
+use Test::More tests => 6;
use_ok('MDV::Distribconf::MediaCFG');
-is(MDV::Distribconf::MediaCFG::valid_param('foo', 'name', 'toto'), 0,
+is(MDV::Distribconf::MediaCFG::_valid_param('foo', 'name', 'toto'), 0,
"Valid value return no error");
+
+is(MDV::Distribconf::MediaCFG::_valid_param('foo', 'size', '1'), 0,
+ "valide size value");
+is(MDV::Distribconf::MediaCFG::_valid_param('foo', 'size', '1k'), 0,
+ "valide size value");
+is(MDV::Distribconf::MediaCFG::_valid_param('foo', 'size', '1d'), 1,
+ "non valide size value");
+is(MDV::Distribconf::MediaCFG::_valid_param('foo', 'size', 'coin'), 1,
+ "non valide size value");