Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Merge branch 'master' of git://github.com/rjbs/rx

Conflicts:
	js/rx/coretypes.js
	js/rx/test/loader.js
	js/rx/test/runner.js
	php/Rx.php
	php/rx-test.php
	python/Rx.py
	python/rx-test.py
	ruby/Rx.rb
	ruby/rx-test.rb
  • Loading branch information...
commit 05c03cb09cd69e17e95b882297ba886b29ae6ddc 2 parents 24b9e06 + a23ae0f
Ronald J Kimball authored April 05, 2012
1  .gitignore
@@ -5,4 +5,5 @@ php/Test.php
5 5
 python/*.pyc
6 6
 perl/spec
7 7
 perl/.build
  8
+perl/Data-Rx-*
8 9
 *~
6  js/rx/coretypes.js
@@ -123,13 +123,12 @@ Rx.CoreType.numType.prototype.check  = function (v) {
123 123
 };
124 124
 
125 125
 Rx.CoreType.strType  = function (opt) {
126  
-  if (! Rx.Util._x_subset_keys_y(opt, {type: true, length: true, value: true }))
  126
+  if (! Rx.Util._x_subset_keys_y(opt, {type: true, value: true, length: true }))
127 127
     throw new Rx.Error('unknown argument for str type');
128 128
   if (typeof(opt.value) != "undefined")
129 129
     if (opt.value.constructor != String)
130 130
       throw new Rx.Error('invalid value parameter for str type');
131 131
     this.value = opt.value;
132  
-
133 132
   if (opt.length) {
134 133
     this.length_check = new Rx.Util.RangeChecker( opt.length );
135 134
   }
@@ -139,6 +138,9 @@ Rx.CoreType.strType.prototype.check  = function (v) {
139 138
   if (! ((typeof(v) == 'string') || (v instanceof String))) return false;
140 139
   if (this.length_check && ! this.length_check.check(v.length)) return false;
141 140
   if (this.value != null && v != this.value) return false;
  141
+  if (this.length_check && ! this.length_check.check(v.length)) {
  142
+    return false;
  143
+  }
142 144
   return true;
143 145
 };
144 146
 
2  js/rx/test/runner.js
@@ -85,7 +85,7 @@ for (i in schemaToTest) {
85 85
         var testDesc = (expect ? 'VALID  : ' : 'INVALID: ')
86 86
                      + sourceName + '/' + sourceEntry
87 87
                      + ' against ' + schemaName;
88  
-        
  88
+
89 89
         // JavaScript needs logical xor! -- rjbs, 2008-07-31
90 90
         if ((valid && !expect) || (!valid && expect)) {
91 91
           fail(testDesc);
2  perl/Changes
@@ -2,6 +2,8 @@ Revision history for Data-Rx
2 2
 
3 3
 {{$NEXT}}
4 4
 
  5
+0.200000  2012-04-01 14:33:26 Europe/Paris
  6
+
5 7
 0.100110  2010-01-11 22:42:19 America/New_York
6 8
           do not require JSON::XS
7 9
 
10  perl/dist.ini
@@ -3,8 +3,6 @@ author  = Ricardo SIGNES <rjbs@cpan.org>
3 3
 license = Perl_5
4 4
 copyright_holder = Ricardo SIGNES
5 5
 
6  
-version = 2 ; placeholder
7  
-
8 6
 [Prereqs]
9 7
 Carp = 0
10 8
 File::Find::Rule = 0
@@ -19,9 +17,15 @@ autodie = 0
19 17
 
20 18
 [@Filter]
21 19
 bundle = @RJBS
22  
-remove = Repository
  20
+remove = Git::Tag
23 21
 remove = Git::NextVersion
24 22
 
  23
+[Git::Tag]
  24
+tag_format = perl-%v
  25
+
  26
+[Git::NextVersion]
  27
+version_regexp  = ^perl-(.+)$
  28
+
25 29
 [GatherDir]
26 30
 root   = ../spec
27 31
 prefix = spec
2  perl/lib/Data/Rx.pm
@@ -130,7 +130,7 @@ sub make_schema {
130 130
     }
131 131
     $checker = $self->make_schema($handler->{'schema'});
132 132
   } else {
133  
-    $checker = $handler->new_checker($type, $schema_arg, $self);
  133
+    $checker = $handler->new_checker($schema_arg, $self, $type);
134 134
   }
135 135
 
136 136
   return $checker;
6  perl/lib/Data/Rx/CoreType.pm
@@ -9,7 +9,7 @@ use Data::Rx::Failure;
9 9
 use Data::Rx::Failures;
10 10
 
11 11
 sub new_checker {
12  
-  my ($class, $type, $arg, $rx) = @_;
  12
+  my ($class, $arg, $rx, $type) = @_;
13 13
   Carp::croak "$class does not take check arguments" if %$arg;
14 14
   bless { type => $type, rx => $rx } => $class;
15 15
 }
@@ -22,7 +22,7 @@ sub check {
22 22
   my ($self, $value) = @_;
23 23
   local $@;
24 24
 
25  
-  return 1 if eval { $self->validate($value); };
  25
+  return 1 if eval { $self->assert_valid($value); };
26 26
   my $failures = $@;
27 27
 
28 28
   if (eval { $failures->isa('Data::Rx::Failures') }) {
@@ -76,7 +76,7 @@ sub _subchecks {
76 76
 
77 77
     my ($value, $checker, $context) = @$subcheck;
78 78
 
79  
-    next if eval { $checker->validate($value) };
  79
+    next if eval { $checker->assert_valid($value) };
80 80
 
81 81
     my $failures = $@;
82 82
     Carp::confess($failures)
8  perl/lib/Data/Rx/CoreType/all.pm
@@ -7,12 +7,12 @@ use base 'Data::Rx::CoreType';
7 7
 use Scalar::Util ();
8 8
 
9 9
 sub new_checker {
10  
-  my ($class, $type, $arg, $rx) = @_;
  10
+  my ($class, $arg, $rx, $type) = @_;
11 11
 
12 12
   Carp::croak("unknown arguments to new")
13 13
     unless Data::Rx::Util->_x_subset_keys_y($arg, { of  => 1});
14 14
 
15  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  15
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
16 16
 
17 17
   Carp::croak("no 'of' parameter given to //all") unless exists $arg->{of};
18 18
 
@@ -20,13 +20,13 @@ sub new_checker {
20 20
 
21 21
   Carp::croak("invalid 'of' argument to //all") unless
22 22
     defined $of and Scalar::Util::reftype $of eq 'ARRAY' and @$of;
23  
-    
  23
+
24 24
   $self->{of} = [ map {; $rx->make_schema($_) } @$of ];
25 25
 
26 26
   return $self;
27 27
 }
28 28
 
29  
-sub validate {
  29
+sub assert_valid {
30 30
   my ($self, $value) = @_;
31 31
 
32 32
   my @subchecks;
8  perl/lib/Data/Rx/CoreType/any.pm
@@ -7,12 +7,12 @@ use base 'Data::Rx::CoreType';
7 7
 use Scalar::Util ();
8 8
 
9 9
 sub new_checker {
10  
-  my ($class, $type, $arg, $rx) = @_;
  10
+  my ($class, $arg, $rx, $type) = @_;
11 11
 
12 12
   Carp::croak("unknown arguments to new")
13 13
     unless Data::Rx::Util->_x_subset_keys_y($arg, { of  => 1});
14 14
 
15  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  15
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
16 16
 
17 17
   if (my $of = $arg->{of}) {
18 18
     Carp::croak("invalid 'of' argument to //any") unless
@@ -24,7 +24,7 @@ sub new_checker {
24 24
   return $self;
25 25
 }
26 26
 
27  
-sub validate {
  27
+sub assert_valid {
28 28
   return 1 unless $_[0]->{of};
29 29
 
30 30
   my ($self, $value) = @_;
@@ -32,7 +32,7 @@ sub validate {
32 32
   my @failures;
33 33
   for my $i (0 .. $#{ $self->{of} }) {
34 34
     my $check = $self->{of}[ $i ];
35  
-    return 1 if eval { $check->validate($value) };
  35
+    return 1 if eval { $check->assert_valid($value) };
36 36
 
37 37
     my $failure = $@;
38 38
     $failure->contextualize({
6  perl/lib/Data/Rx/CoreType/arr.pm
@@ -9,7 +9,7 @@ use Scalar::Util ();
9 9
 sub subname   { 'arr' }
10 10
 
11 11
 sub new_checker {
12  
-  my ($class, $type, $arg, $rx) = @_;
  12
+  my ($class, $arg, $rx, $type) = @_;
13 13
 
14 14
   Carp::croak("unknown arguments to new")
15 15
     unless Data::Rx::Util->_x_subset_keys_y($arg, {length=>1, contents=>1,
@@ -18,7 +18,7 @@ sub new_checker {
18 18
   Carp::croak("no contents schema given")
19 19
     unless $arg->{contents} and (ref $arg->{contents} || 'HASH' eq 'HASH');
20 20
 
21  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  21
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
22 22
 
23 23
   my $content_check = $rx->make_schema($arg->{contents});
24 24
 
@@ -32,7 +32,7 @@ sub new_checker {
32 32
   return $self;
33 33
 }
34 34
 
35  
-sub validate {
  35
+sub assert_valid {
36 36
   my ($self, $value) = @_;
37 37
 
38 38
   unless (! Scalar::Util::blessed($value) and ref $value eq 'ARRAY') {
2  perl/lib/Data/Rx/CoreType/bool.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::bool;
4 4
 use base 'Data::Rx::CoreType';
5 5
 # ABSTRACT: the Rx //bool type
6 6
 
7  
-sub validate {
  7
+sub assert_valid {
8 8
   my ($self, $value) = @_;
9 9
 
10 10
   return 1 if (
2  perl/lib/Data/Rx/CoreType/def.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::def;
4 4
 use base 'Data::Rx::CoreType';
5 5
 # ABSTRACT: the Rx //def type
6 6
 
7  
-sub validate {
  7
+sub assert_valid {
8 8
   my ($self, $value) = @_;
9 9
 
10 10
   return 1 if defined $value;
2  perl/lib/Data/Rx/CoreType/fail.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::fail;
4 4
 use base 'Data::Rx::CoreType';
5 5
 # ABSTRACT: the Rx //fail type
6 6
 
7  
-sub validate {
  7
+sub assert_valid {
8 8
   $_[0]->fail({
9 9
     error   => [ qw(fail) ],
10 10
     message => "matching reached an always-fail check",
6  perl/lib/Data/Rx/CoreType/map.pm
@@ -9,12 +9,12 @@ use Scalar::Util ();
9 9
 sub subname   { 'map' }
10 10
 
11 11
 sub new_checker {
12  
-  my ($class, $type, $arg, $rx) = @_;
  12
+  my ($class, $arg, $rx, $type) = @_;
13 13
 
14 14
   Carp::croak("unknown arguments to new") unless
15 15
     Data::Rx::Util->_x_subset_keys_y($arg, { values => 1 });
16 16
 
17  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  17
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
18 18
 
19 19
   Carp::croak("no values constraint given") unless $arg->{values};
20 20
 
@@ -23,7 +23,7 @@ sub new_checker {
23 23
   return $self;
24 24
 }
25 25
 
26  
-sub validate {
  26
+sub assert_valid {
27 27
   my ($self, $value) = @_;
28 28
 
29 29
   unless (! Scalar::Util::blessed($value) and ref $value eq 'HASH') {
2  perl/lib/Data/Rx/CoreType/nil.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::nil;
4 4
 use base 'Data::Rx::CoreType';
5 5
 # ABSTRACT: the Rx //nil type
6 6
 
7  
-sub validate {
  7
+sub assert_valid {
8 8
   my ($self, $value) = @_;
9 9
 
10 10
   return 1 if ! defined $value;
6  perl/lib/Data/Rx/CoreType/num.pm
@@ -5,12 +5,12 @@ use base 'Data::Rx::CoreType';
5 5
 # ABSTRACT: the Rx //num type
6 6
 
7 7
 sub new_checker {
8  
-  my ($class, $type, $arg, $rx) = @_;
  8
+  my ($class, $arg, $rx, $type) = @_;
9 9
 
10 10
   Carp::croak("unknown arguments to new")
11 11
     unless Data::Rx::Util->_x_subset_keys_y($arg, { range => 1, value => 1});
12 12
 
13  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  13
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
14 14
 
15 15
   $self->{range_check} = Data::Rx::Util->_make_range_check($arg->{range})
16 16
     if $arg->{range};
@@ -64,7 +64,7 @@ sub _value_is_of_type {
64 64
   return $value =~ $_NUM_RE;
65 65
 }
66 66
 
67  
-sub validate {
  67
+sub assert_valid {
68 68
   my ($self, $value) = @_;
69 69
 
70 70
   $self->__type_fail($value) unless defined $value and length $value;
2  perl/lib/Data/Rx/CoreType/one.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::one;
4 4
 use base 'Data::Rx::CoreType';
5 5
 # ABSTRACT: the Rx //one type
6 6
 
7  
-sub validate {
  7
+sub assert_valid {
8 8
   my ($self, $value) = @_;
9 9
 
10 10
   if (! defined $value) {
6  perl/lib/Data/Rx/CoreType/rec.pm
@@ -9,7 +9,7 @@ use Scalar::Util ();
9 9
 sub subname   { 'rec' }
10 10
 
11 11
 sub new_checker {
12  
-  my ($class, $type, $arg, $rx) = @_;
  12
+  my ($class, $arg, $rx, $type) = @_;
13 13
 
14 14
   Carp::croak("unknown arguments to new") unless
15 15
     Data::Rx::Util->_x_subset_keys_y($arg, {
@@ -18,7 +18,7 @@ sub new_checker {
18 18
       optional => 1,
19 19
     });
20 20
 
21  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  21
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
22 22
 
23 23
   my $content_schema = {};
24 24
 
@@ -42,7 +42,7 @@ sub new_checker {
42 42
   return $self;
43 43
 }
44 44
 
45  
-sub validate {
  45
+sub assert_valid {
46 46
   my ($self, $value) = @_;
47 47
 
48 48
   unless (! Scalar::Util::blessed($value) and ref $value eq 'HASH') {
6  perl/lib/Data/Rx/CoreType/seq.pm
@@ -9,7 +9,7 @@ use Scalar::Util ();
9 9
 sub subname   { 'seq' }
10 10
 
11 11
 sub new_checker {
12  
-  my ($class, $type, $arg, $rx) = @_;
  12
+  my ($class, $arg, $rx, $type) = @_;
13 13
 
14 14
   Carp::croak("unknown arguments to new")
15 15
     unless Data::Rx::Util->_x_subset_keys_y($arg, {contents=>1,tail=>1});
@@ -17,7 +17,7 @@ sub new_checker {
17 17
   Carp::croak("no contents array given")
18 18
     unless $arg->{contents} and (ref $arg->{contents} eq 'ARRAY');
19 19
 
20  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  20
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
21 21
 
22 22
   my @content_schemata = map { $rx->make_schema($_) }
23 23
                          @{ $arg->{contents} };
@@ -31,7 +31,7 @@ sub new_checker {
31 31
   return $self;
32 32
 }
33 33
 
34  
-sub validate {
  34
+sub assert_valid {
35 35
   my ($self, $value) = @_;
36 36
 
37 37
   unless (! Scalar::Util::blessed($value) and ref $value eq 'ARRAY') {
6  perl/lib/Data/Rx/CoreType/str.pm
@@ -7,7 +7,7 @@ use base 'Data::Rx::CoreType';
7 7
 use Data::Rx::Util;
8 8
 
9 9
 sub new_checker {
10  
-  my ($class, $type, $arg, $rx) = @_;
  10
+  my ($class, $arg, $rx, $type) = @_;
11 11
 
12 12
   Carp::croak("unknown arguments to new")
13 13
     unless Data::Rx::Util->_x_subset_keys_y($arg, { length => 1, value => 1});
@@ -26,7 +26,7 @@ sub new_checker {
26 26
     }
27 27
   }
28 28
 
29  
-  my $self = $class->SUPER::new_checker($type, {}, $rx);
  29
+  my $self = $class->SUPER::new_checker({}, $rx, $type);
30 30
 
31 31
   $self->{length_check} = Data::Rx::Util->_make_range_check($arg->{length})
32 32
     if $arg->{length};
@@ -36,7 +36,7 @@ sub new_checker {
36 36
   return $self;
37 37
 }
38 38
 
39  
-sub validate {
  39
+sub assert_valid {
40 40
   my ($self, $value) = @_;
41 41
 
42 42
   unless (defined $value) {
4  perl/t/lib/Test/RxTester.pm
@@ -83,7 +83,7 @@ sub assert_pass {
83 83
   my $desc = "$schema_desc should ACCEPT $input_desc";
84 84
 
85 85
   try {
86  
-    $schema->validate($input);
  86
+    $schema->assert_valid($input);
87 87
     Test::More::pass("$desc");
88 88
   } catch {
89 89
     my $fails = $_;
@@ -102,7 +102,7 @@ sub assert_fail {
102 102
   my $desc = "$schema_desc should REJECT $input_desc";
103 103
 
104 104
   try {
105  
-    $schema->validate($input);
  105
+    $schema->assert_valid($input);
106 106
     Test::More::fail($desc);
107 107
     if ($want_struct) {
108 108
       Test::More::fail("$desc, failures struct");
6  php/Rx.php
@@ -368,10 +368,12 @@ class RxCoretypeStr {
368 368
 
369 369
   function check($value) {
370 370
     if (! is_string($value)) return false;
371  
-    if ($this->length_checker)
372  
-      if (! $this->length_checker->check(strlen($value))) return false;
373 371
     if ($this->fixed_value !== null and $value != $this->fixed_value)
374 372
       return false;
  373
+
  374
+    if ($this->length_checker)
  375
+      if (! $this->length_checker->check(strlen($value))) return false;
  376
+
375 377
     return true;
376 378
   }
377 379
 
20  python/Rx.py
@@ -13,7 +13,7 @@ def make_range_check(opt):
13 13
     range = { }
14 14
     for entry in opt.keys():
15 15
       if entry not in ('min', 'max', 'min-ex', 'max-ex'):
16  
-        raise Error("illegal argument to make_range_check")
  16
+        raise ValueError("illegal argument to make_range_check")
17 17
 
18 18
       range[entry] = opt[entry]
19 19
 
@@ -45,10 +45,13 @@ def expand_uri(self, type_name):
45 45
 
46 46
     m = re.match('^/([-._a-z0-9]*)/([-._a-z0-9]+)$', type_name)
47 47
 
48  
-    if not m: raise Error("couldn't understand type name '%s'" % type_name)
  48
+    if not m:
  49
+      raise ValueError("couldn't understand type name '%s'" % type_name)
49 50
 
50 51
     if not self.prefix_registry.get(m.group(1)):
51  
-      raise Error("unknown prefix '%s' in type name '%s'" % (m.group(1), type_name))
  52
+      raise ValueError(
  53
+        "unknown prefix '%s' in type name '%s'" % (m.group(1), type_name)
  54
+      )
52 55
 
53 56
     return '%s%s' % (self.prefix_registry[ m.group(1) ], m.group(2))
54 57
 
@@ -62,7 +65,7 @@ def register_type(self, t):
62 65
     t_uri = t.uri()
63 66
 
64 67
     if self.type_registry.get(t_uri, None):
65  
-      raise Error("type already registered for %s" % t_uri)
  68
+      raise ValueError("type already registered for %s" % t_uri)
66 69
 
67 70
     self.type_registry[t_uri] = t
68 71
 
@@ -82,7 +85,7 @@ def make_schema(self, schema):
82 85
 
83 86
     if not type(schema) is dict:
84 87
       raise Error('invalid schema argument to make_schema')
85  
-  
  88
+
86 89
     uri = self.expand_uri(schema["type"])
87 90
 
88 91
     if not self.type_registry.get(uri): raise Error("unknown type %s" % uri)
@@ -141,7 +144,7 @@ def __init__(self, schema, rx):
141 144
 
142 145
   def check(self, value):
143 146
     if self.alts is None: return True
144  
-    
  147
+
145 148
     for alt in self.alts:
146 149
       if alt.check(value): return True
147 150
 
@@ -156,7 +159,7 @@ def __init__(self, schema, rx):
156 159
 
157 160
     if not set(schema.keys()).issubset(set(('type', 'contents', 'length'))):
158 161
       raise Error('unknown parameter for //arr')
159  
-    
  162
+
160 163
     if not schema.get('contents'):
161 164
       raise Error('no contents provided for //arr')
162 165
 
@@ -317,7 +320,7 @@ def check(self, value):
317 320
 
318 321
     if len(unknown) and not self.rest_schema: return False
319 322
 
320  
-    for field in self.required.keys(): 
  323
+    for field in self.required.keys():
321 324
       if not value.has_key(field): return False
322 325
       if not self.required[field].check( value[field] ): return False
323 326
 
@@ -389,6 +392,7 @@ def check(self, value):
389 392
     if not type(value) in (str, unicode): return False
390 393
     if self.length and not self.length(len(value)): return False
391 394
     if (not self.value is None) and value != self.value: return False
  395
+    if self.length and not self.length(len(value)): return False
392 396
     return True
393 397
 
394 398
 core_types = [
7  python/rx-test.py
@@ -39,7 +39,7 @@ def normalize(entries, test_data):
39 39
 
40 40
   leaf_name = '/'.join(parts[1:])
41 41
   leaf_name = re.sub('\.json$', '', leaf_name)
42  
-  
  42
+
43 43
   filetype = parts.pop(0)
44 44
 
45 45
   if filetype == 'schemata':
@@ -57,7 +57,7 @@ def normalize(entries, test_data):
57 57
         boxed_data = json.loads("[ %s ]" % payload[entry])
58 58
         test_data[ leaf_name ][ entry ] = boxed_data[0]
59 59
   else:
60  
-    raise Exception("weird file in data dir: %s" % filename)
  60
+    raise StandardError("weird file in data dir: %s" % filename)
61 61
 
62 62
 schema_names = test_schemata.keys()
63 63
 schema_names.sort()
@@ -80,7 +80,6 @@ def normalize(entries, test_data):
80 80
 
81 81
     if schema_test_spec['composedtype'].get("invalid", False):
82 82
       ok(0, "BAD COMPOSED TYPE: schemata %s" % schema_name)
83  
-      continue
84 83
 
85 84
     if schema_test_spec['composedtype'].get("prefix", False):
86 85
        rx.add_prefix(schema_test_spec['composedtype']['prefix'][0],
@@ -99,7 +98,7 @@ def normalize(entries, test_data):
99 98
     ok(0, "BAD SCHEMA: schemata %s" % schema_name)
100 99
     continue
101 100
 
102  
-  if not schema: raise Exception("got no schema obj for valid input")
  101
+  if not schema: raise StandardError("got no schema obj for valid input")
103 102
 
104 103
   for pf in [ 'pass', 'fail' ]:
105 104
     for source in schema_test_spec.get(pf, []):
43  ruby/Rx.rb
@@ -3,7 +3,7 @@ class Rx
3 3
   def self.schema(schema)
4 4
     Rx.new(:load_core => true).make_schema(schema)
5 5
   end
6  
-  
  6
+
7 7
   def initialize(opt={})
8 8
     @type_registry = {}
9 9
     @prefix = {
@@ -16,7 +16,6 @@ def initialize(opt={})
16 16
     end
17 17
   end
18 18
 
19  
-
20 19
   def register_type(type)
21 20
     uri = type.uri
22 21
 
@@ -90,10 +89,10 @@ def make_schema(schema)
90 89
       return type_class.new(schema, self)
91 90
     end
92 91
   end
93  
-  
  92
+
94 93
   class Helper; end;
95 94
   class Helper::Range
96  
-    
  95
+
97 96
     def initialize(arg)
98 97
       @range = { }
99 98
 
@@ -125,11 +124,11 @@ def initialize(message, path)
125 124
       @message = message
126 125
       @path = path
127 126
     end
128  
-    
  127
+
129 128
     def path
130 129
       @path ||= ""
131 130
     end
132  
-    
  131
+
133 132
     def message
134 133
       "#{@message} (#{@path})"
135 134
     end
@@ -142,7 +141,7 @@ def to_s
142 141
       inspect
143 142
     end
144 143
   end
145  
-  
  144
+
146 145
   class Type
147 146
     def initialize(param, rx)
148 147
       assert_valid_params(param)
@@ -205,7 +204,7 @@ def initialize(param, rx)
205 204
         class << self; def subname; return 'all'; end; end
206 205
 
207 206
         def check!(value)
208  
-          @alts.each do |alt| 
  207
+          @alts.each do |alt|
209 208
             begin
210 209
               alt.check!(value)
211 210
             rescue ValidationError => e
@@ -247,7 +246,7 @@ def check!(value)
247 246
             rescue ValidationError
248 247
             end
249 248
           end
250  
-          
  249
+
251 250
           raise ValidationError.new("expected one to match", "/any")
252 251
         end
253 252
       end
@@ -272,7 +271,7 @@ def initialize(param, rx)
272 271
           end
273 272
         end
274 273
 
275  
-        def check!(value) 
  274
+        def check!(value)
276 275
           unless value.instance_of?(Array)
277 276
             raise ValidationError.new("expected array got #{value.class}", "/arr")
278 277
           end
@@ -284,7 +283,7 @@ def check!(value)
284 283
           end
285 284
 
286 285
           if @contents_schema then
287  
-            value.each do |v| 
  286
+            value.each do |v|
288 287
               begin
289 288
                 @contents_schema.check!(v)
290 289
               rescue ValidationError => e
@@ -306,7 +305,7 @@ class << self; def subname; return 'bool'; end; end
306 305
         def check!(value)
307 306
           unless value.instance_of?(TrueClass) or value.instance_of?(FalseClass)
308 307
             raise ValidationError.new("expected bool got #{value.inspect}", "/bool")
309  
-          end        
  308
+          end
310 309
           true
311 310
         end
312 311
       end
@@ -317,14 +316,14 @@ class << self; def subname; return 'fail'; end; end
317 316
         def check(value); return false; end
318 317
         def check!(value); raise ValidationError.new("explicit fail", "/fail"); end
319 318
       end
320  
-      
  319
+
321 320
       #
322 321
       # Added by dan - 81030
323 322
       class Date < Type::Core
324 323
         class << self; def subname; return 'date'; end; end
325  
-        
  324
+
326 325
         include Type::NoParams
327  
-        
  326
+
328 327
         def check!(value)
329 328
           unless value.instance_of?(::Date)
330 329
             raise ValidationError("expected Date got #{value.inspect}", "/date")
@@ -360,7 +359,7 @@ def check!(value)
360 359
           end
361 360
 
362 361
           if @value_schema
363  
-            value.each_value do |v| 
  362
+            value.each_value do |v|
364 363
               begin
365 364
                 @value_schema.check!(v)
366 365
               rescue ValidationError => e
@@ -586,6 +585,10 @@ def allowed_param?(p); return @@allowed_param[p]; end
586 585
         def initialize(param, rx)
587 586
           super
588 587
 
  588
+          if param['length'] then
  589
+            @length_range = Rx::Helper::Range.new( param['length'] )
  590
+          end
  591
+
589 592
           if param.has_key?('value') then
590 593
             if ! param['value'].instance_of?(String) then
591 594
               raise Rx::Exception.new("invalid value parameter for #{uri}")
@@ -603,11 +606,13 @@ def check!(value)
603 606
           unless value.instance_of?(String)
604 607
             raise ValidationError.new("expected String got #{value.inspect}", "/str")
605 608
           end
  609
+
606 610
           if @length_range
607 611
             unless @length_range.check(value.length)
608  
-              raise ValidationError.new("expected String of length #{@length_range}, got #{value.length}", "/str")
  612
+              raise ValidationError.new("expected string with #{@length_range} characters, got #{value.length}", "/str")
609 613
             end
610 614
           end
  615
+
611 616
           if @value and value != @value
612 617
             raise ValidationError.new("expected #{@value.inspect} got #{value.inspect}", "/str")
613 618
           end
@@ -619,9 +624,9 @@ def check!(value)
619 624
       # Added by dan - 81106
620 625
       class Time < Type::Core
621 626
         class << self; def subname; return 'time'; end; end
622  
-        
  627
+
623 628
         include Type::NoParams
624  
-        
  629
+
625 630
         def check!(value)
626 631
           unless value.instance_of?(::Time)
627 632
             raise ValidationError.new("expected Time got #{value.inspect}", "/time")
21  ruby/rx-test.rb
@@ -50,9 +50,17 @@ def normalize(entries, test_data)
50 50
 
51 51
 class TAP_Emitter
52 52
   attr_reader :i
  53
+  attr_reader :failures
  54
+
  55
+  def initialize()
  56
+    @failures = 0
  57
+  end
53 58
 
54 59
   def ok(bool, desc)
55 60
     @i === nil ? @i = 1 : @i += 1
  61
+    if ! bool then
  62
+      @failures += 1
  63
+    end
56 64
     printf("%s %s - %s\n", bool ? 'ok' : 'not ok', @i, desc);
57 65
   end
58 66
 end
@@ -138,12 +146,12 @@ def ok(bool, desc)
138 146
       }
139 147
 
140 148
       entries.each_pair { |entry, want|
141  
-        result = begin 
142  
-                   schema.check!(test_data[source][entry])
143  
-                   true
144  
-                 rescue Rx::ValidationError => e
145  
-                   false
146  
-                 end
  149
+        result = begin
  150
+          schema.check!(test_data[source][entry])
  151
+          true
  152
+        rescue Rx::ValidationError => e
  153
+          false
  154
+        end
147 155
         ok = (pf == 'pass' and result) || (pf == 'fail' and !result)
148 156
 
149 157
         desc = sprintf "%s: %s-%s against %s",
@@ -156,3 +164,4 @@ def ok(bool, desc)
156 164
 }
157 165
 
158 166
 puts "1..#{tap.i}"
  167
+exit(tap.failures > 0 ? 1 : 0)
10  sf-demo.pl
... ...
@@ -1,6 +1,7 @@
1 1
 #!perl
2 2
 use strict;
3 3
 use warnings;
  4
+use 5.10.1;
4 5
 
5 6
 use lib 'perl/lib';
6 7
 use Data::Rx;
@@ -38,15 +39,12 @@
38 39
   undef,
39 40
   {
40 41
     foo => 1,
41  
-    bar => 2,
  42
+    bar => 2.2,
42 43
     baz => [ 3, 4, 5, 6.2, 7 ],
43 44
   },
44 45
 ];
45 46
 
46  
-eval { $schema->validate($input); };
  47
+eval { $schema->assert_valid($input); };
47 48
 my $fail = $@;
  49
+say $fail;
48 50
 
49  
-print Dump($fail->struct);
50  
-
51  
-print "PATH TO VALUE: " . join(q{ }, $fail->path_to_value) . "\n";
52  
-print "PATH TO CHECK: " . join(q{ }, $fail->path_to_check) . "\n";

0 notes on commit 05c03cb

Please sign in to comment.
Something went wrong with that request. Please try again.