lutaml-model 0.8.202 → 0.8.204

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.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/dependent-tests.yml +59 -2
  4. data/.github/workflows/lml-conformance.yml +71 -0
  5. data/.gitignore +7 -0
  6. data/README.adoc +1 -0
  7. data/docs/_guides/lml-cross-language.adoc +128 -0
  8. data/docs/_references/lml-conformance.adoc +78 -0
  9. data/docs/_references/lml-contract.adoc +290 -0
  10. data/docs/_references/plan-schema-spec.adoc +45 -18
  11. data/lib/lutaml/key_value/transform.rb +140 -21
  12. data/lib/lutaml/model/attribute.rb +11 -6
  13. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  14. data/lib/lutaml/model/lml/bundle.rb +171 -0
  15. data/lib/lutaml/model/lml/document.rb +46 -10
  16. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  17. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  18. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  19. data/lib/lutaml/model/lml/exporter.rb +223 -87
  20. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  21. data/lib/lutaml/model/lml/importer.rb +180 -0
  22. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  23. data/lib/lutaml/model/lml/materialization.rb +257 -0
  24. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  25. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  26. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  27. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  28. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  29. data/lib/lutaml/model/lml.rb +61 -0
  30. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  31. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  32. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  33. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  34. data/lib/lutaml/model/sequence.rb +7 -0
  35. data/lib/lutaml/model/serialize/deserialization_context.rb +21 -0
  36. data/lib/lutaml/model/serialize/initialization.rb +19 -9
  37. data/lib/lutaml/model/version.rb +1 -1
  38. data/lib/lutaml/xml/leptris/native_emission.rb +4 -0
  39. data/lib/lutaml/xml/leptris/plan_serializer.rb +14 -9
  40. data/schema/lml/document.schema.json +179 -0
  41. data/scripts/lml/export_choices.rb +57 -0
  42. data/scripts/lml/export_conformance.rb +23 -0
  43. data/scripts/lml/export_mml.rb +253 -0
  44. data/scripts/lml/export_preservation.rb +421 -0
  45. data/scripts/lml/verify_artifacts.rb +31 -0
  46. data/spec/address_person_spec.rb +8 -0
  47. data/spec/address_spec.rb +4 -0
  48. data/spec/fixtures/lml_artifacts/model.json +1 -0
  49. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  50. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  51. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  52. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  53. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  54. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  55. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  56. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  57. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  58. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  59. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  60. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  61. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  62. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  63. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  64. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  65. data/spec/fixtures/lml_contract/bundle.json +81 -0
  66. data/spec/fixtures/lml_contract/manifest.json +30 -0
  67. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  68. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  69. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  70. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  71. data/spec/fixtures/lml_mml/inventory.json +1 -0
  72. data/spec/fixtures/lml_mml/manifest.json +1 -0
  73. data/spec/fixtures/lml_mml/oracle.json +1 -0
  74. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  75. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  76. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  77. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  78. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  79. data/spec/lutaml/key_value/group_instantiate_spec.rb +204 -1
  80. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  81. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  82. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  83. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  84. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  85. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  86. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  87. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  88. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  89. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  90. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  91. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  92. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  93. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  94. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  95. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  96. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  97. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  98. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  99. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  100. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  101. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  102. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  103. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  104. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  105. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  106. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  107. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  108. data/spec/lutaml/model/sequence_spec.rb +48 -0
  109. data/spec/lutaml/model/serialize/deserialization_context_spec.rb +68 -0
  110. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  111. data/spec/spec_helper.rb +4 -5
  112. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  113. data/spec/support/lml_conformance.rb +292 -0
  114. data/spec/support/lml_corpus.rb +75 -0
  115. data/spec/support/lml_export_fixtures.rb +190 -0
  116. data/spec/support/lml_graph_fixtures.rb +108 -0
  117. data/spec/support/lml_import_fixtures.rb +52 -0
  118. data/spec/support/lml_mapping_fixtures.rb +170 -0
  119. data/spec/support/lml_materialization_fixtures.rb +53 -0
  120. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  121. data/spec/support/lml_order_fixtures.rb +151 -0
  122. metadata +93 -2
  123. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -0,0 +1,256 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "json"
