kwalify 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,20 +1,21 @@
1
1
  ###
2
- ### $Rev: 21 $
3
- ### $Release: 0.3.0 $
2
+ ### $Rev: 35 $
3
+ ### $Release: 0.4.0 $
4
4
  ### copyright(c) 2005 kuwata-lab all rights reserved.
5
5
  ###
6
6
 
7
- if $0 == __FILE__
8
- testdir = File.dirname(__FILE__)
9
- libdir = File.dirname(testdir) + "/lib"
10
- $LOAD_PATH << libdir << testdir
11
- require 'test/unit'
12
- require 'test/unit/ui/console/testrunner'
13
- require 'kwalify'
14
- require 'kwalify/util/assert-diff'
15
- require 'yaml'
7
+ unless defined?(TESTDIR)
8
+ TESTDIR = File.dirname(__FILE__)
9
+ libdir = File.dirname(TESTDIR) + "/lib"
10
+ $LOAD_PATH << libdir << TESTDIR
16
11
  end
17
12
 
13
+ require 'test/unit'
14
+ require 'test/unit/ui/console/testrunner'
15
+ require 'kwalify'
16
+ require 'kwalify/util/assert-diff'
17
+ require 'yaml'
18
+
18
19
 
19
20
  class ValidatorTest < Test::Unit::TestCase
20
21
 
@@ -39,37 +40,52 @@ class ValidatorTest < Test::Unit::TestCase
39
40
  eval s
40
41
  end
41
42
 
43
+
42
44
  ## execute test
43
45
  def _test()
44
46
  return if $target && $target != @name
47
+ # Syck parser
45
48
  schema = YAML.load(@schema)
46
49
  validator = Kwalify::Validator.new(schema)
47
- _test_body(validator, @valid, '')
48
- _test_body(validator, @invalid, @error)
50
+ error2 = @error.gsub(/\(line \d+\)/, '')
51
+ _test_body(validator, @valid, '' , false)
52
+ _test_body(validator, @invalid, error2, false)
53
+ # Kwalify::Parser
54
+ schema = Kwalify::Parser.new(@schema).parse()
55
+ validator = Kwalify::Validator.new(schema)
56
+ _test_body(validator, @valid, '' , true)
57
+ _test_body(validator, @invalid, @error, true)
49
58
  end
50
59
 
51
- def _test_body(validator, input, expected)
52
- document = YAML.load(input)
60
+ def _test_body(validator, input, expected, flag_parser)
61
+ if flag_parser
62
+ parser = Kwalify::Parser.new(input)
63
+ document = parser.parse()
64
+ else
65
+ document = YAML.load(input)
66
+ end
53
67
  error_list = validator.validate(document)
54
68
  actual = ""
55
69
  error_list.each do |error|
56
- actual << "%-20s: [%s] %s\n" % [error.error_symbol.inspect, error.path, error.message]
70
+ errsym = error.error_symbol.inspect
71
+ if flag_parser
72
+ linenum = parser.path_linenum(error.path)
73
+ actual << "%-20s: (line %d)[%s] %s\n" % [errsym, linenum, error.path, error.message]
74
+ else
75
+ actual << "%-20s: [%s] %s\n" % [errsym, error.path, error.message]
76
+ end
57
77
  end
58
- if $flag_print
78
+ if $print
59
79
  print actual
60
80
  else
61
81
  assert_equal_with_diff(expected, actual)
62
82
  end
63
- #return error_list
64
83
  end
65
84
 
66
85
  end
67
86
 
68
-
69
87
  #if $0 == __FILE__
70
- # suite = Test::Unit::TestSuite.new()
71
- # suite << ValidatorTest.suite()
72
- # Test::Unit::UI::Console::TestRunner.run(suite)
88
+ # Test::Unit::UI::Console::TestRunner.run(ValidatorTest)
73
89
  #end
74
90
 
75
91
  __END__
