lutaml-model 0.8.201 → 0.8.202
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.
- checksums.yaml +4 -4
- data/.github/scripts/test_installed_gem.rb +1 -13
- data/.github/workflows/downstream-performance.yml +1 -22
- data/.github/workflows/performance.yml +1 -33
- data/.github/workflows/rake.yml +2 -6
- data/.rubocop.yml +0 -10
- data/.rubocop_todo.yml +0 -4
- data/Gemfile +0 -23
- data/README.adoc +25 -86
- data/Rakefile +2 -167
- data/docs/_guides/advanced-mapping.adoc +6 -6
- data/docs/_guides/character-encoding.adoc +0 -3
- data/docs/_guides/gem-integration.adoc +1 -1
- data/docs/_guides/index.adoc +0 -4
- data/docs/_guides/xml/type-namespaces.adoc +1 -1
- data/docs/_guides/xml-guide-0.8.0.adoc +2 -2
- data/docs/_guides/xml-namespaces.adoc +2 -3
- data/docs/_guides/xml_mappings/03_architecture.adoc +2 -2
- data/docs/_guides/xml_mappings/07_best_practices.adoc +2 -3
- data/docs/_guides/xml_mappings/08_troubleshooting.adoc +1 -9
- data/docs/_pages/comparison-with-shale.adoc +1 -1
- data/docs/_pages/configuration.adoc +6 -40
- data/docs/_pages/index.adoc +0 -1
- data/docs/_pages/serialization_adapters.adoc +4 -16
- data/docs/_references/architecture.adoc +1 -1
- data/docs/_references/lutaml-namespace-planning.md +1 -1
- data/docs/_references/three-phase-namespace-architecture.adoc +1 -2
- data/docs/_references/xsd-generation-with-namespace-support.adoc +3 -8
- data/docs/_tutorials/lutaml-xml-architecture.adoc +2 -8
- data/docs/architecture.md +7 -11
- data/docs/conversion_caching.adoc +1 -1
- data/docs/index.adoc +0 -1
- data/docs/key_value_data_model.adoc +1 -1
- data/docs/namespace-management.adoc +4 -10
- data/lib/lutaml/json/format.rb +0 -1
- data/lib/lutaml/json/generator_options.rb +0 -1
- data/lib/lutaml/key_value/transform.rb +9 -16
- data/lib/lutaml/lml/document_builder.rb +2 -2
- data/lib/lutaml/lml/entity_types.rb +2 -2
- data/lib/lutaml/model/adapter_resolver.rb +5 -15
- data/lib/lutaml/model/cached_type_resolver.rb +1 -7
- data/lib/lutaml/model/error/unknown_adapter_type_error.rb +1 -1
- data/lib/lutaml/model/global_context.rb +3 -5
- data/lib/lutaml/model/instrumentation.rb +0 -3
- data/lib/lutaml/model/mapping/mapping_rule.rb +6 -10
- data/lib/lutaml/model/register.rb +20 -31
- data/lib/lutaml/model/runtime_compatibility.rb +0 -92
- data/lib/lutaml/model/schema/class_loader.rb +2 -4
- data/lib/lutaml/model/schema/xml_compiler/spec_builder/simple_types.rb +2 -3
- data/lib/lutaml/model/serialize/attribute_definition.rb +10 -184
- data/lib/lutaml/model/serialize/conversion_caching.rb +1 -5
- data/lib/lutaml/model/serialize/model_import.rb +0 -5
- data/lib/lutaml/model/serialize/value_mapping.rb +0 -3
- data/lib/lutaml/model/services/rule_value_extractor.rb +4 -7
- data/lib/lutaml/model/store.rb +10 -17
- data/lib/lutaml/model/toml.rb +0 -2
- data/lib/lutaml/model/transformation_registry.rb +3 -7
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/model.rb +3 -19
- data/lib/lutaml/toml/format.rb +4 -8
- data/lib/lutaml/xml/adapter/moxml_adapter.rb +1 -7
- data/lib/lutaml/xml/adapter.rb +0 -4
- data/lib/lutaml/xml/adapter_loader.rb +1 -6
- data/lib/lutaml/xml/builder/base.rb +0 -3
- data/lib/lutaml/xml/builder.rb +0 -8
- data/lib/lutaml/xml/data_model.rb +1 -1
- data/lib/lutaml/xml/doctype_extractor.rb +3 -3
- data/lib/lutaml/xml/format.rb +22 -74
- data/lib/lutaml/xml/leptris/plan_compiler.rb +1 -2
- data/lib/lutaml/xml/model_transform.rb +14 -24
- data/lib/lutaml/xml/polymorphic_value_handler.rb +1 -1
- data/lib/lutaml/xml/schema/builder/moxml.rb +1 -1
- data/lib/lutaml/xml/schema/builder.rb +6 -9
- data/lib/lutaml/xml/schema/relaxng_schema.rb +1 -1
- data/lib/lutaml/xml/xml_element.rb +3 -2
- data/lib/lutaml/xml/xsd_validator.rb +4 -11
- data/lib/lutaml/yaml/adapter/yeptris_adapter.rb +2 -2
- data/lib/lutaml/yamls/adapter_loader.rb +1 -5
- data/lib/tasks/performance_comparator.rb +1 -1
- data/lutaml-model.gemspec +3 -8
- data/spec/benchmarks/xml_parsing_benchmark_spec.rb +1 -1
- data/spec/lutaml/json/adapter/standard_adapter_spec.rb +6 -9
- data/spec/lutaml/model/adapter_resolver_yeptris_switch_spec.rb +0 -2
- data/spec/lutaml/model/cached_type_resolver_spec.rb +2 -54
- data/spec/lutaml/model/cdata_spec.rb +2 -15
- data/spec/lutaml/model/encoding_spec.rb +0 -9
- data/spec/lutaml/model/except_spec.rb +0 -7
- data/spec/lutaml/model/json_spec.rb +0 -14
- data/spec/lutaml/model/mixed_content_spec.rb +6 -46
- data/spec/lutaml/model/ordered_content_spec.rb +0 -7
- data/spec/lutaml/model/processing_instruction_spec.rb +0 -6
- data/spec/lutaml/model/runtime_compatibility_spec.rb +0 -37
- data/spec/lutaml/model/serializable_spec.rb +0 -2
- data/spec/lutaml/model/toml_spec.rb +0 -23
- data/spec/lutaml/xml/adapter/base_adapter_regression_spec.rb +0 -4
- data/spec/lutaml/xml/adapter/order_spec.rb +1 -1
- data/spec/lutaml/xml/adapter/xml_namespace_spec.rb +0 -7
- data/spec/lutaml/xml/attribute_namespace_disjointness_spec.rb +6 -6
- data/spec/lutaml/xml/builder/builder_spec.rb +0 -24
- data/spec/lutaml/xml/default_namespace_spec.rb +1 -8
- data/spec/lutaml/xml/doctype_preservation_spec.rb +0 -4
- data/spec/lutaml/xml/entity_fragmentation_spec.rb +0 -6
- data/spec/lutaml/xml/fpi_namespace_spec.rb +0 -21
- data/spec/lutaml/xml/mapping_spec.rb +8 -15
- data/spec/lutaml/xml/namespace_scope_vcard_spec.rb +0 -7
- data/spec/lutaml/xml/namespace_spec.rb +0 -13
- data/spec/lutaml/xml/prefix_control_spec.rb +0 -6
- data/spec/lutaml/xml/schema/builder_moxml_spec.rb +1 -1
- data/spec/lutaml/xml/xml_adapter_spec.rb +0 -7
- data/spec/lutaml/xml/xml_spec.rb +2 -88
- data/spec/spec_helper.rb +5 -24
- data/spec/support/test_adapter_config.rb +6 -6
- metadata +1 -32
- data/.github/workflows/js-pr-check.yml +0 -106
- data/.github/workflows/js-sync.yml +0 -55
- data/.github/workflows/opal.yml +0 -65
- data/.gitmodules +0 -6
- data/.rspec-opal +0 -5
- data/docs/_guides/opal.adoc +0 -232
- data/lib/compat/opal/generate_boot.rb +0 -137
- data/lib/compat/opal/js_bundle_entry.rb +0 -42
- data/lib/compat/opal/lutaml_model_boot.rb +0 -638
- data/lib/compat/opal/monitor.rb +0 -31
- data/lib/compat/opal/moxml/adapter/customized_rexml/entity_reference.rb +0 -9
- data/lib/compat/opal/moxml/adapter/customized_rexml/formatter.rb +0 -225
- data/lib/compat/opal/moxml/adapter/customized_rexml.rb +0 -11
- data/lib/compat/opal/moxml_boot.rb +0 -68
- data/lib/compat/opal/objectspace_shim.rb +0 -23
- data/lib/compat/opal/rexml/namespace.rb +0 -59
- data/lib/compat/opal/rexml/parsers/baseparser.rb +0 -1016
- data/lib/compat/opal/rexml/source.rb +0 -214
- data/lib/compat/opal/rexml/text.rb +0 -426
- data/lib/compat/opal/rexml/xmltokens.rb +0 -45
- data/lib/compat/opal/rexml_compat.rb +0 -85
- data/lib/compat/opal/yaml_compat.rb +0 -32
- data/lib/lutaml/model/cached_type_resolver/mutex_hash_cache.rb +0 -53
- data/lib/lutaml/model/schema/schema_builder/oga.rb +0 -92
- data/lib/lutaml/xml/schema/builder/oga.rb +0 -92
- data/spec/lutaml/model/boot_manifest_spec.rb +0 -30
- data/spec/lutaml/model/opal_smoke_spec.rb +0 -199
- data/spec/lutaml/model/serialize/attribute_definition_block_form_spec.rb +0 -244
- data/spec/lutaml/xml/adapter/oga_adapter_spec.rb +0 -135
- data/spec/lutaml/xml/opal_xml_spec.rb +0 -204
- data/spec/support/opal.rb +0 -9
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "spec_helper"
|
|
4
|
-
require_relative "../../../../lib/lutaml/model"
|
|
5
|
-
require "lutaml/xml/adapter/moxml_adapter"
|
|
6
|
-
|
|
7
|
-
# Runtimes without a source compiler (Opal) define the attribute
|
|
8
|
-
# accessors and the state-defaults seed from blocks instead of a String
|
|
9
|
-
# class_eval. These specs force that block form on MRI and check it
|
|
10
|
-
# behaves exactly like the compiled form.
|
|
11
|
-
module AttributeDefinitionBlockFormSpec
|
|
12
|
-
module_function
|
|
13
|
-
|
|
14
|
-
def define_model(klass)
|
|
15
|
-
klass.class_eval do
|
|
16
|
-
attribute :name, :string
|
|
17
|
-
attribute :count, :integer
|
|
18
|
-
attribute :tags, :string, collection: true
|
|
19
|
-
# "char" is an enum shorthand, so the second :char definition
|
|
20
|
-
# redefines the setter but keeps the first Attribute's accessor.
|
|
21
|
-
attribute :align, :string, values: %w[char left]
|
|
22
|
-
attribute :char, :integer
|
|
23
|
-
attribute :char, :string
|
|
24
|
-
# String names, mutated once defined: the accessors must keep
|
|
25
|
-
# reading the ivar named at definition time.
|
|
26
|
-
label = +"label"
|
|
27
|
-
notes = +"notes"
|
|
28
|
-
attribute label, :integer
|
|
29
|
-
attribute notes, :string, collection: true
|
|
30
|
-
label << "x"
|
|
31
|
-
notes << "x"
|
|
32
|
-
|
|
33
|
-
# A model may define its own #send; the setters must not dispatch
|
|
34
|
-
# through it.
|
|
35
|
-
define_method(:send) { |*| "domain-method" }
|
|
36
|
-
|
|
37
|
-
xml do
|
|
38
|
-
element "item"
|
|
39
|
-
ordered
|
|
40
|
-
map_attribute "name", to: :name
|
|
41
|
-
map_element "count", to: :count
|
|
42
|
-
map_element "tag", to: :tags
|
|
43
|
-
map_element "char", to: :char
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
key_value do
|
|
47
|
-
map "name", to: :name
|
|
48
|
-
map "count", to: :count
|
|
49
|
-
map "tags", to: :tags
|
|
50
|
-
map "char", to: :char
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
klass
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# A model overriding the core methods the block form might dispatch
|
|
57
|
-
# through: the accessors must behave as the compiled ones do.
|
|
58
|
-
def override_model(klass)
|
|
59
|
-
klass.class_eval do
|
|
60
|
-
attribute :count, :integer
|
|
61
|
-
attribute :tags, :string, collection: true
|
|
62
|
-
%i[__send__ instance_variable_get instance_variable_set].each do |m|
|
|
63
|
-
define_method(m) { |*| :overridden }
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
m = klass.new
|
|
67
|
-
m.count = "5"
|
|
68
|
-
m.tags = %w[a]
|
|
69
|
-
m.tags("b")
|
|
70
|
-
# The collection getter itself reads through the overridden method,
|
|
71
|
-
# so check what was stored.
|
|
72
|
-
stored = ::Kernel.instance_method(:instance_variable_get)
|
|
73
|
-
[stored.bind_call(m, :@count), stored.bind_call(m, :@tags)]
|
|
74
|
-
end
|
|
75
|
-
|
|
76
|
-
# A model whose scope defines its own Array: appends must resolve the
|
|
77
|
-
# constant where the compiled getter does.
|
|
78
|
-
# Checked on the defining class and on a subclass that shadows Array,
|
|
79
|
-
# with the defining class overriding const_get.
|
|
80
|
-
def array_scope_model(klass)
|
|
81
|
-
klass.const_set(:Array, Class.new(::Array))
|
|
82
|
-
klass.class_eval { attribute :tags, :string, collection: true }
|
|
83
|
-
klass.define_singleton_method(:const_get) { |*| raise "const_get called" }
|
|
84
|
-
sub = Class.new(klass)
|
|
85
|
-
sub.const_set(:Array, ::Array)
|
|
86
|
-
[klass, sub].map do |k|
|
|
87
|
-
m = k.new
|
|
88
|
-
m.tags = %w[a]
|
|
89
|
-
m.tags("b")
|
|
90
|
-
::Kernel.instance_method(:instance_variable_get).bind_call(m, :@tags)
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
# Raw-state readers after the state defaults compiled for a smaller
|
|
95
|
-
# attribute set: an attribute imported later, and one declared later,
|
|
96
|
-
# must both get a reader (#933).
|
|
97
|
-
def late_raw_readers(klass)
|
|
98
|
-
klass.class_eval { attribute :name, :string }
|
|
99
|
-
klass.new
|
|
100
|
-
imported = Class.new(Lutaml::Model::Serializable) do
|
|
101
|
-
attribute :br, :string
|
|
102
|
-
end
|
|
103
|
-
klass.import_model_attributes(imported)
|
|
104
|
-
klass.class_eval { attribute :late, :string, collection: true }
|
|
105
|
-
m = klass.new
|
|
106
|
-
m.br = "b"
|
|
107
|
-
readers = %i[name br late].map do |a|
|
|
108
|
-
raw = m.public_send(:"__lutaml_raw_#{a}")
|
|
109
|
-
[a, Lutaml::Model::Utils.uninitialized?(raw), raw.equal?(Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION) || raw]
|
|
110
|
-
end
|
|
111
|
-
readers << register_choice_raw_reader
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
# A choice import into a non-default register merges attribute records
|
|
115
|
-
# without define_attribute_methods: only the state-defaults
|
|
116
|
-
# compilation gives the imported name its raw reader.
|
|
117
|
-
def register_choice_raw_reader
|
|
118
|
-
register = Lutaml::Model::Register.new(:raw_reader_block_form_spec)
|
|
119
|
-
Lutaml::Model::GlobalRegister.register(register)
|
|
120
|
-
imported = Class.new(Lutaml::Model::Serializable) do
|
|
121
|
-
attribute :br, :string, collection: true
|
|
122
|
-
end
|
|
123
|
-
klass = Class.new(Lutaml::Model::Serializable) do
|
|
124
|
-
attribute :name, :string
|
|
125
|
-
choice { import_model_attributes(imported, :raw_reader_block_form_spec) }
|
|
126
|
-
end
|
|
127
|
-
klass.allocate_for_deserialization(:raw_reader_block_form_spec).__lutaml_raw_br
|
|
128
|
-
.equal?(Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION)
|
|
129
|
-
ensure
|
|
130
|
-
Lutaml::Model::GlobalRegister.unregister(:raw_reader_block_form_spec)
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def build_model
|
|
134
|
-
define_model(Class.new(Lutaml::Model::Serializable))
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def element_order(model)
|
|
138
|
-
model.element_order.map { |e| [e.type, e.name, e.text_content] }
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
def arity_error(model, name)
|
|
142
|
-
model.public_send(name, "1", "2")
|
|
143
|
-
nil
|
|
144
|
-
rescue ArgumentError => e
|
|
145
|
-
e.message
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
def observe(klass)
|
|
149
|
-
fresh = klass.new
|
|
150
|
-
set = klass.new
|
|
151
|
-
set.name = "a"
|
|
152
|
-
set.count = "5"
|
|
153
|
-
set.tags("x")
|
|
154
|
-
set.tags("y")
|
|
155
|
-
set.char = "7"
|
|
156
|
-
# A parsed instance with no tags still holds the lazy sentinel.
|
|
157
|
-
emptied = klass.from_xml('<item name="z"/>')
|
|
158
|
-
sentinel_before_nil = emptied.instance_variable_get(:@tags)
|
|
159
|
-
.equal?(Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION)
|
|
160
|
-
emptied.tags = nil
|
|
161
|
-
xml = set.to_xml
|
|
162
|
-
parsed = klass.from_xml(xml)
|
|
163
|
-
|
|
164
|
-
seed = klass.__send__(:compiled_state_defaults_name!, :default)
|
|
165
|
-
seeded = klass.new.__send__(seed)
|
|
166
|
-
|
|
167
|
-
{
|
|
168
|
-
seed_return: [seeded.class, seeded.equal?(Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION)],
|
|
169
|
-
fresh_scalar_uninitialized: Lutaml::Model::Utils.uninitialized?(
|
|
170
|
-
fresh.instance_variable_get(:@name),
|
|
171
|
-
),
|
|
172
|
-
fresh_collection_sentinel: fresh.instance_variable_get(:@tags)
|
|
173
|
-
.equal?(Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION),
|
|
174
|
-
fresh_readers: [fresh.name, fresh.count, fresh.tags],
|
|
175
|
-
set_readers: [set.name, set.count, set.tags, set.char],
|
|
176
|
-
set_using_default: %i[name count tags].map { |a| set.using_default?(a) },
|
|
177
|
-
sentinel_before_nil: sentinel_before_nil,
|
|
178
|
-
nil_keeps_sentinel: emptied.instance_variable_get(:@tags)
|
|
179
|
-
.equal?(Lutaml::Model::Serialize::LAZY_EMPTY_COLLECTION),
|
|
180
|
-
getter_with_arg: klass.new.tap { |m| m.count("3") }.count,
|
|
181
|
-
assign_parsed_skips_cast: klass.new.tap { |m| m.__assign_parsed_count = "3" }.count,
|
|
182
|
-
built_order: element_order(klass.new do |m|
|
|
183
|
-
m.count = "1"
|
|
184
|
-
m.tags = %w[p q]
|
|
185
|
-
m.tags("r")
|
|
186
|
-
end),
|
|
187
|
-
string_names: klass.new.tap do |m|
|
|
188
|
-
m.label = "4"
|
|
189
|
-
m.notes("n")
|
|
190
|
-
end.then { |m| [m.label, m.notes, m.instance_variables.grep(/label|notes/).sort] },
|
|
191
|
-
two_args: %i[count tags label notes].map { |a| arity_error(klass.new, a) },
|
|
192
|
-
xml: xml,
|
|
193
|
-
json: set.to_json,
|
|
194
|
-
parsed: [parsed.name, parsed.count, parsed.tags, parsed.char],
|
|
195
|
-
parsed_equal: parsed == set,
|
|
196
|
-
}
|
|
197
|
-
end
|
|
198
|
-
end
|
|
199
|
-
|
|
200
|
-
RSpec.describe Lutaml::Model::Serialize::AttributeDefinition do
|
|
201
|
-
let(:compiled) { AttributeDefinitionBlockFormSpec.observe(AttributeDefinitionBlockFormSpec.build_model) }
|
|
202
|
-
|
|
203
|
-
it "compiles accessors from source by default on MRI" do
|
|
204
|
-
expect(described_class.source_compilation?).to be(true)
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
it "defines the block form without String class_eval and matches the compiled form" do
|
|
208
|
-
expected = compiled
|
|
209
|
-
allow(described_class).to receive(:source_compilation?).and_return(false)
|
|
210
|
-
klass = Class.new(Lutaml::Model::Serializable)
|
|
211
|
-
allow(klass).to receive(:class_eval).and_wrap_original do |original, *args, &block|
|
|
212
|
-
raise "String class_eval used: #{args.first}" if args.first.is_a?(String)
|
|
213
|
-
|
|
214
|
-
original.call(*args, &block)
|
|
215
|
-
end
|
|
216
|
-
AttributeDefinitionBlockFormSpec.define_model(klass)
|
|
217
|
-
|
|
218
|
-
expect(AttributeDefinitionBlockFormSpec.observe(klass)).to eq(expected)
|
|
219
|
-
end
|
|
220
|
-
|
|
221
|
-
it "does not dispatch through a model's own core methods" do
|
|
222
|
-
expected = AttributeDefinitionBlockFormSpec.override_model(Class.new(Lutaml::Model::Serializable))
|
|
223
|
-
allow(described_class).to receive(:source_compilation?).and_return(false)
|
|
224
|
-
actual = AttributeDefinitionBlockFormSpec.override_model(Class.new(Lutaml::Model::Serializable))
|
|
225
|
-
|
|
226
|
-
expect(actual).to eq(expected)
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
it "resolves Array in the model's scope when appending" do
|
|
230
|
-
expected = AttributeDefinitionBlockFormSpec.array_scope_model(Class.new(Lutaml::Model::Serializable))
|
|
231
|
-
allow(described_class).to receive(:source_compilation?).and_return(false)
|
|
232
|
-
actual = AttributeDefinitionBlockFormSpec.array_scope_model(Class.new(Lutaml::Model::Serializable))
|
|
233
|
-
|
|
234
|
-
expect(actual).to eq(expected)
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
it "defines raw-state readers for imported and late attributes as the compiled form does" do
|
|
238
|
-
expected = AttributeDefinitionBlockFormSpec.late_raw_readers(Class.new(Lutaml::Model::Serializable))
|
|
239
|
-
allow(described_class).to receive(:source_compilation?).and_return(false)
|
|
240
|
-
actual = AttributeDefinitionBlockFormSpec.late_raw_readers(Class.new(Lutaml::Model::Serializable))
|
|
241
|
-
|
|
242
|
-
expect(actual).to eq(expected)
|
|
243
|
-
end
|
|
244
|
-
end
|
|
@@ -1,135 +0,0 @@
|
|
|
1
|
-
require "spec_helper"
|
|
2
|
-
require "oga"
|
|
3
|
-
require "lutaml/xml/adapter/moxml_adapter"
|
|
4
|
-
|
|
5
|
-
RSpec.describe Lutaml::Xml::Adapter::MoxmlAdapter.for_engine(:oga), :oga_adapter do
|
|
6
|
-
let(:xml_string) do
|
|
7
|
-
<<~XML
|
|
8
|
-
<root xmlns="http://example.com/default" xmlns:prefix="http://example.com/prefixed">
|
|
9
|
-
<prefix:child attr="value" prefix:attr="prefixed_value">Text</prefix:child>
|
|
10
|
-
</root>
|
|
11
|
-
XML
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
let(:document) { described_class.parse(xml_string) }
|
|
15
|
-
|
|
16
|
-
context "when parsing XML with namespaces" do
|
|
17
|
-
let(:child) { document.root.element_children.first }
|
|
18
|
-
|
|
19
|
-
it "parses the root element with default namespace" do
|
|
20
|
-
expect(document.root.name).to eq("root")
|
|
21
|
-
expect(document.root.namespace.uri).to eq("http://example.com/default")
|
|
22
|
-
expect(document.root.namespace.prefix).to be_nil
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
it "parses child element with prefixed namespace" do
|
|
26
|
-
expect(child.name).to eq("prefix:child")
|
|
27
|
-
expect(child.namespace.uri).to eq("http://example.com/prefixed")
|
|
28
|
-
expect(child.namespace.prefix).to eq("prefix")
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it "parses attributes with and without namespaces" do
|
|
32
|
-
expect(child.attributes["attr"].value).to eq("value")
|
|
33
|
-
expect(child.attributes["attr"].namespace).to be_nil
|
|
34
|
-
expect(child.attributes["prefix:attr"].value).to eq("prefixed_value")
|
|
35
|
-
expect(child.attributes["prefix:attr"].namespace).to eq("http://example.com/prefixed")
|
|
36
|
-
expect(child.attributes["prefix:attr"].namespace_prefix).to eq("prefix")
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
context "when parsing elements in default namespace" do
|
|
41
|
-
let(:xml_with_default_ns) do
|
|
42
|
-
<<~XML
|
|
43
|
-
<root xmlns="http://example.com/default">
|
|
44
|
-
<child>Content</child>
|
|
45
|
-
<another>More content</another>
|
|
46
|
-
</root>
|
|
47
|
-
XML
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
let(:doc_with_default) { described_class.parse(xml_with_default_ns) }
|
|
51
|
-
|
|
52
|
-
it "treats unprefixed child elements as being in the default namespace" do
|
|
53
|
-
child = doc_with_default.root.element_children.first
|
|
54
|
-
expect(child.name).to eq("child")
|
|
55
|
-
expect(child.namespace.uri).to eq("http://example.com/default")
|
|
56
|
-
expect(child.namespace.prefix).to be_nil
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
it "applies default namespace to all unprefixed elements" do
|
|
60
|
-
children = doc_with_default.root.element_children
|
|
61
|
-
children.each do |child|
|
|
62
|
-
expect(child.namespace.uri).to eq("http://example.com/default")
|
|
63
|
-
expect(child.namespace.prefix).to be_nil
|
|
64
|
-
end
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
context "when parsing XML with comments" do
|
|
69
|
-
let(:xml_with_comment) do
|
|
70
|
-
<<~XML
|
|
71
|
-
<root>
|
|
72
|
-
<child>text</child>
|
|
73
|
-
<!-- a comment -->
|
|
74
|
-
</root>
|
|
75
|
-
XML
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
let(:doc_with_comment) { described_class.parse(xml_with_comment) }
|
|
79
|
-
|
|
80
|
-
let(:moxml_comment) do
|
|
81
|
-
ctx = Moxml.new(:oga)
|
|
82
|
-
doc = ctx.parse(xml_with_comment)
|
|
83
|
-
doc.root.children.find { |n| n.is_a?(Moxml::Comment) }
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
it "returns 'comment' for Moxml::Comment nodes via name_of" do
|
|
87
|
-
expect(described_class.name_of(moxml_comment)).to eq("comment")
|
|
88
|
-
end
|
|
89
|
-
|
|
90
|
-
it "parses comment nodes with the correct node type" do
|
|
91
|
-
comment = doc_with_comment.root.children.find(&:comment?)
|
|
92
|
-
|
|
93
|
-
expect(comment).not_to be_nil
|
|
94
|
-
expect(comment.name).to eq("comment")
|
|
95
|
-
expect(comment.node_type).to eq(:comment)
|
|
96
|
-
expect(comment.text).to eq(" a comment ")
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
it "serializes parsed comments back to XML comments" do
|
|
100
|
-
expect(doc_with_comment.root.to_xml).to include("<!-- a comment -->")
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
context "when generating XML with namespaces" do
|
|
105
|
-
it "generates XML with namespaces correctly" do
|
|
106
|
-
xml_output = document.root.to_xml
|
|
107
|
-
parsed_output = Moxml::Adapter::Oga.parse(xml_output)
|
|
108
|
-
|
|
109
|
-
root = parsed_output.root
|
|
110
|
-
expect(root.name).to eq("root")
|
|
111
|
-
expect(root.namespace.uri).to eq("http://example.com/default")
|
|
112
|
-
|
|
113
|
-
child = root.children.find { |c| c.is_a?(Moxml::Element) }
|
|
114
|
-
expect(described_class.prefixed_name_of(child)).to eq("prefix:child")
|
|
115
|
-
expect(child.namespace.uri).to eq("http://example.com/prefixed")
|
|
116
|
-
unprefixed_attr = child.attributes.find { |attr| attr.name == "attr" }
|
|
117
|
-
expect(unprefixed_attr.value).to eq("value")
|
|
118
|
-
prefixed_attr = child.attributes.find do |attr|
|
|
119
|
-
described_class.prefixed_name_of(attr) == "prefix:attr"
|
|
120
|
-
end
|
|
121
|
-
expect(prefixed_attr.value).to eq("prefixed_value")
|
|
122
|
-
end
|
|
123
|
-
end
|
|
124
|
-
|
|
125
|
-
describe "comment round-trip" do
|
|
126
|
-
it "preserves comments when round-tripping through build_xml" do
|
|
127
|
-
xml = "<root><!-- hello --><child>text</child></root>"
|
|
128
|
-
parsed = described_class.parse(xml)
|
|
129
|
-
output = parsed.to_xml
|
|
130
|
-
|
|
131
|
-
expect(output).to include("<!-- hello -->").or include("<!--hello-->")
|
|
132
|
-
expect(output).not_to include("<comment")
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
end
|
|
@@ -1,204 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
require "spec_helper"
|
|
4
|
-
|
|
5
|
-
# Adapter-agnostic XML round-trip scenarios, shared between Oga and
|
|
6
|
-
# REXML smoke runs. Both adapters are pure Ruby and work under Opal;
|
|
7
|
-
# this shared example is the contract that any future Opal-capable
|
|
8
|
-
# adapter would also need to satisfy.
|
|
9
|
-
# moxml 0.6's packaged REXML adapter references CustomizedRexml
|
|
10
|
-
# (Formatter/EntityReference) but no longer ships the module, and the
|
|
11
|
-
# vendored 0.5 formatter writes empty output against it under Opal
|
|
12
|
-
# (tracked in lutaml/moxml). REXML stays parse-only under Opal until
|
|
13
|
-
# that ships; Oga carries the serialize contract.
|
|
14
|
-
REXML_SERIALIZE_SKIP = "moxml 0.6 REXML adapter lacks CustomizedRexml " \
|
|
15
|
-
"under Opal (lutaml/moxml tracking)"
|
|
16
|
-
|
|
17
|
-
RSpec.shared_examples "opal XML adapter round-trip" do |adapter_name|
|
|
18
|
-
before do
|
|
19
|
-
Lutaml::Model::Config.xml_adapter_type = adapter_name
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it "round-trips XML through parse and serialize" do
|
|
23
|
-
skip(REXML_SERIALIZE_SKIP) if adapter_name == :rexml
|
|
24
|
-
klass = Class.new do
|
|
25
|
-
include Lutaml::Model::Serialize
|
|
26
|
-
|
|
27
|
-
attribute :name, :string
|
|
28
|
-
xml do
|
|
29
|
-
element "person"
|
|
30
|
-
map_element "name", to: :name
|
|
31
|
-
end
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
instance = klass.from_xml("<person><name>Alice</name></person>")
|
|
35
|
-
expect(instance.name).to eq("Alice")
|
|
36
|
-
expect(instance.to_xml).to include("<name>Alice</name>")
|
|
37
|
-
end
|
|
38
|
-
|
|
39
|
-
it "handles XML attributes" do
|
|
40
|
-
klass = Class.new do
|
|
41
|
-
include Lutaml::Model::Serialize
|
|
42
|
-
|
|
43
|
-
attribute :id, :string
|
|
44
|
-
attribute :value, :string
|
|
45
|
-
xml do
|
|
46
|
-
element "item"
|
|
47
|
-
map_attribute "id", to: :id
|
|
48
|
-
map_element "value", to: :value
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
instance = klass.from_xml('<item id="42"><value>test</value></item>')
|
|
53
|
-
expect(instance.id).to eq("42")
|
|
54
|
-
expect(instance.value).to eq("test")
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
it "handles nested elements" do
|
|
58
|
-
address = Class.new do
|
|
59
|
-
include Lutaml::Model::Serialize
|
|
60
|
-
|
|
61
|
-
attribute :city, :string
|
|
62
|
-
xml do
|
|
63
|
-
element "address"
|
|
64
|
-
map_element "city", to: :city
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
person = Class.new do
|
|
69
|
-
include Lutaml::Model::Serialize
|
|
70
|
-
|
|
71
|
-
attribute :name, :string
|
|
72
|
-
attribute :address, address
|
|
73
|
-
xml do
|
|
74
|
-
element "person"
|
|
75
|
-
map_element "name", to: :name
|
|
76
|
-
map_element "address", to: :address
|
|
77
|
-
end
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
instance = person.from_xml("<person><name>Alice</name><address><city>NYC</city></address></person>")
|
|
81
|
-
expect(instance.name).to eq("Alice")
|
|
82
|
-
expect(instance.address.city).to eq("NYC")
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
it "handles element collections" do
|
|
86
|
-
list = Class.new do
|
|
87
|
-
include Lutaml::Model::Serialize
|
|
88
|
-
|
|
89
|
-
attribute :items, :string, collection: true
|
|
90
|
-
xml do
|
|
91
|
-
element "list"
|
|
92
|
-
map_element "item", to: :items
|
|
93
|
-
end
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
instance = list.from_xml("<list><item>a</item><item>b</item><item>c</item></list>")
|
|
97
|
-
expect(instance.items).to eq(%w[a b c])
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
it "handles mixed content" do
|
|
101
|
-
paragraph = Class.new do
|
|
102
|
-
include Lutaml::Model::Serialize
|
|
103
|
-
|
|
104
|
-
attribute :content, :string, collection: true
|
|
105
|
-
xml do
|
|
106
|
-
element "p"
|
|
107
|
-
mixed_content
|
|
108
|
-
map_content to: :content
|
|
109
|
-
end
|
|
110
|
-
end
|
|
111
|
-
|
|
112
|
-
instance = paragraph.from_xml("<p>Hello <b>world</b> end</p>")
|
|
113
|
-
expect(instance.content.join).to include("Hello")
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
it "serializes to XML with correct structure" do
|
|
117
|
-
skip(REXML_SERIALIZE_SKIP) if adapter_name == :rexml
|
|
118
|
-
item = Class.new do
|
|
119
|
-
include Lutaml::Model::Serialize
|
|
120
|
-
|
|
121
|
-
attribute :title, :string
|
|
122
|
-
attribute :count, :integer
|
|
123
|
-
xml do
|
|
124
|
-
element "item"
|
|
125
|
-
map_element "title", to: :title
|
|
126
|
-
map_element "count", to: :count
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
instance = item.new(title: "Widget", count: 5)
|
|
131
|
-
xml = instance.to_xml
|
|
132
|
-
expect(xml).to include("<title>Widget</title>")
|
|
133
|
-
expect(xml).to include("<count>5</count>")
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
# Pure-Ruby XML adapters that ship in the Opal bundle. Each nested
|
|
138
|
-
# group runs the shared contract for its adapter; the top-level guard
|
|
139
|
-
# gates the whole file under Opal.
|
|
140
|
-
RSpec.describe "XML adapters under Opal", if: RUBY_ENGINE == "opal" do
|
|
141
|
-
describe "with Oga (default)" do
|
|
142
|
-
it_behaves_like "opal XML adapter round-trip", :oga
|
|
143
|
-
|
|
144
|
-
it "uses Oga adapter by default under Opal" do
|
|
145
|
-
expect(Lutaml::Model::Config.xml_adapter_type).to eq(:oga)
|
|
146
|
-
end
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
describe "with REXML" do
|
|
150
|
-
it_behaves_like "opal XML adapter round-trip", :rexml
|
|
151
|
-
|
|
152
|
-
it "selects REXML when explicitly configured" do
|
|
153
|
-
Lutaml::Model::Config.xml_adapter_type = :rexml
|
|
154
|
-
expect(Lutaml::Model::Config.xml_adapter_type).to eq(:rexml)
|
|
155
|
-
end
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
# Opal does not carry modules prepended into Serialize over to
|
|
159
|
-
# Serializable, which included it at boot; lib/lutaml/model.rb and
|
|
160
|
-
# lib/lutaml/xml/format.rb prepend onto Serializable directly.
|
|
161
|
-
describe "Serializable subclasses" do
|
|
162
|
-
before { Lutaml::Model::Config.xml_adapter_type = :oga }
|
|
163
|
-
|
|
164
|
-
it "builds an ordered model from a builder block" do
|
|
165
|
-
klass = Class.new(Lutaml::Model::Serializable) do
|
|
166
|
-
attribute :first, :string
|
|
167
|
-
attribute :second, :string
|
|
168
|
-
xml do
|
|
169
|
-
element "pair"
|
|
170
|
-
ordered
|
|
171
|
-
map_element "first", to: :first
|
|
172
|
-
map_element "second", to: :second
|
|
173
|
-
end
|
|
174
|
-
end
|
|
175
|
-
|
|
176
|
-
instance = klass.new do
|
|
177
|
-
second "b"
|
|
178
|
-
first "a"
|
|
179
|
-
end
|
|
180
|
-
|
|
181
|
-
expect([instance.first, instance.second]).to eq(%w[a b])
|
|
182
|
-
expect(instance.element_order.map(&:name)).to eq(%w[second first])
|
|
183
|
-
expect(instance.to_xml)
|
|
184
|
-
.to include("<pair><second>b</second><first>a</first></pair>")
|
|
185
|
-
end
|
|
186
|
-
|
|
187
|
-
it "installs the XML accessors on a plain model class" do
|
|
188
|
-
plain = Class.new { attr_accessor :name }
|
|
189
|
-
mapper = Class.new(Lutaml::Model::Serializable) do
|
|
190
|
-
model plain
|
|
191
|
-
attribute :name, :string
|
|
192
|
-
xml do
|
|
193
|
-
element "person"
|
|
194
|
-
map_element "name", to: :name
|
|
195
|
-
end
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
expect(plain.method_defined?(:encoding=)).to be true
|
|
199
|
-
parsed = mapper.from_xml("<person><name>Alice</name></person>")
|
|
200
|
-
expect(parsed).to be_a(plain)
|
|
201
|
-
expect(parsed.name).to eq("Alice")
|
|
202
|
-
end
|
|
203
|
-
end
|
|
204
|
-
end
|
data/spec/support/opal.rb
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
# Opal runtime patches for lutaml-model specs.
|
|
4
|
-
# Oga (via moxml's vendored opal-oga fork) is the default XML adapter
|
|
5
|
-
# under Opal; REXML (bundled stdlib gem, pure Ruby) is also available.
|
|
6
|
-
# Smoke specs parameterize over both — see spec/lutaml/xml/opal_xml_spec.rb.
|
|
7
|
-
if RUBY_ENGINE == "opal"
|
|
8
|
-
Lutaml::Model::Config.xml_adapter_type = :oga
|
|
9
|
-
end
|