5
+ require "yaml"
6
+ require "lutaml/model/lml/mapping_compiler"
7
+ require "lutaml/model/lml/ordered_content"
8
+ require_relative "../../support/lml_mapping_fixtures"
9
+
10
+ # lutaml-model#1033 card 03b: rebuild Ruby mapping policies from a
11
+ # neutral bundle.
12
+ RSpec.describe Lutaml::Model::Lml::MappingCompiler do
13
+ let(:fx) { LmlMappingFixtures }
14
+ let(:exporter) { Lutaml::Model::Lml::Exporter }
15
+ let(:rejected) { Lutaml::Model::Lml::RejectedError }
16
+ let(:holder) { stub_const("LmlMapGen", Module.new) }
17
+
18
+ def compile(bundle, into: holder)
19
+ classes = fx.fresh_classes(bundle, into)
20
+ described_class.apply(classes, bundle)
21
+ classes
22
+ end
23
+
24
+ def refusal(bundle, into: holder)
25
+ compile(bundle, into: into)
26
+ nil
27
+ rescue rejected => e
28
+ e
29
+ end
30
+
31
+ # A bundle with one hand-edited model document.
32
+ def edited(source = fx::Doc)
33
+ bundle = Marshal.load(Marshal.dump(fx.bundle_for(source, fx::Item, fx::Entry)))
34
+ yield bundle["models"]["#{source.name}@default"]["mappings"]
35
+ bundle
36
+ end
37
+
38
+ describe "restoring exported policies" do
39
+ let(:bundle) { fx.bundle_for(fx::Doc, fx::Item, fx::Entry) }
40
+ let(:classes) { compile(bundle) }
41
+ let(:doc) { classes["LmlMappingFixtures::Doc@default"] }
42
+
43
+ it "exports fresh mappings identical to the source models (xml/json/yaml)" do
44
+ classes.each do |id, klass|
45
+ expect(exporter.project(klass).fetch("mappings")).to eq(bundle["models"][id]["mappings"]), id
46
+ end
47
+ end
48
+
49
+ it "serializes like the source in every format" do
50
+ attrs = { title: "T", count: 3, note: "", kind: "b",
51
+ items: [{ id: "i", label: "<x>" }], entries: [{ name: "n", value: "v" }] }
52
+ source = fx::Doc.new(attrs.merge(items: [fx::Item.new(id: "i", label: "<x>")],
53
+ entries: [fx::Entry.new(name: "n", value: "v")]))
54
+ fresh = doc.new(attrs.merge(items: [classes["LmlMappingFixtures::Item@default"].new(id: "i", label: "<x>")],
55
+ entries: [classes["LmlMappingFixtures::Entry@default"].new(name: "n", value: "v")]))
56
+
57
+ expect(fresh.to_xml).to eq(source.to_xml)
58
+ expect(fresh.to_json).to eq(source.to_json)
59
+ expect(fresh.to_yaml).to eq(source.to_yaml)
60
+ expect(fresh.to_xml).to include('xmlns="urn:lml-map:a"', "<![CDATA[<x>]]>")
61
+ end
62
+
63
+ it "restores child_mappings on a key-value rule" do
64
+ with = fx.with_child_mappings(bundle)
65
+ klass = compile(with)["LmlMappingFixtures::Doc@default"]
66
+ entries = klass.mappings_for(:json).mappings.find { |r| r.to == :entries }
67
+ expect(entries.child_mappings).to eq(name: :key, value: :value)
68
+ expect(klass.new(count: 1, entries: [klass.attributes[:entries].type(nil).new(name: "n", value: "v")]).to_json)
69
+ .to include('"entries":{"n":"v"}')
70
+ end
71
+
72
+ it "parses documents the source parses" do
73
+ xml = fx::Doc.new(title: "t", count: 1, note: "n").to_xml
74
+ expect(doc.from_xml(xml).to_xml).to eq(xml)
75
+ json = fx::Doc.new(title: "t", count: 1, entries: [fx::Entry.new(name: "a", value: "b")]).to_json
76
+ expect(doc.from_json(json).to_json).to eq(fx::Doc.from_json(json).to_json)
77
+ end
78
+
79
+ it "does not touch the source classes" do
80
+ classes
81
+ expect(fx::Doc.mappings_for(:xml).root_element).to eq("doc")
82
+ expect(doc).not_to equal(fx::Doc)
83
+ expect(classes.values).to all(satisfy { |k| k.name.start_with?("LmlMapGen::") })
84
+ end
85
+
86
+ it "gives equal namespaces one class and restores the prefix" do
87
+ xml = doc.mappings_for(:xml)
88
+ expect([xml.namespace_uri, xml.namespace_prefix]).to eq(["urn:lml-map:a", "m"])
89
+ end
90
+
91
+ it "declares nothing for formats the bundle omits" do
92
+ bundle = edited { |m| m.delete("yaml") }
93
+ expect(compile(bundle)["LmlMappingFixtures::Doc@default"].mappings.keys).to contain_exactly(:xml, :json)
94
+ end
95
+ end
96
+
97
+ describe "render and namespace policy" do
98
+ it "restores the resolved nil/empty/default of each rule" do
99
+ doc = compile(fx.bundle_for(fx::Doc, fx::Item, fx::Entry))["LmlMappingFixtures::Doc@default"]
100
+ by_name = doc.mappings_for(:xml).mappings.to_h { |r| [Array(r.name).first, r] }
101
+ expect(by_name["note"].value_map(:to)).to include(nil: :omitted, empty: :blank)
102
+ expect(by_name["note"].render_default).to be(true)
103
+ expect(by_name["title"].value_map(:to)).to include(nil: :blank)
104
+ expect(by_name["kind"].value_map(:to)).to include(nil: :nil)
105
+ end
106
+
107
+ it "treats unqualified as the explicit opt-out" do
108
+ bundle = edited { |m| m["xml"]["rules"][0]["namespace"] = { "mode" => "unqualified" } }
109
+ rule = compile(bundle)["LmlMappingFixtures::Doc@default"].mappings_for(:xml).mappings.first
110
+ expect(rule.namespace_set?).to be(true)
111
+ expect(rule.namespace).to be_nil
112
+ end
113
+
114
+ it "restores an unqualified model namespace" do
115
+ bundle = edited { |m| m["xml"]["namespace"] = { "mode" => "unqualified" } }
116
+ expect(compile(bundle)["LmlMappingFixtures::Doc@default"].mappings_for(:xml).namespace_param).to eq(:blank)
117
+ end
118
+
119
+ it "refuses a rule-level namespace uri, which the Ruby DSL cannot express" do
120
+ bundle = edited { |m| m["xml"]["rules"][0]["namespace"] = { "mode" => "uri", "uri" => "urn:other" } }
121
+ expect(refusal(bundle)).to have_attributes(reason: :unsupported_mapping, model_id: "LmlMappingFixtures::Doc@default")
122
+ end
123
+ end
124
+
125
+ describe "ordered content" do
126
+ let(:para) { LmlOrderFixtures::Para }
127
+ let(:ids) { LmlOrderFixtures.constants }
128
+ let(:bundle) { fx.bundle_for(para, LmlOrderFixtures::Emphasis) }
129
+ let(:classes) { compile(bundle) }
130
+ let(:fresh) { classes["LmlOrderFixtures::Para@default"] }
131
+
132
+ def descriptor(klass)
133
+ Lutaml::Model::Lml::OrderedContent.descriptor_for(klass, mapping: klass.mappings_for(:xml))
134
+ end
135
+
136
+ it "rebuilds mixed mappings that capture the same tokens as the source" do
137
+ xml = LmlOrderFixtures::PARA_XML
138
+ expect(fresh.mappings_for(:xml)).to be_mixed_content
139
+ tokens = Lutaml::Model::Lml::OrderedContent.capture(descriptor(fresh), fresh.from_xml(xml))
140
+ expect(tokens).to eq(Lutaml::Model::Lml::OrderedContent.capture(descriptor(para), para.from_xml(xml)))
141
+ end
142
+
143
+ it "applies tokens to a rebuilt instance and serializes in token order" do
144
+ xml = LmlOrderFixtures::PARA_XML
145
+ tokens = Lutaml::Model::Lml::OrderedContent.capture(descriptor(para), para.from_xml(xml))
146
+ parsed = fresh.from_xml(xml)
147
+ target = fresh.new(fresh.attributes.keys.to_h { |k| [k, parsed.public_send(k)] })
148
+ Lutaml::Model::Lml::OrderedContent.apply(descriptor(fresh), tokens, target)
149
+ expect(target.to_xml).to eq(para.from_xml(xml).to_xml)
150
+ end
151
+
152
+ it "makes a model that declares ordered_content an ordered mapping" do
153
+ bundle = fx.bundle_for(LmlOrderFixtures::Transport)
154
+ expect(bundle["models"].values.first["mappings"]["xml"]).not_to have_key("mixed_content")
155
+ bundle["models"].values.first["mappings"]["xml"].delete("ordered")
156
+ mapping = compile(bundle)["LmlOrderFixtures::Transport@default"].mappings_for(:xml)
157
+ expect(mapping).to be_ordered
158
+ end
159
+
160
+ it "refuses a malformed ordered_content descriptor and the reserved member" do
161
+ bad = Marshal.load(Marshal.dump(bundle))
162
+ bad["models"]["LmlOrderFixtures::Para@default"]["ordered_content"] = { "member" => "x", "version" => 1 }
163
+ expect(refusal(bad)&.reason).to eq(:invalid_ordered_content)
164
+
165
+ reserved = Marshal.load(Marshal.dump(bundle))
166
+ reserved["models"]["LmlOrderFixtures::Para@default"]["attributes"] <<
167
+ { "name" => "__lml_content", "type" => { "kind" => "builtin", "name" => "string" } }
168
+ expect(refusal(reserved, into: stub_const("LmlMapGenTwo", Module.new))&.reason).to eq(:reserved_member)
169
+ end
170
+ end
171
+
172
+ describe "polymorphism" do
173
+ let(:bundle) { fx.with_discriminator(fx.bundle_for(fx::Shape, fx::Circle)) }
174
+
175
+ it "resolves cases to installed constants and round-trips them" do
176
+ classes = compile(bundle)
177
+ shape = classes["LmlMappingFixtures::Shape@default"]
178
+ rule = shape.mappings_for(:xml).mappings.first
179
+ expect(rule.polymorphic).to eq(attribute: "kind", class_map: { "circle" => "LmlMapGen::Circle" })
180
+ json_rule = shape.mappings_for(:json).mappings.first
181
+ expect(json_rule.polymorphic).to eq(attribute: "kind", class_map: { "circle" => "LmlMapGen::Circle" })
182
+ end
183
+
184
+ it "refuses a case target that is not in the model map" do
185
+ error = refusal(Marshal.load(Marshal.dump(bundle)).tap do |b|
186
+ b["models"]["LmlMappingFixtures::Shape@default"]["mappings"]["xml"]["rules"][0]["discriminator"]["cases"]["circle"] = "Nope@default"
187
+ end)
188
+ expect(error).to have_attributes(reason: :unknown_discriminator)
189
+ end
190
+
191
+ it "refuses a target with no installed constant (Ruby resolves by Object.const_get)" do
192
+ classes = bundle["models"].keys.to_h { |id| [id, Class.new(Lutaml::Model::Serializable)] }
193
+ bundle["models"].each { |id, doc| fx.declare_attributes(classes[id], doc, classes) }
194
+ expect { described_class.apply(classes, bundle) }
195
+ .to raise_error(rejected) { |e| expect(e.reason).to eq(:unsupported_mapping) }
196
+ end
197
+ end
198
+
199
+ describe "refusals" do
200
+ {
201
+ "an unknown format" => [->(m) { m["toml"] = { "rules" => [] } }, :unsupported_format],
202
+ "an xml sequence" => [->(m) { m["xml"]["sequence"] = ["x"] }, :unsupported_mapping],
203
+ "a sequence rule" => [->(m) { m["xml"]["rules"][0]["kind"] = "sequence" }, :unsupported_mapping],
204
+ "format-level root_mappings" => [->(m) { m["json"]["root_mappings"] = { "a" => "b" } }, :unsupported_mapping],
205
+ "an executable key" => [->(m) { m["xml"]["rules"][0]["with"] = {} }, :unsupported_mapping],
206
+ "polymorphic_map" => [->(m) { m["xml"]["rules"][0]["polymorphic_map"] = {} }, :unsupported_mapping],
207
+ "a missing target" => [->(m) { m["xml"]["rules"][0].delete("to") }, :unsupported_mapping],
208
+ "an unknown target" => [->(m) { m["xml"]["rules"][0]["to"] = "ghost" }, :dangling_reference],
209
+ "an unknown render policy" => [->(m) { m["xml"]["rules"][0]["render"] = { "nil" => "shrug" } }, :unsupported_mapping],
210
+ "an unknown render slot" => [->(m) { m["xml"]["rules"][0]["render"] = { "blank" => "omit" } }, :unsupported_mapping],
211
+ "an alias-less name" => [->(m) { m["xml"]["rules"][0]["name"] = [] }, :invalid_document],
212
+ "cdata on an attribute" => [->(m) { m["xml"]["rules"].find { |r| r["kind"] == "attribute" }["cdata"] = true }, :unsupported_mapping],
213
+ "as_blank for json" => [->(m) { m["json"]["rules"][0]["render"] = { "nil" => "as_blank" } }, :unsupported_mapping],
214
+ }.each do |label, (mutation, reason)|
215
+ it "refuses #{label} by name (#{reason})" do
216
+ error = refusal(edited { |m| mutation.call(m) })
217
+ expect(error).to be_a(rejected)
218
+ expect(error.reason).to eq(reason)
219
+ expect(error.model_id).to eq("LmlMappingFixtures::Doc@default")
220
+ end
221
+ end
222
+
223
+ it "refuses a class that already has the mapping" do
224
+ bundle = fx.bundle_for(fx::Item)
225
+ classes = fx.fresh_classes(bundle, holder)
226
+ described_class.apply(classes, bundle)
227
+ expect { described_class.apply(classes, bundle) }.to raise_error(rejected) { |e|
228
+ expect(e.reason).to eq(:unsupported_mapping)
229
+ }
230
+ end
231
+
232
+ it "refuses a model that is missing from the model map" do
233
+ bundle = fx.bundle_for(fx::Item)
234
+ expect { described_class.apply({}, bundle) }.to raise_error(rejected) { |e|
235
+ expect(e.reason).to eq(:dangling_reference)
236
+ }
237
+ end
238
+
239
+ it "declares nothing when any model is refused" do
240
+ bundle = edited { |m| m["xml"]["rules"][0]["to"] = "ghost" }
241
+ classes = fx.fresh_classes(bundle, holder)
242
+ expect { described_class.apply(classes, bundle) }.to raise_error(rejected)
243
+ expect(classes.values.map { |k| k.mappings.keys }).to all(be_empty)
244
+ end
245
+ end
246
+
247
+ describe "composition with the validator" do
248
+ it "accepts what the validator accepted for a plain model" do
249
+ bundle = fx.bundle_for(fx::Doc, fx::Item, fx::Entry)
250
+ validated = Lutaml::Model::Lml::DocumentValidator.validate!(bundle, kind: :bundle)
251
+ expect(validated).to be_frozen
252
+ classes = fx.fresh_classes(validated, holder)
253
+ expect(described_class.apply(classes, validated)).to equal(classes)
254
+ end
255
+ end
256
+ end
@@ -0,0 +1,138 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "lutaml/model"
4
+ require "lutaml/model/lml/materialization"
5
+ require "lutaml/model/lml/exporter"
6
+ require_relative "../../support/lml_materialization_fixtures"
7
+
8
+ RSpec.describe Lutaml::Model::Lml::Materialization do
9
+ let(:fixtures) { LmlMaterializationFixtures }
10
+ let(:order_id) { "Shop::Order@default" }
11
+ let(:line_id) { "Shop::Line@default" }
12
+
13
+ def build(bundle, **opts)
14
+ described_class.build(bundle, namespace: Module.new, **opts)
15
+ end
16
+
17
+ it "needs no source constants" do
18
+ expect(Object.const_defined?(:Shop)).to be(false)
19
+ expect(build(fixtures.cyclic).keys).to eq([line_id, order_id])
20
+ expect(Object.const_defined?(:Shop)).to be(false)
21
+ end
22
+
23
+ it "links nested and cyclic models by identity" do
24
+ classes = build(fixtures.cyclic)
25
+ order = classes[order_id]
26
+ line = classes[line_id]
27
+
28
+ expect(order.attributes[:lines].type).to equal(line)
29
+ expect(order.attributes[:parent].type).to equal(order)
30
+ expect(line.attributes[:order].type).to equal(order)
31
+ end
32
+
33
+ it "declares required, enum, default and collection bounds" do
34
+ order = build(fixtures.cyclic)[order_id]
35
+ line = build(fixtures.cyclic)[line_id]
36
+
37
+ expect(order.attributes[:id].options[:required]).to be(true)
38
+ expect(order.attributes[:status].enum_values).to eq(%w[open closed])
39
+ expect(order.new.status).to eq("open")
40
+ expect(line.attributes[:qty].resolved_collection).to eq(1..3)
41
+ expect(order.attributes[:lines].resolved_collection.max).to eq(Float::INFINITY)
42
+ end
43
+
44
+ it "builds working models for a cyclic instance graph" do
45
+ classes = build(fixtures.cyclic)
46
+ order = classes[order_id].new(id: "1")
47
+ order.lines = [classes[line_id].new(sku: "a", order: order)]
48
+
49
+ expect(order.lines.first.order).to equal(order)
50
+ end
51
+
52
+ it "installs generated constants under the namespace only" do
53
+ holder = Module.new
54
+ classes = described_class.build(fixtures.cyclic, namespace: holder)
55
+
56
+ expect(holder::Shop::Order).to equal(classes[order_id])
57
+ expect(classes[order_id].name).to end_with("::Shop::Order")
58
+ end
59
+
60
+ it "keeps explicit contexts independent" do
61
+ first = build(fixtures.cyclic)
62
+ second = build(fixtures.cyclic)
63
+
64
+ expect(first[order_id]).not_to equal(second[order_id])
65
+ expect(first[order_id].attributes[:lines].type).to equal(first[line_id])
66
+ expect(second[order_id].attributes[:lines].type).to equal(second[line_id])
67
+ end
68
+
69
+ it "re-exports the same attribute declarations from each context" do
70
+ exported = ->(classes) { Lutaml::Model::Lml::Exporter.export(classes[order_id]).attributes }
71
+ first = exported.call(build(fixtures.cyclic))
72
+ second = exported.call(build(fixtures.cyclic))
73
+
74
+ expect(first.map { |a| a["name"] }).to eq(%w[id status lines parent])
75
+ expect(first.map { |a| a.except("type") }).to eq(second.map { |a| a.except("type") })
76
+ end
77
+
78
+ it "refuses a second build into the same namespace" do
79
+ holder = Module.new
80
+ described_class.build(fixtures.cyclic, namespace: holder)
81
+
82
+ expect { described_class.build(fixtures.cyclic, namespace: holder) }
83
+ .to raise_error(Lutaml::Model::Lml::RejectedError) { |e| expect(e.reason).to eq(:duplicate_id) }
84
+ end
85
+
86
+ describe "value type bindings" do
87
+ it "uses the trusted binding" do
88
+ sku = Class.new(Lutaml::Model::Type::String)
89
+ classes = build(fixtures.with_value_type, type_bindings: { "Shop::Sku" => sku })
90
+
91
+ expect(classes["Shop::Item@default"].attributes[:code].type).to equal(sku)
92
+ end
93
+
94
+ it "fails by name when the binding is missing" do
95
+ expect { build(fixtures.with_value_type) }
96
+ .to raise_error(Lutaml::Model::Lml::RejectedError, /Shop::Sku/) { |e| expect(e.reason).to eq(:missing_type_binding) }
97
+ end
98
+ end
99
+
100
+ it "fails by name on an unknown builtin" do
101
+ bundle = fixtures.bundle("A@default" => fixtures.model("A", [
102
+ { "name" => "x", "type" => { "kind" => "builtin", "name" => "nope" } },
103
+ ]))
104
+
105
+ expect { build(bundle) }
106
+ .to raise_error(Lutaml::Model::Lml::RejectedError, /nope/) { |e| expect(e.reason).to eq(:unknown_type) }
107
+ end
108
+
109
+ it "fails on a dangling model reference" do
110
+ bundle = fixtures.bundle("A@default" => fixtures.model("A", [
111
+ { "name" => "x", "type" => fixtures.model_type("Gone@default") },
112
+ ]))
113
+
114
+ expect { build(bundle) }
115
+ .to raise_error(Lutaml::Model::Lml::RejectedError) { |e| expect(e.reason).to eq(:dangling_reference) }
116
+ end
117
+
118
+ it "refuses attribute keys it cannot materialize" do
119
+ bundle = fixtures.bundle("A@default" => fixtures.model("A", [
120
+ { "name" => "x", "type" => fixtures.string_type, "discriminator" => {} },
121
+ ]))
122
+
123
+ expect { build(bundle) }
124
+ .to raise_error(Lutaml::Model::Lml::RejectedError, /discriminator/)
125
+ end
126
+
127
+ it "materializes a float attribute as the Float builtin with a binary64 default" do
128
+ require_relative "../../support/lml_export_fixtures"
129
+ bundle = Lutaml::Model::Lml.export_bundle(LmlExportFixtures::FloatAttr)
130
+ classes = Lutaml::Model::Lml.import_bundle(bundle, namespace: Module.new)
131
+ klass = classes.fetch("LmlExportFixtures::FloatAttr@default")
132
+ expect(klass.attributes.values.map(&:type)).to all(eq(Lutaml::Model::Type::Float))
133
+ expect(klass.attributes.values_at(:ratio, :tiny, :whole).map { |attribute| attribute.options[:default] })
134
+ .to eq([0.71, 1.0e-05, 3.0])
135
+ expect(klass.new.ratio).to eq(0.71)
136
+ expect(Lutaml::Model::Lml.verify_materialization(bundle).checksum).to eq(bundle.checksum)
137
+ end
138
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require_relative "../../support/lml_corpus"
5
+ require "json"
6
+ require "open3"
7
+ require "tmpdir"
8
+ require "fileutils"
9
+
10
+ RSpec.describe "Fresh-process real MML materialization" do
11
+ let(:package) { LmlCorpus.materialize!(File.expand_path("../../fixtures/lml_mml", __dir__)) }
12
+ let(:runner) { File.expand_path("../../fixtures/lml_mml_materialization/replay.rb", __dir__) }
13
+ let(:manifest) { JSON.parse(File.read(File.join(package, "manifest.json"))) }
14
+ let(:oracles) { JSON.parse(File.read(File.join(package, "oracle.json"))).fetch("cases") }
15
+ let(:temporary) { Dir.mktmpdir("lml-mml-materialization") }
16
+
17
+ after { FileUtils.remove_entry(temporary) }
18
+
19
+ def run_replay
20
+ result_path = File.join(temporary, "result.json")
21
+ _stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-Ilib", runner, package, result_path)
22
+ expect(status.success?).to be(true), stderr
23
+ JSON.parse(File.read(result_path))
24
+ end
25
+
26
+ it "reconstructs every exported mml graph in a fresh process without the source constants" do
27
+ result = run_replay
28
+ expect(result.fetch("source_constants_loaded")).to be(false)
29
+ expect(result.fetch("cases").map { |entry| entry.fetch("id") }).to match_array(oracles.map { |entry| entry.fetch("id") })
30
+ rebuilt = result.fetch("cases")
31
+ expect(rebuilt.map { |entry| entry.key?("refused") }).to all(be(false))
32
+ expect(rebuilt.map { |entry| entry.fetch("id") }).to contain_exactly(
33
+ "v3-mi-mixed", "v3-math-nested", "v4-mi-mixed", "v4-math-nested"
34
+ )
35
+ rebuilt.each do |entry|
36
+ graph = manifest.fetch("graphs").fetch(entry.fetch("id"))
37
+ expect(entry.fetch("checksum")).to eq(graph.fetch("checksum"))
38
+ expect(entry.fetch("model_count")).to eq(graph.fetch("source_references").size)
39
+ source = oracles.find { |oracle| oracle.fetch("id") == entry.fetch("id") }
40
+ expect(entry.fetch("actual")).to eq(source.fetch("expected"))
41
+ end
42
+ expect(rebuilt.to_h { |entry| [entry.fetch("id"), entry.fetch("model_count")] }).to eq(
43
+ "v3-mi-mixed" => 3, "v3-math-nested" => 194, "v4-mi-mixed" => 3, "v4-math-nested" => 195,
44
+ )
45
+ end
46
+
47
+ it "rejects modified oracle bytes before reconstruction" do
48
+ FileUtils.cp_r("#{package}/.", temporary)
49
+ File.write(File.join(temporary, "oracle.json"), '{"cases":[]}')
50
+ _stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-Ilib", runner, temporary, File.join(temporary, "result.json"))
51
+ expect(status.success?).to be(false)
52
+ expect(stderr).to include("payload digest mismatch: oracle.json")
53
+ end
54
+ end
@@ -0,0 +1,150 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require_relative "../../../scripts/lml/export_mml"
5
+ require_relative "../../support/lml_corpus"
6
+ require "tmpdir"
7
+
8
+ RSpec.describe LmlMml do
9
+ let(:directory) { LmlCorpus.materialize!(described_class::PACKAGE) }
10
+ let(:manifest) { JSON.parse(File.read(File.join(directory, "manifest.json"))) }
11
+ let(:oracles) { JSON.parse(File.read(File.join(directory, "oracle.json"))).fetch("cases") }
12
+ let(:inventory) { JSON.parse(File.read(File.join(directory, "inventory.json"))) }
13
+ let(:temporary) { Dir.mktmpdir("lml-mml") }
14
+
15
+ let(:payloads) { described_class.files }
16
+
17
+ # Regeneration needs the pinned mml 2.4.7 source (never a runtime or
18
+ # development dependency: mml pins its own lutaml-model range). Specs that
19
+ # read only the committed package always run.
20
+ def require_source
21
+ described_class.source_root
22
+ rescue ArgumentError
23
+ skip "pinned mml #{described_class::SOURCE_VERSION} source not installed (set LML_MML_ROOT)"
24
+ end
25
+
26
+ after { FileUtils.remove_entry(temporary) }
27
+
28
+ it "pins the real source revision, version, library bytes and framework dependency" do
29
+ expect(manifest).to include(
30
+ "source_revision" => "d826d80f6b1477569c8320ccd945268a6ce1062f",
31
+ "source_version" => "2.4.7", "source_digest" => described_class::SOURCE_DIGEST,
32
+ "framework_revision" => described_class::FRAMEWORK_REVISION,
33
+ "preservation_revision" => "9716740b6b7feb755b8557a2bfab89d066e855bd"
34
+ )
35
+ require_source
36
+ expect(described_class.source_digest(described_class.source_root)).to eq(described_class::SOURCE_DIGEST)
37
+ end
38
+
39
+ it "refuses a changed source before loading any of its Ruby code" do
40
+ FileUtils.mkdir_p(File.join(temporary, "lib"))
41
+ File.write(File.join(temporary, "lib/mml.rb"), 'raise "must not execute"')
42
+ expect { described_class.load_source(temporary) }.to raise_error(ArgumentError, /source digest differs/)
43
+ end
44
+
45
+ it "exports all four closed graphs, including both Math graphs with the portable float" do
46
+ graphs = manifest.fetch("graphs")
47
+ expect(graphs.keys).to contain_exactly("v3-mi-mixed", "v3-math-nested", "v4-mi-mixed", "v4-math-nested")
48
+ expect(graphs.values.map { |graph| graph.key?("refusal") }).to all(be(false))
49
+ expect(graphs.transform_values { |graph| graph.fetch("model_count") }).to eq(
50
+ "v3-mi-mixed" => 3, "v3-math-nested" => 194, "v4-mi-mixed" => 3, "v4-math-nested" => 195,
51
+ )
52
+ %w[v3 v4].each do |version|
53
+ bundle = Lutaml::Model::Lml::Importer.bundle(File.read(File.join(directory, "#{version}-math-nested.json")))
54
+ expect(bundle.checksum).to eq(graphs.fetch("#{version}-math-nested").fetch("checksum"))
55
+ scripts = bundle.models.fetch("Mml::V#{version.delete_prefix('v')}::Mscarries@mml_#{version}")
56
+ .fetch("attributes").find { |attribute| attribute["name"] == "scriptsizemultiplier" }
57
+ expect(scripts.fetch("type")).to eq("kind" => "builtin", "name" => "float")
58
+ end
59
+ end
60
+
61
+ it "passes the completeness check with every closed graph exported" do
62
+ require_source
63
+ report = described_class.check(directory, payloads, require_complete: true)
64
+ expect(report).to include("exported_count" => 4, "refused" => {}, "errors" => [])
65
+ end
66
+
67
+ it "retains all child references of the Mi graphs, including every referenced model" do
68
+ %w[v3 v4].each do |version|
69
+ register = "mml_#{version}"
70
+ bundle = Lutaml::Model::Lml::Importer.bundle(File.read(File.join(directory, "#{version}-mi-mixed.json")))
71
+ expect { Lutaml::Model::Lml::DocumentValidator.validate!(bundle.to_h, kind: :bundle) }.not_to raise_error
72
+ graph = manifest.fetch("graphs").fetch("#{version}-mi-mixed")
73
+ expect(bundle.checksum).to eq(graph.fetch("checksum"))
74
+ references = graph.fetch("source_references")
75
+ expect(bundle.models.keys).to match_array(references.keys)
76
+ expect(references.values.flatten.uniq - bundle.models.keys).to be_empty
77
+ expect(bundle.models.keys).to include("Mml::V#{version.delete_prefix('v')}::Mi@#{register}")
78
+ expect(YAML.safe_load_file(File.join(directory, "#{version}-mi-mixed.yaml"), aliases: false)).to eq(bundle.to_h)
79
+ end
80
+ end
81
+
82
+ it "captures real mixed text and inline element order in both source versions" do
83
+ oracles.select { |entry| entry.fetch("id").include?("mi-mixed") }.each do |entry|
84
+ data = entry.fetch("expected").fetch("data")
85
+ expect(data.fetch("value")).to eq(%w[x y z])
86
+ expect(data.fetch("__lml_content").map { |token| token.fetch("attribute") }).to eq(
87
+ %w[value malignmark_value value mglyph_value value],
88
+ )
89
+ expect(data.fetch("mglyph_value").first).to include("src" => "g.png", "alt" => "g")
90
+ expect(entry.fetch("expected").fetch("output")).to eq(entry.fetch("input"))
91
+ end
92
+ end
93
+
94
+ it "captures ordinary and recursive child order instead of grouping repeated members" do
95
+ oracles.select { |entry| entry.fetch("id").include?("math-nested") }.each do |entry|
96
+ data = entry.fetch("expected").fetch("data")
97
+ expect(data.fetch("__lml_content").map { |token| token.fetch("attribute") }).to eq(%w[mrow_value mi_value])
98
+ row = data.fetch("mrow_value").first
99
+ expect(row.fetch("__lml_content").map { |token| token.fetch("attribute") }).to eq(
100
+ %w[mi_value mo_value mfrac_value mi_value],
101
+ )
102
+ fraction = row.fetch("mfrac_value").first
103
+ expect(fraction.fetch("__lml_content").map { |token| token.fetch("attribute") }).to eq(%w[mi_value mrow_value])
104
+ expect(fraction.fetch("mi_value").first.fetch("value")).to eq(%w[x y])
105
+ expect(fraction.fetch("mrow_value").first.fetch("mn_value").map { |node| node.fetch("value") }).to eq([["1"], ["2"]])
106
+ tree = entry.fetch("expected").fetch("tree")
107
+ expect(tree.first).to eq([described_class::NAMESPACE, "math"])
108
+ end
109
+ end
110
+
111
+ it "reports every source ordered model as exported, including the float-bearing Mscarries" do
112
+ versions = inventory.fetch("versions")
113
+ expect(versions.map { |entry| entry.values_at("register", "registered_count", "source_ordered_count", "exported_ordered_count", "float_ordered_count") }).to eq(
114
+ [["mml_v2", 189, 62, 62, 1], ["mml_v3", 194, 64, 64, 1], ["mml_v4", 195, 65, 65, 1]],
115
+ )
116
+ expect(versions.sum { |entry| entry.fetch("source_ordered_count") }).to eq(191)
117
+ versions.each do |version|
118
+ expect(version.fetch("exported_count")).to eq(version.fetch("registered_count"))
119
+ expect(version.fetch("declarations").select { |entry| entry.key?("reason") }).to eq([])
120
+ mscarries = version.fetch("declarations").find { |entry| entry["model"] == "#{version.fetch('version')}::Mscarries" }
121
+ expect(mscarries).to include("source_ordered" => true, "ordered_content" => true,
122
+ "float_attributes" => ["scriptsizemultiplier"])
123
+ end
124
+ exclusions = inventory.fetch("existing_exclusions")
125
+ expect(exclusions).to include("reported_historical_ordered_refusals" => 191, "historical_ordered_model_count_matched" => true)
126
+ expect(exclusions.fetch("v3").fetch("existing_filters")).to include("share requires XInclude", "ErrorHandling")
127
+ expect(exclusions.fetch("v3").fetch("negative_tests_pending")).to eq("Parser does not validate MathML schema compliance")
128
+ expect(exclusions.fetch("v4").fetch("existing_filters")).to include("mtd outside table")
129
+ end
130
+
131
+ it "pins regenerated payload bytes and all manifest digests" do
132
+ require_source
133
+ payloads.each { |path, bytes| expect(File.binread(File.join(directory, path))).to eq(bytes.b) }
134
+ manifest.fetch("files").each do |entry|
135
+ expect(LmlPreservation.digest(File.binread(File.join(directory, entry.fetch("path"))))).to eq(entry.fetch("sha256"))
136
+ end
137
+ end
138
+
139
+ it "reports changed and unknown package files without rewriting them" do
140
+ require_source
141
+ FileUtils.cp_r("#{directory}/.", temporary)
142
+ File.write(File.join(temporary, "oracle.json"), "{}")
143
+ File.write(File.join(temporary, "unknown.json"), "{}")
144
+ expect(described_class.check(temporary, payloads).fetch("errors")).to include(
145
+ { "id" => "oracle.json", "error" => "missing or changed file" },
146
+ { "id" => "unknown.json", "error" => "unknown file" },
147
+ )
148
+ expect(File.read(File.join(temporary, "oracle.json"))).to eq("{}")
149
+ end
150
+ end