kwalify 0.4.1 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  ###
2
- ### $Rev: 39 $
3
- ### $Release: 0.4.1 $
2
+ ### $Rev: 42 $
3
+ ### $Release: 0.5.0 $
4
4
  ### copyright(c) 2005 kuwata-lab all rights reserved.
5
5
  ###
6
6
 
@@ -20,43 +20,57 @@ require 'yaml'
20
20
  class MetaValidatorTest < Test::Unit::TestCase
21
21
 
22
22
  ## define test methods
23
- #str = DATA.read() # doesn't work when required by other script
24
- str = File.read(__FILE__)
25
- str.gsub!(/.*^__END__$/m, '')
26
- name_table = {}
23
+ filename = __FILE__.sub(/\.rb$/, '.yaml')
24
+ str = File.read(filename)
25
+ @@docs = {}
27
26
  YAML.load_documents(str) do |doc|
28
- doc.default = ''
29
- !name_table.key?(doc['name']) or raise "*** test name '#{doc['name']}' is duplicated."
30
- name_table[doc['name']] = true
27
+ #doc.default = ''
28
+ name = doc['name']
29
+ !@@docs.key?(name) or raise "*** test name '#{name}' is duplicated."
30
+ doc.each do |key, val|
31
+ if key =~ /(.*)\*$/
32
+ doc[$1] = val['ruby']
33
+ end
34
+ end
35
+ @@docs[name] = doc
31
36
  s = <<-END
32
- def test_meta_#{doc['name']}
33
- @name = #{doc['name'].dump}
34
- @desc = #{doc['desc'].dump}
35
- @schema = #{doc['schema'].dump}
36
- @meta_msg = #{doc['meta-msg'].dump}
37
- # @rule_msg = #{doc['rule-msg'].dump}
37
+ def test_meta_#{name}
38
+ doc = @@docs['#{name}']
39
+ @name = doc['name']
40
+ @desc = doc['desc']
41
+ @schema = doc['schema']
42
+ @meta_msg = doc['meta-msg']
43
+ # @rule_msg = doc['rule-msg']
38
44
  _test(:meta)
39
45
  end
40
- def test_rule_#{doc['name']}
41
- @name = #{doc['name'].dump}
42
- @desc = #{doc['desc'].dump}
43
- @schema = #{doc['schema'].dump}
44
- # @meta_msg = #{doc['meta-msg'].dump}
45
- @rule_msg = #{doc['rule-msg'].dump}
46
+ END
47
+ eval s if doc['meta-msg']
48
+ s = <<-END
49
+ def test_rule_#{name}
50
+ doc = @@docs['#{name}']
51
+ @name = doc['name']
52
+ @desc = doc['desc']
53
+ @schema = doc['schema']
54
+ # @meta_msg = doc['meta-msg']
55
+ @rule_msg = doc['rule-msg']
46
56
  _test(:rule)
47
57
  end
48
58
  END
49
- eval s
59
+ eval s if doc['rule-msg']
50
60
  end
51
61
 
52
62
  ## execute test
53
63
  def _test(test_type)
54
64
  return if $target && $target != @name
55
- schema = YAML.load(@schema)
65
+ #schema = YAML.load(@schema)
66
+ parser = Kwalify::YamlParser.new(@schema)
67
+ schema = parser.parse()
56
68
  case test_type
57
69
  when :meta
58
- meta_validator = Kwalify.meta_validator()
70
+ meta_validator = Kwalify::MetaValidator.instance()
59
71
  errors = meta_validator.validate(schema)
72
+ parser.set_errors_linenum(errors)
73
+ errors.sort!
60
74
  expected = @meta_msg
61
75
  when :rule
62
76
  errors = []
@@ -84,700 +98,3 @@ end
84
98
  #if $0 == __FILE__
85
99
  # Test::Unit::UI::Console::TestRunner.run(MetaValidatorTest)
86
100
  #end