@@ -115,12 +131,12 @@ invalid: |
115
131
  birth: Jul 01, 1985
116
132
  #
117
133
  error: |
118
- :required_nokey : [/] key 'name:' is required.
119
- :key_undefined : [/] key 'nam:' is undefined.
120
- :enum_notexist : [/blood] 'ab': invalid blood value.
121
- :type_unmatch : [/birth] 'Jul 01, 1985': not a date.
122
- :type_unmatch : [/age] 'twenty': not a integer.
123
- :pattern_unmatch : [/email] 'foo(at)mail.com': not matched to pattern /@/.
134
+ :required_nokey : (line 1)[/] key 'name:' is required.
135
+ :key_undefined : (line 1)[/] key 'nam:' is undefined.
136
+ :enum_notexist : (line 4)[/blood] 'ab': invalid blood value.
137
+ :type_unmatch : (line 5)[/birth] 'Jul 01, 1985': not a date.
138
+ :type_unmatch : (line 3)[/age] 'twenty': not a integer.
139
+ :pattern_unmatch : (line 2)[/email] 'foo(at)mail.com': not matched to pattern /@/.
124
140
  #
125
141
  ---
126
142
  name: sequence1
@@ -146,8 +162,8 @@ invalid: |
146
162
  - 100
147
163
  #
148
164
  error: |
149
- :required_novalue : [/2] value required but none.
150
- :type_unmatch : [/4] '100': not a string.
165
+ :required_novalue : (line 3)[/2] value required but none.
166
+ :type_unmatch : (line 5)[/4] '100': not a string.
151
167
  #
152
168
  ---
153
169
  name: nested1
@@ -203,12 +219,12 @@ invalid: |
203
219
  email: bar(at)mail.com
204
220
  #
205
221
  error: |
206
- :required_nokey : [/address-book/0] key 'email:' is required.
207
- :key_undefined : [/address-book/0] key 'mail:' is undefined.
208
- :enum_notexist : [/address-book/0/blood] 'ab': invalid blood value.
209
- :type_unmatch : [/address-book/0/birth] '1985/01/01': not a date.
210
- :type_unmatch : [/address-book/0/age] 'twenty': not a integer.
211
- :pattern_unmatch : [/address-book/1/email] 'bar(at)mail.com': not matched to pattern /@/.
222
+ :required_nokey : (line 2)[/address-book/0] key 'email:' is required.
223
+ :key_undefined : (line 2)[/address-book/0] key 'mail:' is undefined.
224
+ :enum_notexist : (line 5)[/address-book/0/blood] 'ab': invalid blood value.
225
+ :type_unmatch : (line 6)[/address-book/0/birth] '1985/01/01': not a date.
226
+ :type_unmatch : (line 4)[/address-book/0/age] 'twenty': not a integer.
227
+ :pattern_unmatch : (line 8)[/address-book/1/email] 'bar(at)mail.com': not matched to pattern /@/.
212
228
  #
213
229
  ---
214
230
  name: anchor1
@@ -239,9 +255,9 @@ invalid: |
239
255
  family-name: 100
240
256
  #
241
257
  error: |
242
- :required_nokey : [/0] key 'family-name:' is required.
243
- :key_undefined : [/0] key 'last-name:' is undefined.
244
- :type_unmatch : [/1/family-name] '100': not a string.
258
+ :required_nokey : (line 1)[/0] key 'family-name:' is required.
259
+ :key_undefined : (line 1)[/0] key 'last-name:' is undefined.
260
+ :type_unmatch : (line 4)[/1/family-name] '100': not a string.
245
261
  #
246
262
  ---
247
263
  name: anchor2
@@ -282,9 +298,9 @@ invalid: |
282
298
  email: bar@mail.com
283
299
  #
284
300
  error: |
