sdl-ng 0.1.1 → 0.1.2

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.
@@ -1,12 +1,9 @@
1
- require_relative '../lib/sdl'
2
1
  require_relative 'spec_helper'
3
2
  require_relative 'shared_test_compendium'
4
3
 
5
4
  require 'rspec'
6
5
 
7
- describe 'Documentation of SDL objects' do
8
- include_context 'the default compendium'
9
-
6
+ store_translation = lambda {
10
7
  I18n.backend.store_translations('en', :sdl => {
11
8
  :type => {
12
9
  :color => 'A color'
@@ -25,15 +22,22 @@ describe 'Documentation of SDL objects' do
25
22
  :instance => {
26
23
  :type => {
27
24
  :color => {
28
- :red => 'The color "red"'
25
+ :red => 'The color "red"',
26
+ :blue => '#{hex_value}'
29
27
  }
30
28
  }
31
29
  }
32
30
  })
31
+ }
32
+
33
+ describe 'Documentation of SDL objects' do
34
+ include_context 'the default compendium'
33
35
 
34
36
  # Registers the classes of the default compendium
35
37
  before(:each) do
36
38
  compendium.register_classes_globally
39
+
40
+ store_translation.call
37
41
  end
38
42
 
39
43
  it 'can document types' do
@@ -61,4 +65,8 @@ describe 'Documentation of SDL objects' do
61
65
 
62
66
  expect(I18n.backend.instance_variable_get(:@translations)[:en][:sdl][:instance][:type][:color][:green]).to eq('Translate')
63
67
  end
68
+
69
+ it 'evaluates statements enclosed within #{}' do
70
+ expect(compendium.type_instances[Color][:blue].documentation).to eq('#00F')
71
+ end
64
72
  end
@@ -0,0 +1,82 @@
1
+ require_relative 'spec_helper'
2
+ require_relative 'shared_test_compendium'
3
+
4
+ require 'rspec'
5
+
6
+ describe 'The exporters' do
7
+ include_context 'the default compendium'
8
+
9
+ let :xsd_exporter do
10
+ SDL::Exporters::XSDSchemaExporter.new(compendium)
11
+ end
12
+
13
+ let :schema do
14
+ xsd_exporter.export_schema
15
+ end
16
+
17
+ let :parsed_schema do
18
+ Nokogiri::XML::Schema(schema)
19
+ end
20
+
21
+ context 'The XSD exporter' do
22
+ it 'creates a valid XML Schema Definition' do
23
+ expect {
24
+ parsed_schema
25
+ }.to_not raise_exception
26
+ end
27
+ end
28
+
29
+ context 'The XML exporter' do
30
+ it 'creates valid XML documents according to the schema' do
31
+ compendium.services.each do |name, service|
32
+ xml_export = service.to_xml
33
+
34
+ errors = parsed_schema.validate(Nokogiri::XML::Document.parse(xml_export))
35
+
36
+ expect(errors).to be_empty
37
+ end
38
+ end
39
+ end
40
+
41
+ context 'The RDF exporter' do
42
+ it 'creates valid RDF documents' do
43
+ compendium.services.each do |name, service|
44
+ rdf_export = service.to_rdf
45
+
46
+ RDF::RDFXML::Reader.new(rdf_export, :validate => true) do |reader|
47
+ expect(reader.valid?)
48
+ end
49
+ end
50
+ end
51
+ end
52
+
53
+ context 'The exported files' do
54
+ it 'contains the XSD export' do
55
+ file = Tempfile.new('export.xsd')
56
+ xsd_export = xsd_exporter.export_schema
57
+
58
+ begin
59
+ xsd_exporter.export_schema_to_file file.path
60
+ expect(file.read).to eq xsd_export
61
+ ensure
62
+ file.close
63
+ file.unlink
64
+ end
65
+ end
66
+
67
+ it 'contains XML export' do
68
+ compendium.services.each do |name, service|
69
+ file = Tempfile.new("#{name}.xml")
70
+ xml_export = service.to_xml
71
+
72
+ begin
73
+ SDL::Exporters::XMLServiceExporter.new(compendium).export_service_to_file(service, file.path)
74
+ expect(file.read).to eq xml_export
75
+ ensure
76
+ file.close
77
+ file.unlink
78
+ end
79
+ end
80
+ end
81
+ end
82
+ end
@@ -1,4 +1,3 @@
1
- require_relative '../lib/sdl'
2
1
  require_relative 'spec_helper'
3
2
  require_relative 'shared_test_compendium'
4
3
 
@@ -93,6 +92,16 @@ describe 'Doing type instance definition' do
93
92
  end.to raise_exception
94
93
  end
95
94
 
95
+ it 'raises an error, if a value with an invalid type is given for multi valued properties' do
96
+ expect {
97
+ service = compendium.service(:invalid_service) do
98
+ has_multicolor do
99
+ color "Blue"
100
+ end
101
+ end
102
+ }.to raise_exception
103
+ end
104
+
96
105
  it 'can be done through multiple arguments and associative hashes' do
97
106
  compendium.facts_definition do
98
107
  type :multi do
@@ -158,6 +167,7 @@ describe 'Doing type instance definition' do
158
167
  has_color :yellow, annotation: "Yuck!"
159
168
  end
160
169
 
170
+ expect(compendium.services[:yellow_service].facts[0].annotated?).to eq true
161
171
  expect(compendium.services[:yellow_service].facts[0].annotations).to include("Yuck!")
162
172
  end
163
173
 
@@ -169,6 +179,8 @@ describe 'Doing type instance definition' do
169
179
  end
170
180
  end
171
181
 
182
+ expect(compendium.services[:favourite_service].facts[0].favourites[0].class.list_item?).to eq true
183
+
172
184
  expect(compendium.services[:favourite_service].facts[0].favourites[0].color).to eq(compendium.type_instances[Color][:red])
173
185
  expect(compendium.services[:favourite_service].facts[0].favourites[1].color).to eq(compendium.type_instances[Color][:green])
174
186
  expect(compendium.services[:favourite_service].facts[0].favourites[0].rating).to eq 5
@@ -176,13 +188,151 @@ describe 'Doing type instance definition' do
176
188
  end
177
189
 
178
190
  it 'returns the values of all properties by calling #property_values on a type' do
179
- compendium.service :imaginative_service do
191
+ service = compendium.service :imaginative_service do
180
192
  has_color :yellow, 'Yellow'
181
193
  end
182
194
 
183
- property_values = compendium.services[:imaginative_service].facts[0].property_values
195
+ property_values = service.facts[0].property_values
184
196
 
185
197
  expect(property_values[property_values.keys[0]]).to eq(compendium.type_instances[Color][:yellow])
186
198
  expect(property_values[property_values.keys[1]]).to eq 'Yellow'
187
199
  end
200
+
201
+ it 'can reject empty property values if specifying include_empty' do
202
+ property_values = compendium.service :empty_service do
203
+ has_color
204
+ end.color.property_values(false)
205
+
206
+ expect(property_values).to be_empty
207
+ end
208
+
209
+ it 'returns its service for the parent of facts' do
210
+ red_service = compendium.services[:red_service]
211
+
212
+ expect(red_service.facts[0].parent).to eq red_service
213
+ end
214
+
215
+ it 'returns a parent type or fact for the parent of types' do
216
+ compendium.facts_definition do
217
+ type :third_level_type
218
+
219
+ type :second_level_type do
220
+ third_level_type
221
+ end
222
+
223
+ type :first_level_type do
224
+ second_level_type
225
+ end
226
+
227
+ fact :fact_with_children do
228
+ first_level_type
229
+ end
230
+
231
+ third_level_type :third
232
+
233
+ second_level_type :second do
234
+ third_level_type :third
235
+ end
236
+
237
+ first_level_type :first do
238
+ second_level_type :second
239
+ end
240
+ end
241
+
242
+ service = compendium.service :service_with_children do
243
+ has_fact_with_children do
244
+ first_level_type :first
245
+ end
246
+ end
247
+
248
+ first_level = service.facts[0].first_level_type
249
+ second_level = first_level.second_level_type
250
+ third_level = second_level.third_level_type
251
+
252
+ expect(third_level.parent).to eq second_level
253
+ expect(second_level.parent).to eq first_level
254
+ end
255
+
256
+ it 'returns nil for #parent_index, if the type is used as value of a single-valued property' do
257
+ new_color = Color.new
258
+
259
+ compendium.service :service_single_value do
260
+ has_color do
261
+ color new_color
262
+ end
263
+ end
264
+
265
+ expect(new_color.parent_index).to eq nil
266
+ end
267
+
268
+ it 'returns the index of a value in a multi-valued property when giving a compatible value for the list' do
269
+ first_color = Color.new
270
+ second_color = Color.new
271
+
272
+ compendium.service :service_multi_value do
273
+ has_multicolor do
274
+ color first_color
275
+ color second_color
276
+ end
277
+ end
278
+
279
+ expect(first_color.parent_index).to eq 0
280
+ expect(second_color.parent_index).to eq 1
281
+ end
282
+
283
+ it 'returns the index of a value in a multi-valued property when specifying a list value' do
284
+ service = compendium.service :service_multi_value do
285
+ has_multicolor do
286
+ color do
287
+ hex_value '#123'
288
+ end
289
+
290
+ color do
291
+ hex_value '#456'
292
+ end
293
+ end
294
+ end
295
+
296
+ expect(service.multicolor.colors[0].parent_index).to eq 0
297
+ expect(service.multicolor.colors[1].parent_index).to eq 1
298
+ end
299
+
300
+ it 'does not set #parent_index if given a predefined type' do
301
+ service = compendium.service :service_multi_value_predefined_type do
302
+ color red
303
+ end
304
+
305
+ expect(compendium.type_instances[Color][:red].parent_index).to eq nil
306
+ end
307
+
308
+ it 'returns the index of a fact instance in relation to its fact class' do
309
+ service = compendium.service :service_multiple_facts do
310
+ has_color :red
311
+ has_color :green
312
+ has_color :blue
313
+
314
+ has_multicolor
315
+ end
316
+
317
+ expect(service.colors[0].parent_index).to eq 0
318
+ expect(service.colors[1].parent_index).to eq 1
319
+ expect(service.colors[2].parent_index).to eq 2
320
+ expect(service.multicolors[0].parent_index).to eq 0
321
+ end
322
+
323
+ it 'marks fact types with multiple properties as multi_property?' do
324
+ compendium.fact_classes.each do |fact_class|
325
+ if fact_class.properties.count > 1
326
+ expect(fact_class.multi_property?).to eq true
327
+ end
328
+ end
329
+ end
330
+
331
+ it 'raises an error, if it gets the wrong type of property value' do
332
+ expect {
333
+ service = compendium.service :simple_service do
334
+ name Object.new
335
+ end
336
+ }.to raise_exception
337
+ end
188
338
  end
@@ -0,0 +1,54 @@
1
+ require_relative 'spec_helper'
2
+
3
+ require 'rspec'
4
+
5
+ describe 'The nokogiri helper' do
6
+ # Mocks also open()
7
+ let :nokogiri_utils do
8
+ util_object = Object.new
9
+
10
+ util_object.class_eval do
11
+ include SDL::Util::NokogiriUtils
12
+
13
+ def open(url)
14
+ if(url.eql? 'http://www.shared-test-html.html/test')
15
+ Kernel.open(File.join(__dir__, 'shared_test_html.html'))
16
+ else
17
+ raise SocketError.new
18
+ end
19
+ end
20
+ end
21
+
22
+ util_object
23
+ end
24
+
25
+ let :html_root do
26
+ nokogiri_utils.fetch_from_url 'http://www.shared-test-html.html/test', 'html'
27
+ end
28
+
29
+ let :first_link do
30
+ html_root.search('a[@href]')[0]
31
+ end
32
+
33
+ it 'can open and process html content' do
34
+ expect {
35
+ html_root
36
+ }.to_not raise_exception
37
+ end
38
+
39
+ it 'can rewrite relative to absolute links' do
40
+ expect(first_link[:href]).to eq 'http://www.shared-test-html.html/test'
41
+ end
42
+
43
+ it 'can rewrite link targets' do
44
+ expect(first_link[:target]).to eq '_new'
45
+ end
46
+
47
+ it 'silently ignores invalid link urls' do
48
+ expect(html_root.search('a[@href]')[1][:href]).to eq '::#::'
49
+ end
50
+
51
+ it 'returns an empty array if getting an SocketError' do
52
+ expect(nokogiri_utils.fetch_from_url('http://SocketError', 'html')).to eq []
53
+ end
54
+ end
@@ -1,4 +1,3 @@
1
- require_relative '../lib/sdl'
2
1
  require_relative 'spec_helper'
3
2
 
4
3
  require 'rspec'
@@ -13,6 +12,9 @@ describe 'The definition of properties' do
13
12
 
14
13
  defined_property = subject.klass.properties.first
15
14
 
15
+ expect(subject.klass.single_property?).to eq true
16
+ expect(subject.klass.single_property).to eq defined_property
17
+
16
18
  expect(defined_property.name).to eq "my_string_property"
17
19
  expect(defined_property.type).to be SDL::Types::SDLString
18
20
  expect(defined_property.multi).to be_false
@@ -1,4 +1,3 @@
1
- require_relative '../lib/sdl'
2
1
  require_relative 'spec_helper'
3
2
 
4
3
  require 'rspec'
@@ -18,6 +17,7 @@ describe 'The service compendium' do
18
17
  subject.fact :has_example_fact
19
18
 
20
19
  expect(subject.fact_classes.first).to be < SDL::Base::Fact
20
+ expect(subject.fact_classes.first.propertyless?).to eq true
21
21
  end
22
22
 
23
23
  it 'allows the definition of fact subclasses' do
@@ -1,4 +1,3 @@
1
- require_relative '../lib/sdl'
2
1
  require_relative 'spec_helper'
3
2
 
4
3
  require 'rspec'
@@ -21,29 +20,28 @@ describe 'The definition of services' do
21
20
  end
22
21
 
23
22
  context 'the addition of a Fact instance' do
24
- after(:all) do
25
- example_service = example_compendium.services[:example_service]
26
- example_fact_instance = example_service.facts.first
27
-
28
- expect(example_fact_instance).to be_a(example_compendium.fact_classes.first)
29
- end
30
-
31
23
  it 'allows the addition of a Fact instance using has_{Fact class name} syntax' do
32
- example_compendium.service(:example_service) do
24
+ service = example_compendium.service(:example_service_1) do
33
25
  has_fact
34
26
  end
27
+
28
+ expect(service.facts[0]).to be_a(example_compendium.fact_classes.first)
35
29
  end
36
30
 
37
31
  it 'allows the addition of a Fact instance using {Fact class name} syntax' do
38
- example_compendium.service(:example_service) do
32
+ service = example_compendium.service(:example_service_2) do
39
33
  fact
40
34
  end
35
+
36
+ expect(service.facts[0]).to be_a(example_compendium.fact_classes.first)
41
37
  end
42
38
 
43
39
  it 'allows the addition of a Fact instance using is_{Fact class name past participle} syntax' do
44
- example_compendium.service(:example_service) do
40
+ service = example_compendium.service(:example_service_3) do
45
41
  is_facted
46
42
  end
43
+
44
+ expect(service.facts[0]).to be_a(example_compendium.fact_classes.first)
47
45
  end
48
46
  end
49
47
 
@@ -78,4 +76,28 @@ describe 'The definition of services' do
78
76
 
79
77
  expect(service.my_value.my_value).to eq "ABC"
80
78
  end
79
+
80
+ it 'allows to access all fact instances by their plural name' do
81
+ service = example_compendium.service(:different_service) do
82
+ my_value 'GHI'
83
+ my_value 'JKL'
84
+ my_value 'MNO'
85
+ end
86
+
87
+ expect(service.my_values.join).to eq 'GHIJKLMNO'
88
+ end
89
+
90
+ it 'lets facts be grouped by their fact class' do
91
+ service = example_compendium.service(:another_service) do
92
+ my_value 'ABC'
93
+ my_value 'DEF'
94
+ end
95
+
96
+ fact_class_facts_map = service.fact_class_facts_map
97
+
98
+ expect(fact_class_facts_map.keys[0]).to eq service.facts[0].class
99
+
100
+ expect(fact_class_facts_map[service.facts[0].class][0]).to eq service.facts[0]
101
+ expect(fact_class_facts_map[service.facts[0].class][1]).to eq service.facts[1]
102
+ end
81
103
  end