lutaml-model 0.8.203 → 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 (112) hide show
  1. checksums.yaml +4 -4
  2. data/.gitattributes +5 -0
  3. data/.github/workflows/lml-conformance.yml +71 -0
  4. data/.gitignore +7 -0
  5. data/README.adoc +1 -0
  6. data/docs/_guides/lml-cross-language.adoc +128 -0
  7. data/docs/_references/lml-conformance.adoc +78 -0
  8. data/docs/_references/lml-contract.adoc +290 -0
  9. data/lib/lutaml/model/lml/artifact_verifier.rb +205 -0
  10. data/lib/lutaml/model/lml/bundle.rb +171 -0
  11. data/lib/lutaml/model/lml/document.rb +46 -10
  12. data/lib/lutaml/model/lml/document_validator.rb +840 -0
  13. data/lib/lutaml/model/lml/exporter/v1_projector.rb +102 -0
  14. data/lib/lutaml/model/lml/exporter/v2_projector.rb +401 -0
  15. data/lib/lutaml/model/lml/exporter.rb +223 -87
  16. data/lib/lutaml/model/lml/graph_exporter.rb +248 -0
  17. data/lib/lutaml/model/lml/importer.rb +180 -0
  18. data/lib/lutaml/model/lml/mapping_compiler.rb +401 -0
  19. data/lib/lutaml/model/lml/materialization.rb +257 -0
  20. data/lib/lutaml/model/lml/ordered_content.rb +577 -0
  21. data/lib/lutaml/model/lml/portable_json.rb +385 -0
  22. data/lib/lutaml/model/lml/rejected_error.rb +64 -15
  23. data/lib/lutaml/model/lml/round_trip.rb +86 -0
  24. data/lib/lutaml/model/lml/value_declarations.rb +84 -0
  25. data/lib/lutaml/model/lml.rb +61 -0
  26. data/lib/lutaml/model/schema/definitions/attribute.rb +4 -2
  27. data/lib/lutaml/model/schema/registry_generator.rb +1 -1
  28. data/lib/lutaml/model/schema/renderers/member_decls.rb +1 -0
  29. data/lib/lutaml/model/schema/xml_compiler/spec_builder/members.rb +10 -0
  30. data/lib/lutaml/model/sequence.rb +7 -0
  31. data/lib/lutaml/model/version.rb +1 -1
  32. data/lib/lutaml/xml/leptris/native_emission.rb +4 -0
  33. data/lib/lutaml/xml/leptris/plan_serializer.rb +14 -9
  34. data/schema/lml/document.schema.json +179 -0
  35. data/scripts/lml/export_choices.rb +57 -0
  36. data/scripts/lml/export_conformance.rb +23 -0
  37. data/scripts/lml/export_mml.rb +253 -0
  38. data/scripts/lml/export_preservation.rb +421 -0
  39. data/scripts/lml/verify_artifacts.rb +31 -0
  40. data/spec/fixtures/lml_artifacts/model.json +1 -0
  41. data/spec/fixtures/lml_artifacts/valid.json +1 -0
  42. data/spec/fixtures/lml_artifacts/valid.sha256 +1 -0
  43. data/spec/fixtures/lml_conformance/core/index.json +1 -0
  44. data/spec/fixtures/lml_conformance/core/manifest.json +1 -0
  45. data/spec/fixtures/lml_conformance/core/schema.json +1 -0
  46. data/spec/fixtures/lml_conformance/core/schema.yaml +79 -0
  47. data/spec/fixtures/lml_conformance/float_vectors.generator.rb +60 -0
  48. data/spec/fixtures/lml_conformance/float_vectors.json +21550 -0
  49. data/spec/fixtures/lml_conformance/preservation/index.json +1 -0
  50. data/spec/fixtures/lml_conformance/preservation/manifest.json +1 -0
  51. data/spec/fixtures/lml_conformance/preservation/schema.json +1 -0
  52. data/spec/fixtures/lml_conformance/preservation/schema.yaml +319 -0
  53. data/spec/fixtures/lml_conformance/r2_refusals.generator.rb +153 -0
  54. data/spec/fixtures/lml_conformance/r2_refusals.json +1662 -0
  55. data/spec/fixtures/lml_conformance/ruby_portable_interop.generator.rb +40 -0
  56. data/spec/fixtures/lml_conformance/ruby_portable_interop.json +1 -0
  57. data/spec/fixtures/lml_contract/bundle.json +81 -0
  58. data/spec/fixtures/lml_contract/manifest.json +30 -0
  59. data/spec/fixtures/lml_contract/v1_unit.canonical.json +1 -0
  60. data/spec/fixtures/lml_contract/v1_unit.json +1 -0
  61. data/spec/fixtures/lml_contract/v2_node.canonical.json +1 -0
  62. data/spec/fixtures/lml_contract/v2_node.json +32 -0
  63. data/spec/fixtures/lml_mml/inventory.json +1 -0
  64. data/spec/fixtures/lml_mml/manifest.json +1 -0
  65. data/spec/fixtures/lml_mml/oracle.json +1 -0
  66. data/spec/fixtures/lml_mml/v3-mi-mixed.json +1 -0
  67. data/spec/fixtures/lml_mml/v3-mi-mixed.yaml +970 -0
  68. data/spec/fixtures/lml_mml/v4-mi-mixed.json +1 -0
  69. data/spec/fixtures/lml_mml/v4-mi-mixed.yaml +1516 -0
  70. data/spec/fixtures/lml_mml_materialization/replay.rb +37 -0
  71. data/spec/lutaml/model/lml_any_attribute_spec.rb +91 -0
  72. data/spec/lutaml/model/lml_artifact_verifier_spec.rb +86 -0
  73. data/spec/lutaml/model/lml_builtin_types_spec.rb +49 -0
  74. data/spec/lutaml/model/lml_conformance_spec.rb +178 -0
  75. data/spec/lutaml/model/lml_document_spec.rb +279 -0
  76. data/spec/lutaml/model/lml_export_validation_spec.rb +96 -0
  77. data/spec/lutaml/model/lml_exporter_spec.rb +127 -5
  78. data/spec/lutaml/model/lml_graph_exporter_spec.rb +205 -0
  79. data/spec/lutaml/model/lml_importer_spec.rb +290 -0
  80. data/spec/lutaml/model/lml_input_hardening_spec.rb +156 -0
  81. data/spec/lutaml/model/lml_mapping_compiler_spec.rb +256 -0
  82. data/spec/lutaml/model/lml_materialization_spec.rb +138 -0
  83. data/spec/lutaml/model/lml_mml_materialization_spec.rb +54 -0
  84. data/spec/lutaml/model/lml_mml_pilot_spec.rb +150 -0
  85. data/spec/lutaml/model/lml_model_choices_spec.rb +88 -0
  86. data/spec/lutaml/model/lml_ordered_content_spec.rb +243 -0
  87. data/spec/lutaml/model/lml_portable_json_spec.rb +310 -0
  88. data/spec/lutaml/model/lml_preservation_spec.rb +211 -0
  89. data/spec/lutaml/model/lml_review_export_spec.rb +247 -0
  90. data/spec/lutaml/model/lml_review_import_spec.rb +390 -0
  91. data/spec/lutaml/model/lml_review_misc_spec.rb +152 -0
  92. data/spec/lutaml/model/lml_review_round4_spec.rb +200 -0
  93. data/spec/lutaml/model/lml_review_round5_spec.rb +194 -0
  94. data/spec/lutaml/model/lml_ruby_interop_fixture_spec.rb +37 -0
  95. data/spec/lutaml/model/lml_shared_corpus_spec.rb +132 -0
  96. data/spec/lutaml/model/lml_value_declarations_spec.rb +128 -0
  97. data/spec/lutaml/model/schema/class_loader_spec.rb +51 -0
  98. data/spec/lutaml/model/schema/xml_compiler_runtime_spec.rb +20 -0
  99. data/spec/lutaml/model/sequence_spec.rb +48 -0
  100. data/spec/lutaml/xml/cdata_emission_spec.rb +68 -0
  101. data/spec/support/lml_any_attribute_fixtures.rb +26 -0
  102. data/spec/support/lml_conformance.rb +292 -0
  103. data/spec/support/lml_corpus.rb +75 -0
  104. data/spec/support/lml_export_fixtures.rb +190 -0
  105. data/spec/support/lml_graph_fixtures.rb +108 -0
  106. data/spec/support/lml_import_fixtures.rb +52 -0
  107. data/spec/support/lml_mapping_fixtures.rb +170 -0
  108. data/spec/support/lml_materialization_fixtures.rb +53 -0
  109. data/spec/support/lml_model_choices_fixtures.rb +150 -0
  110. data/spec/support/lml_order_fixtures.rb +151 -0
  111. metadata +92 -2
  112. data/lib/lutaml/model/schema/schema_builder/nokogiri.rb +0 -41