285
- :type_unmatch : [/address-book/0/name] '100': not a string.
286
- :required_nokey : [/address-book/1] key 'name:' is required.
287
- :key_undefined : [/address-book/1] key 'first-name:' is undefined.
301
+ :type_unmatch : (line 3)[/address-book/0/name] '100': not a string.
302
+ :required_nokey : (line 5)[/address-book/1] key 'name:' is required.
303
+ :key_undefined : (line 5)[/address-book/1] key 'first-name:' is undefined.
288
304
  #
289
305
  ---
290
306
  name: anchor3
@@ -334,8 +350,8 @@ invalid: |
334
350
  supervisor: *foo
335
351
  #
336
352
  error: |
337
- :type_unmatch : [/0/name] '100': not a string.
338
- :enum_notexist : [/1/post] 'worker': invalid post value.
353
+ :type_unmatch : (line 2)[/0/name] '100': not a string.
354
+ :enum_notexist : (line 7)[/1/post] 'worker': invalid post value.
339
355
  #
340
356
  ---
341
357
  name: range1
@@ -390,14 +406,14 @@ invalid: |
390
406
  - 9.99
391
407
  #
392
408
  error: |
393
- :range_toosmall : [/min-only/0] '9': too small (< min 10.0).
394
- :range_toosmall : [/min-only/1] '9.99': too small (< min 10.0).
395
- :range_toolarge : [/max-and-min/0] '101': too large (> max 100.0).
396
- :range_toolarge : [/max-and-min/1] '100.1': too large (> max 100.0).
397
- :range_toosmall : [/max-and-min/2] '9': too small (< min 10.0).
398
- :range_toosmall : [/max-and-min/3] '9.99': too small (< min 10.0).
399
- :range_toolarge : [/max-only/0] '101': too large (> max 100).
400
- :range_toolarge : [/max-only/1] '100.1': too large (> max 100).
409
+ :range_toosmall : (line 5)[/min-only/0] '9': too small (< min 10.0).
410
+ :range_toosmall : (line 6)[/min-only/1] '9.99': too small (< min 10.0).
411
+ :range_toolarge : (line 8)[/max-and-min/0] '101': too large (> max 100.0).
412
+ :range_toolarge : (line 9)[/max-and-min/1] '100.1': too large (> max 100.0).
413
+ :range_toosmall : (line 10)[/max-and-min/2] '9': too small (< min 10.0).
414
+ :range_toosmall : (line 11)[/max-and-min/3] '9.99': too small (< min 10.0).
415
+ :range_toolarge : (line 2)[/max-only/0] '101': too large (> max 100).
416
+ :range_toolarge : (line 3)[/max-only/1] '100.1': too large (> max 100).
401
417
  #
402
418
  ---
403
419
  name: range2
@@ -452,14 +468,14 @@ invalid: |
452
468
  - 10.0
453
469
  #
454
470
  error: |
455
- :range_toosmallex : [/min-ex-only/0] '10': too small (<= min 10.0).
456
- :range_toosmallex : [/min-ex-only/1] '10.0': too small (<= min 10.0).
457
- :range_toolargeex : [/max-ex-only/0] '100': too large (>= max 100).
458
- :range_toolargeex : [/max-ex-only/1] '100.0': too large (>= max 100).
459
- :range_toolargeex : [/max-ex-and-min-ex/0] '100': too large (>= max 100.0).
460
- :range_toolargeex : [/max-ex-and-min-ex/1] '100.0': too large (>= max 100.0).
461
- :range_toosmallex : [/max-ex-and-min-ex/2] '10': too small (<= min 10.0).
462
- :range_toosmallex : [/max-ex-and-min-ex/3] '10.0': too small (<= min 10.0).
471
+ :range_toosmallex : (line 5)[/min-ex-only/0] '10': too small (<= min 10.0).
472
+ :range_toosmallex : (line 6)[/min-ex-only/1] '10.0': too small (<= min 10.0).
473
+ :range_toolargeex : (line 2)[/max-ex-only/0] '100': too large (>= max 100).
474
+ :range_toolargeex : (line 3)[/max-ex-only/1] '100.0': too large (>= max 100).
475
+ :range_toolargeex : (line 8)[/max-ex-and-min-ex/0] '100': too large (>= max 100.0).
476
+ :range_toolargeex : (line 9)[/max-ex-and-min-ex/1] '100.0': too large (>= max 100.0).
477
+ :range_toosmallex : (line 10)[/max-ex-and-min-ex/2] '10': too small (<= min 10.0).
478
+ :range_toosmallex : (line 11)[/max-ex-and-min-ex/3] '10.0': too small (<= min 10.0).
463
479
  #
