kwalify 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,490 @@
1
+ ###
2
+ ### $Rev: 16 $
3
+ ### $Release: 0.2.0 $
4
+ ### copyright(c) 2005 kuwata-lab all rights reserved.
5
+ ###
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'
16
+ end
17
+
18
+
19
+ class MetaValidatorTest < Test::Unit::TestCase
20
+
21
+ ## define test methods
22
+ #str = DATA.read() # doesn't work when required by other script
23
+ str = File.read(__FILE__)
24
+ str.gsub!(/.*^__END__$/m, '')
25
+ name_table = {}
26
+ YAML.load_documents(str) do |doc|
27
+ doc.default = ''
28
+ !name_table.key?(doc['name']) or raise "*** test name '#{doc['name']}' is duplicated."
29
+ name_table[doc['name']] = true
30
+ s = <<-END
31
+ def test_meta_#{doc['name']}
32
+ @name = #{doc['name'].dump}
33
+ @desc = #{doc['desc'].dump}
34
+ @schema = #{doc['schema'].dump}
35
+ @meta_msg = #{doc['meta-msg'].dump}
36
+ # @rule_msg = #{doc['rule-msg'].dump}
37
+ _test(:meta)
38
+ end
39
+ def test_rule_#{doc['name']}
40
+ @name = #{doc['name'].dump}
41
+ @desc = #{doc['desc'].dump}
42
+ @schema = #{doc['schema'].dump}
43
+ # @meta_msg = #{doc['meta-msg'].dump}
44
+ @rule_msg = #{doc['rule-msg'].dump}
45
+ _test(:rule)
46
+ end
47
+ END
48
+ eval s
49
+ end
50
+
51
+ ## execute test
52
+ def _test(test_type)
53
+ return if $target && $target != @name
54
+ schema = YAML.load(@schema)
55
+ case test_type
56
+ when :meta
57
+ meta_validator = Kwalify.meta_validator()
58
+ errors = meta_validator.validate(schema)
59
+ expected = @meta_msg
60
+ when :rule
61
+ errors = []
62
+ begin
63
+ rule = Kwalify::Validator.new(schema)
64
+ rescue Kwalify::KwalifyError => error
65
+ errors << error
66
+ end
67
+ expected = @rule_msg
68
+ end
69
+ actual = ''
70
+ errors.each do |error|
71
+ raise error if error.is_a?(Kwalify::AssertionError)
72
+ actual << "%-20s: %s\n" % [error.error_symbol.inspect, error.message]
73
+ end
74
+ if $print
75
+ print actual
76
+ else
77
+ assert_equal_with_diff(expected, actual)
78
+ end
79
+ end
80
+
81
+ end
82
+
83
+ __END__
84
+ ---
85
+ name: schema_notmap
86
+ desc: schema_notmap
87
+ #
88
+ schema: |
89
+ - type: str
90
+ - type: int
91
+ #
92
+ meta-msg: |
93
+ :type_unmatch : [/] not a mapping.
94
+ #
95
+ rule-msg: |
96
+ :schema_notmap : [/] schema definition is not a mapping.
97
+ # :invalid_schema : [/] schema definition is invalid.
98
+ #
99
+ ---
100
+ name: type_unknown
101
+ desc: type_unknown
102
+ #
103
+ schema: |
104
+ type: map
105
+ mapping:
106
+ "name1":
107
+ type: str
108
+ "name2":
109
+ type: string
110
+ "age1":
111
+ type: int
112
+ "age2":
113
+ type: integer
114
+ "desc1":
115
+ type: text
116
+ "amount1":
117
+ type: float
118
+ "amount2":
119
+ type: number
120
+ "birth1":
121
+ type: date
122
+ "birth2":
123
+ type: time
124
+ "birth3":
125
+ type: timestamp
126
+ "data1":
127
+ type: scalar
128
+ "data2":
129
+ type: schalar
130
+ #
131
+ meta-msg: |
132
+ :enum_notexist : [/mapping/age2/type] 'integer': invalid type value.
133
+ :enum_notexist : [/mapping/name2/type] 'string': invalid type value.
134
+ :enum_notexist : [/mapping/data2/type] 'schalar': invalid type value.
135
+ #
136
+ rule-msg: |
137
+ :type_unknown : [/mapping/data2/type] 'schalar': unknown type.
138
+ # :type_unknown : [/mapping/data2] type 'schalar' is not found.
139
+ #
140
+ ---
141
+ name: required_notbool
142
+ desc: required_notbool
143
+ #
144
+ schema: |
145
+ type: map
146
+ mapping:
147
+ "name":
148
+ type: str
149
+ required: 1
150
+ #
151
+ meta-msg: |
152
+ :type_unmatch : [/mapping/name/required] '1': not a boolean.
153
+ #
154
+ rule-msg: |
155
+ :required_notbool : [/mapping/name/required] '1': not a boolean.
156
+ # :required_notbool : [/mapping/name] 'required:' is not boolean.
157
+ #
158
+ ---
159
+ name: pattern_syntaxerr
160
+ desc: pattern_syntaxerr
161
+ #
162
+ schema: |
163
+ type: str
164
+ pattern: /[A-/
165
+ #
166
+ meta-msg: |
167
+ :pattern_syntaxerr : [/pattern] '/[A-/': has regexp error.
168
+ #
169
+ rule-msg: |
170
+ :pattern_syntaxerr : [/pattern] '/[A-/': has regexp error.
171
+ # :pattern_syntaxerr : [/] 'pattern:' has regexp error (= /[A-/).
172
+ #
173
+ ---
174
+ name: enum_notseq
175
+ desc: enum_notseq
176
+ #
177
+ schema: |
178
+ type: str
179
+ enum: A, B, C
180
+ #
181
+ meta-msg: |
182
+ :type_unmatch : [/enum] 'A, B, C': not a sequence.
183
+ #
184
+ rule-msg: |
185
+ :enum_notseq : [/enum] 'A, B, C': not a sequence.
186
+ # :enum_notseq : [/] 'enum:' is not a sequence.
187
+ #
188
+ ---
189
+ name: assert_notstr
190
+ desc: assert_notstr
191
+ #
192
+ schema: |
193
+ type: number
194
+ assert: 100
195
+ #
196
+ meta-msg: |
197
+ :type_unmatch : [/assert] '100': not a string.
198
+ #
199
+ rule-msg: |
200
+ :assert_notstr : [/assert] '100': not a string.
201
+ # :assert_notstr : [/] 'assert:' is not a string.
202
+ #
203
+ ---
204
+ name: assert_noval
205
+ desc: assert_noval
206
+ #
207
+ schema: |
208
+ type: int
209
+ assert: value > 100
210
+ #
211
+ meta-msg: |
212
+ :pattern_unmatch : [/assert] 'value > 100': not matched to pattern /\bval\b/.
213
+ #
214
+ rule-msg: |
215
+ :assert_noval : [/assert] 'value > 100': 'val' is not used.
216
+ ---
217
+ name: assert_syntaxerr
218
+ desc: assert_syntaxerr
219
+ #
220
+ schema: |
221
+ type: int
222
+ assert: 0 < val &&
223
+ #
224
+ meta-msg: |
225
+ :assert_syntaxerr : [/assert] '0 < val &&': expression syntax error.
226
+ #
227
+ rule-msg: |
228
+ :assert_syntaxerr : [/assert] '0 < val &&': expression syntax error.
229
+ #
230
+ ---
231
+ name: range_notmap
232
+ desc: range_notmap
233
+ #
234
+ schema: |
235
+ type: int
236
+ range: 0 < val && val < 100
237
+ #
238
+ meta-msg: |
239
+ :type_unmatch : [/range] '0 < val && val < 100': not a mapping.
240
+ #
241
+ rule-msg: |
242
+ :range_notmap : [/range] '0 < val && val < 100': not a mapping.
243
+ #
244
+ ---
245
+ name: range_type_unmatch
246
+ desc: range_type_unmatch
247
+ #
248
+ schema: |
249
+ type: int
250
+ range: { max: 10, min: 1.0 }
251
+ #
252
+ meta-msg: |
253
+ :range_type_unmatch : [/range/min] '1.0': not a integer.
254
+ #
255
+ rule-msg: |
256
+ :range_type_unmatch : [/range/min] '1.0': not a integer.
257
+ ---
258
+ name: range_undefined
259
+ desc: range_undefined
260
+ #
261
+ schema: |
262
+ type: int
263
+ range: { max: 10, mim: 1 }
264
+ #
265
+ meta-msg: |
266
+ :key_undefined : [/range] key 'mim:' is undefined.
267
+ # :key_undefined : [/range] 'mim:': undefined key.
268
+ #
269
+ rule-msg: |
270
+ :range_undefined : [/range] 'mim:': undefined key.
271
+ # :range_undefined : [/range] key 'mim:' undefined key.
272
+ #
273
+ ---
274
+ name: length_notmap
275
+ desc: length_notmap
276
+ #
277
+ schema: |
278
+ type: text
279
+ length: [ max:10, min:4 ]
280
+ #
281
+ meta-msg: |
282
+ :type_unmatch : [/length] not a mapping.
283
+ #
284
+ rule-msg: |
285
+ :length_notmap : [/length] not a mapping.
286
+ #
287
+ ---
288
+ name: length_notint
289
+ desc: length_notint
290
+ #
291
+ schema: |
292
+ type: text
293
+ length: { max: 10.1 }
294
+ #
295
+ meta-msg: |
296
+ :type_unmatch : [/length/max] '10.1': not a integer.
297
+ #
298
+ rule-msg: |
299
+ :length_notint : [/length/max] '10.1': not an integer.
300
+ #
301
+ ---
302
+ name: length_undefined
303
+ desc: length_undefined
304
+ #
305
+ schema: |
306
+ type: text
307
+ length: { maximum: 10 }
308
+ #
309
+ meta-msg: |
310
+ :key_undefined : [/length] key 'maximum:' is undefined.
311
+ # :key_undefined : [/length] 'maximum:': undefined key.
312
+ #
313
+ rule-msg: |
314
+ :length_undefined : [/length] 'maximum:': undefined key.
315
+ # :length_undefined : [/length] key 'maximum:' is undefined.
316
+ #
317
+ ---
318
+ name: sequence_notseq
319
+ desc: sequence_notseq
320
+ #
321
+ schema: |
322
+ type: seq
323
+ sequence: |
324
+ - type: str
325
+ #
326
+ meta-msg: |
327
+ :type_unmatch : [/sequence] '- type: str\n': not a sequence.
328
+ #
329
+ rule-msg: |
330
+ :sequence_notseq : [/sequence] '- type: str\n': not a sequence.
331
+ #
332
+ ---
333
+ name: sequence_noelem
334
+ desc: sequence_noelem
335
+ #
336
+ schema: |
337
+ type: seq
338
+ sequence:
339
+ #
340
+ meta-msg: |
341
+ :sequence_noelem : [/sequence] required one element.
342
+ # :type_unmatch : [/sequence] not a sequence.
343
+ #
344
+ rule-msg: |
345
+ :sequence_noelem : [/sequence] required one element.
346
+ #
347
+ ---
348
+ name: sequence_toomany
349
+ desc: sequence_toomany
350
+ #
351
+ schema: |
352
+ type: seq
353
+ sequence:
354
+ - type: text
355
+ - type: text
356
+ #
357
+ meta-msg: |
358
+ :sequence_toomany : [/sequence] required just one element.
359
+ #
360
+ rule-msg: |
361
+ :sequence_toomany : [/sequence] required just one element.
362
+ #
363
+ ---
364
+ name: mapping_notmap
365
+ desc: mapping_notmap
366
+ #
367
+ schema: |
368
+ type: map
369
+ mapping: |
370
+ "name":
371
+ type: str
372
+ #
373
+ meta-msg: |
374
+ :type_unmatch : [/mapping] '"name":\n type: str\n': not a mapping.
375
+ #
376
+ rule-msg: |
377
+ :mapping_notmap : [/mapping] '"name":\n type: str\n': not a mapping.
378
+ #
379
+ ---
380
+ name: mapping_noelem
381
+ desc: mapping_noelem
382
+ #
383
+ schema: |
384
+ type: map
385
+ mapping:
386
+ #
387
+ meta-msg: |
388
+ :mapping_noelem : [/mapping] required at least one element.
389
+ #
390
+ rule-msg: |
391
+ :mapping_noelem : [/mapping] required at least one element.
392
+ #
393
+ ---
394
+ name: key_unknown
395
+ desc: key_unknown
396
+ #
397
+ schema: |
398
+ type: str
399
+ values:
400
+ - one
401
+ - two
402
+ #
403
+ meta-msg: |
404
+ :key_undefined : [/] key 'values:' is undefined.
405
+ # :key_undefined : [/] 'values:': undefined key.
406
+ #
407
+ rule-msg: |
408
+ :key_unknown : [/] 'values:': unknown key.
409
+ # :key_unknown : [/] key 'values:' is unknown.
410
+ #
411
+ ---
412
+ name: seq_nosequence
413
+ desc: seq_nosequence
414
+ #
415
+ schema: |
416
+ type: seq
417
+ #
418
+ meta-msg: |
419
+ :seq_nosequence : [/] type 'seq' requires 'sequence:'.
420
+ #
421
+ rule-msg: |
422
+ :seq_nosequence : [/] type 'seq' requires 'sequence:'.
423
+ #
424
+ ---
425
+ name: seq_conflict
426
+ desc: seq_conflict
427
+ #
428
+ schema: |
429
+ type: seq
430
+ mapping: { name: {type: str} }
431
+ sequence:
432
+ - type: str
433
+ # pattern: /abc/
434
+ # range: { max: 10 }
435
+ # length: { max: 10 }
436
+ # enum: [ A, B, C ]
437
+ #
438
+ meta-msg: |
439
+ :seq_conflict : [/] 'mapping:': not available with sequence.
440
+ # :seq_conflict : [/] 'enum:': not available with sequence.
441
+ # :seq_conflict : [/] 'pattern:': not available with sequence.
442
+ # :seq_conflict : [/] 'range:': not available with sequence.
443
+ # :seq_conflict : [/] 'length:': not available with sequence.
444
+ #
445
+ rule-msg: |
446
+ :seq_conflict : [/] 'mapping:': not available with sequence.
447
+ # :enum_notscalar : [/] 'enum:': not available with seq or map.
448
+ # :seq_conflict : [/] 'enum:': not available with sequence.
449
+ # :seq_conflict : [/] 'pattern:': not available with sequence.
450
+ # :seq_conflict : [/] 'range:': not available with sequence.
451
+ # :seq_conflict : [/] 'length:': not available with sequence.
452
+ #
453
+ ---
454
+ name: map_nomapping
455
+ desc: map_nomapping
456
+ #
457
+ schema: |
458
+ type: map
459
+ required: true
460
+ #
461
+ meta-msg: |
462
+ :map_nomapping : [/] type 'map' requires 'mapping:'.
463
+ #
464
+ rule-msg: |
465
+ :map_nomapping : [/] type 'map' requires 'mapping:'.
466
+ #
467
+ ---
468
+ name: enum_conflict
469
+ desc: enum_conflict
470
+ #
471
+ schema: |
472
+ type: str
473
+ required: yes
474
+ enum:
475
+ - aa
476
+ - bb
477
+ range: { max: aa, min: xx }
478
+ length: { max: 3 }
479
+ pattern: /0/
480
+ #
481
+ meta-msg: |
482
+ :enum_conflict : [/] 'range:': not available with 'enum:'.
483
+ :enum_conflict : [/] 'length:': not available with 'enum:'.
484
+ :enum_conflict : [/] 'pattern:': not available with 'enum:'.
485
+ #
486
+ rule-msg: |
487
+ :enum_conflict : [/] 'range:': not available with 'enum:'.
488
+ # :enum_conflict : [/] 'length:': not available with 'enum:'.
489
+ # :enum_conflict : [/] 'pattern:': not available with 'enum:'.
490
+ #