lutaml-model 0.8.196 → 0.8.198
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/docs/_references/plan-schema-spec.adoc +308 -0
- data/lib/compat/opal/lutaml_model_boot.rb +1 -0
- data/lib/lutaml/lml/format/adapter/standard_adapter.rb +10 -3
- data/lib/lutaml/lml/models/top_element_attribute.rb +7 -2
- data/lib/lutaml/model/schema/xml_compiler/spec_builder.rb +9 -0
- data/lib/lutaml/model/serialize/annotations.rb +74 -0
- data/lib/lutaml/model/serialize.rb +2 -0
- data/lib/lutaml/model/version.rb +1 -1
- data/lib/lutaml/xml/mapping.rb +15 -0
- data/lib/lutaml/xml/schema/xsd/all.rb +2 -0
- data/lib/lutaml/xml/schema/xsd/attribute.rb +3 -0
- data/lib/lutaml/xml/schema/xsd/element.rb +3 -0
- data/lib/lutaml/xml/schema/xsd/key.rb +2 -0
- data/lib/lutaml/xml/schema/xsd/keyref.rb +2 -0
- data/lib/lutaml/xml/schema/xsd/max_scale.rb +25 -0
- data/lib/lutaml/xml/schema/xsd/min_scale.rb +25 -0
- data/lib/lutaml/xml/schema/xsd/override.rb +91 -0
- data/lib/lutaml/xml/schema/xsd/restriction_simple_type.rb +9 -0
- data/lib/lutaml/xml/schema/xsd/schema.rb +48 -0
- data/lib/lutaml/xml/schema/xsd/unique.rb +2 -0
- data/lib/lutaml/xml/schema/xsd/vc_conditional_attributes.rb +62 -0
- data/lib/lutaml/xml/schema/xsd.rb +10 -3
- data/lib/lutaml/xml/schema/xsd_schema.rb +2 -0
- data/spec/lml/lutaml/lml/format_adapter_spec.rb +24 -0
- data/spec/lml/lutaml/lml/json_transfer_spec.rb +188 -0
- data/spec/lml/lutaml/lml/property_laws_spec.rb +3 -38
- data/spec/lml/support/lml_property_support.rb +168 -0
- data/spec/lutaml/model/annotations_spec.rb +97 -0
- data/spec/lutaml/xml/schema/xsd_all_any_spec.rb +45 -0
- data/spec/lutaml/xml/schema/xsd_identity_ref_spec.rb +60 -0
- data/spec/lutaml/xml/schema/xsd_override_assembly_spec.rb +93 -0
- data/spec/lutaml/xml/schema/xsd_override_spec.rb +62 -0
- data/spec/lutaml/xml/schema/xsd_scale_vc_spec.rb +59 -0
- metadata +16 -2
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Xml
|
|
5
|
+
module Schema
|
|
6
|
+
module Xsd
|
|
7
|
+
# xs:override (XSD 1.1 §4.2.4): a schema-wide component
|
|
8
|
+
# replacement mechanism — the referenced document's components
|
|
9
|
+
# with matching names are replaced by the children carried
|
|
10
|
+
# here (#1286).
|
|
11
|
+
class Override < Base
|
|
12
|
+
# The component kinds an override may replace, keyed by the
|
|
13
|
+
# attribute carrying the component name.
|
|
14
|
+
COMPONENT_KINDS = {
|
|
15
|
+
element: :name,
|
|
16
|
+
attribute: :name,
|
|
17
|
+
notation: :name,
|
|
18
|
+
simple_type: :name,
|
|
19
|
+
complex_type: :name,
|
|
20
|
+
group: :name,
|
|
21
|
+
attribute_group: :name,
|
|
22
|
+
}.freeze
|
|
23
|
+
|
|
24
|
+
attribute :id, :string
|
|
25
|
+
attribute :schema_path, :string
|
|
26
|
+
attribute :annotation, :annotation
|
|
27
|
+
attribute :element, :element, collection: true, initialize_empty: true
|
|
28
|
+
attribute :attribute, :attribute, collection: true, initialize_empty: true
|
|
29
|
+
attribute :notation, :notation, collection: true, initialize_empty: true
|
|
30
|
+
attribute :simple_type, :simple_type, collection: true, initialize_empty: true
|
|
31
|
+
attribute :complex_type, :complex_type, collection: true, initialize_empty: true
|
|
32
|
+
attribute :group, :group, collection: true, initialize_empty: true
|
|
33
|
+
attribute :attribute_group, :attribute_group, collection: true,
|
|
34
|
+
initialize_empty: true
|
|
35
|
+
|
|
36
|
+
xml do
|
|
37
|
+
element "override"
|
|
38
|
+
mixed_content
|
|
39
|
+
namespace Lutaml::Xml::Schema::XsdNamespace
|
|
40
|
+
|
|
41
|
+
map_attribute :id, to: :id
|
|
42
|
+
map_attribute :schemaLocation, to: :schema_path
|
|
43
|
+
map_element :annotation, to: :annotation
|
|
44
|
+
map_element :element, to: :element
|
|
45
|
+
map_element :attribute, to: :attribute
|
|
46
|
+
map_element :notation, to: :notation
|
|
47
|
+
map_element :simpleType, to: :simple_type
|
|
48
|
+
map_element :complexType, to: :complex_type
|
|
49
|
+
map_element :group, to: :group
|
|
50
|
+
map_element :attributeGroup, to: :attribute_group
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The referenced document, read through the same location
|
|
54
|
+
# machinery includes and imports use (the assembly pass).
|
|
55
|
+
def fetch_schema
|
|
56
|
+
Glob.include_schema(schema_path) if schema_path && Glob.location?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# XSD 1.1 §4.2.4: the referenced document's components are
|
|
60
|
+
# replaced by this override's same-named children. Matching is
|
|
61
|
+
# by component kind and name; a child with no counterpart adds
|
|
62
|
+
# to the referenced document (lenient); children without a name
|
|
63
|
+
# (ref-form, anonymous) carry no replacement. Applied on a copy
|
|
64
|
+
# of the referenced schema so the override stays local to the
|
|
65
|
+
# referencing document.
|
|
66
|
+
def apply_to!(target_schema)
|
|
67
|
+
COMPONENT_KINDS.each do |kind, name_attr|
|
|
68
|
+
Array(public_send(kind)).each do |replacement|
|
|
69
|
+
name = replacement.public_send(name_attr)
|
|
70
|
+
next if name.nil?
|
|
71
|
+
|
|
72
|
+
components = target_schema.public_send(kind)
|
|
73
|
+
index = components.index do |component|
|
|
74
|
+
component.public_send(name_attr) == name
|
|
75
|
+
end
|
|
76
|
+
if index
|
|
77
|
+
components[index] = replacement
|
|
78
|
+
else
|
|
79
|
+
components << replacement
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
target_schema
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
Lutaml::Xml::Schema::Xsd.register_model(self, :override)
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -5,6 +5,8 @@ module Lutaml
|
|
|
5
5
|
module Schema
|
|
6
6
|
module Xsd
|
|
7
7
|
class RestrictionSimpleType < Base
|
|
8
|
+
include VcConditionalAttributes
|
|
9
|
+
|
|
8
10
|
attribute :id, :string
|
|
9
11
|
attribute :base, :string
|
|
10
12
|
attribute :annotation, :annotation
|
|
@@ -21,6 +23,10 @@ module Lutaml
|
|
|
21
23
|
initialize_empty: true
|
|
22
24
|
attribute :total_digits, :total_digits, collection: true,
|
|
23
25
|
initialize_empty: true
|
|
26
|
+
attribute :min_scale, :min_scale, collection: true,
|
|
27
|
+
initialize_empty: true
|
|
28
|
+
attribute :max_scale, :max_scale, collection: true,
|
|
29
|
+
initialize_empty: true
|
|
24
30
|
attribute :max_exclusive, :max_exclusive, collection: true,
|
|
25
31
|
initialize_empty: true
|
|
26
32
|
attribute :min_exclusive, :min_exclusive, collection: true,
|
|
@@ -50,6 +56,9 @@ module Lutaml
|
|
|
50
56
|
map_element :simple_type, to: :simple_type
|
|
51
57
|
map_element :enumeration, to: :enumeration
|
|
52
58
|
map_element :totalDigits, to: :total_digits
|
|
59
|
+
map_vc_attributes
|
|
60
|
+
map_element :minScale, to: :min_scale
|
|
61
|
+
map_element :maxScale, to: :max_scale
|
|
53
62
|
map_element :maxExclusive, to: :max_exclusive
|
|
54
63
|
map_element :minExclusive, to: :min_exclusive
|
|
55
64
|
map_element :maxInclusive, to: :max_inclusive
|
|
@@ -41,6 +41,14 @@ module Lutaml
|
|
|
41
41
|
initialize_empty: true
|
|
42
42
|
attribute :attribute_group, :attribute_group, collection: true,
|
|
43
43
|
initialize_empty: true
|
|
44
|
+
attribute :override, :override, collection: true,
|
|
45
|
+
initialize_empty: true
|
|
46
|
+
attribute :unique, :unique, collection: true,
|
|
47
|
+
initialize_empty: true
|
|
48
|
+
attribute :key, :key, collection: true,
|
|
49
|
+
initialize_empty: true
|
|
50
|
+
attribute :keyref, :keyref, collection: true,
|
|
51
|
+
initialize_empty: true
|
|
44
52
|
|
|
45
53
|
xml do
|
|
46
54
|
element "schema"
|
|
@@ -56,6 +64,10 @@ module Lutaml
|
|
|
56
64
|
map_element :simpleType, to: :simple_type
|
|
57
65
|
map_element :complexType, to: :complex_type
|
|
58
66
|
map_element :attributeGroup, to: :attribute_group
|
|
67
|
+
map_element :override, to: :override
|
|
68
|
+
map_element :unique, to: :unique
|
|
69
|
+
map_element :key, to: :key
|
|
70
|
+
map_element :keyref, to: :keyref
|
|
59
71
|
map_element :import, to: :import,
|
|
60
72
|
with: { from: :import_from_schema, to: :import_to_schema }
|
|
61
73
|
map_element :include, to: :include,
|
|
@@ -282,6 +294,33 @@ module Lutaml
|
|
|
282
294
|
alias attributes attribute
|
|
283
295
|
alias groups group
|
|
284
296
|
|
|
297
|
+
def apply_overrides!(seen = nil)
|
|
298
|
+
seen ||= {}.compare_by_identity
|
|
299
|
+
return self if seen[self]
|
|
300
|
+
|
|
301
|
+
seen[self] = true
|
|
302
|
+
Array(override).each do |entry|
|
|
303
|
+
next unless entry.is_a?(Override) && entry.schema_path
|
|
304
|
+
next if self.class.in_progress?(entry.schema_path)
|
|
305
|
+
|
|
306
|
+
self.class.add_in_progress(entry.schema_path)
|
|
307
|
+
referenced = insert_in_processed_schemas(entry)
|
|
308
|
+
self.class.remove_in_progress(entry.schema_path)
|
|
309
|
+
next unless referenced
|
|
310
|
+
|
|
311
|
+
applied = Lutaml::Model::Utils.deep_dup(referenced)
|
|
312
|
+
entry.apply_to!(applied)
|
|
313
|
+
override(applied)
|
|
314
|
+
applied.apply_overrides!(seen)
|
|
315
|
+
end
|
|
316
|
+
%i[include import].each do |kind|
|
|
317
|
+
Array(public_send(kind)).each do |nested|
|
|
318
|
+
nested.apply_overrides!(seen) if nested.is_a?(Schema)
|
|
319
|
+
end
|
|
320
|
+
end
|
|
321
|
+
self
|
|
322
|
+
end
|
|
323
|
+
|
|
285
324
|
private
|
|
286
325
|
|
|
287
326
|
def namespace_prefix_for(element, namespace_uri)
|
|
@@ -299,6 +338,15 @@ module Lutaml
|
|
|
299
338
|
namespaces.uniq
|
|
300
339
|
end
|
|
301
340
|
|
|
341
|
+
# XSD 1.1 s4.2.4 override application (the assembly pass): each
|
|
342
|
+
# override resolves its referenced document through the same
|
|
343
|
+
# location machinery includes use, replaces the same-named
|
|
344
|
+
# components on a COPY (the processed-schemas memo shares
|
|
345
|
+
# originals across references; an override is local to this
|
|
346
|
+
# reference), and appends the applied document to the override
|
|
347
|
+
# collection so the schema walk reaches it in place of the raw
|
|
348
|
+
# document. Runs after the tree is parsed (the override's
|
|
349
|
+
# children are populated), before root assignment.
|
|
302
350
|
def setup_import_and_include(klass, model, schema, args = {})
|
|
303
351
|
instance = init_instance_of(klass, schema.attributes || {}, args)
|
|
304
352
|
annotation_object(instance, schema)
|
|
@@ -7,6 +7,7 @@ module Lutaml
|
|
|
7
7
|
class Unique < Base
|
|
8
8
|
attribute :id, :string
|
|
9
9
|
attribute :name, :string
|
|
10
|
+
attribute :ref, :string
|
|
10
11
|
attribute :selector, :selector
|
|
11
12
|
attribute :annotation, :annotation
|
|
12
13
|
attribute :field, :field, collection: true, initialize_empty: true
|
|
@@ -18,6 +19,7 @@ module Lutaml
|
|
|
18
19
|
|
|
19
20
|
map_attribute :id, to: :id
|
|
20
21
|
map_attribute :name, to: :name
|
|
22
|
+
map_attribute :ref, to: :ref
|
|
21
23
|
map_element :annotation, to: :annotation
|
|
22
24
|
map_element :selector, to: :selector
|
|
23
25
|
map_element :field, to: :field
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Lutaml
|
|
4
|
+
module Xml
|
|
5
|
+
module Schema
|
|
6
|
+
module Xsd
|
|
7
|
+
# The component-level conditional-inclusion attributes of the
|
|
8
|
+
# XML Schema Versioning namespace (XSD 1.1 §4.2, issue #1281).
|
|
9
|
+
#
|
|
10
|
+
# The schema ROOT carries vc:minVersion/vc:maxVersion (mapped
|
|
11
|
+
# on Schema since #1222); every component declaration can carry
|
|
12
|
+
# all six spellings. Including this module declares the six
|
|
13
|
+
# attributes; the model's `xml do` block adds the
|
|
14
|
+
# map_vc_attributes lines.
|
|
15
|
+
module VcConditionalAttributes
|
|
16
|
+
VC_ATTRIBUTES = %i[
|
|
17
|
+
vc_type_available
|
|
18
|
+
vc_type_unavailable
|
|
19
|
+
vc_facet_available
|
|
20
|
+
vc_facet_unavailable
|
|
21
|
+
vc_min_version
|
|
22
|
+
vc_max_version
|
|
23
|
+
].freeze
|
|
24
|
+
|
|
25
|
+
VC_SPELLINGS = {
|
|
26
|
+
vc_type_available: "typeAvailable",
|
|
27
|
+
vc_type_unavailable: "typeUnavailable",
|
|
28
|
+
vc_facet_available: "facetAvailable",
|
|
29
|
+
vc_facet_unavailable: "facetUnavailable",
|
|
30
|
+
vc_min_version: "minVersion",
|
|
31
|
+
vc_max_version: "maxVersion",
|
|
32
|
+
}.freeze
|
|
33
|
+
|
|
34
|
+
def self.included(base)
|
|
35
|
+
VC_ATTRIBUTES.each do |name|
|
|
36
|
+
base.attribute name, :string
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
VC_ATTRIBUTES.each do |name|
|
|
41
|
+
spelling = VC_SPELLINGS[name]
|
|
42
|
+
|
|
43
|
+
define_method(:"#{name}_from") do |model, value|
|
|
44
|
+
model.public_send(:"#{name}=", value)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
define_method(:"#{name}_to") do |model, parent, _wrapper|
|
|
48
|
+
value = model.public_send(name)
|
|
49
|
+
return if value.nil?
|
|
50
|
+
|
|
51
|
+
parent.add_attribute(
|
|
52
|
+
::Lutaml::Xml::DataModel::XmlAttribute.new(
|
|
53
|
+
spelling, value, Lutaml::Xml::Schema::VcNamespace
|
|
54
|
+
),
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -33,8 +33,8 @@ module Lutaml
|
|
|
33
33
|
AttributeGroup Choice ComplexContent ComplexType Documentation
|
|
34
34
|
Element Enumeration ExtensionComplexContent ExtensionSimpleContent
|
|
35
35
|
Field FractionDigits Glob Group Import Include Key Keyref Length List
|
|
36
|
-
MaxExclusive MaxInclusive MaxLength MinExclusive MinInclusive
|
|
37
|
-
MinLength Notation OpenContent Pattern Redefine
|
|
36
|
+
MaxExclusive MaxInclusive MaxLength MaxScale MinExclusive MinInclusive
|
|
37
|
+
MinLength MinScale Notation OpenContent Override Pattern Redefine
|
|
38
38
|
RestrictionComplexContent RestrictionSimpleContent
|
|
39
39
|
RestrictionSimpleType Schema SchemaPath Selector Sequence SimpleContent
|
|
40
40
|
SimpleType TotalDigits Union Unique WhiteSpace
|
|
@@ -42,6 +42,8 @@ module Lutaml
|
|
|
42
42
|
].freeze
|
|
43
43
|
|
|
44
44
|
# Autoload all XSD model files (lazy loading)
|
|
45
|
+
autoload :VcConditionalAttributes,
|
|
46
|
+
"#{__dir__}/xsd/vc_conditional_attributes"
|
|
45
47
|
autoload :VERSION, "#{__dir__}/xsd/version"
|
|
46
48
|
autoload :Error, "#{__dir__}/xsd/errors"
|
|
47
49
|
autoload :SchemaValidator, "#{__dir__}/xsd/schema_validator"
|
|
@@ -95,9 +97,12 @@ module Lutaml
|
|
|
95
97
|
autoload :MinExclusive, "#{__dir__}/xsd/min_exclusive"
|
|
96
98
|
autoload :MinInclusive, "#{__dir__}/xsd/min_inclusive"
|
|
97
99
|
autoload :MinLength, "#{__dir__}/xsd/min_length"
|
|
100
|
+
autoload :MinScale, "#{__dir__}/xsd/min_scale"
|
|
101
|
+
autoload :MaxScale, "#{__dir__}/xsd/max_scale"
|
|
98
102
|
autoload :Notation, "#{__dir__}/xsd/notation"
|
|
99
103
|
autoload :Pattern, "#{__dir__}/xsd/pattern"
|
|
100
104
|
autoload :Redefine, "#{__dir__}/xsd/redefine"
|
|
105
|
+
autoload :Override, "#{__dir__}/xsd/override"
|
|
101
106
|
autoload :RestrictionComplexContent,
|
|
102
107
|
"#{__dir__}/xsd/restriction_complex_content"
|
|
103
108
|
autoload :RestrictionSimpleContent,
|
|
@@ -160,7 +165,9 @@ module Lutaml
|
|
|
160
165
|
Schema.reset_processed_schemas unless nested_schema
|
|
161
166
|
|
|
162
167
|
Glob.with_location(location) do
|
|
163
|
-
Schema.from_xml(xsd, register: register)
|
|
168
|
+
Schema.from_xml(xsd, register: register)
|
|
169
|
+
.apply_overrides!
|
|
170
|
+
.assign_root!
|
|
164
171
|
end
|
|
165
172
|
end
|
|
166
173
|
end
|
|
@@ -693,6 +693,8 @@ skip_unmapped: false)
|
|
|
693
693
|
emit_facet(xml, :length, facets[:length])
|
|
694
694
|
emit_facet(xml, :minLength, facets[:min_length])
|
|
695
695
|
emit_facet(xml, :maxLength, facets[:max_length])
|
|
696
|
+
emit_facet(xml, :minScale, facets[:min_scale])
|
|
697
|
+
emit_facet(xml, :maxScale, facets[:max_scale])
|
|
696
698
|
Array(facets[:enumeration]).each do |value|
|
|
697
699
|
emit_facet(xml, :enumeration, lexical_value(type, value))
|
|
698
700
|
end
|
|
@@ -269,4 +269,28 @@ RSpec.describe "LML format adapter" do
|
|
|
269
269
|
expect(model_class.new).to respond_to(:to_lml)
|
|
270
270
|
end
|
|
271
271
|
end
|
|
272
|
+
|
|
273
|
+
describe "reference cargo after a JSON reload" do
|
|
274
|
+
let(:adapter) { Lutaml::Lml::Format::Adapter::StandardAdapter }
|
|
275
|
+
|
|
276
|
+
it "emits string-keyed reference cargo as ref:(...)" do
|
|
277
|
+
lml = adapter.new({ "glaze" => { "reference" => "Glazes.blue_matte" } }).to_lml
|
|
278
|
+
|
|
279
|
+
expect(lml).to include("glaze = ref:(Glazes.blue_matte)")
|
|
280
|
+
expect(lml).not_to match(/instance \{\s*reference/)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
it "emits string-keyed reference items in a list as ref:(...)" do
|
|
284
|
+
lml = adapter.new({ "glazes" => [{ "reference" => "Glazes.a" }] }).to_lml
|
|
285
|
+
|
|
286
|
+
expect(lml).to include("ref:(Glazes.a)")
|
|
287
|
+
expect(lml).not_to match(/instance \{\s*reference/)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
it "emits symbol-keyed reference cargo the same way" do
|
|
291
|
+
lml = adapter.new({ glaze: { reference: "Glazes.blue_matte" } }).to_lml
|
|
292
|
+
|
|
293
|
+
expect(lml).to include("glaze = ref:(Glazes.blue_matte)")
|
|
294
|
+
end
|
|
295
|
+
end
|
|
272
296
|
end
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "json"
|
|
5
|
+
require_relative "../../support/lml_property_support"
|
|
6
|
+
|
|
7
|
+
# JSON transfer of LML documents (lutaml-lml#100, slice 1): document, class,
|
|
8
|
+
# enum, attribute, package, mapping-block and instance nodes. Interleaving
|
|
9
|
+
# order, the XML namespace and the XML/YAML bindings are open questions and
|
|
10
|
+
# are not asserted here.
|
|
11
|
+
RSpec.describe "LML JSON transfer" do
|
|
12
|
+
include LmlDocumentGenerator
|
|
13
|
+
|
|
14
|
+
# Defaults the model materialises on load; a rendering may omit them.
|
|
15
|
+
let(:defaults) do
|
|
16
|
+
{ "visibility" => "public", "literal" => false, "is_derived" => false,
|
|
17
|
+
"is_static" => false, "is_read_only" => false, "is_abstract" => false,
|
|
18
|
+
"keyword" => "enumeration", "mixed_content" => false }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def parse(source)
|
|
22
|
+
Lutaml::Lml.parse_document(source)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def graph(document)
|
|
26
|
+
strip(document.to_hash)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Only model defaults (and nil, which a rendering omits) are dropped;
|
|
30
|
+
# literal payloads such as `value => []` or `{}` are compared as is.
|
|
31
|
+
def strip(node)
|
|
32
|
+
case node
|
|
33
|
+
when Hash
|
|
34
|
+
node.each_with_object({}) do |(key, value), result|
|
|
35
|
+
next if defaults[key] == value || value.nil?
|
|
36
|
+
|
|
37
|
+
# JSON has no symbols: a parsed map literal's symbol keys read back as strings.
|
|
38
|
+
result[key.to_s] = strip(value)
|
|
39
|
+
end
|
|
40
|
+
when Array then node.map { |item| strip(item) }
|
|
41
|
+
else node
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def transfer(document)
|
|
46
|
+
Lutaml::Lml::Document.from_json(document.to_json)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
describe "a declared model" do
|
|
50
|
+
let(:source) do
|
|
51
|
+
<<~LML
|
|
52
|
+
lml "1.0"
|
|
53
|
+
|
|
54
|
+
models Shop {
|
|
55
|
+
class Item < Base {
|
|
56
|
+
definition "a thing for sale"
|
|
57
|
+
attribute price, Float { cardinality 0..n }
|
|
58
|
+
attribute kind, String { values { "a", "b" } }
|
|
59
|
+
attribute sku, String { pattern /a[0-9]+/ }
|
|
60
|
+
attribute note, String { default: "none" }
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
enum Grade {
|
|
64
|
+
definition "quality"
|
|
65
|
+
value "good" {
|
|
66
|
+
definition "member"
|
|
67
|
+
}
|
|
68
|
+
value ok
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
package Stock {
|
|
72
|
+
class Bin { attribute x, String }
|
|
73
|
+
enum Size { value "s" }
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
LML
|
|
77
|
+
end
|
|
78
|
+
let(:document) { parse(source) }
|
|
79
|
+
let(:json) { JSON.parse(document.to_json) }
|
|
80
|
+
|
|
81
|
+
it "renders every node kind as JSON keys" do
|
|
82
|
+
names = json.values_at("classes", "enums", "packages").flatten.map { |n| n["name"] }
|
|
83
|
+
|
|
84
|
+
expect(names).to include("Item", "Grade", "Stock")
|
|
85
|
+
expect(json["packages"].first.values_at("classes", "enums").flatten.map { |n| n["name"] })
|
|
86
|
+
.to contain_exactly("Bin", "Size")
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "keeps class parent, definition and attribute facets" do
|
|
90
|
+
item = json["classes"].find { |c| c["name"] == "Item" }
|
|
91
|
+
facets = item["attributes"].to_h { |a| [a["name"], a] }
|
|
92
|
+
|
|
93
|
+
expect(item).to include("parent_class" => "Base", "definition" => "a thing for sale")
|
|
94
|
+
expect(facets["price"]["cardinality"]).to include("min" => "0", "max" => "n")
|
|
95
|
+
expect(facets["kind"]["value_set"]).to eq(%w[a b])
|
|
96
|
+
expect(facets["sku"]["pattern"]).to include("a[0-9]+")
|
|
97
|
+
expect(facets["note"]["properties"]).to include(a_hash_including("name" => "default", "value" => "none"))
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "parses back to the identical document graph" do
|
|
101
|
+
expect(graph(transfer(document))).to eq(graph(document))
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
it "reaches a fixed point after one transfer" do
|
|
105
|
+
once = transfer(document).to_json
|
|
106
|
+
|
|
107
|
+
expect(Lutaml::Lml::Document.from_json(once).to_json).to eq(once)
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
describe "the RS 3001 corpus" do
|
|
112
|
+
corpus = Dir[File.expand_path("../../fixtures/rs3001/*.lml", __dir__)]
|
|
113
|
+
.map { |path| File.basename(path, ".lml") }.sort
|
|
114
|
+
|
|
115
|
+
corpus.each do |name|
|
|
116
|
+
it "round-trips #{name}" do
|
|
117
|
+
path = File.expand_path("../../fixtures/rs3001/#{name}.lml", __dir__)
|
|
118
|
+
document = parse(File.read(path))
|
|
119
|
+
|
|
120
|
+
expect(graph(transfer(document))).to eq(graph(document))
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
describe "empty literal values" do
|
|
126
|
+
let(:source) do
|
|
127
|
+
"instances Data {\n Row \"r\" { list = [] }\n}\n"
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def list(document)
|
|
131
|
+
document.instances.instances.first.attributes.find { |a| a.name == "list" }
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
it "keeps an empty list value through a JSON round trip" do
|
|
135
|
+
document = parse(source)
|
|
136
|
+
reloaded = transfer(document)
|
|
137
|
+
|
|
138
|
+
expect(list(document).to_hash).to include("value" => [])
|
|
139
|
+
expect(list(reloaded).to_hash).to include("value" => [])
|
|
140
|
+
expect(strip(list(reloaded).to_hash)).to eq(strip(list(document).to_hash))
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
it "does not strip a literal empty value when comparing graphs" do
|
|
144
|
+
with_empty = { "attributes" => [{ "name" => "a", "value" => [] }] }
|
|
145
|
+
without = { "attributes" => [{ "name" => "a" }] }
|
|
146
|
+
|
|
147
|
+
expect(strip(with_empty)).not_to eq(strip(without))
|
|
148
|
+
expect(strip(with_empty)).to eq(with_empty)
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
describe "references" do
|
|
153
|
+
let(:source) do
|
|
154
|
+
"instances Glazes {\n GlazeFormula \"blue_matte\" { a = 1 }\n " \
|
|
155
|
+
"CeramicTile \"t\" { glaze = ref:(Glazes.blue_matte) }\n}\n"
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def glaze(document)
|
|
159
|
+
document.instances.instances.last.attributes.first
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
it "keeps the typed reference after a JSON reload" do
|
|
163
|
+
reloaded = transfer(parse(source))
|
|
164
|
+
|
|
165
|
+
expect(glaze(reloaded).reference&.path).to eq("Glazes.blue_matte")
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
it "gives the same typed reference before and after" do
|
|
169
|
+
document = parse(source)
|
|
170
|
+
|
|
171
|
+
expect(glaze(transfer(document)).reference&.path).to eq(glaze(document).reference&.path)
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
describe "generated documents" do
|
|
176
|
+
it "round-trips whole documents through JSON" do
|
|
177
|
+
warn "LML property seed: #{PropertyLawsSupport::SEED}"
|
|
178
|
+
property = property_of { Tuple.new(Array.new(10) { range(0, 24) }) }
|
|
179
|
+
|
|
180
|
+
property.check(PropertyLawsSupport::COUNT) do |params|
|
|
181
|
+
source = generated_document(params)
|
|
182
|
+
document = parse(source)
|
|
183
|
+
|
|
184
|
+
expect(graph(transfer(document))).to eq(graph(document)), "source:\n#{source}"
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
end
|
|
188
|
+
end
|
|
@@ -8,46 +8,11 @@ require "rantly"
|
|
|
8
8
|
require "rantly/rspec_extensions"
|
|
9
9
|
require "rantly/shrinks"
|
|
10
10
|
require "lutaml/lml/format"
|
|
11
|
-
|
|
12
|
-
# Constants shared by the generative laws (TODO.next/sota/properties.md).
|
|
13
|
-
# Seeded and reproducible: the seed is printed before every property run,
|
|
14
|
-
# and Rantly shrinks the generated tuple on failure. PR runs use a
|
|
15
|
-
# reduced count and depth; the nightly workflow raises both through
|
|
16
|
-
# LML_PROPERTY_COUNT / LML_PROPERTY_DEPTH and supplies a fresh seed.
|
|
17
|
-
module PropertyLawsSupport
|
|
18
|
-
SEED = Integer(ENV.fetch("LML_PROPERTY_SEED", "20261020"))
|
|
19
|
-
COUNT = Integer(ENV.fetch("LML_PROPERTY_COUNT", "40"))
|
|
20
|
-
DEPTH = Integer(ENV.fetch("LML_PROPERTY_DEPTH", "2"))
|
|
21
|
-
|
|
22
|
-
TYPES = {
|
|
23
|
-
string: :string, integer: :integer, float: :float, boolean: :boolean
|
|
24
|
-
}.freeze
|
|
25
|
-
|
|
26
|
-
# Values that a bare-word emission would read back as something other
|
|
27
|
-
# than a string (or not read at all).
|
|
28
|
-
EDGE_STRINGS = [
|
|
29
|
-
"", "null", "true", "false", "01", "1.50", "2dCi9o", "0x10",
|
|
30
|
-
"with space", "a-b", 'quote"x', "back\\slash", " padded ",
|
|
31
|
-
"line\nbreak", "tab\there", "üñï", "🎨", "12", "-3"
|
|
32
|
-
].freeze
|
|
33
|
-
|
|
34
|
-
STRING_GEN = proc do
|
|
35
|
-
case choose(:print, :ascii, :edge)
|
|
36
|
-
when :print then string(:print)
|
|
37
|
-
when :ascii then string(:ascii)
|
|
38
|
-
else choose(*EDGE_STRINGS)
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# The document-model comparison defined with TODO.canon/5.
|
|
43
|
-
DEFAULT_VALUES = {
|
|
44
|
-
"visibility" => "public", "literal" => false, "is_derived" => false,
|
|
45
|
-
"is_static" => false, "is_read_only" => false, "is_abstract" => false,
|
|
46
|
-
"direction" => "in"
|
|
47
|
-
}.freeze
|
|
48
|
-
end
|
|
11
|
+
require_relative "../../support/lml_property_support"
|
|
49
12
|
|
|
50
13
|
RSpec.describe "LML round-trip properties" do
|
|
14
|
+
include LmlDocumentGenerator
|
|
15
|
+
|
|
51
16
|
def report_seed
|
|
52
17
|
warn "LML property seed: #{PropertyLawsSupport::SEED}"
|
|
53
18
|
end
|