87
-
88
- __END__
89
- ---
90
- name: schema_notmap
91
- desc: schema_notmap
92
- #
93
- schema: |
94
- - type: str
95
- - type: int
96
- #
97
- meta-msg: |
98
- :type_unmatch : [/] not a mapping.
99
- #
100
- rule-msg: |
101
- :schema_notmap : [/] schema definition is not a mapping.
102
- # :invalid_schema : [/] schema definition is invalid.
103
- #
104
- ---
105
- name: type_unknown
106
- desc: type_unknown
107
- #
108
- schema: |
109
- type: map
110
- mapping:
111
- "name1":
112
- type: str
113
- "name2":
114
- type: string
115
- "age1":
116
- type: int
117
- "age2":
118
- type: integer
119
- "desc1":
120
- type: text
121
- "amount1":
122
- type: float
123
- "amount2":
124
- type: number
125
- "birth1":
126
- type: date
127
- "birth2":
128
- type: time
129
- "birth3":
130
- type: timestamp
131
- "data1":
132
- type: scalar
133
- "data2":
134
- type: schalar
135
- #
136
- meta-msg: |
137
- :enum_notexist : [/mapping/age2/type] 'integer': invalid type value.
138
- :enum_notexist : [/mapping/name2/type] 'string': invalid type value.
139
- :enum_notexist : [/mapping/data2/type] 'schalar': invalid type value.
140
- #
141
- rule-msg: |
142
- :type_unknown : [/mapping/age2/type] 'integer': unknown type.
143
- # :type_unknown : [/mapping/data2/type] 'schalar': unknown type.
144
- # :type_unknown : [/mapping/data2] type 'schalar' is not found.
145
- #
146
- ---
147
- name: required_notbool
148
- desc: required_notbool
149
- #
150
- schema: |
151
- type: map
152
- mapping:
153
- "name":
154
- type: str
155
- required: 1
156
- #
157
- meta-msg: |
158
- :type_unmatch : [/mapping/name/required] '1': not a boolean.
159
- #
160
- rule-msg: |
161
- :required_notbool : [/mapping/name/required] '1': not a boolean.
162
- # :required_notbool : [/mapping/name] 'required:' is not boolean.
163
- #
164
- ---
165
- name: pattern_syntaxerr
166
- desc: pattern_syntaxerr
167
- #
168
- schema: |
169
- type: str
170
- pattern: /[A-/
171
- #
172
- meta-msg: |
173
- :pattern_syntaxerr : [/pattern] '/[A-/': has regexp error.
174
- #
175
- rule-msg: |
176
- :pattern_syntaxerr : [/pattern] '/[A-/': has regexp error.
177
- # :pattern_syntaxerr : [/] 'pattern:' has regexp error (= /[A-/).
178
- #
179
- ---
180
- name: enum_notseq
181
- desc: enum_notseq
182
- #
183
- schema: |
184
- type: str
185
- enum: A, B, C
186
- #
187
- meta-msg: |
188
- :type_unmatch : [/enum] 'A, B, C': not a sequence.
189
- #
190
- rule-msg: |
191
- :enum_notseq : [/enum] 'A, B, C': not a sequence.
192
- # :enum_notseq : [/] 'enum:' is not a sequence.
193
- #
194
- ---
195
- name: enum_notscalar
196
- desc: enum_notscalar
197
- #
198
- schema: |
199
- type: seq
200
- enum:
201
- - A
202
- - B
203
- sequence:
204
- - type: str
205
- #
206
- meta-msg: |
207
- :enum_notscalar : [/] 'enum:': not available with seq or map.
208
- #
209
- rule-msg: |
210
- :enum_notscalar : [/] 'enum:': not available with seq or map.
211
- #
212
- ---
213
- name: enum_duplicate
214
- desc: enum_duplicate
215
- #
216
- schema: |
217
- enum:
218
- - A
219
- - B
220
- - A
221
- #
222
- meta-msg: |
223
- :enum_duplicate : [/enum] 'A': duplicated enum value.
224
- #
225
- rule-msg: |
226
- :enum_duplicate : [/enum] 'A': duplicated enum value.
227
- #
228
- ---
229
- name: enum_type_unmatch
230
- desc: enum_type_unmatch
231
- #
232
- schema: |
233
- enum:
234
- - 100
235
- - 200
236
- #
237
- meta-msg: |
238
- :enum_type_unmatch : [/enum] '100': string type expected.
239
- :enum_type_unmatch : [/enum] '200': string type expected.
240
- #
241
- rule-msg: |
242
- :enum_type_unmatch : [/enum] '100': string type expected.
243
- #
244
- ---
245
- name: assert_notstr
246
- desc: assert_notstr
247
- #
248
- schema: |
249
- type: number
250
- assert: 100
251
- #
252
- meta-msg: |
253
- :type_unmatch : [/assert] '100': not a string.
254
- #
255
- rule-msg: |
256
- :assert_notstr : [/assert] '100': not a string.
257
- # :assert_notstr : [/] 'assert:' is not a string.
258
- #
259
- ---
260
- name: assert_noval
261
- desc: assert_noval
262
- #
263
- schema: |
264
- type: int
265
- assert: value > 100
266
- #
267
- meta-msg: |
268
- :pattern_unmatch : [/assert] 'value > 100': not matched to pattern /\bval\b/.
269
- #
270
- rule-msg: |
271
- :assert_noval : [/assert] 'value > 100': 'val' is not used.
272
- ---
273
- name: assert_syntaxerr
274
- desc: assert_syntaxerr
275
- #
276
- schema: |
277
- type: int
278
- assert: 0 < val &&
279
- #
280
- meta-msg: |
281
- :assert_syntaxerr : [/assert] '0 < val &&': expression syntax error.
282
- #
283
- rule-msg: |
284
- :assert_syntaxerr : [/assert] '0 < val &&': expression syntax error.
285
- #
286
- ---
287
- name: range_notmap
288
- desc: range_notmap
289
- #
290
- schema: |
291
- type: int
292
- range: 0 < val && val < 100
293
- #
294
- meta-msg: |
295
- :type_unmatch : [/range] '0 < val && val < 100': not a mapping.
296
- #
297
- rule-msg: |
298
- :range_notmap : [/range] '0 < val && val < 100': not a mapping.
299
- #
300
- ---
301
- name: range_notscalar
302
- desc: range_notscalar
303
- #
304
- schema: |
305
- type: seq
306
- sequence:
307
- - type: str
308
- range: { max: 10, min: 10 }
309
- #
310
- meta-msg: |
311
- :range_notscalar : [/] 'range:': is available only with scalar type.
312
- #
313
- rule-msg: |
314
- :range_notscalar : [/] 'range:': is available only with scalar type.
315
- #
316
- ---
317
- name: range_type_unmatch
318
- desc: range_type_unmatch
319
- #
320
- schema: |
321
- type: int
322
- range: { max: 10, min: 1.0 }
323
- #
324
- meta-msg: |
325
- :range_type_unmatch : [/range/min] '1.0': not a integer.
326
- #
327
- rule-msg: |
328
- :range_type_unmatch : [/range/min] '1.0': not a integer.
329
- ---
330
- name: range_undefined
331
- desc: range_undefined
332
- #
333
- schema: |
334
- type: int
335
- range: { max: 10, mim: 1 }
336
- #
337
- meta-msg: |
338
- :key_undefined : [/range/mim] key 'mim:' is undefined.
339
- # :key_undefined : [/range/mim] 'mim:': undefined key.
340
- #
341
- rule-msg: |
342
- :range_undefined : [/range/mim] 'mim:': undefined key.
343
- # :range_undefined : [/range/mim] key 'mim:' undefined key.
344
- #
345
- ---
346
- name: range_twomax
347
- desc: range_twomax
348
- #
349
- schema: |
350
- type: float
351
- range: { max: 10.0, max-ex: 1.0 }
352
- #
353
- meta-msg: |
354
- :range_twomax : [/range] both 'max' and 'max-ex' are not available at once.
355
- #
356
- rule-msg: |
357
- :range_twomax : [/range] both 'max' and 'max-ex' are not available at once.
358
- #
359
- ---
360
- name: length_notmap
361
- desc: length_notmap
362
- #
363
- schema: |
364
- type: text
365
- length: [ max:10, min:4 ]
366
- #
367
- meta-msg: |
368
- :type_unmatch : [/length] not a mapping.
369
- #
370
- rule-msg: |
371
- :length_notmap : [/length] not a mapping.
372
- #
373
- ---
374
- name: length_nottext
375
- desc: length_nottext
376
- #
377
- schema: |
378
- type: number
379
- length: { max: 10, min: 0 }
380
- #
381
- meta-msg: |
382
- :length_nottext : [/] 'length:': is available only with string or text.
383
- #
384
- rule-msg: |
385
- :length_nottext : [/] 'length:': is available only with string or text.
386
- #
387
- ---
388
- name: length_notint
389
- desc: length_notint
390
- #
391
- schema: |
392
- type: text
393
- length: { max: 10.1 }
394
- #
395
- meta-msg: |
396
- :type_unmatch : [/length/max] '10.1': not a integer.
397
- #
398
- rule-msg: |
399
- :length_notint : [/length/max] '10.1': not an integer.
400
- #
401
- ---
402
- name: length_undefined
403
- desc: length_undefined
404
- #
405
- schema: |
406
- type: text
407
- length: { maximum: 10 }
408
- #
409
- meta-msg: |
410
- :key_undefined : [/length/maximum] key 'maximum:' is undefined.
411
- # :key_undefined : [/length/maximum] 'maximum:': undefined key.
412
- #
413
- rule-msg: |
414
- :length_undefined : [/length/maximum] 'maximum:': undefined key.
415
- # :length_undefined : [/length/maximum] key 'maximum:' is undefined.
416
- #
417
- ---
418
- name: length_twomax
419
- desc: length_twomax
420
- #
421
- schema: |
422
- type: str
423
- length: { max: 10, max-ex: 1 }
424
- #
425
- meta-msg: |
426
- :length_twomax : [/length] both 'max' and 'max-ex' are not available at once.
427
- #
428
- rule-msg: |
429
- :length_twomax : [/length] both 'max' and 'max-ex' are not available at once.
430
- #
431
- ---
432
- name: sequence_notseq
433
- desc: sequence_notseq
434
- #
435
- schema: |
436
- type: seq
437
- sequence: |
438
- - type: str
439
- #
440
- meta-msg: |
441
- :type_unmatch : [/sequence] '- type: str\n': not a sequence.
442
- #
443
- rule-msg: |
444
- :sequence_notseq : [/sequence] '- type: str\n': not a sequence.
445
- #
446
- ---
447
- name: sequence_noelem
448
- desc: sequence_noelem
449
- #
450
- schema: |
451
- type: seq
452
- sequence:
453
- #
454
- meta-msg: |
455
- :sequence_noelem : [/sequence] required one element.
456
- # :type_unmatch : [/sequence] not a sequence.
457
- #
458
- rule-msg: |
459
- :sequence_noelem : [/sequence] required one element.
460
- #
461
- ---
462
- name: sequence_toomany
463
- desc: sequence_toomany
464
- #
465
- schema: |
466
- type: seq
467
- sequence:
468
- - type: text
469
- - type: text
470
- #
471
- meta-msg: |
472
- :sequence_toomany : [/sequence] required just one element.
473
- #
474
- rule-msg: |
475
- :sequence_toomany : [/sequence] required just one element.
476
- #
477
- ---
478
- name: mapping_notmap
479
- desc: mapping_notmap
480
- #
481
- schema: |
482
- type: map
483
- mapping: |
484
- "name":
485
- type: str
486
- #
487
- meta-msg: |
488
- :type_unmatch : [/mapping] '"name":\n type: str\n': not a mapping.
489
- #
490
- rule-msg: |
491
- :mapping_notmap : [/mapping] '"name":\n type: str\n': not a mapping.
492
- #
493
- ---
494
- name: mapping_noelem
495
- desc: mapping_noelem
496
- #
497
- schema: |
498
- type: map
499
- mapping:
500
- #
501
- meta-msg: |
502
- :mapping_noelem : [/mapping] required at least one element.
503
- #
504
- rule-msg: |
505
- :mapping_noelem : [/mapping] required at least one element.
506
- #
507
- ---
508
- name: key_unknown
509
- desc: key_unknown
510
- #
511
- schema: |
512
- type: str
513
- values:
514
- - one
515
- - two
516
- #
517
- meta-msg: |
518
- :key_undefined : [/values] key 'values:' is undefined.
519
- # :key_undefined : [/values] 'values:': undefined key.
520
- #
521
- rule-msg: |
522
- :key_unknown : [/values] 'values:': unknown key.
523
- # :key_unknown : [/values] key 'values:' is unknown.
524
- #
525
- ---
526
- name: seq_nosequence
527
- desc: seq_nosequence
528
- #
529
- schema: |
530
- type: seq
531
- #
532
- meta-msg: |
533
- :seq_nosequence : [/] type 'seq' requires 'sequence:'.
534
- #
535
- rule-msg: |
536
- :seq_nosequence : [/] type 'seq' requires 'sequence:'.
537
- #
538
- ---
539
- name: seq_conflict
540
- desc: seq_conflict
541
- #
542
- schema: |
543
- type: seq
544
- mapping: { name: {type: str} }
545
- sequence:
546
- - type: str
547
- # pattern: /abc/
548
- # range: { max: 10 }
549
- # length: { max: 10 }
550
- # enum: [ A, B, C ]
551
- #
552
- meta-msg: |
553
- :seq_conflict : [/] 'mapping:': not available with sequence.
554
- # :seq_conflict : [/] 'enum:': not available with sequence.
555
- # :seq_conflict : [/] 'pattern:': not available with sequence.
556
- # :seq_conflict : [/] 'range:': not available with sequence.
557
- # :seq_conflict : [/] 'length:': not available with sequence.
558
- #
559
- rule-msg: |
560
- :seq_conflict : [/] 'mapping:': not available with sequence.
561
- # :enum_notscalar : [/] 'enum:': not available with seq or map.
562
- # :seq_conflict : [/] 'enum:': not available with sequence.
563
- # :seq_conflict : [/] 'pattern:': not available with sequence.
564
- # :seq_conflict : [/] 'range:': not available with sequence.
565
- # :seq_conflict : [/] 'length:': not available with sequence.
566
- #
567
- ---
568
- name: map_nomapping
569
- desc: map_nomapping
570
- #
571
- schema: |
572
- type: map
573
- #
574
- meta-msg: |
575
- :map_nomapping : [/] type 'map' requires 'mapping:'.
576
- #
577
- rule-msg: |
578
- :map_nomapping : [/] type 'map' requires 'mapping:'.
579
- #
580
- ---
581
- name: map_conflict
582
- desc: map_conflict
583
- #
584
- schema: |
585
- type: map
586
- mapping:
587
- "name":
588
- type: str
589
- sequence:
590
- - type: str
591
- #
592
- meta-msg: |
593
- :map_conflict : [/] 'sequence:': not available with mapping.
594
- #
595
- rule-msg: |
596
- :map_conflict : [/] 'sequence:': not available with mapping.
597
- #
598
- ---
599
- name: scalar_confict
600
- desc: scalar_confict
601
- #
602
- schema: |
603
- type: int
604
- sequence:
605
- - type: str
606
- mapping:
607
- "name":
608
- type: str
609
- #
610
- meta-msg: |
611
- :scalar_conflict : [/] 'sequence:': not available with scalar type.
612
- :scalar_conflict : [/] 'mapping:': not available with scalar type.
613
- #
614
- rule-msg: |
615
- :scalar_conflict : [/] 'sequence:': not available with scalar type.
616
- # :scalar_conflict : [/] 'mapping:': not available with scalar type.
617
- #
618
- ---
619
- name: enum_conflict
620
- desc: enum_conflict
621
- #
622
- schema: |
623
- type: str
624
- required: yes
625
- enum:
626
- - aa
627
- - bb
628
- range: { max: aa, min: xx }
629
- length: { max: 3 }
630
- pattern: /0/
631
- #
632
- meta-msg: |
633
- :enum_conflict : [/] 'range:': not available with 'enum:'.
634
- :enum_conflict : [/] 'length:': not available with 'enum:'.
635
- :enum_conflict : [/] 'pattern:': not available with 'enum:'.
636
- #
637
- rule-msg: |
638
- :enum_conflict : [/] 'range:': not available with 'enum:'.
639
- # :enum_conflict : [/] 'length:': not available with 'enum:'.
640
- # :enum_conflict : [/] 'pattern:': not available with 'enum:'.
641
- #
642
- ---
643
- name: unique_notbool
644
- desc: unique_notbool
645
- #
646
- schema: |
647
- type: seq
648
- sequence:
649
- - type: map
650
- mapping:
651
- "id":
652
- type: int
653
- unique: 'yes'
654
- "name":
655
- type: str
656
- #
657
- meta-msg: |
658
- :type_unmatch : [/sequence/0/mapping/id/unique] 'yes': not a boolean.
659
- #
660
- rule-msg: |
661
- :unique_notbool : [/sequence/0/mapping/id/unique] 'yes': not a boolean.
662
- #
663
- ---
664
- name: unique_notscalar
665
- desc: unique_notscalar
666
- #
667
- schema: |
668
- type: seq
669
- sequence:
670
- - type: map
671
- mapping:
672
- "id":
673
- type: int
674
- unique: yes
675
- "values":
676
- type: seq
677
- unique: yes
678
- sequence:
679
- - type: str
680
- #
681
- meta-msg: |
682
- :unique_notscalar : [/sequence/0/mapping/values] 'unique:': is available only with a scalar type.
683
- #
684
- rule-msg: |
685
- :unique_notscalar : [/sequence/0/mapping/values] 'unique:': is available only with a scalar type.
686
- #
687
- ---
688
- name: unique_root
689
- desc: unique_root
690
- #
691
- schema: |
692
- type: str
693
- unique: yes
694
- #
695
- meta-msg: |
696
- :unique_onroot : [/] 'unique:': is not available on root element.
697
- #
698
- rule-msg: |
699
- :unique_onroot : [/] 'unique:': is not available on root element.
700
- #
701
- ---
702
- name: ident_notbool
703
- desc: ident_notbool
704
- #
705
- schema: |
706
- type: seq
707
- sequence:
708
- - type: map
709
- mapping:
710
- "id":
711
- type: int
712
- ident: 'yes'
713
- "name":
714
- type: str
715
- #
716
- meta-msg: |
717
- :type_unmatch : [/sequence/0/mapping/id/ident] 'yes': not a boolean.
718
- #
719
- rule-msg: |
720
- :ident_notbool : [/sequence/0/mapping/id/ident] 'yes': not a boolean.
721
- #
722
- ---
723
- name: ident_notscalar
724
- desc: ident_notscalar
725
- #
726
- schema: |
727
- type: seq
728
- sequence:
729
- - type: map
730
- mapping:
731
- "id":
732
- type: int
733
- ident: yes
734
- "values":
735
- type: seq
736
- ident: yes
737
- sequence:
738
- - type: str
739
- #
740
- meta-msg: |
741
- :ident_notscalar : [/sequence/0/mapping/values] 'ident:': is available only with a scalar type.
742
- #
743
- rule-msg: |
744
- :ident_notscalar : [/sequence/0/mapping/values] 'ident:': is available only with a scalar type.
745
- #
746
- ---
747
- name: ident_root
748
- desc: ident_root
749
- #
750
- schema: |
751
- type: str
752
- ident: yes
753
- #
754
- meta-msg: |
755
- :ident_onroot : [/] 'ident:': is not available on root element.
756
- #
757
- rule-msg: |
758
- :ident_onroot : [/] 'ident:': is not available on root element.
759
- #
760
- ---
761
- name: ident_notmap
762
- desc: ident_notmap
763
- #
764
- schema: |
765
- type: seq
766
- sequence:
767
- - type: map
768
- mapping:
769
- "id":
770
- type: int
771
- ident: yes
772
- "values":
773
- type: seq
774
- sequence:
775
- - type: str
776
- ident: yes
777
- #
778
- meta-msg: |
779
- :ident_notmap : [/sequence/0/mapping/values/sequence/0] 'ident:': is available only with an element of mapping.
780
- #
781
- rule-msg: |
782
- :ident_notmap : [/sequence/0/mapping/values/sequence/0] 'ident:': is available only with an element of mapping.
783
- #