lutaml-model 0.8.204 → 0.8.206

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.
@@ -0,0 +1,901 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "lutaml/store"
5
+
6
+ # Production-lane parity for shapes the XML plan lanes got wrong while a
7
+ # leaked adapter pin kept most of the suite off leptris: every example
8
+ # pins the leptris lane itself and asserts the interpretive writer's
9
+ # output, so the coverage does not depend on suite-wide lane integrity.
10
+ module ProductionLaneParitySpec
11
+ class TextElement < Lutaml::Model::Serializable
12
+ attribute :sup, :string
13
+ attribute :sub, :string
14
+ attribute :text, :string, collection: true
15
+
16
+ xml do
17
+ element "text-element"
18
+
19
+ map_content to: :text
20
+
21
+ map_element "sup", to: :sup
22
+ map_element "sub", to: :sub
23
+ end
24
+ end
25
+
26
+ class DocidModel
27
+ attr_accessor :id, :prefix
28
+ end
29
+
30
+ class Docid < Lutaml::Model::Serializable
31
+ model DocidModel
32
+ attribute :id, TextElement
33
+
34
+ xml do
35
+ element "docid"
36
+ mixed_content
37
+
38
+ map_content to: :text, delegate: :id
39
+ map_element :sub, to: :sub, delegate: :id
40
+ map_element :sup, to: :sup, delegate: :id
41
+ end
42
+ end
43
+
44
+ # elementFormDefault not set: local elements are in no namespace.
45
+ UnsetFormNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
46
+ uri "http://example.com/production-lane/unset"
47
+ prefix_default "ex"
48
+ end
49
+
50
+ class UnsetForm < Lutaml::Model::Serializable
51
+ attribute :a, :string
52
+ attribute :b, :string
53
+
54
+ xml do
55
+ element "unset"
56
+ namespace UnsetFormNs
57
+ map_element "a", to: :a
58
+ map_element "b", to: :b
59
+ end
60
+ end
61
+
62
+ # elementFormDefault explicitly :unqualified: the root keeps the
63
+ # default-namespace format and locals spell bare.
64
+ UnqualifiedFormNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
65
+ uri "http://example.com/production-lane/unqualified"
66
+ prefix_default "ex"
67
+ element_form_default :unqualified
68
+ end
69
+
70
+ class UnqualifiedForm < Lutaml::Model::Serializable
71
+ attribute :a, :string
72
+
73
+ xml do
74
+ element "unqualified"
75
+ namespace UnqualifiedFormNs
76
+ map_element "a", to: :a
77
+ end
78
+ end
79
+
80
+ QualifiedNs = Class.new(Lutaml::Xml::W3c::XmlNamespace) do
81
+ uri "http://example.com/production-lane/qualified"
82
+ prefix_default "ex"
83
+ element_form_default :qualified
84
+ attribute_form_default :qualified
85
+ end
86
+
87
+ class FormOverride < Lutaml::Model::Serializable
88
+ attribute :content, :string
89
+
90
+ xml do
91
+ element "override"
92
+ namespace QualifiedNs
93
+ map_element "content", to: :content, form: :unqualified
94
+ end
95
+ end
96
+
97
+ class BlankRule < Lutaml::Model::Serializable
98
+ attribute :note, :string
99
+
100
+ xml do
101
+ element "blank-rule"
102
+ namespace UnsetFormNs
103
+ map_element "note", to: :note, namespace: nil
104
+ end
105
+ end
106
+
107
+ class Declared < Lutaml::Model::Serializable
108
+ attribute :name, :string
109
+
110
+ xml do
111
+ element "declared"
112
+ map_element "name", to: :name
113
+ end
114
+ end
115
+
116
+ class Actual < Lutaml::Model::Serializable
117
+ attribute :different_name, :string
118
+
119
+ xml do
120
+ element "actual"
121
+ map_element "different-name", to: :different_name
122
+ end
123
+ end
124
+
125
+ class Holder < Lutaml::Model::Serializable
126
+ attribute :child, Declared
127
+
128
+ xml do
129
+ element "holder"
130
+ map_element "child", to: :child
131
+ end
132
+ end
133
+
134
+ class Vase < Lutaml::Model::Serializable
135
+ attribute :material, :string
136
+ attribute :height, :integer
137
+
138
+ xml do
139
+ element "Vase"
140
+ map_element "material", to: :material
141
+ map_element "height", to: :height
142
+ end
143
+ end
144
+
145
+ class Cached < Lutaml::Model::Serializable
146
+ attribute :name, :string
147
+
148
+ xml do
149
+ element "cached"
150
+ map_element "name", to: :name
151
+ end
152
+
153
+ cache_conversions
154
+ end
155
+
156
+ class ExplicitNil < Lutaml::Model::Serializable
157
+ attribute :items, :string, collection: true
158
+
159
+ xml do
160
+ element "explicit-nil"
161
+ map_element "items", to: :items, render_nil: :as_nil
162
+ end
163
+ end
164
+
165
+ class ExplicitBlank < Lutaml::Model::Serializable
166
+ attribute :items, :string, collection: true
167
+
168
+ xml do
169
+ element "explicit-blank"
170
+ map_element "items", to: :items, render_empty: :as_blank
171
+ end
172
+ end
173
+
174
+ class NilScalar < Lutaml::Model::Serializable
175
+ attribute :name, :string, default: "Unnamed"
176
+
177
+ xml do
178
+ element "nil-scalar"
179
+ map_element "name", to: :name, render_nil: true
180
+ end
181
+ end
182
+
183
+ class Pushed < Lutaml::Model::Serializable
184
+ attribute :lead, :string
185
+ attribute :items, :string, collection: true, default: -> { [] }
186
+
187
+ xml do
188
+ element "pushed"
189
+ ordered
190
+ map_element "lead", to: :lead
191
+ map_element "item", to: :items
192
+ end
193
+ end
194
+
195
+ class Nilled < Lutaml::Model::Serializable
196
+ attribute :lead, :string
197
+ attribute :items, :string, collection: true
198
+
199
+ xml do
200
+ element "nilled"
201
+ ordered
202
+ map_element "lead", to: :lead
203
+ map_element "item", to: :items, render_nil: :as_nil
204
+ end
205
+ end
206
+
207
+ class OrderedBlank < Lutaml::Model::Serializable
208
+ attribute :authority, :string
209
+ attribute :topic, :string, collection: true
210
+
211
+ xml do
212
+ element "subject"
213
+ ordered
214
+ map_attribute "authority", to: :authority
215
+ map_element "topic", to: :topic, render_empty: :as_nil
216
+ end
217
+ end
218
+
219
+ class ReservedPrefix < Lutaml::Model::Serializable
220
+ attribute :lang, :string
221
+
222
+ xml do
223
+ element "reserved"
224
+ map_attribute "xml:lang", to: :lang
225
+ end
226
+ end
227
+
228
+ class ForeignOrder < Lutaml::Model::Serializable
229
+ attribute :lead, :string
230
+
231
+ xml do
232
+ element "foreign"
233
+ ordered
234
+ map_element "lead", to: :lead
235
+ end
236
+ end
237
+
238
+ class Hashed < Lutaml::Model::Serializable
239
+ attribute :h, :hash
240
+
241
+ xml do
242
+ element "hashed"
243
+ map_element "h", to: :h
244
+ end
245
+ end
246
+
247
+ class OverCount < Lutaml::Model::Serializable
248
+ attribute :name, :string
249
+
250
+ xml do
251
+ element "over-count"
252
+ map_element "name", to: :name
253
+ end
254
+ end
255
+
256
+ class HookedType < Lutaml::Model::Type::Value
257
+ def self.from_xml(_value)
258
+ "from_xml_hooked"
259
+ end
260
+
261
+ def to_xml
262
+ "to_xml_hooked"
263
+ end
264
+ end
265
+
266
+ class Hooked < Lutaml::Model::Serializable
267
+ attribute :value, HookedType
268
+ attribute :attr, HookedType
269
+
270
+ xml do
271
+ element "hooked"
272
+ map_element "value", to: :value
273
+ map_attribute "attr", to: :attr
274
+ end
275
+ end
276
+
277
+ class RefBase < Lutaml::Model::Serializable
278
+ attribute :name, :string
279
+
280
+ xml do
281
+ map_element "name", to: :name
282
+ end
283
+ end
284
+
285
+ class DocRef < RefBase
286
+ attribute :_class, :string
287
+ attribute :document_id, :string
288
+
289
+ xml do
290
+ map_element "document_id", to: :document_id
291
+ map_attribute "_class", to: :_class
292
+ end
293
+ end
294
+
295
+ class RefSet < Lutaml::Model::Serializable
296
+ attribute :references, RefBase, collection: true, polymorphic: [DocRef]
297
+
298
+ xml do
299
+ element "refset"
300
+ map_element "references", to: :references, polymorphic: {
301
+ attribute: "_class",
302
+ class_map: { "document-ref" => "ProductionLaneParitySpec::DocRef" },
303
+ }
304
+ end
305
+ end
306
+
307
+ class Animal < Lutaml::Model::Serializable
308
+ attribute :type, :string
309
+ attribute :name, :string
310
+
311
+ xml do
312
+ element "animal"
313
+ map_attribute "type", to: :type
314
+ map_element "name", to: :name
315
+ end
316
+ end
317
+
318
+ class Dog < Animal
319
+ attribute :breed, :string
320
+
321
+ xml do
322
+ map_element "breed", to: :breed
323
+ end
324
+ end
325
+
326
+ class Zoo < Lutaml::Model::Collection
327
+ instances :animals, Animal, polymorphic: true
328
+
329
+ xml do
330
+ element "zoo"
331
+ map_element "animal", to: :animals, polymorphic: {
332
+ attribute: "type",
333
+ class_map: { "dog" => "ProductionLaneParitySpec::Dog" },
334
+ }
335
+ end
336
+ end
337
+
338
+ class Omitted < Lutaml::Model::Serializable
339
+ attribute :as_nil, :string
340
+ attribute :as_empty, :string
341
+
342
+ xml do
343
+ element "omitted"
344
+ map_element "as_nil", to: :as_nil, value_map: { to: { omitted: :nil } }
345
+ map_element "as_empty", to: :as_empty,
346
+ value_map: { from: { omitted: :empty } }
347
+ end
348
+ end
349
+
350
+ class Imported < Lutaml::Model::Serializable
351
+ attribute :shout, :string
352
+
353
+ xml do
354
+ element "imported"
355
+ map_element "shout", to: :shout,
356
+ transform: { import: ->(v) { v.to_s.upcase } }
357
+ end
358
+ end
359
+
360
+ class LateBound < Lutaml::Model::Serializable
361
+ attribute :name, :string
362
+
363
+ xml do
364
+ element "late"
365
+ map_element "name", to: :name
366
+ end
367
+ end
368
+
369
+ class TypeOnly < Lutaml::Model::Serializable
370
+ attribute :type, :string
371
+
372
+ xml do
373
+ map_element "type", to: :type
374
+ end
375
+ end
376
+
377
+ class RawCapture < Lutaml::Model::Serializable
378
+ attribute :svg_data, :string
379
+
380
+ xml do
381
+ element "container"
382
+ map_element "svg", to: :svg_data, raw: :element
383
+ end
384
+ end
385
+
386
+ class MixedRawCollection < Lutaml::Model::Serializable
387
+ attribute :name, :string
388
+ attribute :fragments, :string, collection: true
389
+
390
+ xml do
391
+ element "container"
392
+ mixed_content
393
+ map_element "name", to: :name
394
+ map_element "fragment", to: :fragments, raw: :element
395
+ end
396
+ end
397
+
398
+ class InnerContent < Lutaml::Model::Serializable
399
+ attribute :street, :string
400
+
401
+ xml do
402
+ element "address"
403
+ map_element "street", to: :street, raw: :content
404
+ end
405
+ end
406
+
407
+ class NestedTitle < Lutaml::Model::Serializable
408
+ attribute :content, :string
409
+
410
+ xml do
411
+ element "artifact"
412
+ map_element "content", to: :content
413
+ end
414
+ end
415
+
416
+ class NestedTitles < Lutaml::Model::Collection
417
+ instances :items, NestedTitle
418
+
419
+ xml do
420
+ element "title-group"
421
+ map_element "artifact", to: :items
422
+ end
423
+ end
424
+
425
+ class NestedBibitem < Lutaml::Model::Serializable
426
+ attribute :titles, NestedTitles
427
+
428
+ xml do
429
+ element "bibitem"
430
+ map_element "titles", to: :titles
431
+ end
432
+ end
433
+
434
+ class Sequenced < Lutaml::Model::Serializable
435
+ attribute :title, :string
436
+ attribute :note, :string
437
+ attribute :size, :string
438
+
439
+ xml do
440
+ element "sequenced"
441
+ map_element :title, to: :title
442
+ sequence do
443
+ map_element :note, to: :note
444
+ map_element :size, to: :size
445
+ end
446
+ end
447
+ end
448
+
449
+ class SequencedHolder < Lutaml::Model::Serializable
450
+ attribute :items, Sequenced, collection: true
451
+
452
+ xml do
453
+ element "holder"
454
+ map_element "sequenced", to: :items
455
+ end
456
+ end
457
+
458
+ class CustomSingular < Lutaml::Model::Serializable
459
+ attribute :label, :string
460
+
461
+ xml do
462
+ element "custom"
463
+ map_element "label", to: :label,
464
+ with: { from: :label_from, to: :label_to }
465
+ end
466
+
467
+ def self.received
468
+ @received ||= []
469
+ end
470
+
471
+ def label_from(model, node)
472
+ self.class.received << node
473
+ texts = Array(node).map(&:text)
474
+ model.label = texts.size > 1 ? texts : texts.first
475
+ end
476
+
477
+ def label_to(model, parent, doc); end
478
+ end
479
+
480
+ class SingularContent < Lutaml::Model::Serializable
481
+ attribute :bold, :string, collection: true
482
+ attribute :content, :string
483
+
484
+ xml do
485
+ element "r"
486
+ map_element "bold", to: :bold
487
+ map_content to: :content
488
+ end
489
+ end
490
+
491
+ class UnmappedMixedText < Lutaml::Model::Serializable
492
+ attribute :singular, :string
493
+ attribute :items, :string, collection: true
494
+
495
+ xml do
496
+ element "container"
497
+ mixed_content
498
+ map_element "singular", to: :singular
499
+ map_element "items", to: :items
500
+ end
501
+ end
502
+
503
+ # An unordered mapping whose instances may carry a tracked
504
+ # element_order (the order-tracker pattern, lutaml-model#977).
505
+ class Tracked < Lutaml::Model::Serializable
506
+ attribute :singular, :string
507
+ attribute :items, :string, collection: true
508
+
509
+ xml do
510
+ element "container"
511
+ map_element "singular", to: :singular
512
+ map_element "items", to: :items
513
+ end
514
+ end
515
+ end
516
+
517
+ RSpec.describe "production-lane parity" do
518
+ around do |example|
519
+ previous = Lutaml::Model::Config.xml_adapter_type
520
+ fixed = previous == Lutaml::Model::FIXED_ADAPTER_TYPE
521
+ previous_adapter = Lutaml::Model::Config.xml_adapter if fixed
522
+ Lutaml::Model::Config.xml_adapter_type = :leptris
523
+ example.run
524
+ ensure
525
+ if fixed
526
+ Lutaml::Model::Config.xml_adapter = previous_adapter
527
+ elsif previous
528
+ Lutaml::Model::Config.xml_adapter_type = previous
529
+ else
530
+ Lutaml::Model::Config.unset_xml_adapter_type
531
+ end
532
+ end
533
+
534
+ it "round-trips rules delegated to a mapped child" do
535
+ xml = "<docid>Str<sub>2</sub>text<sup>1</sup>123</docid>"
536
+ docid = ProductionLaneParitySpec::Docid.from_xml(xml)
537
+
538
+ expect(ProductionLaneParitySpec::Docid.to_xml(docid)).to be_xml_equivalent_to(xml)
539
+ end
540
+
541
+ describe "elementFormDefault" do
542
+ it "opts form-not-set locals out of the default namespace" do
543
+ model = ProductionLaneParitySpec::UnsetForm.new(a: "1", b: "2")
544
+
545
+ expect(model.to_xml).to include('<unset xmlns="http://example.com/production-lane/unset">')
546
+ expect(model.to_xml).to include('<a xmlns="">1</a>', '<b xmlns="">2</b>')
547
+ expect(model.to_xml(pretty: true)).to include('<a xmlns="">1</a>', "<b>2</b>")
548
+ end
549
+
550
+ it "keeps the default-namespace root and bare locals under an explicit :unqualified form" do
551
+ model = ProductionLaneParitySpec::UnqualifiedForm.new(a: "1")
552
+ xml = model.to_xml
553
+
554
+ expect(xml).to include('<unqualified xmlns="http://example.com/production-lane/unqualified">')
555
+ expect(xml).to include("<a>1</a>")
556
+ expect(xml).not_to include("xmlns=\"\"", "<ex:a")
557
+ end
558
+
559
+ it "spells a form: :unqualified rule bare under a qualified namespace" do
560
+ model = ProductionLaneParitySpec::FormOverride.new(content: "x")
561
+
562
+ expect(model.to_xml).to include("<content>x</content>")
563
+ expect(model.to_xml).not_to include("<ex:content")
564
+ end
565
+
566
+ it "opts a namespace: nil rule out of the default namespace" do
567
+ model = ProductionLaneParitySpec::BlankRule.new(note: "n")
568
+
569
+ expect(model.to_xml).to include('<note xmlns="">n</note>')
570
+ end
571
+ end
572
+
573
+ it "serializes a nested value of another class through its own mapping" do
574
+ model = ProductionLaneParitySpec::Holder.new
575
+ model.child = ProductionLaneParitySpec::Actual.new(different_name: "x")
576
+
577
+ expect(model.to_xml).to include("<different-name>x</different-name>")
578
+ end
579
+
580
+ it "never declares the reserved xml prefix" do
581
+ model = ProductionLaneParitySpec::ReservedPrefix.new(lang: "en")
582
+
583
+ expect(model.to_xml).to include('xml:lang="en"')
584
+ expect(model.to_xml).not_to include("xmlns:xml=")
585
+ end
586
+
587
+ it "generates schemas in the Nokogiri wrapper's byte shape" do
588
+ nokogiri = Lutaml::Xml::Schema::XsdSchema.generate(ProductionLaneParitySpec::Vase,
589
+ pretty: true, adapter: :nokogiri)
590
+ leptris = Lutaml::Xml::Schema::XsdSchema.generate(ProductionLaneParitySpec::Vase,
591
+ pretty: true, adapter: :leptris)
592
+
593
+ expect(leptris).to eq(nokogiri)
594
+ end
595
+
596
+ describe "render policies" do
597
+ it "renders an explicit nil element" do
598
+ model = ProductionLaneParitySpec::ExplicitNil.new(items: nil)
599
+
600
+ expect(model.to_xml).to include('<items xsi:nil="true"/>')
601
+ end
602
+
603
+ it "renders a blank element for an empty collection" do
604
+ model = ProductionLaneParitySpec::ExplicitBlank.new(items: [])
605
+
606
+ expect(model.to_xml).to include("<items/>")
607
+ end
608
+
609
+ it "binds an xsi:nil element as an empty collection" do
610
+ parsed = ProductionLaneParitySpec::ExplicitNil
611
+ .from_xml('<explicit-nil><items xsi:nil="true"/></explicit-nil>')
612
+
613
+ expect(parsed.items).to eq([])
614
+ end
615
+
616
+ it "binds an xsi:nil element as a nil scalar" do
617
+ parsed = ProductionLaneParitySpec::NilScalar
618
+ .from_xml('<nil-scalar><name xsi:nil="true"/></nil-scalar>')
619
+
620
+ expect(parsed.name).to be_nil
621
+ end
622
+
623
+ it "renders the nil element of a parsed ordered model" do
624
+ xml = '<subject authority="lcsh"><topic xsi:nil="true"/></subject>'
625
+ parsed = ProductionLaneParitySpec::OrderedBlank.from_xml(xml)
626
+
627
+ expect(parsed.to_xml).to include('<topic xsi:nil="true"/>')
628
+ end
629
+ end
630
+
631
+ describe "parsed-model mutation" do
632
+ it "emits an item pushed onto a default collection after parse" do
633
+ model = ProductionLaneParitySpec::Pushed.from_xml("<pushed><lead>L</lead></pushed>")
634
+ model.items << "y"
635
+
636
+ expect(model.to_xml).to include("<item>y</item>")
637
+ end
638
+
639
+ it "renders a collection set to nil after parse when the element was present" do
640
+ model = ProductionLaneParitySpec::Nilled.from_xml("<nilled><lead>L</lead><item>x</item></nilled>")
641
+ model.items = nil
642
+
643
+ expect(model.to_xml).to include('xsi:nil="true"')
644
+ end
645
+
646
+ it "emits nothing when the element was absent" do
647
+ model = ProductionLaneParitySpec::Nilled.from_xml("<nilled><lead>L</lead></nilled>")
648
+ model.items = nil
649
+
650
+ expect(model.to_xml).not_to include("<item")
651
+ end
652
+
653
+ it "skips foreign entries in element_order" do
654
+ model = ProductionLaneParitySpec::ForeignOrder.from_xml("<foreign><lead>L</lead></foreign>")
655
+ model.element_order = model.element_order.dup << 42
656
+
657
+ expect { model.to_xml }.not_to raise_error
658
+ expect(model.to_xml).to include("<lead>L</lead>")
659
+ end
660
+ end
661
+
662
+ it "expands a Hash-typed element into child elements and reads it back" do
663
+ xml = "<hashed><h><key>value</key></h></hashed>"
664
+ expect(ProductionLaneParitySpec::Hashed.new(h: { key: "value" }).to_xml)
665
+ .to be_xml_equivalent_to(xml)
666
+ expect(ProductionLaneParitySpec::Hashed.from_xml(xml).h).to eq("key" => "value")
667
+ end
668
+
669
+ it "keeps an over-count for a singular attribute so validate reports it" do
670
+ obj = ProductionLaneParitySpec::OverCount.from_xml(
671
+ "<over-count><name>A</name><name>B</name></over-count>",
672
+ )
673
+
674
+ expect(obj.name).to eq(%w[A B])
675
+ expect(obj.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
676
+ end
677
+
678
+ it "spells a Value type's own XML hooks" do
679
+ model = ProductionLaneParitySpec::Hooked.new(value: "x", attr: "y")
680
+
681
+ expect(model.to_xml).to include("<value>to_xml_hooked</value>", 'attr="to_xml_hooked"')
682
+ parsed = ProductionLaneParitySpec::Hooked.from_xml("<hooked attr=\"v\"><value>v</value></hooked>")
683
+ expect(parsed.value).to eq("from_xml_hooked")
684
+ expect(parsed.attr).to eq("from_xml_hooked")
685
+ end
686
+
687
+ it "resolves a polymorphic class map on deferred rows" do
688
+ xml = '<refset><references _class="document-ref"><name>n</name>' \
689
+ "<document_id>d</document_id></references></refset>"
690
+ parsed = ProductionLaneParitySpec::RefSet.from_xml(xml)
691
+
692
+ expect(parsed.references.first).to be_a(ProductionLaneParitySpec::DocRef)
693
+ expect(ProductionLaneParitySpec::RefSet.to_xml(parsed)).to be_xml_equivalent_to(xml)
694
+ end
695
+
696
+ it "resolves a polymorphic Collection's instances by their class map" do
697
+ zoo = ProductionLaneParitySpec::Zoo.from_xml(
698
+ '<zoo><animal type="dog"><name>Fido</name><breed>Lab</breed></animal></zoo>',
699
+ )
700
+
701
+ expect(zoo.animals.first).to be_a(ProductionLaneParitySpec::Dog)
702
+ expect(zoo.animals.first.breed).to eq("Lab")
703
+ end
704
+
705
+ it "renders an uninitialized value under its value_map and binds an omitted one" do
706
+ # An uninitialized value is decided by its value_map's :omitted key:
707
+ # :nil renders the xsi:nil element.
708
+ model = ProductionLaneParitySpec::Omitted.new({}, { omitted: :omitted })
709
+
710
+ expect(model.to_xml).to include('as_nil xsi:nil="true"')
711
+ expect(ProductionLaneParitySpec::Omitted.from_xml("<omitted/>").as_empty).to eq("")
712
+ end
713
+
714
+ it "applies a hash/proc import transform" do
715
+ parsed = ProductionLaneParitySpec::Imported.from_xml("<imported><shout>soft</shout></imported>")
716
+
717
+ expect(parsed.shout).to eq("SOFT")
718
+ end
719
+
720
+ it "follows an attribute and mapping added after the first parse" do
721
+ klass = ProductionLaneParitySpec::LateBound
722
+ klass.from_xml("<late><name>initial</name></late>")
723
+ klass.class_eval do
724
+ attribute :extra, :string
725
+
726
+ xml do
727
+ map_element "extra", to: :extra
728
+ end
729
+ end
730
+ parsed = klass.from_xml("<late><name>hello</name><extra>world</extra></late>")
731
+
732
+ expect(parsed.name).to eq("hello")
733
+ expect(parsed.extra).to eq("world")
734
+ end
735
+
736
+ it "leaves the encoding option and a type-only model's error to the standard pipeline" do
737
+ expect { ProductionLaneParitySpec::ForeignOrder.new(lead: "L").to_xml(encoding: "ABC") }
738
+ .to raise_error(StandardError, /ABC/)
739
+ expect { ProductionLaneParitySpec::TypeOnly.from_xml("<type>Data</type>") }
740
+ .to raise_error(Lutaml::Model::TypeOnlyMappingError)
741
+ end
742
+
743
+ describe "raw captures" do
744
+ it "captures an explicitly prefixed element by local name" do
745
+ doc = ProductionLaneParitySpec::RawCapture.from_xml(
746
+ '<container xmlns:s="http://www.w3.org/2000/svg"><s:svg><s:rect/></s:svg></container>',
747
+ )
748
+ expect(doc.svg_data).to include("<s:svg", "<s:rect")
749
+ end
750
+
751
+ it "injects one captured fragment per order entry of a mixed collection" do
752
+ xml = <<~XML
753
+ <container>
754
+ before
755
+ <name>multi</name>
756
+ <fragment id="1">first</fragment>
757
+ <fragment id="2"><nested>second</nested></fragment>
758
+ after
759
+ </container>
760
+ XML
761
+ doc = ProductionLaneParitySpec::MixedRawCollection.from_xml(xml)
762
+ output = doc.to_xml
763
+
764
+ expect(output).not_to include("<fragment><fragment")
765
+ expect(output).to include('id="1"', "first", 'id="2"', "<nested>second</nested>")
766
+ expect(ProductionLaneParitySpec::MixedRawCollection.from_xml(output).fragments.length)
767
+ .to eq(2)
768
+ end
769
+
770
+ it "captures the inner XML for raw: :content" do
771
+ doc = ProductionLaneParitySpec::InnerContent.from_xml(
772
+ "<address><street><b>123</b> Main St</street></address>",
773
+ )
774
+ expect(doc.street).to include("<b>123</b>", "Main St")
775
+ expect(doc.street).not_to include("<street>")
776
+ expect(ProductionLaneParitySpec::InnerContent.from_xml(doc.to_xml).street)
777
+ .to include("<b>123</b>")
778
+ end
779
+ end
780
+
781
+ describe "sequence validation and element order" do
782
+ it "flags an out-of-order member on the parsed root" do
783
+ doc = ProductionLaneParitySpec::Sequenced.from_xml(
784
+ "<sequenced><size>S</size><note>N</note><title>T</title></sequenced>",
785
+ )
786
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::IncorrectSequenceError))
787
+
788
+ ordered = ProductionLaneParitySpec::Sequenced.from_xml(
789
+ "<sequenced><title>T</title><note>N</note><size>S</size></sequenced>",
790
+ )
791
+ expect(ordered.validate).to be_empty
792
+ end
793
+
794
+ it "flags an out-of-order member on a nested member" do
795
+ doc = ProductionLaneParitySpec::SequencedHolder.from_xml(
796
+ "<holder><sequenced><title>T</title><size>S</size><note>N</note></sequenced></holder>",
797
+ )
798
+ expect(doc.items.first.validate)
799
+ .to include(an_instance_of(Lutaml::Model::IncorrectSequenceError))
800
+ end
801
+
802
+ it "serializes a parsed sequence model in mapping order" do
803
+ doc = ProductionLaneParitySpec::Sequenced.from_xml(
804
+ "<sequenced><note>N</note><size>S</size><title>T</title></sequenced>",
805
+ )
806
+ expect(doc.to_xml).to be_xml_equivalent_to(
807
+ "<sequenced><title>T</title><note>N</note><size>S</size></sequenced>",
808
+ )
809
+ end
810
+
811
+ it "serializes a tracked element_order on an unordered plan through the ordered walk" do
812
+ model = ProductionLaneParitySpec::Tracked.new(items: %w[a b], singular: "s")
813
+ model.element_order = [
814
+ Lutaml::Xml::Element.new("Element", "items", node_type: :element),
815
+ Lutaml::Xml::Element.new("Element", "singular", node_type: :element),
816
+ Lutaml::Xml::Element.new("Element", "items", node_type: :element),
817
+ ]
818
+ xml = model.to_xml
819
+ expect(xml.index("<items>a</items>")).to be < xml.index("<singular>s</singular>")
820
+ expect(xml.index("<singular>s</singular>")).to be < xml.index("<items>b</items>")
821
+ end
822
+ end
823
+
824
+ it "hands a singular attribute's over-count to the custom method as a whole array" do
825
+ klass = ProductionLaneParitySpec::CustomSingular
826
+ klass.received.clear
827
+ doc = klass.from_xml("<custom><label>a</label><label>b</label></custom>")
828
+
829
+ expect(klass.received.size).to eq(1)
830
+ expect(klass.received.first).to be_a(Array)
831
+ expect(doc.label).to eq(%w[a b])
832
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
833
+ end
834
+
835
+ describe "singular content" do
836
+ it "binds the text runs when the element has element children, and flags the over-count" do
837
+ source = "<r>one <bold>b</bold> two <bold>c</bold> three</r>"
838
+ doc = ProductionLaneParitySpec::SingularContent.from_xml(source)
839
+ expect(doc.content).to eq(["one ", " two ", " three"])
840
+ expect(doc.validate).to include(an_instance_of(Lutaml::Model::CollectionTrueMissingError))
841
+ expect(doc.to_xml).to include("one <bold>b</bold> two <bold>c</bold> three")
842
+
843
+ plain = ProductionLaneParitySpec::SingularContent.from_xml("<r>plain</r>")
844
+ expect(plain.content).to eq("plain")
845
+ end
846
+ end
847
+
848
+ it "round-trips mixed content whose text is unmapped" do
849
+ source = "<container>before <items>a</items> mid <singular>x</singular> end</container>"
850
+ parsed = ProductionLaneParitySpec::UnmappedMixedText.from_xml(source)
851
+
852
+ expect(parsed.to_xml).to include("before <items>a</items> mid <singular>x</singular> end")
853
+ end
854
+
855
+ it "unwraps a collection's own root under the mapped element" do
856
+ xml = <<~XML
857
+ <bibitem>
858
+ <titles>
859
+ <title-group>
860
+ <artifact><content>Title One</content></artifact>
861
+ <artifact><content>Title Two</content></artifact>
862
+ </title-group>
863
+ </titles>
864
+ </bibitem>
865
+ XML
866
+ doc = ProductionLaneParitySpec::NestedBibitem.from_xml(xml)
867
+
868
+ expect(doc.titles).to be_a(ProductionLaneParitySpec::NestedTitles)
869
+ expect(doc.titles.items.map(&:content)).to eq(["Title One", "Title Two"])
870
+ expect(doc.to_xml).to be_xml_equivalent_to(xml)
871
+ end
872
+
873
+ describe "conversion caching" do
874
+ let(:store) { Lutaml::Store::BasicStore.new(adapter: { type: :memory }) }
875
+
876
+ around do |example|
877
+ previous = Lutaml::Model::Config.conversion_cache
878
+ Lutaml::Model::Config.conversion_cache = store
879
+ example.run
880
+ ensure
881
+ Lutaml::Model::Config.conversion_cache = previous
882
+ end
883
+
884
+ it "serves repeated XML serialization from the store" do
885
+ instance = ProductionLaneParitySpec::Cached.new(name: "a")
886
+ instance.to_xml
887
+ expect(store.size).to eq(1)
888
+ store.each_key { |key| store.set(key, "<sentinel/>") }
889
+
890
+ expect(instance.to_xml).to eq("<sentinel/>")
891
+ end
892
+
893
+ it "serves repeated XML parsing from the store" do
894
+ xml = "<cached><name>a</name></cached>"
895
+ first = ProductionLaneParitySpec::Cached.from_xml(xml)
896
+ expect(store.size).to eq(1)
897
+
898
+ expect(ProductionLaneParitySpec::Cached.from_xml(xml)).to equal(first)
899
+ end
900
+ end
901
+ end