lutaml-model 0.8.196 → 0.8.197
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/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/attribute.rb +3 -0
- data/lib/lutaml/xml/schema/xsd/element.rb +3 -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/restriction_simple_type.rb +9 -0
- data/lib/lutaml/xml/schema/xsd/vc_conditional_attributes.rb +62 -0
- data/lib/lutaml/xml/schema/xsd.rb +6 -2
- 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_scale_vc_spec.rb +59 -0
- metadata +10 -1
|
@@ -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 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,6 +97,8 @@ 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"
|
|
@@ -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
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
ENV["RANTLY_VERBOSE"] ||= "0"
|
|
4
|
+
require "rantly"
|
|
5
|
+
require "rantly/rspec_extensions"
|
|
6
|
+
require "rantly/shrinks"
|
|
7
|
+
require "lutaml/lml/format"
|
|
8
|
+
|
|
9
|
+
# Constants shared by the generative laws (TODO.next/sota/properties.md).
|
|
10
|
+
# Seeded and reproducible: the seed is printed before every property run,
|
|
11
|
+
# and Rantly shrinks the generated tuple on failure. PR runs use a
|
|
12
|
+
# reduced count and depth; the nightly workflow raises both through
|
|
13
|
+
# LML_PROPERTY_COUNT / LML_PROPERTY_DEPTH and supplies a fresh seed.
|
|
14
|
+
module PropertyLawsSupport
|
|
15
|
+
SEED = Integer(ENV.fetch("LML_PROPERTY_SEED", "20261020"))
|
|
16
|
+
COUNT = Integer(ENV.fetch("LML_PROPERTY_COUNT", "40"))
|
|
17
|
+
DEPTH = Integer(ENV.fetch("LML_PROPERTY_DEPTH", "2"))
|
|
18
|
+
|
|
19
|
+
TYPES = {
|
|
20
|
+
string: :string, integer: :integer, float: :float, boolean: :boolean
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# Values that a bare-word emission would read back as something other
|
|
24
|
+
# than a string (or not read at all).
|
|
25
|
+
EDGE_STRINGS = [
|
|
26
|
+
"", "null", "true", "false", "01", "1.50", "2dCi9o", "0x10",
|
|
27
|
+
"with space", "a-b", 'quote"x', "back\\slash", " padded ",
|
|
28
|
+
"line\nbreak", "tab\there", "üñï", "🎨", "12", "-3"
|
|
29
|
+
].freeze
|
|
30
|
+
|
|
31
|
+
STRING_GEN = proc do
|
|
32
|
+
case choose(:print, :ascii, :edge)
|
|
33
|
+
when :print then string(:print)
|
|
34
|
+
when :ascii then string(:ascii)
|
|
35
|
+
else choose(*EDGE_STRINGS)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The document-model comparison defined with TODO.canon/5.
|
|
40
|
+
DEFAULT_VALUES = {
|
|
41
|
+
"visibility" => "public", "literal" => false, "is_derived" => false,
|
|
42
|
+
"is_static" => false, "is_read_only" => false, "is_abstract" => false,
|
|
43
|
+
"direction" => "in"
|
|
44
|
+
}.freeze
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Grammar-directed LML document generator shared by the generative laws.
|
|
48
|
+
module LmlDocumentGenerator
|
|
49
|
+
# ---- grammar-directed document generation ----
|
|
50
|
+
|
|
51
|
+
def generated_document(params)
|
|
52
|
+
numbers = params.array
|
|
53
|
+
counts = Array.new(7) { |i| numbers[i] % (PropertyLawsSupport::DEPTH + 1) }
|
|
54
|
+
seed = numbers[7]
|
|
55
|
+
|
|
56
|
+
model = []
|
|
57
|
+
model.concat(generated_classes(seed, counts[0]))
|
|
58
|
+
model.concat(generated_enums(seed, counts[1], counts[2]))
|
|
59
|
+
model.concat(generated_packages(seed, counts[3]))
|
|
60
|
+
model.concat(generated_mappings(counts[4]))
|
|
61
|
+
|
|
62
|
+
source = +""
|
|
63
|
+
source << "lml \"1.0\"\n\n" if numbers[9].even?
|
|
64
|
+
source << "require \"gen.lml\"\n\n" if numbers[8].even?
|
|
65
|
+
source << wrap_models(seed, model) unless model.empty?
|
|
66
|
+
source << generated_instances(counts[5], counts[6])
|
|
67
|
+
source
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def wrap_models(seed, members)
|
|
71
|
+
"models Gen#{seed % 10} {\n#{indent(members.join("\n\n"))}\n}\n\n"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def generated_classes(seed, count)
|
|
75
|
+
Array.new(count) do |i|
|
|
76
|
+
attrs = Array.new((seed + i) % (PropertyLawsSupport::DEPTH + 2)) do |j|
|
|
77
|
+
generated_attribute(seed + i + j, j)
|
|
78
|
+
end
|
|
79
|
+
body = attrs.map { |line| indent(line) }
|
|
80
|
+
body << indent("definition \"generated class #{i}\"") if seed.even?
|
|
81
|
+
body << indent("op#{i}(in x: String) : Boolean") if (seed + i) % 4 == 1
|
|
82
|
+
body << indent("** class comment #{i}") if (seed + i) % 4 == 2
|
|
83
|
+
|
|
84
|
+
header = "class C#{i}"
|
|
85
|
+
header += " < Base" if (seed + i) % 5 == 3
|
|
86
|
+
"#{header} {\n#{body.join("\n\n")}\n}"
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def generated_attribute(seed, index)
|
|
91
|
+
name = "a#{index}"
|
|
92
|
+
type = PropertyLawsSupport::TYPES.keys.map { |key| key.to_s.capitalize }[seed % 4]
|
|
93
|
+
case seed % 6
|
|
94
|
+
when 0 then "attribute #{name}, #{type}"
|
|
95
|
+
when 1 then "attribute #{name}, #{type} { cardinality 0..n }"
|
|
96
|
+
when 2 then "attribute #{name}, #{type} { values { \"x\", \"y\" } }"
|
|
97
|
+
when 3 then "attribute #{name}, #{type} { pattern /a[0-9]+/ }"
|
|
98
|
+
when 4 then "-a#{index}: #{type} [1..2]"
|
|
99
|
+
else "attribute #{name}, #{type} { default: \"d#{index}\" }"
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def generated_enums(seed, count, members)
|
|
104
|
+
Array.new(count) do |i|
|
|
105
|
+
body = ["definition \"generated enum #{i}\""]
|
|
106
|
+
Array.new(members) do |m|
|
|
107
|
+
body << generated_enum_member(seed + m, m)
|
|
108
|
+
end
|
|
109
|
+
"enum E#{i} {\n#{indent(body.join("\n\n"))}\n}"
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def generated_enum_member(seed, index)
|
|
114
|
+
case seed % 4
|
|
115
|
+
when 0 then "value \"v#{index}\" {\n definition \"member #{index}\"\n}"
|
|
116
|
+
when 1 then "value v#{index}"
|
|
117
|
+
when 2 then "m#{index}: String"
|
|
118
|
+
else "value \"p#{index}\" {\n payload = \"x\"\n instance Payload {\n a = \"b\"\n }\n}"
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def generated_packages(seed, count)
|
|
123
|
+
Array.new(count) do
|
|
124
|
+
"package P#{seed % 10} {\n" \
|
|
125
|
+
"#{indent('class PClass { attribute x, String }')}\n\n" \
|
|
126
|
+
"#{indent('enum PEnum { value "p" }')}\n}"
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def generated_mappings(count)
|
|
131
|
+
return [] if count.zero?
|
|
132
|
+
|
|
133
|
+
["mapping xml Thing {\n" \
|
|
134
|
+
"#{indent('element "thing"')}\n\n" \
|
|
135
|
+
"#{indent('map_element "wire" a0')}\n}"]
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def generated_instances(collections, instances)
|
|
139
|
+
lines = []
|
|
140
|
+
instances.times do |i|
|
|
141
|
+
lines << "instance \"t#{i}\" Thing {"
|
|
142
|
+
lines << indent("a0 = \"value #{i}\"")
|
|
143
|
+
lines << indent("a1 = 1")
|
|
144
|
+
lines << indent("a2 = true")
|
|
145
|
+
lines << indent("a3 = null")
|
|
146
|
+
lines << indent("a4 = [\"x\", \"null\", \"é\"]")
|
|
147
|
+
lines << "}"
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
if collections.positive?
|
|
151
|
+
lines << "instances GenInstances {"
|
|
152
|
+
Array.new(collections) do |i|
|
|
153
|
+
lines << indent("collection \"col#{i}\" {")
|
|
154
|
+
lines << indent(indent("includes [\"t#{i}\"]"))
|
|
155
|
+
lines << indent(indent('validation { condition "count >= 0" }'))
|
|
156
|
+
lines << indent("}")
|
|
157
|
+
end
|
|
158
|
+
lines << "}"
|
|
159
|
+
end
|
|
160
|
+
return "" if lines.empty?
|
|
161
|
+
|
|
162
|
+
"#{lines.join("\n\n")}\n"
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def indent(text)
|
|
166
|
+
text.lines.map { |line| " #{line}" }.join
|
|
167
|
+
end
|
|
168
|
+
end
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
# lutaml-model#288 step 1: the class-level annotation DSL —
|
|
6
|
+
# description/example/annotation — stored in a frozen per-class table.
|
|
7
|
+
# Purely additive: the table is invisible to serialization and to the
|
|
8
|
+
# #1033 exporter's declaration walk.
|
|
9
|
+
RSpec.describe "class-level annotations DSL" do
|
|
10
|
+
let(:annotated_class) do
|
|
11
|
+
Class.new(Lutaml::Model::Serializable) do
|
|
12
|
+
attribute :name, :string
|
|
13
|
+
xml do
|
|
14
|
+
element "annotated"
|
|
15
|
+
map_element "name", to: :name
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
description "A model carrying annotations."
|
|
19
|
+
description "Second prose line."
|
|
20
|
+
example %(name: "x"), description: "minimal shape"
|
|
21
|
+
example %(name: "y")
|
|
22
|
+
annotation :schema, "follows the base pattern"
|
|
23
|
+
annotation :schema, "extended per the profile"
|
|
24
|
+
annotation :source, "the base register"
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
before do
|
|
29
|
+
stub_const("AnnotatedSpecModel", annotated_class)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "stores repeatable descriptions in declaration order" do
|
|
33
|
+
expect(AnnotatedSpecModel.annotations[:descriptions]).to eq(
|
|
34
|
+
["A model carrying annotations.", "Second prose line."],
|
|
35
|
+
)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "stores examples with optional descriptions" do
|
|
39
|
+
expect(AnnotatedSpecModel.annotations[:examples]).to eq(
|
|
40
|
+
[
|
|
41
|
+
{ snippet: %(name: "x"), description: "minimal shape" },
|
|
42
|
+
{ snippet: %(name: "y") },
|
|
43
|
+
],
|
|
44
|
+
)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "groups keyed annotations per term, keeping declaration order" do
|
|
48
|
+
expect(AnnotatedSpecModel.annotations[:annotations][:schema]).to eq(
|
|
49
|
+
["follows the base pattern", "extended per the profile"],
|
|
50
|
+
)
|
|
51
|
+
expect(AnnotatedSpecModel.annotations[:annotations][:source]).to eq(
|
|
52
|
+
["the base register"],
|
|
53
|
+
)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "returns a frozen table" do
|
|
57
|
+
table = AnnotatedSpecModel.annotations
|
|
58
|
+
expect(table).to be_frozen
|
|
59
|
+
expect(table[:descriptions]).to be_frozen
|
|
60
|
+
expect(table[:examples]).to be_frozen
|
|
61
|
+
expect(table[:annotations]).to be_frozen
|
|
62
|
+
expect(table[:annotations][:schema]).to be_frozen
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "isolates classes: an unannotated class reads an empty table" do
|
|
66
|
+
plain = Class.new(Lutaml::Model::Serializable)
|
|
67
|
+
stub_const("PlainSpecModel", plain)
|
|
68
|
+
|
|
69
|
+
expect(PlainSpecModel.annotations).to eq(
|
|
70
|
+
descriptions: [], examples: [], annotations: {},
|
|
71
|
+
)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "does not leak annotations across sibling classes" do
|
|
75
|
+
sibling = Class.new(Lutaml::Model::Serializable)
|
|
76
|
+
stub_const("SiblingSpecModel", sibling)
|
|
77
|
+
SiblingSpecModel.description "sibling only"
|
|
78
|
+
|
|
79
|
+
expect(SiblingSpecModel.annotations[:descriptions]).to eq(["sibling only"])
|
|
80
|
+
expect(AnnotatedSpecModel.annotations[:descriptions]).not_to include("sibling only")
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
it "leaves serialization byte-identical" do
|
|
84
|
+
annotated = AnnotatedSpecModel.new(name: "x")
|
|
85
|
+
plain = Class.new(Lutaml::Model::Serializable) do
|
|
86
|
+
attribute :name, :string
|
|
87
|
+
xml do
|
|
88
|
+
element "annotated"
|
|
89
|
+
map_element "name", to: :name
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
stub_const("PlainShapeSpecModel", plain)
|
|
93
|
+
|
|
94
|
+
expect(annotated.to_xml.strip).to eq(PlainShapeSpecModel.new(name: "x").to_xml.strip)
|
|
95
|
+
expect(annotated.to_json.strip).to eq(PlainShapeSpecModel.new(name: "x").to_json.strip)
|
|
96
|
+
end
|
|
97
|
+
end
|