464
480
  ---
465
481
  name: range3
@@ -498,10 +514,10 @@ invalid: |
498
514
  - 100.0
499
515
  #
500
516
  error: |
501
- :range_toolarge : [/A/0] '100.00001': too large (> max 100).
502
- :range_toosmallex : [/A/1] '10.0': too small (<= min 10.0).
503
- :range_toosmall : [/B/0] '9.99999': too small (< min 10).
504
- :range_toolargeex : [/B/1] '100.0': too large (>= max 100.0).
517
+ :range_toolarge : (line 2)[/A/0] '100.00001': too large (> max 100).
518
+ :range_toosmallex : (line 3)[/A/1] '10.0': too small (<= min 10.0).
519
+ :range_toosmall : (line 5)[/B/0] '9.99999': too small (< min 10).
520
+ :range_toolargeex : (line 6)[/B/1] '100.0': too large (>= max 100.0).
505
521
  #
506
522
  ---
507
523
  name: length1
@@ -553,10 +569,10 @@ invalid: |
553
569
  - foo
554
570
  #
555
571
  error: |
556
- :length_tooshort : [/min-only/0] 'foo': too short (length 3 < min 4).
557
- :length_toolong : [/max-and-min/0] 'foobarbaz': too long (length 9 > max 8).
558
- :length_tooshort : [/max-and-min/1] 'foo': too short (length 3 < min 4).
559
- :length_toolong : [/max-only/0] 'hogehoge!': too long (length 9 > max 8).
572
+ :length_tooshort : (line 4)[/min-only/0] 'foo': too short (length 3 < min 4).
573
+ :length_toolong : (line 7)[/max-and-min/0] 'foobarbaz': too long (length 9 > max 8).
574
+ :length_tooshort : (line 8)[/max-and-min/1] 'foo': too short (length 3 < min 4).
575
+ :length_toolong : (line 2)[/max-only/0] 'hogehoge!': too long (length 9 > max 8).
560
576
  #
561
577
  ---
562
578
  name: length2
@@ -608,10 +624,10 @@ invalid: |
608
624
  - foo!
609
625
  #
610
626
  error: |
611
- :length_tooshortex : [/min-ex-only/0] 'foo!': too short (length 4 <= min 4).
612
- :length_toolongex : [/max-ex-only/0] 'hogehoge': too long (length 8 >= max 8).
613
- :length_toolongex : [/max-ex-and-min-ex/0] 'foobarba': too long (length 8 >= max 8).
614
- :length_tooshortex : [/max-ex-and-min-ex/1] 'foo!': too short (length 4 <= min 4).
627
+ :length_tooshortex : (line 4)[/min-ex-only/0] 'foo!': too short (length 4 <= min 4).
628
+ :length_toolongex : (line 2)[/max-ex-only/0] 'hogehoge': too long (length 8 >= max 8).
629
+ :length_toolongex : (line 7)[/max-ex-and-min-ex/0] 'foobarba': too long (length 8 >= max 8).
630
+ :length_tooshortex : (line 8)[/max-ex-and-min-ex/1] 'foo!': too short (length 4 <= min 4).
615
631
  #
616
632
  ---
617
633
  name: length3
@@ -648,10 +664,10 @@ invalid: |
648
664
  - 123
649
665
  #
650
666
  error: |
