moxml 0.6.3 → 0.6.5

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.
data/lib/moxml/xsd.rb CHANGED
@@ -97,9 +97,10 @@ module Moxml
97
97
  end
98
98
 
99
99
  def initialize(handle, context, schema_source = nil,
100
- self_contained: nil)
100
+ self_contained: nil, path: nil)
101
101
  @handle = handle
102
102
  @context = context
103
+ @schema_path = path
103
104
  @rules_index =
104
105
  if schema_source
105
106
  SchemaRules.index(schema_source, context,
@@ -157,8 +158,9 @@ module Moxml
157
158
  end
158
159
  end
159
160
  engine_errors = @context.config.adapter.xsd_validate(
160
- @handle, doc.native
161
+ validation_handle(doc), doc.native
161
162
  )
163
+ engine_errors = dismiss_refuted_not_declared(engine_errors, doc)
162
164
  return engine_errors unless @rules_index
163
165
 
164
166
  # The #452 post-validation rules only ever add findings for
@@ -170,6 +172,438 @@ module Moxml
170
172
  SchemaRules.findings(doc, @rules_index)).uniq
171
173
  end
172
174
 
175
+ # Validations run against the compile-time handle. The
176
+ # composition fix (#522) is dismissal-side: the facade indexes
177
+ # every unit of the composition (the schema + the instance's
178
+ # xsi:schemaLocation hints) and drops engine strict-admission
179
+ # rows the index refutes — engines whose admission consults
180
+ # only the head unit's declarations never see the aux units,
181
+ # and recompiling a mixed-targetNamespace union would swap one
182
+ # false answer for another.
183
+ def validation_handle(_doc)
184
+ @handle
185
+ end
186
+
187
+ XSI_NS = "http://www.w3.org/2001/XMLSchema-instance"
188
+
189
+ # xsi:schemaLocation (namespace/path pairs) and
190
+ # xsi:noNamespaceSchemaLocation paths from the validated
191
+ # instance's root, resolved against this schema's directory
192
+ # and filtered to files that exist. Empty when this schema was
193
+ # compiled from a string (no base to resolve against).
194
+ def instance_schema_hints(doc)
195
+ return [].freeze unless @schema_path
196
+ return [].freeze unless doc&.root
197
+
198
+ base = File.dirname(@schema_path)
199
+ root = doc.root
200
+ hints = []
201
+ root.attributes.each do |attribute|
202
+ next unless attribute.namespace&.uri == XSI_NS
203
+
204
+ value = attribute.value.to_s
205
+ case attribute.name
206
+ when "noNamespaceSchemaLocation"
207
+ hints << value
208
+ when "schemaLocation"
209
+ hints.concat(value.split(/\s+/).reject(&:empty?)
210
+ .each_slice(2).map(&:last))
211
+ end
212
+ end
213
+ hints.uniq
214
+ .map { |hint| File.expand_path(hint, base) }
215
+ .select do |p|
216
+ File.file?(p) && p != File.expand_path(@schema_path)
217
+ end.freeze
218
+ end
219
+
220
+ # Engine strict-admission rows the composed unit set REFUTES:
221
+ # engines whose wildcard admission consults only the head
222
+ # unit's declaration table report "attribute 'x': not declared
223
+ # (strict)" for attributes declared in another unit of the
224
+ # same compiled composition, and the mixed-tns fresh union
225
+ # compile itself can mis-admit a root element the same way
226
+ # (leptris#1964 family, moxml#522). The facade owns the
227
+ # composition, so it can refute a row when the named item, as
228
+ # it appears in THIS instance, is a global declaration of any
229
+ # unit (namespace + local name). Only exact "not declared
230
+ # (strict)" rows are ever dropped; every other engine finding
231
+ # passes through untouched.
232
+ def dismiss_refuted_not_declared(engine_errors, doc)
233
+ return engine_errors if engine_errors.empty? || !@schema_path
234
+
235
+ hints = instance_schema_hints(doc)
236
+ units = composed_unit_set([@schema_path, *hints])
237
+ declared = composed_declarations(units)
238
+ admissible = element_admissibility(units)
239
+ return engine_errors if declared.empty? && admissible.empty?
240
+
241
+ attr_index = {}
242
+ element_ns = {}
243
+ stack = [doc.root]
244
+ while (element = stack.pop)
245
+ element_ns[(element.namespace&.uri || "")] ||= true
246
+ stack.concat(element.children.grep(Moxml::Element))
247
+ element.attributes.each do |attribute|
248
+ attr_uri = attribute.namespace&.uri || ""
249
+ ((attr_index[[attr_uri, attribute.name]] ||= []) <<
250
+ element.name)
251
+ end
252
+ end
253
+ engine_errors.reject do |finding|
254
+ match = NOT_DECLARED_ROW.match(finding.message)
255
+ next false unless match
256
+
257
+ element_name = match[1]
258
+ attribute_name = match[2]
259
+ if attribute_name
260
+ attr_index.any? do |(uri, attr_name), holders|
261
+ next false unless attr_name == attribute_name &&
262
+ holders.include?(element_name)
263
+
264
+ # A global declaration of some unit, or a group member
265
+ # the element's type admits (moxml#522 attgC class —
266
+ # exact (element, attribute, namespace) match).
267
+ declared.key?([uri, attribute_name]) ||
268
+ admissible.dig(element_name, attribute_name) == uri
269
+ end
270
+ else
271
+ declared.key?(["", element_name]) ||
272
+ (element_ns[element_name] &&
273
+ declared.key?([element_ns[element_name], element_name]))
274
+ end
275
+ end
276
+ end
277
+
278
+ # The composition closure of the unit set: every unit's
279
+ # xs:include / xs:import / xs:redefine schemaLocation targets
280
+ # (existing files), cycle-guarded, resolved relative to each
281
+ # unit. The engine compiles these documents as ONE composition;
282
+ # the facade's index must see the same set even when the
283
+ # instance carries no xsi hints (attgC035's redefined groups
284
+ # live in documents only the schema graph knows).
285
+ def composed_unit_set(units)
286
+ @composed_unit_set ||= {}
287
+ cached = @composed_unit_set[units]
288
+ return cached if cached
289
+
290
+ seen = {}
291
+ queue = units.dup
292
+ until queue.empty?
293
+ unit = queue.pop
294
+ next if seen.key?(unit)
295
+
296
+ seen[unit] = true
297
+ base = File.dirname(unit)
298
+ unit_schema_refs(unit).each do |ref|
299
+ target = File.expand_path(ref, base)
300
+ queue << target if File.file?(target) && !seen.key?(target)
301
+ end
302
+ end
303
+ closure = seen.keys.sort
304
+ @composed_unit_set[units] = closure
305
+ closure
306
+ end
307
+
308
+ # schemaLocation hrefs on the unit's own xs:include, xs:import
309
+ # and xs:redefine (poison-immune REXML read; the engine's
310
+ # component tables are process-global and a later parse can
311
+ # poison compiled handles, moxml#522).
312
+ def unit_schema_refs(unit)
313
+ require "rexml/document"
314
+ root = REXML::Document.new(File.read(unit)).root
315
+ return [].freeze unless root&.name == "schema"
316
+
317
+ refs = %w[include import redefine].map do |tag|
318
+ root.elements.each_with_object([]) do |node, out|
319
+ next unless node.name == tag
320
+
321
+ loc = node.attributes["schemaLocation"]
322
+ out << loc if loc.to_s.match?(/\S/)
323
+ end
324
+ end.flatten
325
+ refs.freeze
326
+ rescue StandardError
327
+ [].freeze
328
+ end
329
+
330
+ # {element name => {attribute name => namespace}}: every
331
+ # attribute the composition admits for each element — the
332
+ # element's type's attributeGroup members resolved transitively
333
+ # across units (import groups; an xs:redefine body's self-ref
334
+ # is the 4.2.2 hook to the ORIGINAL definition, so the
335
+ # effective group is body UNION original). Only exact members
336
+ # of the real composition land here, so a genuinely undeclared
337
+ # attribute never becomes refutable.
338
+ def element_admissibility(units)
339
+ @element_admissibility ||= {}
340
+ cached = @element_admissibility[units]
341
+ return cached if cached
342
+
343
+ facts = {}
344
+ units.each do |unit|
345
+ unit_facts = schema_unit_facts(unit)
346
+ facts[unit] = unit_facts if unit_facts
347
+ end
348
+
349
+ # Effective group definitions: redefining bodies merge the
350
+ # target unit's original when (and only when) the body hooks
351
+ # it with a self-ref. Keys: bare name AND {uri}name (refs
352
+ # resolve with the referencing unit's xmlns, bare-name
353
+ # fallback — same-name groups are composition-unique).
354
+ plain = {}
355
+ bodies = {}
356
+ facts.each_value do |unit|
357
+ unit[:redefines].each do |name, target_path|
358
+ body = unit[:groups][name]
359
+ original = facts[target_path]&.dig(:groups, name)
360
+ bodies[name] = merge_group_def(body, original, name)
361
+ end
362
+ unit[:groups].each do |name, defn|
363
+ plain[name] = defn unless unit[:redefines].key?(name)
364
+ end
365
+ end
366
+ # Redefine bodies are the EFFECTIVE definitions — they win
367
+ # over the plain originals.
368
+ groups = plain.merge(bodies)
369
+
370
+ resolved = {}
371
+ facts.each_value do |unit|
372
+ unit[:elements].each do |element_name, element|
373
+ attrs = {}
374
+ element[:groups].each do |qref|
375
+ resolve_group_ref(qref, unit, groups, attrs, {})
376
+ end
377
+ element[:attrs].each do |name, uri|
378
+ attrs[name] = uri
379
+ end
380
+ resolved[element_name] = attrs unless attrs.empty?
381
+ end
382
+ end
383
+ @element_admissibility[units] = resolved.freeze
384
+ end
385
+
386
+ # body UNION original when the body hooks the original with a
387
+ # self-ref; the body alone otherwise.
388
+ def merge_group_def(body, original, name)
389
+ return body unless body && original.is_a?(Hash) &&
390
+ body[:refs].include?(name)
391
+
392
+ {
393
+ attrs: body[:attrs] + original[:attrs],
394
+ refs: body[:refs] + original[:refs],
395
+ }
396
+ end
397
+
398
+ # Resolve one attributeGroup ref (raw QName string) into attrs
399
+ # {name => uri}, transitively; cycle-guarded by +seen+. Keys
400
+ # are bare names with qualified "{uri}name" fallbacks.
401
+ def resolve_group_ref(qref, from_unit, groups, attrs, seen)
402
+ return if seen.key?(qref)
403
+
404
+ seen[qref] = true
405
+ prefix, _, local = qref.rpartition(":")
406
+ xmlns = from_unit[:xmlns]
407
+ uri = if prefix.empty?
408
+ xmlns["xmlns"].to_s
409
+ else
410
+ xmlns["xmlns:#{prefix}"].to_s
411
+ end
412
+ defn = groups["{#{uri}}#{local}"] || groups[local]
413
+ return unless defn.is_a?(Hash)
414
+
415
+ defn[:attrs].each do |name, attr_uri|
416
+ attrs[name] = attr_uri
417
+ end
418
+ defn[:refs].each do |ref|
419
+ resolve_group_ref(ref, from_unit, groups, attrs, seen)
420
+ end
421
+ end
422
+
423
+ # Per-unit schema facts for the admissibility index:
424
+ # {tns, afd (attributeFormDefault qualified?), xmlns,
425
+ # groups: {name => {attrs, refs}}, redefines: {name => target},
426
+ # elements: {name => {groups, attrs}}} — poison-immune REXML.
427
+ # Attribute member URIs use the DEFINING unit's form rules;
428
+ # group refs stay raw QNames (resolved at chase time).
429
+ def schema_unit_facts(unit)
430
+ require "rexml/document"
431
+ root = REXML::Document.new(File.read(unit)).root
432
+ return nil unless root&.name == "schema"
433
+
434
+ xmlns = {}
435
+ root.attributes.each do |name, value|
436
+ xmlns[name] = value.to_s if name.start_with?("xmlns")
437
+ end
438
+ tns = root.attributes["targetNamespace"].to_s
439
+ afd = root.attributes["attributeFormDefault"] == "qualified"
440
+
441
+ member_uri = lambda do |node|
442
+ qualified = node.attributes["form"] == "qualified" || afd
443
+ qualified ? tns : ""
444
+ end
445
+
446
+ groups = {}
447
+ redefines = {}
448
+ root.elements.each do |node|
449
+ case node.name
450
+ when "attributeGroup"
451
+ name = node.attributes["name"]
452
+ next unless name
453
+
454
+ groups[name] = walk_group_members(node, member_uri)
455
+ when "redefine"
456
+ loc = node.attributes["schemaLocation"]
457
+ node.elements.each do |body|
458
+ next unless body.name == "attributeGroup"
459
+
460
+ name = body.attributes["name"]
461
+ next unless name
462
+
463
+ groups[name] = walk_group_members(body, member_uri)
464
+ redefines[name] = File.expand_path(loc, File.dirname(unit)) if
465
+ loc.to_s.match?(/\S/)
466
+ end
467
+ end
468
+ end
469
+
470
+ elements = {}
471
+ root.elements.each do |node|
472
+ next unless node.name == "element"
473
+ next unless (name = node.attributes["name"])
474
+
475
+ container = node.elements.find { |child| child.name == "complexType" }
476
+ type_name = node.attributes["type"].to_s
477
+ container ||= root.elements.find do |child|
478
+ child.name == "complexType" && child.attributes["name"] == type_name
479
+ end
480
+ next unless container.is_a?(REXML::Element)
481
+
482
+ groups_seen, attrs = walk_type_attributes(container, member_uri)
483
+ elements[name] = { groups: groups_seen, attrs: attrs }
484
+ end
485
+
486
+ {
487
+ tns: tns, afd: afd, xmlns: xmlns, groups: groups,
488
+ redefines: redefines, elements: elements
489
+ }
490
+ rescue StandardError
491
+ nil
492
+ end
493
+
494
+ # Refs and attribute members of one attributeGroup definition.
495
+ # Refs stay raw QNames — the chase resolves prefixes through
496
+ # the referencing unit's xmlns.
497
+ def walk_group_members(node, member_uri)
498
+ refs = []
499
+ attrs = []
500
+ node.elements.each do |child|
501
+ case child.name
502
+ when "attribute"
503
+ name = child.attributes["name"]
504
+ attrs << [name, member_uri.call(child)] if name
505
+ when "attributeGroup"
506
+ ref = child.attributes["ref"]
507
+ refs << ref if ref.to_s.match?(/\S/)
508
+ end
509
+ end
510
+ { attrs: attrs, refs: refs }
511
+ end
512
+
513
+ # attributeGroup refs + own attribute members of a complexType
514
+ # (one level; nested group-through-group chains resolve at
515
+ # chase time). Returns [raw refs, attrs].
516
+ def walk_type_attributes(container, member_uri)
517
+ refs = []
518
+ attrs = []
519
+ container.elements.each do |child|
520
+ case child.name
521
+ when "attributeGroup"
522
+ ref = child.attributes["ref"]
523
+ refs << ref if ref.to_s.match?(/\S/)
524
+ when "attribute"
525
+ name = child.attributes["name"]
526
+ attrs << [name, member_uri.call(child)] if name
527
+ end
528
+ end
529
+ [refs, attrs]
530
+ end
531
+
532
+ # {uri, local} => true for every global xs:attribute and
533
+ # xs:element declared across the unit set: a global
534
+ # declaration always takes its unit's targetNamespace.
535
+ # Parsed with REXML when available: the engine's component
536
+ # tables are process-global and can be poisoned by later
537
+ # parses between compile and validate (moxml#522) — a
538
+ # declaration index must be poison-immune. Falls back to the
539
+ # context parse when REXML is unavailable.
540
+ def composed_declarations(units)
541
+ @composed_declarations ||= {}
542
+ cached = @composed_declarations[units]
543
+ return cached if cached
544
+
545
+ declared = {}
546
+ parser = begin
547
+ require "rexml"
548
+ :rexml
549
+ rescue LoadError
550
+ :context
551
+ end
552
+ units.each do |unit|
553
+ tns, decls = unit_declarations(unit, parser)
554
+ next unless decls
555
+
556
+ decls.each do |name|
557
+ declared[[tns, name]] = true
558
+ end
559
+ end
560
+ @composed_declarations[units] = declared
561
+ end
562
+
563
+ # [targetNamespace, [global attribute/element names]] for one
564
+ # schema unit, per the parser flavor. REXML's read is
565
+ # poison-immune; the context parse is the fallback.
566
+ def unit_declarations(unit, parser)
567
+ text = File.read(unit)
568
+ if parser == :rexml
569
+ require "rexml/document"
570
+ root = REXML::Document.new(text).root
571
+ return nil unless root&.name == "schema"
572
+
573
+ tns = root.attributes["targetNamespace"].to_s
574
+ names = %w[attribute element].freeze
575
+ decls = root.elements.filter_map do |decl|
576
+ next unless names.include?(decl.name)
577
+
578
+ decl.attributes["name"]
579
+ end
580
+ else
581
+ root = @context.parse(text).root
582
+ return nil unless root&.name == "schema"
583
+
584
+ tns = root.attributes.find { |a| a.name == "targetNamespace" }
585
+ &.value.to_s
586
+ names = %w[attribute element].freeze
587
+ decls = root.children
588
+ .grep(Moxml::Element)
589
+ .filter_map do |decl|
590
+ next unless names.include?(decl.name)
591
+
592
+ decl.attributes.find { |a| a.name == "name" }&.value
593
+ end
594
+ end
595
+ [tns, decls]
596
+ rescue StandardError
597
+ nil
598
+ end
599
+
600
+ # Matches both row shapes the strict admission emits — with and
601
+ # without the attribute segment:
602
+ # element 'doc' @2:144: element 'doc': not declared (strict)
603
+ # element 'elem' @3:46: attribute 'foo': not declared (strict)
604
+ NOT_DECLARED_ROW =
605
+ /\Aelement '([^']+)'(?: @\S+)? (?:(?:element|attribute) '([^']+)': )?not declared \(strict\)\z/
606
+
173
607
  def valid?(node_or_xml)
174
608
  check_released!
175
609
  validate(node_or_xml).empty?
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Moxml
4
+ # The XSD-structural conformance family (#490 item 8, family 7):
5
+ # single-file schema + instance pairs — namespace-aligned admission
6
+ # shapes (builtin lexical, strict wildcard with a global in the
7
+ # target namespace, substitution groups, lax ##other wildcards).
8
+ # Every row's verdict is verified identical on both primary engines
9
+ # through `Context#xsd_file`. Schemas are compact single-line
10
+ # documents (whitespace-insensitive to the compiler); instances use
11
+ # explicit prefixes because attribute defaults never qualify.
12
+ # Multi-file composition shapes (imports, redefines) stay in the
13
+ # W3C corpus lanes — these rows are deliberately self-contained for
14
+ # the cross-language runners.
15
+ module XsdStructuralCases
16
+ XSD_LEXICAL =
17
+ '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:t" xmlns="urn:t"><xs:element name="doc"><xs:complexType><xs:sequence><xs:element name="n" type="xs:int"/><xs:element name="flag" type="xs:boolean"/></xs:sequence><xs:attribute name="a" type="xs:string"/><xs:anyAttribute namespace="##any" processContents="strict"/></xs:complexType></xs:element><xs:attribute name="ext" type="xs:string"/></xs:schema>'
18
+ XSD_SUBS =
19
+ '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:s" xmlns="urn:s"><xs:element name="shape" type="xs:string" abstract="true"/><xs:element name="circle" type="xs:string" substitutionGroup="shape"/><xs:element name="canvas"><xs:complexType><xs:sequence><xs:element ref="shape" maxOccurs="unbounded"/></xs:sequence></xs:complexType></xs:element></xs:schema>'
20
+ XSD_WILDCARD =
21
+ '<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:t" xmlns="urn:t"><xs:element name="doc"><xs:complexType><xs:sequence><xs:any processContents="lax" namespace="##other"/></xs:sequence></xs:complexType></xs:element></xs:schema>'
22
+
23
+ CONFORMANCE_CASES = [
24
+ ["builtin-lexical-valid", XSD_LEXICAL,
25
+ '<d:doc xmlns:d="urn:t" a="x"><n>42</n><flag>true</flag></d:doc>',
26
+ true],
27
+ ["builtin-lexical-invalid", XSD_LEXICAL,
28
+ '<d:doc xmlns:d="urn:t" a="x"><n>not-an-int</n><flag>true</flag></d:doc>',
29
+ false],
30
+ ["strict-admission-declared", XSD_LEXICAL,
31
+ '<d:doc xmlns:d="urn:t" xmlns:t="urn:t" t:ext="y" a="x"><n>1</n><flag>false</flag></d:doc>',
32
+ true],
33
+ ["strict-admission-undeclared", XSD_LEXICAL,
34
+ '<d:doc xmlns:d="urn:t" ghost="z" a="x"><n>1</n><flag>false</flag></d:doc>',
35
+ false],
36
+ ["substitution-member", XSD_SUBS,
37
+ '<s:canvas xmlns:s="urn:s"><s:circle>unit</s:circle></s:canvas>',
38
+ true],
39
+ ["wildcard-lax-other-ns", XSD_WILDCARD,
40
+ '<d:doc xmlns:d="urn:t" xmlns:o="urn:o"><o:kid/></d:doc>',
41
+ true],
42
+ ].map(&:freeze).freeze
43
+ end
44
+ end
data/lib/moxml.rb CHANGED
@@ -89,6 +89,9 @@ module Moxml
89
89
  autoload :Builder, "moxml/builder"
90
90
  autoload :Entity, "moxml/entity"
91
91
  autoload :EntityRegistry, "moxml/entity_registry"
92
+ autoload :SerializationCases, "moxml/serialization_cases"
93
+ autoload :C14nCases, "moxml/c14n_cases"
94
+ autoload :XsdStructuralCases, "moxml/xsd_structural_cases"
92
95
  autoload :NativeAttachment, "moxml/native_attachment"
93
96
  autoload :XmlUtils, "moxml/xml_utils"
94
97
  autoload :XmlEmitter, "moxml/xml_emitter"
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "tmpdir"
5
+ require "spec_helper"
6
+
7
+ # Conformance families 6 and 7 (#490 item 8):
8
+ #
9
+ # - c14n: canonical C14N 1.0 vectors (comments dropped). Every row
10
+ # canonicalizes to IDENTICAL bytes on all adapters — the Ruby
11
+ # reference and the leptris native delegation are pinned
12
+ # byte-for-byte by the c14n suite.
13
+ # - xsd_structural: single-file schema + instance pairs, the
14
+ # committed facade verdict, verified identical on both primary
15
+ # engines through `Context#xsd_file`.
16
+ #
17
+ # Both export into conformance/moxml-conformance.json (schema 5) for
18
+ # the cross-language runners; a runner mismatch on any row is a REAL
19
+ # model finding — report it, don't bend the row.
20
+ RSpec.describe "conformance families: c14n and xsd_structural (schema 5)" do
21
+ let(:primary) { %i[nokogiri leptris] }
22
+
23
+ describe "c14n" do
24
+ Moxml::C14nCases::CONFORMANCE_CASES.each do |id, input, canonical|
25
+ it "canonicalizes #{id} to the committed bytes" do
26
+ Moxml::Adapter::AVAILABLE_ADAPTERS.each do |name|
27
+ begin
28
+ doc = Moxml.new(name).parse(input)
29
+ rescue Moxml::Error, NotImplementedError, LoadError
30
+ next
31
+ end
32
+
33
+ expect(Moxml::C14n.canonicalize(doc)).to eq(canonical)
34
+ end
35
+ end
36
+ end
37
+
38
+ it "is exported by the dataset with the committed rows" do
39
+ path = File.expand_path("../../conformance/moxml-conformance.json",
40
+ __dir__)
41
+ family = JSON.parse(File.read(path)).dig("cases", "c14n") || []
42
+ expect(family.map { |row| row["id"] })
43
+ .to eq(Moxml::C14nCases::CONFORMANCE_CASES.map(&:first))
44
+ end
45
+ end
46
+
47
+ describe "xsd_structural" do
48
+ Moxml::XsdStructuralCases::CONFORMANCE_CASES.each do |id, schema, instance, valid|
49
+ it "answers #{id} as #{valid ? 'valid' : 'invalid'} on both primaries" do
50
+ Dir.mktmpdir do |dir|
51
+ schema_path = File.join(dir, "#{id}.xsd")
52
+ File.write(schema_path, schema)
53
+ primary.each do |name|
54
+ ctx = Moxml.new(name)
55
+ errors = ctx.xsd_file(schema_path).validate(ctx.parse(instance))
56
+ expect(errors.empty?).to be(valid),
57
+ "#{id} on #{name}: #{errors.map(&:message).join('; ')}"
58
+ end
59
+ end
60
+ end
61
+ end
62
+
63
+ it "is exported by the dataset with the committed rows" do
64
+ path = File.expand_path("../../conformance/moxml-conformance.json",
65
+ __dir__)
66
+ family = JSON.parse(File.read(path)).dig("cases", "xsd_structural") || []
67
+ expect(family.map { |row| row["id"] })
68
+ .to eq(Moxml::XsdStructuralCases::CONFORMANCE_CASES.map(&:first))
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "spec_helper"
5
+
6
+ # The serialization conformance family (#490 item 8, family 5): the
7
+ # committed element-level serialization answers — the rows are the
8
+ # model, exported to conformance/moxml-conformance.json (schema 4)
9
+ # for the cross-language runners. Every row round-trips idempotently
10
+ # on both primary engines (parse -> serialize twice, byte equality).
11
+ #
12
+ # Cross-adapter note (D-25): oga, rexml, ox, headed_ox and libxml
13
+ # emit a literal newline for a `&#10;` character reference in an
14
+ # attribute value; the committed answer is the round-trip-preserving
15
+ # escaped form (nokogiri, leptris), since a literal newline
16
+ # normalizes to space on the next parse. The family pins the primary
17
+ # adapters; the differential's per-adapter rows follow the register.
18
+ RSpec.describe "serialization conformance family (schema 4)" do
19
+ let(:primary) { %i[nokogiri leptris] }
20
+
21
+ Moxml::SerializationCases::CONFORMANCE_CASES.each do |id, input, output|
22
+ it "serializes #{id} to the committed answer" do
23
+ primary.each do |name|
24
+ doc = Moxml.new(name).parse(input)
25
+ expect(doc.root.to_xml(indent: 0)).to eq(output)
26
+ end
27
+ end
28
+ end
29
+
30
+ it "is exported by the dataset with the committed rows" do
31
+ path = File.expand_path("../../conformance/moxml-conformance.json",
32
+ __dir__)
33
+ dataset = JSON.parse(File.read(path))
34
+ family = dataset.dig("cases", "serialization") || []
35
+ expect(family.size).to eq(
36
+ Moxml::SerializationCases::CONFORMANCE_CASES.size,
37
+ )
38
+ expect(family.map { |row| row["id"] })
39
+ .to eq(Moxml::SerializationCases::CONFORMANCE_CASES.map(&:first))
40
+ end
41
+ end