rxsd 0.2 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,609 @@
1
+ # tests the resolver module
2
+ #
3
+ # Copyright (C) 2010 Mohammed Morsi <movitto@yahoo.com>
4
+ # See COPYING for the License of this software
5
+
6
+ require File.dirname(__FILE__) + '/spec_helper'
7
+
8
+ describe "Resolver" do
9
+
10
+ # test children method on all XSD classes
11
+
12
+ it "should return schema children" do
13
+ schema = Schema.new
14
+ elem1 = Element.new
15
+ elem2 = Element.new
16
+ st1 = SimpleType.new
17
+ ct1 = ComplexType.new
18
+ at1 = Attribute.new
19
+ at2 = Attribute.new
20
+ at3 = Attribute.new
21
+ atg1 = AttributeGroup.new
22
+ grp1 = Group.new
23
+ grp2 = Group.new
24
+
25
+ schema.elements = [elem1, elem2]
26
+ schema.simple_types = [st1]
27
+ schema.complex_types = [ct1]
28
+ schema.attributes = [at1, at2, at3]
29
+ schema.attribute_groups = [atg1]
30
+ schema.groups = [grp1, grp2]
31
+
32
+ children = schema.children
33
+ children.include?(elem1).should be_true
34
+ children.include?(elem2).should be_true
35
+ children.include?(st1).should be_true
36
+ children.include?(ct1).should be_true
37
+ children.include?(at1).should be_true
38
+ children.include?(at2).should be_true
39
+ children.include?(at3).should be_true
40
+ children.include?(atg1).should be_true
41
+ children.include?(grp1).should be_true
42
+ children.include?(grp2).should be_true
43
+ end
44
+
45
+ it "should return element children" do
46
+ elem = Element.new
47
+ st1 = SimpleType.new
48
+ ct1 = ComplexType.new
49
+ elem.simple_type = st1
50
+ elem.complex_type = ct1
51
+
52
+ children = elem.children
53
+ children.include?(st1).should be_true
54
+ children.include?(ct1).should be_true
55
+ end
56
+
57
+ it "should return complex type children" do
58
+ complex_type = ComplexType.new
59
+ at1 = Attribute.new
60
+ atg1 = AttributeGroup.new
61
+ simple_content = SimpleContent.new
62
+ complex_content = ComplexContent.new
63
+ choice = Choice.new
64
+ group = Group.new
65
+ sequence = Sequence.new
66
+
67
+ complex_type.attributes = [at1]
68
+ complex_type.attribute_groups = [atg1]
69
+ complex_type.simple_content = simple_content
70
+ complex_type.complex_content = complex_content
71
+ complex_type.choice = choice
72
+ complex_type.group = group
73
+ complex_type.sequence = sequence
74
+
75
+ children = complex_type.children
76
+ children.include?(at1).should be_true
77
+ children.include?(atg1).should be_true
78
+ children.include?(simple_content).should be_true
79
+ children.include?(complex_content).should be_true
80
+ children.include?(choice).should be_true
81
+ children.include?(group).should be_true
82
+ children.include?(sequence).should be_true
83
+ end
84
+
85
+ it "should return simple type children" do
86
+ simple_type = SimpleType.new
87
+ list = List.new
88
+ res = Restriction.new
89
+
90
+ simple_type.list = list
91
+ simple_type.restriction = res
92
+
93
+ children = simple_type.children
94
+ children.include?(list).should be_true
95
+ children.include?(res).should be_true
96
+ end
97
+
98
+ it "should return attribute children" do
99
+ att = Attribute.new
100
+ simple_type = SimpleType.new
101
+
102
+ att.simple_type = simple_type
103
+
104
+ children = att.children
105
+ children.include?(simple_type).should be_true
106
+ end
107
+
108
+ it "should return attribute group children" do
109
+ atg = AttributeGroup.new
110
+ at1 = Attribute.new
111
+ at2 = Attribute.new
112
+ atg1 = AttributeGroup.new
113
+ atg.attributes = [at1, at2]
114
+ atg.attribute_groups = [atg1]
115
+
116
+ children = atg.children
117
+ children.include?(at1).should be_true
118
+ children.include?(at2).should be_true
119
+ children.include?(atg1).should be_true
120
+ end
121
+
122
+ it "should return group children" do
123
+ grp = Group.new
124
+ choice = Choice.new
125
+ sequence = Sequence.new
126
+
127
+ grp.choice = choice
128
+ grp.sequence = sequence
129
+
130
+ children = grp.children
131
+ children.include?(choice).should be_true
132
+ children.include?(sequence).should be_true
133
+ end
134
+
135
+ it "should return list children" do
136
+ list = List.new
137
+ simple_type = SimpleType.new
138
+
139
+ list.simple_type = simple_type
140
+
141
+ children = list.children
142
+ children.include?(simple_type).should be_true
143
+ end
144
+
145
+ it "should return simple content children" do
146
+ simple_content = SimpleContent.new
147
+ ext = Extension.new
148
+ res = Restriction.new
149
+
150
+ simple_content.extension = ext
151
+ simple_content.restriction = res
152
+
153
+ children = simple_content.children
154
+ children.include?(ext).should be_true
155
+ children.include?(res).should be_true
156
+ end
157
+
158
+ it "should return choice children" do
159
+ choice = Choice.new
160
+ elem1 = Element.new
161
+ elem2 = Element.new
162
+ grp1 = Group.new
163
+ choice1 = Choice.new
164
+ choice2 = Choice.new
165
+ choice3 = Choice.new
166
+ sequence1 = Sequence.new
167
+
168
+ choice.elements = [elem1, elem2]
169
+ choice.groups = [grp1]
170
+ choice.choices = [choice1, choice2, choice3]
171
+ choice.sequences = [sequence1]
172
+
173
+ children = choice.children
174
+ children.include?(grp1).should be_true
175
+ children.include?(choice1).should be_true
176
+ children.include?(choice2).should be_true
177
+ children.include?(choice3).should be_true
178
+ children.include?(sequence1).should be_true
179
+ end
180
+
181
+ it "should return complex content children" do
182
+ complex_content = ComplexContent.new
183
+ ext = Extension.new
184
+ res = Restriction.new
185
+
186
+ complex_content.extension = ext
187
+ complex_content.restriction = res
188
+
189
+ children = complex_content.children
190
+ children.include?(ext).should be_true
191
+ children.include?(res).should be_true
192
+ end
193
+
194
+ it "should return sequence children" do
195
+ sequence = Sequence.new
196
+ elem1 = Element.new
197
+ elem2 = Element.new
198
+ grp1 = Group.new
199
+ choice1 = Choice.new
200
+ choice2 = Choice.new
201
+ choice3 = Choice.new
202
+ sequence1 = Sequence.new
203
+
204
+ sequence.elements = [elem1, elem2]
205
+ sequence.groups = [grp1]
206
+ sequence.choices = [choice1, choice2, choice3]
207
+ sequence.sequences = [sequence1]
208
+
209
+ children = sequence.children
210
+ children.include?(grp1).should be_true
211
+ children.include?(choice1).should be_true
212
+ children.include?(choice2).should be_true
213
+ children.include?(choice3).should be_true
214
+ children.include?(sequence1).should be_true
215
+ end
216
+
217
+ it "should return extension children" do
218
+ extension = Extension.new
219
+ grp1 = Group.new
220
+ choice1 = Choice.new
221
+ sequence1 = Sequence.new
222
+ at1 = Attribute.new
223
+ atg1 = AttributeGroup.new
224
+
225
+ extension.group = grp1
226
+ extension.choice = choice1
227
+ extension.sequence = sequence1
228
+ extension.attributes = [at1]
229
+ extension.attribute_groups = [atg1]
230
+
231
+ children = extension.children
232
+ children.include?(grp1).should be_true
233
+ children.include?(choice1).should be_true
234
+ children.include?(sequence1).should be_true
235
+ children.include?(at1).should be_true
236
+ children.include?(atg1).should be_true
237
+ end
238
+
239
+ it "should return restriction children" do
240
+ restriction = Restriction.new
241
+ grp1 = Group.new
242
+ choice1 = Choice.new
243
+ sequence1 = Sequence.new
244
+ at1 = Attribute.new
245
+ atg1 = AttributeGroup.new
246
+ st1 = SimpleType.new
247
+
248
+ restriction.group = grp1
249
+ restriction.choice = choice1
250
+ restriction.sequence = sequence1
251
+ restriction.attributes = [at1]
252
+ restriction.attribute_groups = [atg1]
253
+ restriction.simple_type = st1
254
+
255
+ children = restriction.children
256
+ children.include?(grp1).should be_true
257
+ children.include?(choice1).should be_true
258
+ children.include?(sequence1).should be_true
259
+ children.include?(at1).should be_true
260
+ children.include?(atg1).should be_true
261
+ children.include?(st1).should be_true
262
+ end
263
+
264
+ ##########
265
+
266
+ # test resolve method on all XSD classes
267
+
268
+ it "should resolve schema relationships" do
269
+ # currently nothing to verify
270
+ end
271
+
272
+ it "should resolve element relationships" do
273
+ schema = Schema.new
274
+ schema.elements = schema.simple_types = schema.complex_types = []
275
+
276
+ element1 = Element.new
277
+ element1.type = "xs:string"
278
+ element1.name = "element1"
279
+
280
+ simple_type = SimpleType.new
281
+ simple_type.name = "simple_type_test"
282
+ element2 = Element.new
283
+ element2.type = simple_type.name
284
+ element2.ref = element1.name
285
+
286
+ complex_type = ComplexType.new
287
+ complex_type.attributes = complex_type.attribute_groups = []
288
+ complex_type.name = "complex_type_test"
289
+ element3 = Element.new
290
+ element3.type = complex_type.name
291
+ element3.substitutionGroup = element1.name
292
+
293
+ schema.elements << element1 << element2 << element3
294
+ schema.simple_types << simple_type
295
+ schema.complex_types << complex_type
296
+
297
+ node_objs = Resolver.node_objects(schema)
298
+ element1.resolve(node_objs)
299
+ element2.resolve(node_objs)
300
+ element3.resolve(node_objs)
301
+
302
+ element1.type.should == String
303
+ element2.type.should == simple_type
304
+ element2.ref.should == element1
305
+ element3.type.should == complex_type
306
+ element3.substitutionGroup.should == element1
307
+ end
308
+
309
+ it "should resolve complex type relationships" do
310
+ # currently nothing to verify
311
+ end
312
+
313
+ it "should resolve simple type relationships" do
314
+ # currently nothing to verify
315
+ end
316
+
317
+ it "should resolve attribute relationships" do
318
+ schema = Schema.new
319
+ schema.attributes = schema.simple_types = []
320
+
321
+ attr1 = Attribute.new
322
+ attr1.type = "xs:int"
323
+ attr1.name = "attr1"
324
+
325
+ simple_type = SimpleType.new
326
+ simple_type.name = "simple_type_test"
327
+ attr2 = Attribute.new
328
+ attr2.type = simple_type.name
329
+ attr2.ref = attr1.name
330
+
331
+ schema.attributes << attr1 << attr2
332
+ schema.simple_types << simple_type
333
+
334
+ node_objs = Resolver.node_objects(schema)
335
+ attr1.resolve(node_objs)
336
+ attr2.resolve(node_objs)
337
+
338
+ attr1.type.should == XSDInteger
339
+ attr2.type.should == simple_type
340
+ attr2.ref.should == attr1
341
+ end
342
+
343
+ it "should resolve attribute group relationships" do
344
+ schema = Schema.new
345
+ schema.attribute_groups = []
346
+
347
+ attr_grp1 = AttributeGroup.new
348
+ attr_grp1.attributes = attr_grp1.attribute_groups = []
349
+ attr_grp1.name = "attr1"
350
+ attr_grp2 = AttributeGroup.new
351
+ attr_grp2.attributes = attr_grp2.attribute_groups = []
352
+ attr_grp2.ref = attr_grp1.name
353
+
354
+ schema.attribute_groups << attr_grp1 << attr_grp2
355
+ node_objs = Resolver.node_objects(schema)
356
+ attr_grp1.resolve(node_objs)
357
+ attr_grp2.resolve(node_objs)
358
+
359
+ attr_grp2.ref.should == attr_grp1
360
+ end
361
+
362
+ it "should resolve group relationships" do
363
+ schema = Schema.new
364
+ schema.groups = []
365
+
366
+ grp1 = Group.new
367
+ grp1.name = "grp1"
368
+ grp2 = Group.new
369
+ grp2.ref = grp1.name
370
+
371
+ schema.groups << grp1 << grp2
372
+ node_objs = Resolver.node_objects(schema)
373
+ grp1.resolve(node_objs)
374
+ grp2.resolve(node_objs)
375
+
376
+ grp2.ref.should == grp1
377
+ end
378
+
379
+ it "should resolve list relationships" do
380
+ schema = Schema.new
381
+ simple_type1 = SimpleType.new
382
+ simple_type1.name = "simple_type_test1"
383
+ simple_type2 = SimpleType.new
384
+ simple_type2.name = "simple_type_test2"
385
+ simple_type3 = SimpleType.new
386
+ simple_type3.name = "simple_type_test3"
387
+ schema.simple_types = [simple_type1, simple_type2, simple_type3]
388
+
389
+ list1 = List.new
390
+ list1.itemType = "xs:float"
391
+
392
+ list2 = List.new
393
+ list2.itemType = simple_type3.name
394
+
395
+ simple_type1.list = list1
396
+ simple_type2.list = list2
397
+
398
+ node_objs = Resolver.node_objects(schema)
399
+ list1.resolve(node_objs)
400
+ list2.resolve(node_objs)
401
+
402
+ list1.itemType.should == XSDFloat
403
+ list2.itemType.should == simple_type3
404
+ end
405
+
406
+ it "should resolve simple content relationships" do
407
+ # currently nothing to verify
408
+ end
409
+
410
+ it "should resolve choice relationships" do
411
+ # currently nothing to verify
412
+ end
413
+
414
+ it "should resolve complex content relationships" do
415
+ # currently nothing to verify
416
+ end
417
+
418
+ it "should resolve sequence relationships" do
419
+ # currently nothing to verify
420
+ end
421
+
422
+ it "should resolve restriction relationships" do
423
+ # currently nothing to verify
424
+ end
425
+
426
+ it "should resolve extension relationships" do
427
+ schema = Schema.new
428
+ simple_type1 = SimpleType.new
429
+ simple_type1.name = "simple_type_test1"
430
+
431
+ complex_content1 = ComplexContent.new
432
+ complex_type1 = ComplexType.new
433
+ complex_type1.name = "complex_type_test1"
434
+ complex_type1.attributes = complex_type1.attribute_groups = []
435
+ complex_type1.complex_content = complex_content1
436
+
437
+ complex_content2 = ComplexContent.new
438
+ complex_type2 = ComplexType.new
439
+ complex_type2.name = "complex_type_test2"
440
+ complex_type2.attributes = complex_type2.attribute_groups = []
441
+ complex_type2.complex_content = complex_content2
442
+
443
+ complex_content3 = ComplexContent.new
444
+ complex_type3 = ComplexType.new
445
+ complex_type3.name = "complex_type_test2"
446
+ complex_type3.attributes = complex_type3.attribute_groups = []
447
+ complex_type3.complex_content = complex_content3
448
+
449
+ schema.simple_types = [simple_type1]
450
+ schema.complex_types = [complex_type1, complex_type2, complex_type3]
451
+
452
+ extension1 = Extension.new
453
+ extension1.base = "xs:byte"
454
+
455
+ extension2 = Extension.new
456
+ extension2.base = simple_type1.name
457
+
458
+ extension3 = Extension.new
459
+ extension3.base = complex_type1.name
460
+
461
+ complex_type1.complex_content.extension = extension1
462
+ complex_type2.complex_content.extension = extension2
463
+ complex_type3.complex_content.extension = extension3
464
+
465
+ node_objs = Resolver.node_objects(schema)
466
+ extension1.resolve(node_objs)
467
+ extension2.resolve(node_objs)
468
+ extension3.resolve(node_objs)
469
+
470
+ extension1.base.should == Char
471
+ extension2.base.should == simple_type1
472
+ extension3.base.should == complex_type1
473
+ end
474
+
475
+ it "should resolve restriction relationships" do
476
+ schema = Schema.new
477
+ simple_type1 = SimpleType.new
478
+ simple_type1.name = "simple_type_test1"
479
+
480
+ complex_content1 = ComplexContent.new
481
+ complex_type1 = ComplexType.new
482
+ complex_type1.name = "complex_type_test1"
483
+ complex_type1.attributes = complex_type1.attribute_groups = []
484
+ complex_type1.complex_content = complex_content1
485
+
486
+ complex_content2 = ComplexContent.new
487
+ complex_type2 = ComplexType.new
488
+ complex_type2.name = "complex_type_test2"
489
+ complex_type2.attributes = complex_type2.attribute_groups = []
490
+ complex_type2.complex_content = complex_content2
491
+
492
+ complex_content3 = ComplexContent.new
493
+ complex_type3 = ComplexType.new
494
+ complex_type3.name = "complex_type_test2"
495
+ complex_type3.attributes = complex_type3.attribute_groups = []
496
+ complex_type3.complex_content = complex_content3
497
+
498
+ schema.simple_types = [simple_type1]
499
+ schema.complex_types = [complex_type1, complex_type2, complex_type3]
500
+
501
+ restriction1 = Restriction.new
502
+ restriction1.base = "xs:byte"
503
+
504
+ restriction2 = Restriction.new
505
+ restriction2.base = simple_type1.name
506
+
507
+ restriction3 = Restriction.new
508
+ restriction3.base = complex_type1.name
509
+
510
+ complex_type1.complex_content.restriction = restriction1
511
+ complex_type2.complex_content.restriction = restriction2
512
+ complex_type3.complex_content.restriction = restriction3
513
+
514
+ node_objs = Resolver.node_objects(schema)
515
+ restriction1.resolve(node_objs)
516
+ restriction2.resolve(node_objs)
517
+ restriction3.resolve(node_objs)
518
+
519
+ restriction1.base.should == Char
520
+ restriction2.base.should == simple_type1
521
+ restriction3.base.should == complex_type1
522
+ end
523
+
524
+
525
+ ###########
526
+
527
+ it "should correctly resolve node objects" do
528
+ data = '<schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' +
529
+ '<xs:complexType id="ct1" name="ct1">' +
530
+ '<xs:complexContent id="cc1" mixed="true">' +
531
+ '<xs:extension id="e1" base="Foo">' +
532
+ '<xs:group id="g1" />' +
533
+ '<xs:attribute id="a1" />' +
534
+ '<xs:attribute id="a2" />' +
535
+ '</xs:extension>' +
536
+ '</xs:complexContent>' +
537
+ '</xs:complexType>' +
538
+ '<xs:complexType id="ct2">' +
539
+ '<xs:choice id="c1" maxOccurs="5" minOccurs="unbounded" >' +
540
+ ' <xs:element id="e1" />' +
541
+ ' <xs:element id="e2" />' +
542
+ ' <xs:element ref="Foobar" />' +
543
+ ' <xs:choice id="c2" />' +
544
+ ' <xs:choice id="c3" />' +
545
+ '</xs:choice>' +
546
+ '</xs:complexType>' +
547
+ '<xs:simpleType id="st1" name="st1">' +
548
+ '<xs:list id="li1" itemType="Foo" />' +
549
+ '</xs:simpleType>' +
550
+ '<xs:element name="Foobar" minOccurs="unbounded"/>' +
551
+ '<xs:element name="Foomanchu"/>' +
552
+ '<xs:attributeGroup name="atg1" />' +
553
+ '<xs:restriction base="Foo">' +
554
+ '<xs:attributeGroup ref="atg1" />' +
555
+ '</xs:restriction>' +
556
+ '<xs:element id="ct3">' +
557
+ '<xs:simpleType>' +
558
+ '<xs:list id="ls1" />' +
559
+ '</xs:simpleType>' +
560
+ '</xs:element>' +
561
+ '<xs:complexType>' +
562
+ '<xs:complexContent>' +
563
+ '<xs:restriction>' +
564
+ '<xs:sequence name="seq1" />' +
565
+ '</xs:restriction>' +
566
+ '</xs:complexContent>' +
567
+ '</xs:complexType>' +
568
+ '</schema>'
569
+
570
+ schema = Parser.parse_xsd :raw => data
571
+ node_objs = Resolver.node_objects schema
572
+
573
+ node_objs.values.flatten.size.should == 26
574
+ node_objs[Schema].size.should == 1
575
+ node_objs[ComplexType].size.should == 3
576
+ node_objs[Element].size.should == 6
577
+ node_objs[Attribute].size.should == 2
578
+ node_objs[AttributeGroup].size.should == 1
579
+ node_objs[Choice].size.should == 3
580
+ node_objs[ComplexContent].size.should == 2
581
+ node_objs[Extension].size.should == 1
582
+ node_objs[Group].size.should == 1
583
+ node_objs[List].size.should == 2
584
+ node_objs[Restriction].size.should == 1
585
+ node_objs[Sequence].size.should == 1
586
+ node_objs[SimpleContent].size.should == 0
587
+ node_objs[SimpleType].size.should == 2
588
+ end
589
+
590
+ it "should correctly resolve nodes" do
591
+ data = '<schema xmlns:xs="http://www.w3.org/2001/XMLSchema">' +
592
+ '<xs:element name="Foo"/>' +
593
+ '<xs:complexType name="ct1">' +
594
+ '<xs:choice id="c1" maxOccurs="5" minOccurs="unbounded" >' +
595
+ ' <xs:element id="e1" />' +
596
+ ' <xs:element ref="Foo" />' +
597
+ '</xs:choice>' +
598
+ '</xs:complexType>' +
599
+ '<xs:element type="ct1" name="Bar"/>' +
600
+ '</schema>'
601
+
602
+ schema = Parser.parse_xsd :raw => data
603
+ #node_objs = Resolver.resolve_nodes schema # XXX don't like doing it this way but this is invoked as part of Parser.parse_xsd, and shouldn't be invoked twice on one data set
604
+
605
+ schema.complex_types[0].should == schema.elements[1].type
606
+ schema.elements[0].should == schema.complex_types[0].choice.elements[1].ref
607
+ end
608
+
609
+ end