651
- :length_toolong : [/A/0] 'hogehoge!': too long (length 9 > max 8).
652
- :length_tooshortex : [/A/1] '1234': too short (length 4 <= min 4).
653
- :length_toolongex : [/B/0] 'hogehoge': too long (length 8 >= max 8).
654
- :length_tooshort : [/B/1] '123': too short (length 3 < min 4).
667
+ :length_toolong : (line 2)[/A/0] 'hogehoge!': too long (length 9 > max 8).
668
+ :length_tooshortex : (line 3)[/A/1] '1234': too short (length 4 <= min 4).
669
+ :length_toolongex : (line 5)[/B/0] 'hogehoge': too long (length 8 >= max 8).
670
+ :length_tooshort : (line 6)[/B/1] '123': too short (length 3 < min 4).
655
671
  #
656
672
  ---
657
673
  name: assert1
@@ -688,10 +704,10 @@ invalid: |
688
704
  - except: 3.1
689
705
  #
690
706
  error: |
691
- :assert_failed : [/0/less-than] '8': assertion expression failed (val < 8).
692
- :assert_failed : [/1/more-than] '3': assertion expression failed (3 < val).
693
- :assert_failed : [/2/between] '2.9': assertion expression failed (3 < val && val < 8).
694
- :assert_failed : [/3/except] '3.1': assertion expression failed (val < 3 || 8 < val).
707
+ :assert_failed : (line 1)[/0/less-than] '8': assertion expression failed (val < 8).
708
+ :assert_failed : (line 2)[/1/more-than] '3': assertion expression failed (3 < val).
709
+ :assert_failed : (line 3)[/2/between] '2.9': assertion expression failed (3 < val && val < 8).
710
+ :assert_failed : (line 4)[/3/except] '3.1': assertion expression failed (val < 3 || 8 < val).
695
711
  #
696
712
  ---
697
713
  name: deftype1
@@ -718,10 +734,10 @@ invalid: |
718
734
  - email: 2004-01-01
719
735
  #
720
736
  error: |
721
- :type_unmatch : [/0/name] '123': not a string.
722
- :type_unmatch : [/0/email] 'true': not a string.
723
- :type_unmatch : [/1/name] '3.14': not a string.
724
- :type_unmatch : [/2/email] '2004-01-01': not a string.
737
+ :type_unmatch : (line 1)[/0/name] '123': not a string.
738
+ :type_unmatch : (line 2)[/0/email] 'true': not a string.
739
+ :type_unmatch : (line 3)[/1/name] '3.14': not a string.
740
+ :type_unmatch : (line 4)[/2/email] '2004-01-01': not a string.
725
741
  #
726
742
  ---
727
743
  name: ident1
@@ -754,7 +770,7 @@ invalid: |
754
770
  age: 10
755
771
  #
756
772
  error: |
757
- :value_notunique : [/2/name] 'bar': is already used at '/1/name'.
773
+ :value_notunique : (line 5)[/2/name] 'bar': is already used at '/1/name'.
758
774
  #
759
775
  ---
760
776
  name: unique1
@@ -787,7 +803,7 @@ invalid: |
787
803
  age: 10
788
804
  #
789
805
  error: |
790
- :value_notunique : [/2/name] 'bar': is already used at '/1/name'.
806
+ :value_notunique : (line 5)[/2/name] 'bar': is already used at '/1/name'.
791
807
  #
792
808
  ---
793
809
  name: unique2
@@ -814,5 +830,74 @@ invalid: |
814
830
  - bar
815
831
  #
816
832
  error: |