@@ -0,0 +1,200 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "open3"
5
+ require "tmpdir"
6
+ require "digest"
7
+ require "lutaml/model/lml"
8
+
9
+ RSpec.describe "LML review round-3 probe fixes (importer, bundle, verifier)" do
10
+ let(:lml) { Lutaml::Model::Lml }
11
+ let(:root_dir) { File.expand_path("../../..", __dir__) }
12
+ let(:klass) do
13
+ Class.new(Lutaml::Model::Serializable) do
14
+ attribute :a, :string
15
+ json { map "a", to: :a }
16
+ end
17
+ end
18
+
19
+ before { stub_const("R4Leaf", klass) }
20
+
21
+ def bundle_hash
22
+ lml.export_bundle([R4Leaf]).to_h
23
+ end
24
+
25
+ def elapsed
26
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
27
+ yield
28
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
29
+ end
30
+
31
+ def rejection
32
+ yield
33
+ nil
34
+ rescue lml::RejectedError => e
35
+ e
36
+ end
37
+
38
+ describe "importer text scanning" do
39
+ it "refuses an unterminated string in linear time" do
40
+ text = "{\"lml_bundle\":1,\"x\":\"#{'\\"' * 100_000}"
41
+ error = nil
42
+ seconds = elapsed { error = rejection { lml::Importer.import(text) } }
43
+
44
+ expect(error.reason).to eq(:invalid_document)
45
+ expect(seconds).to be < 1
46
+ end
47
+
48
+ it "refuses runaway nesting before parsing it all" do
49
+ text = '{"a":' * 200_000
50
+ error = nil
51
+ seconds = elapsed { error = rejection { lml::Importer.import(text) } }
52
+
53
+ expect(error.reason).to eq(:invalid_document)
54
+ expect(seconds).to be < 1
55
+ end
56
+
57
+ it "still rejects duplicate keys and comments" do
58
+ expect(rejection { lml::Importer.import('{"a":1,"a":2}') }.detail).to include("duplicate key")
59
+ expect(rejection { lml::Importer.import('{"a":1 /* c */}') }.detail).to include("comments")
60
+ end
61
+
62
+ %w[UTF-16LE UTF-16BE UTF-32LE].each do |encoding|
63
+ it "refuses #{encoding} text with a RejectedError" do
64
+ text = JSON.generate(bundle_hash).encode(encoding)
65
+
66
+ expect(rejection { lml::Importer.import(text) }.reason).to eq(:invalid_document)
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "importer namespace" do
72
+ it "refuses a non-module namespace" do
73
+ expect(rejection { lml::Importer.import(bundle_hash, namespace: "String") }.reason)
74
+ .to eq(:invalid_document)
75
+ end
76
+
77
+ it "refuses a frozen module namespace" do
78
+ expect(rejection { lml::Importer.import(bundle_hash, namespace: Module.new.freeze) }.reason)
79
+ .to eq(:invalid_document)
80
+ end
81
+
82
+ it "refuses symbol-keyed hashes, as documented" do
83
+ expect(rejection { lml::Importer.import({ lml_bundle: 1 }) }.detail).to include("non-string key")
84
+ end
85
+ end
86
+
87
+ describe "RoundTrip.verify_materialization" do
88
+ it "refuses a bundle in an unregistered register" do
89
+ json = JSON.generate(bundle_hash).gsub("@default", "@zz").gsub('"register":"default"', '"register":"zz"')
90
+ error = rejection { lml::RoundTrip.verify_materialization(JSON.parse(json)) }
91
+
92
+ expect(error).to be_a(lml::RejectedError)
93
+ expect(error.detail).to include("not registered")
94
+ end
95
+
96
+ it "verifies a bundle in the default register" do
97
+ expect(lml::RoundTrip.verify_materialization(bundle_hash)).to eq(lml::Importer.bundle(bundle_hash))
98
+ end
99
+ end
100
+
101
+ describe "Bundle" do
102
+ it "serialises with to_json like a Document" do
103
+ bundle = lml.export_bundle([R4Leaf])
104
+
105
+ expect(JSON.parse(bundle.to_json)).to eq(bundle.to_h)
106
+ expect(lml::Importer.import(bundle.to_json).keys).to eq(["R4Leaf@default"])
107
+ end
108
+
109
+ it "refuses the same model under two registers at export" do
110
+ Lutaml::Model::GlobalRegister.register(Lutaml::Model::Register.new(:r4alt))
111
+ error = rejection { lml.export_bundle([[R4Leaf, "default"], [R4Leaf, "r4alt"]]) }
112
+
113
+ expect(error.reason).to eq(:duplicate_id)
114
+ ensure
115
+ Lutaml::Model::GlobalRegister.remove(:r4alt) if Lutaml::Model::GlobalRegister.respond_to?(:remove)
116
+ end
117
+
118
+ it "refuses cyclic provenance with a RejectedError" do
119
+ cycle = {}
120
+ cycle["a"] = cycle
121
+
122
+ expect(rejection { lml.export_bundle([R4Leaf], provenance: cycle) }.reason).to eq(:invalid_document)
123
+ end
124
+
125
+ it "refuses provenance the importer would refuse" do
126
+ expect(rejection { lml.export_bundle([R4Leaf], provenance: { "t" => Time.now }) }.reason)
127
+ .to eq(:invalid_document)
128
+ expect(rejection { lml.export_bundle([R4Leaf], provenance: { "x" => Float::NAN }) }.reason)
129
+ .to eq(:invalid_document)
130
+ end
131
+
132
+ it "keeps symbol-keyed provenance, stringified, and importable" do
133
+ bundle = lml.export_bundle([R4Leaf], provenance: { tool: "t" })
134
+
135
+ expect(bundle.provenance).to eq("tool" => "t")
136
+ expect(lml::Importer.import(bundle.to_json).keys).to eq(["R4Leaf@default"])
137
+ end
138
+ end
139
+
140
+ describe "ArtifactVerifier non-Error leaks" do
141
+ let(:dir) { Dir.mktmpdir }
142
+ let(:sha) { ->(content) { "sha256:#{Digest::SHA256.hexdigest(content)}" } }
143
+
144
+ after do
145
+ FileUtils.chmod(0o644, Dir.glob("#{dir}/*"))
146
+ FileUtils.remove_entry(dir)
147
+ end
148
+
149
+ def manifest(files, extra = {})
150
+ { "manifest" => 1, "schema_version" => 2, "target" => "ruby", "lml_checksum" => sha.call("x"),
151
+ "generator_version" => "1", "capabilities" => [], "corpora" => [], "dependencies" => [],
152
+ "files" => files }.merge(extra)
153
+ end
154
+
155
+ def verify(root, manifest, expected: manifest)
156
+ verifier = lml::ArtifactVerifier.new(root: root, expected_sha256: lml::ArtifactVerifier.checksum(expected))
157
+ verifier.verify(manifest)
158
+ rescue lml::ArtifactVerifier::Error => e
159
+ e
160
+ end
161
+
162
+ it "reports a UTF-16 checksum as invalid_document" do
163
+ File.write("#{dir}/a.txt", "A")
164
+ files = [{ "path" => "a.txt", "sha256" => sha.call("A") }]
165
+ result = verify(dir, manifest(files, "lml_checksum" => "x".encode("UTF-16LE")),
166
+ expected: manifest(files))
167
+
168
+ expect(result.reason).to eq(:invalid_document)
169
+ end
170
+
171
+ it "reports an unreadable file as invalid_document" do
172
+ File.write("#{dir}/u.txt", "U")
173
+ File.chmod(0, "#{dir}/u.txt")
174
+ skip "root can read mode-000 files" if File.readable?("#{dir}/u.txt")
175
+ result = verify(dir, manifest([{ "path" => "u.txt", "sha256" => sha.call("U") }]))
176
+
177
+ expect([result.reason, result.location]).to eq([:invalid_document, "/files/0/path"])
178
+ end
179
+
180
+ it "reports an empty root as invalid_document" do
181
+ error = begin
182
+ lml::ArtifactVerifier.new(root: "", expected_sha256: sha.call("x"))
183
+ rescue lml::ArtifactVerifier::Error => e
184
+ e
185
+ end
186
+
187
+ expect([error.reason, error.location]).to eq([:invalid_document, "/root"])
188
+ end
189
+
190
+ it "prints a verdict line for a non-UTF-8 manifest" do
191
+ File.binwrite("#{dir}/m.json", "\xff\xfe")
192
+ out, err, status = Open3.capture3(RbConfig.ruby, "scripts/lml/verify_artifacts.rb",
193
+ "--manifest", "#{dir}/m.json",
194
+ "--expected-sha256", sha.call("x"), chdir: root_dir)
195
+
196
+ expect([status.exitstatus, out]).to eq([1, ""])
197
+ expect(JSON.parse(err)).to include("verified" => false)
198
+ end
199
+ end
200
+ end
@@ -0,0 +1,194 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "lutaml/model/lml"
5
+
6
+ RSpec.describe "LML review round-4 probe fixes (encoding, float enums, bundle input)" do
7
+ let(:lml) { Lutaml::Model::Lml }
8
+
9
+ def rejection
10
+ yield
11
+ nil
12
+ rescue lml::RejectedError => e
13
+ e
14
+ end
15
+
16
+ def model_with(type, name, **options)
17
+ klass = Class.new(Lutaml::Model::Serializable)
18
+ klass.define_singleton_method(:name) { name }
19
+ klass.attribute :v, type, **options
20
+ klass.json { map "v", to: :v }
21
+ klass
22
+ end
23
+
24
+ def minimal_model(name)
25
+ { "model" => name, "register" => "default", "attributes" => [], "mappings" => {} }
26
+ end
27
+
28
+ describe "non-UTF-8 string defaults and enum values" do
29
+ {
30
+ "ISO-8859-1" => "é".encode("ISO-8859-1"),
31
+ "UTF-16LE" => "a".encode("UTF-16LE"),
32
+ "Shift_JIS" => "表".encode("Shift_JIS"),
33
+ "binary" => "\xff".b,
34
+ "invalid UTF-8" => "\xff",
35
+ }.each do |label, value|
36
+ it "refuses a #{label} default at export" do
37
+ klass = model_with(:string, "R5Default", default: value)
38
+
39
+ expect(rejection { lml.export_bundle([klass]) }&.reason).to eq(:non_serializable_default)
40
+ end
41
+
42
+ # The model layer cannot declare a UTF-16 or invalid-UTF-8 enum member,
43
+ # so only the other encodings can reach the exporter as enum values.
44
+ next if label.start_with?("UTF-16", "invalid")
45
+
46
+ it "refuses a #{label} enum value at export" do
47
+ klass = model_with(:string, "R5Enum", values: [value])
48
+
49
+ expect(rejection { lml.export_bundle([klass]) }&.reason).to eq(:non_serializable_default)
50
+ end
51
+ end
52
+
53
+ it "refuses a non-UTF-8 string inside a hash default" do
54
+ klass = model_with(:hash, "R5HashDefault", default: { "k" => "\xff".b })
55
+
56
+ expect(rejection { lml.export_bundle([klass]) }&.reason).to eq(:non_serializable_default)
57
+ end
58
+
59
+ it "still exports ASCII and UTF-8 strings" do
60
+ klass = model_with(:string, "R5Fine", default: "café")
61
+
62
+ expect(lml.export_bundle([klass]).canonical_json).to include("café")
63
+ end
64
+
65
+ it "refuses a non-UTF-8 string held by a Bundle with RejectedError, not JSON::GeneratorError" do
66
+ document = minimal_model("R5Raw").merge("note" => "\xff".b)
67
+
68
+ expect(rejection { lml::Bundle.new(roots: [], models: { "R5Raw@default" => document }) }&.reason)
69
+ .to eq(:invalid_document)
70
+ end
71
+ end
72
+
73
+ describe "float enum values" do
74
+ [1.5, 5.0, -0.0, 1e20, 1e-5].each do |value|
75
+ it "round-trips an enum of #{value}" do
76
+ klass = model_with(:float, "R5Float", values: [value])
77
+ bundle = lml.export_bundle([klass])
78
+ classes = lml::Importer.import(bundle.to_h, namespace: Module.new)
79
+
80
+ expect(classes.values.first.attributes[:v].options[:values]).to eq([value])
81
+ expect(lml::RoundTrip.verify_materialization(bundle)).to eq(bundle)
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "Bundle construction" do
87
+ it "scales linearly with the number of models" do
88
+ build = lambda do |count|
89
+ models = (0...count).to_h { |i| ["R5M#{i}@default", minimal_model("R5M#{i}")] }
90
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
91
+ lml::Bundle.new(roots: [], models: models)
92
+ Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
93
+ end
94
+ build.call(500)
95
+ small = [build.call(1000), build.call(1000)].min
96
+ large = [build.call(4000), build.call(4000)].min
97
+
98
+ expect(large / small).to be < 12
99
+ end
100
+
101
+ it "still refuses two registers declaring one model" do
102
+ models = { "R5D@default" => minimal_model("R5D"), "R5D@alt" => minimal_model("R5D") }
103
+
104
+ expect(rejection { lml::Bundle.new(roots: [], models: models) }&.reason).to eq(:duplicate_id)
105
+ end
106
+
107
+ def bundle_with(provenance)
108
+ lml::Bundle.new(roots: [], models: {}, provenance: provenance)
109
+ end
110
+
111
+ it "refuses provenance: false like other non-objects" do
112
+ expect(rejection { bundle_with(false) }&.reason).to eq(:invalid_document)
113
+ end
114
+
115
+ it "refuses a provenance key in a non-UTF-8-compatible encoding" do
116
+ error = rejection { bundle_with({ "a".encode("UTF-16LE") => 1 }) }
117
+
118
+ expect(error&.reason).to eq(:invalid_document)
119
+ end
120
+
121
+ [{ a: 1, "a" => 2 }, { 1 => 1, "1" => 2 }, { nil => 1, "" => 2 }].each do |hash|
122
+ it "refuses provenance keys that collide after to_s: #{hash.inspect}" do
123
+ expect(rejection { bundle_with(hash) }&.reason).to eq(:invalid_document)
124
+ end
125
+ end
126
+
127
+ it "keeps a lone nil provenance key as the empty string" do
128
+ expect(bundle_with({ nil => 1 }).provenance).to eq({ "" => 1 })
129
+ end
130
+ end
131
+
132
+ describe "RoundTrip register diagnosis" do
133
+ let(:leaf) { Class.new(Lutaml::Model::Serializable) { attribute :a, :string } }
134
+ let(:holder) do
135
+ leaf_class = leaf
136
+ Class.new(Lutaml::Model::Serializable) do
137
+ attribute :n, leaf_class
138
+ json { map "n", to: :n }
139
+ end
140
+ end
141
+
142
+ before do
143
+ stub_const("R5Leaf", leaf)
144
+ stub_const("R5Holder", holder)
145
+ leaf.json { map "a", to: :a }
146
+ end
147
+
148
+ def bundle_in_register(register)
149
+ text = JSON.generate(lml.export_bundle([R5Holder]).to_h)
150
+ JSON.parse(text.gsub("@default", "@#{register}").gsub('"register":"default"', "\"register\":\"#{register}\""))
151
+ end
152
+
153
+ it "still reports an unregistered register" do
154
+ error = rejection { lml::RoundTrip.verify_materialization(bundle_in_register("r5zz")) }
155
+
156
+ expect(error.message).to include('register "r5zz" is not registered')
157
+ end
158
+
159
+ it "does not relabel an unrelated NoMethodError as a register problem" do
160
+ allow(lml::GraphExporter).to receive(:export).and_raise(NoMethodError.new("boom", :boom, receiver: Object.new))
161
+
162
+ expect { lml::RoundTrip.verify_materialization(bundle_in_register("r5zz")) }
163
+ .to raise_error(NoMethodError, /boom/)
164
+ end
165
+ end
166
+
167
+ describe "importer string scanning" do
168
+ let(:head) { '{"lml_bundle":1,"roots":[],"models":{},"value_types":{},"capabilities":[],"provenance":{' }
169
+
170
+ it "accepts a very large escape-heavy string and still sees a duplicate key after it" do
171
+ big = "\"#{'\\"' * 1_000_000}\""
172
+ expect(lml::Importer.bundle("#{head}\"note\":#{big}}}").provenance["note"].size).to eq(1_000_000)
173
+
174
+ error = rejection { lml::Importer.bundle("#{head}\"note\":#{big},\"note\":1}}") }
175
+ expect(error.detail).to include("duplicate key")
176
+ end
177
+
178
+ it "refuses a huge unterminated escape-heavy string with RejectedError" do
179
+ text = "#{head}\"note\":\"#{'\\"' * 1_000_000}"
180
+
181
+ expect(rejection { lml::Importer.bundle(text) }&.reason).to eq(:invalid_document)
182
+ end
183
+
184
+ it "refuses a string ending in a lone backslash" do
185
+ expect(rejection { lml::Importer.bundle("#{head}\"note\":\"abc\\") }&.reason).to eq(:invalid_document)
186
+ end
187
+ end
188
+
189
+ describe "version-independent detail rendering" do
190
+ it "spells containers without Hash#inspect" do
191
+ expect(lml::RejectedError.show({ "a" => [1, "b"], "c" => nil })).to eq('{"a" => [1, "b"], "c" => nil}')
192
+ end
193
+ end
194
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "json"
5
+ require "open3"
6
+ require "rbconfig"
7
+ require "tmpdir"
8
+
9
+ # lutaml-model#1033: ruby_portable_interop.json is the Lml.portable_json output
10
+ # the generated TS and Rust JSON codecs must read and write back identically.
11
+ # The TS and Rust repos copy it byte for byte; a Ruby writer change fails here.
12
+ RSpec.describe "LML Ruby portable_json interop fixture" do
13
+ let(:fixtures) { File.expand_path("../../fixtures/lml_conformance", __dir__) }
14
+ let(:lib) { File.expand_path("../../../lib", __dir__) }
15
+ let(:path) { File.join(fixtures, "ruby_portable_interop.json") }
16
+ let(:fixture) { JSON.parse(File.read(path)) }
17
+
18
+ it "is byte-identical to a fresh regeneration" do
19
+ Dir.mktmpdir do |dir|
20
+ out = File.join(dir, "interop.json")
21
+ _stdout, stderr, status = Open3.capture3(
22
+ RbConfig.ruby, "-I", lib, File.join(fixtures, "ruby_portable_interop.generator.rb"), out
23
+ )
24
+ expect(status).to be_success, stderr
25
+ expect(File.binread(out)).to eq(File.binread(path))
26
+ end
27
+ end
28
+
29
+ it "pins the value shapes the ports must reproduce" do
30
+ first = JSON.parse(fixture["outputs"][0])
31
+ expect(fixture["outputs"].size).to eq(3)
32
+ expect(fixture["bundle"]["models"].keys).to contain_exactly("Interop::Rec@default", "Interop::Tag@default")
33
+ expect(first["count"]).to eq("9007199254740993")
34
+ expect(first["counts"]).to eq(["-7", "0", (2**70).to_s])
35
+ expect(first["tag"]["weight"]).to eq("-0.0")
36
+ end
37
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "spec_helper"
4
+ require "json"
5
+ require "open3"
6
+ require "rbconfig"
7
+ require "tmpdir"
8
+
9
+ # lutaml-model#1033: Ruby owns the cross-language LML corpora. The TS and Rust
10
+ # ports copy these goldens byte for byte, so a Ruby validator or float-wire
11
+ # change must fail here (regenerate the goldens deliberately, then re-copy).
12
+ RSpec.describe "LML shared conformance corpora" do
13
+ let(:fixtures) { File.expand_path("../../fixtures/lml_conformance", __dir__) }
14
+ let(:lib) { File.expand_path("../../../lib", __dir__) }
15
+ let(:lml) { Lutaml::Model::Lml }
16
+
17
+ def regenerate(generator, out)
18
+ _stdout, stderr, status = Open3.capture3(RbConfig.ruby, "-I", lib, File.join(fixtures, generator), out)
19
+ expect(status).to be_success, stderr
20
+ File.binread(out)
21
+ end
22
+
23
+ describe "r2_refusals.json" do
24
+ it "is byte-identical to a fresh regeneration" do
25
+ Dir.mktmpdir do |dir|
26
+ fresh = regenerate("r2_refusals.generator.rb", File.join(dir, "r2.json"))
27
+ expect(fresh).to eq(File.binread(File.join(fixtures, "r2_refusals.json")))
28
+ end
29
+ end
30
+ end
31
+
32
+ describe "float_vectors.json" do
33
+ let(:vectors) { JSON.parse(File.read(File.join(fixtures, "float_vectors.json"))) }
34
+
35
+ it "is byte-identical to a fresh regeneration" do
36
+ Dir.mktmpdir do |dir|
37
+ fresh = regenerate("float_vectors.generator.rb", File.join(dir, "fv.json"))
38
+ expect(fresh).to eq(File.binread(File.join(fixtures, "float_vectors.json")))
39
+ end
40
+ end
41
+
42
+ it "covers at least 2000 distinct doubles and the notation boundaries" do
43
+ wires = vectors.to_h { |v| [v["bits"], v["wire"]] }
44
+ expect(wires.size).to be >= 2000
45
+ expect(wires.values_at("430c6bf526340000", "43105cf3917d6f0b", "43118b54f22aeb00", "8000000000000000"))
46
+ .to eq(["1.0e15", "1151450203773890.8", "1.234567890123456e15", "-0.0"])
47
+ end
48
+
49
+ it "accepts every wire string as a canonical float and round-trips the bits" do
50
+ bad = vectors.reject do |v|
51
+ value = [v["bits"].to_i(16)].pack("Q>").unpack1("G")
52
+ lml::DocumentValidator.canonical_float?(v["wire"]) &&
53
+ [Float(v["wire"])].pack("G") == [value].pack("G")
54
+ end
55
+ expect(bad).to be_empty
56
+ end
57
+ end
58
+
59
+ describe "typed defaults" do
60
+ # Ruby value -> the wire default Lml.export_bundle writes (measured),
61
+ # for every registered builtin the document contract names.
62
+ {
63
+ string: ["s", "s"], token: ["t", "t"], uri: ["http://x/", "http://x/"], hostname: ["h.example", "h.example"],
64
+ boolean: [false, false], integer: [5, 5], decimal: [BigDecimal("1.5"), "1.5"], float: [1.5, "1.5"],
65
+ date: [Date.new(2020, 1, 2), "2020-01-02"], time: [Time.utc(2020, 1, 2, 3, 4, 5), "2020-01-02T03:04:05Z"],
66
+ date_time: [DateTime.new(2020, 1, 2, 3, 4, 5), "2020-01-02T03:04:05+00:00"],
67
+ time_without_date: ["03:04:05", "03:04:05"], duration: ["P1D", "P1D"], qname: ["a:b", "a:b"],
68
+ base64_binary: ["aGk=", "aGk="], hex_binary: ["6869", "6869"], symbol: [:sym, "sym"],
69
+ hash: [{ "a" => 1 }, { "a" => 1 }],
70
+ email_address: ["a@b.c", "a@b.c"], ip_v4_address: ["1.2.3.4", "1.2.3.4"], ip_v6_address: ["::1", "::1"],
71
+ uuid: ["123e4567-e89b-12d3-a456-426614174000", "123e4567-e89b-12d3-a456-426614174000"],
72
+ reference: ["r1", "r1"], uri_reference: ["a/b#c", "a/b#c"], positive_integer: [5, 5], non_negative_integer: [0, 0],
73
+ date_with_timezone: [Date.new(2020, 1, 2), "2020-01-02"],
74
+ date_time_with_timezone: [DateTime.new(2020, 1, 2, 3, 4, 5), "2020-01-02T03:04:05+00:00"],
75
+ day_time_duration: ["P1D", "P1D"], year_month_duration: ["P1Y", "P1Y"]
76
+ }.each do |type, (ruby_default, wire)|
77
+ it "accepts the #{type} default the exporter writes (#{wire.inspect})" do
78
+ klass = Class.new(Lutaml::Model::Serializable) do
79
+ def self.name = "TypedDefault"
80
+ end
81
+ klass.attribute :x, type, default: ruby_default
82
+ bundle = lml.export_bundle(klass)
83
+
84
+ expect(bundle.to_h["models"].values.first["attributes"].first["default"]).to eq("present" => true, "value" => wire)
85
+ expect { lml::Importer.bundle(bundle.canonical_json) }.not_to raise_error
86
+ end
87
+ end
88
+
89
+ # The exporter must never write a default the importer refuses.
90
+ {
91
+ "an integer beyond 2**53 - 1" => [:integer, 2**53, {}],
92
+ "an integer below -(2**53 - 1)" => [:integer, -(2**53), {}],
93
+ "a float collection" => [:float, [1.5, 2.5], { collection: true }],
94
+ "an empty float collection" => [:float, [], { collection: true }],
95
+ "a null float" => [:float, nil, {}],
96
+ "a float inside a hash" => [:hash, { "a" => 1.5 }, {}],
97
+ "a hash collection holding a float" => [:hash, [{ "a" => 1.5 }], { collection: true }],
98
+ "an integer too large for a float" => [:float, 10**400, {}],
99
+ "a float collection of too-large integers" => [:float, [10**400], { collection: true }],
100
+ }.each do |label, (type, default, options)|
101
+ it "refuses to export #{label} as a default" do
102
+ klass = Class.new(Lutaml::Model::Serializable) do
103
+ def self.name = "RefusedDefault"
104
+ end
105
+ klass.attribute :x, type, default: default, **options
106
+
107
+ expect { lml.export_bundle(klass) }.to raise_error(lml::RejectedError) { |e|
108
+ expect(e.reason).to eq(:non_serializable_default)
109
+ }
110
+ end
111
+ end
112
+
113
+ it "still exports the largest safe integer default and a scalar float" do
114
+ klass = Class.new(Lutaml::Model::Serializable) do
115
+ def self.name = "SafeDefaults"
116
+ end
117
+ klass.attribute :a, :integer, default: (2**53) - 1
118
+ klass.attribute :b, :float, default: 2**60
119
+
120
+ defaults = lml.export_bundle(klass).to_h["models"].values.first["attributes"].map { |a| a["default"]["value"] }
121
+ expect(defaults).to eq([(2**53) - 1, "1.152921504606847e18"])
122
+ expect { lml::Importer.bundle(lml.export_bundle(klass).canonical_json) }.not_to raise_error
123
+ end
124
+
125
+ it "pins one accept and one refuse vector per builtin type in the corpus" do
126
+ names = JSON.parse(File.read(File.join(fixtures, "r2_refusals.json"))).map { |c| c["name"] }
127
+ lml::DocumentValidator::BUILTIN_TYPES.each do |type|
128
+ expect(names).to include("typed_default_ok_#{type}", "typed_default_refused_#{type}")
129
+ end
130
+ end
131
+ end
132
+ end