817
- :value_notunique : [/4] 'bar': is already used at '/2'.
833
+ :value_notunique : (line 5)[/4] 'bar': is already used at '/2'.
834
+ #
835
+ ---
836
+ name: default1
837
+ desc: default value of map
838
+ #
839
+ schema: |
840
+ type: map
841
+ mapping:
842
+ =:
843
+ type: number
844
+ range: { min: -10, max: 10 }
845
+ #
846
+ valid: |
847
+ value1: 0
848
+ value2: 10
849
+ value3: -10
850
+ #
851
+ invalid: |
852
+ value1: 0
853
+ value2: 20
854
+ value3: -20
855
+ error: |
856
+ :range_toolarge : (line 2)[/value2] '20': too large (> max 10).
857
+ :range_toosmall : (line 3)[/value3] '-20': too small (< min -10).
858
+ ---
859
+ name: merge1
860
+ desc: merge maps
861
+ #
862
+ schema: |
863
+ type: map
864
+ mapping:
865
+ "group":
866
+ type: map
867
+ mapping:
868
+ "name": &name
869
+ type: str
870
+ required: yes
871
+ "email": &email
872
+ type: str
873
+ pattern: /@/
874
+ required: no
875
+ "user":
876
+ type: map
877
+ mapping:
878
+ "name":
879
+ <<: *name # merge
880
+ length: { max: 16 } # add
881
+ "email":
882
+ <<: *email # merge
883
+ required: yes # override
884
+ #
885
+ valid: |
886
+ group:
887
+ name: foo
888
+ email: foo@mail.com
889
+ user:
890
+ name: bar
891
+ email: bar@mail.com
892
+ #
893
+ invalid: |
894
+ group:
895
+ name: foo
896
+ email: foo@mail.com
897
+ user:
898
+ name: toooooo-looooong-naaaame
899
+ #
900
+ error: |
901
+ :required_nokey : (line 4)[/user] key 'email:' is required.
902
+ :length_toolong : (line 5)[/user/name] 'toooooo-looooong-naaaame': too long (length 24 > max 16).
818
903
  #
data/test/test.rb CHANGED
@@ -1,25 +1,22 @@
1
1
  ###
2
- ### $Rev: 15 $
3
- ### $Release: 0.3.0 $
2
+ ### $Rev: 31 $
3
+ ### $Release: 0.4.0 $
4
4
  ### copyright(c) 2005 kuwata-lab all rights reserved.
5
5
  ###
6
6
 
7
- testdir = File.dirname(__FILE__)
8
- libdir = File.dirname(testdir) + "/lib"
9
- $LOAD_PATH << libdir << testdir
10
- require 'test/unit'
11
- require 'test/unit/ui/console/testrunner'
12
- require 'kwalify'
13
- require 'kwalify/util/assert-diff'
14
- require 'yaml'
7
+ unless defined?(TESTDIR)
8
+ TESTDIR = File.dirname(__FILE__)
9
+ libdir = File.dirname(TESTDIR) + "/lib"
10
+ $LOAD_PATH << libdir << TESTDIR
11
+ end
15
12
 
16
13
  require 'test-validator.rb'
17
14
  require 'test-metavalidator.rb'
15
+ require 'test-parser.rb'
18
16
 
19
- if $0 == __FILE__
20
- suite = Test::Unit::TestSuite.new()
21
- suite << ValidatorTest.suite()
22
- suite << MetaValidatorTest.suite()
23
- Test::Unit::UI::Console::TestRunner.run(suite)
24
- end
17
+ #suite = Test::Unit::TestSuite.new()
18
+ #suite << ValidatorTest.suite()
19
+ #suite << MetaValidatorTest.suite()
20
+ #suite << ParserTest.suite()
21
+ #Test::Unit::UI::Console::TestRunner.run(suite)
25
22
 
data/todo.txt CHANGED
@@ -21,7 +21,7 @@
21
21
  .- [v] separate test script
22
22
  .- [v] 'range:' or 'value:'
23
23
  .- [v] 'length:' or 'width:'
24
- .- [_] range 'max-ex' and 'min-ex' (or 'ceiling' and 'floor')
24
+ .- [v] range 'max-ex' and 'min-ex' (or 'ceiling' and 'floor')
25
25
  .- [_] range 'noteq'
26
26
  .- [_] range 'reverse' or 'complement'
27
27
  .- [v] type 'scalar'