jsi 0.3.0 → 0.4.0

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,3 +1,3 @@
1
1
  module JSI
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
@@ -9,10 +9,10 @@ base = {
9
9
  {'type' => 'array', 'items' => {}},
10
10
  ],
11
11
  }
12
- NamedArrayInstance = JSI.class_for_schema(base)
13
- NamedIdArrayInstance = JSI.class_for_schema({'$id' => 'https://schemas.jsi.unth.net/test/base/named_array_schema'}.merge(base))
12
+ NamedArrayInstance = JSI::Schema.new(base).jsi_schema_class
13
+ NamedIdArrayInstance = JSI::Schema.new({'$id' => 'https://schemas.jsi.unth.net/test/base/named_array_schema'}.merge(base)).jsi_schema_class
14
14
 
15
- describe JSI::BaseArray do
15
+ describe 'JSI::Base array' do
16
16
  let(:instance) { ['foo', {'lamp' => [3]}, ['q', 'r'], {'four' => 4}] }
17
17
  let(:schema_content) do
18
18
  {
@@ -181,43 +181,43 @@ describe JSI::BaseArray do
181
181
  end
182
182
  end
183
183
  describe '#inspect with id' do
184
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
184
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
185
185
  let(:subject) { schema.new_jsi(instance) }
186
186
  it 'inspects' do
187
- assert_equal("#[<JSI (https://schemas.jsi.unth.net/test/withid#)> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
187
+ assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#)> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
188
188
  end
189
189
  end
190
190
  describe '#pretty_print with id' do
191
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
191
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
192
192
  let(:subject) { schema.new_jsi(instance) }
193
- it 'inspects' do
194
- assert_equal("#[<JSI (https://schemas.jsi.unth.net/test/withid#)>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/test/withid#/items/1)> \"lamp\" => [3]},\n #[<JSI (https://schemas.jsi.unth.net/test/withid#/items/2)> \"q\", \"r\"],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
193
+ it 'pretty prints' do
194
+ assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#)>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
195
195
  end
196
196
  end
197
197
  describe '#inspect with id SortOfArray' do
198
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
198
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
199
199
  let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
200
200
  it 'inspects' do
201
- assert_equal("#[<JSI (https://schemas.jsi.unth.net/test/withid#) SortOfArray> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
201
+ assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#) SortOfArray> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
202
202
  end
203
203
  end
204
204
  describe '#pretty_print with id SortOfArray' do
205
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
205
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_array_test/withid', 'items' => [{}, {}, {}]} }
206
206
  let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
207
207
  it 'inspects' do
208
- assert_equal("#[<JSI (https://schemas.jsi.unth.net/test/withid#) SortOfArray>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/test/withid#/items/1)> \"lamp\" => [3]},\n #[<JSI (https://schemas.jsi.unth.net/test/withid#/items/2)> \"q\", \"r\"],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
208
+ assert_equal("#[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#) SortOfArray>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/base_array_test/withid#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
209
209
  end
210
210
  end
211
211
  describe '#inspect Node' do
212
212
  let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
213
213
  it 'inspects' do
214
- assert_equal("#[<JSI JSI::JSON::ArrayNode fragment=\"#\"> \"foo\", \#{<JSI JSI::JSON::HashNode fragment=\"#/1\"> \"lamp\" => #[<JSI::JSON::ArrayNode fragment=\"#/1/lamp\"> 3]}, #[<JSI JSI::JSON::ArrayNode fragment=\"#/2\"> \"q\", \"r\"], \#{<JSI::JSON::HashNode fragment=\"#/3\"> \"four\" => 4}]", subject.inspect)
214
+ assert_equal("#[<JSI JSI::JSON::ArrayNode #> \"foo\", \#{<JSI JSI::JSON::HashNode #/1> \"lamp\" => #[<JSI::JSON::ArrayNode #/1/lamp> 3]}, #[<JSI JSI::JSON::ArrayNode #/2> \"q\", \"r\"], \#{<JSI::JSON::HashNode #/3> \"four\" => 4}]", subject.inspect)
215
215
  end
216
216
  end
217
217
  describe '#pretty_print Node' do
218
218
  let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
219
219
  it 'pretty_prints' do
220
- assert_equal("#[<JSI JSI::JSON::ArrayNode fragment=\"#\">\n \"foo\",\n \#{<JSI JSI::JSON::HashNode fragment=\"#/1\">\n \"lamp\" => #[<JSI::JSON::ArrayNode fragment=\"#/1/lamp\"> 3]\n },\n #[<JSI JSI::JSON::ArrayNode fragment=\"#/2\"> \"q\", \"r\"],\n \#{<JSI::JSON::HashNode fragment=\"#/3\"> \"four\" => 4}\n]\n", subject.pretty_inspect)
220
+ assert_equal("#[<JSI JSI::JSON::ArrayNode #>\n \"foo\",\n \#{<JSI JSI::JSON::HashNode #/1>\n \"lamp\" => #[<JSI::JSON::ArrayNode #/1/lamp> 3]\n },\n #[<JSI JSI::JSON::ArrayNode #/2> \"q\", \"r\"],\n \#{<JSI::JSON::HashNode #/3> \"four\" => 4}\n]\n", subject.pretty_inspect)
221
221
  end
222
222
  end
223
223
  end
@@ -8,10 +8,10 @@ base = {
8
8
  'bar' => {},
9
9
  },
10
10
  }
11
- NamedHashInstance = JSI.class_for_schema(base)
12
- NamedIdHashInstance = JSI.class_for_schema({'$id' => 'https://schemas.jsi.unth.net/test/base/named_hash_schema'}.merge(base))
11
+ NamedHashInstance = JSI::Schema.new(base).jsi_schema_class
12
+ NamedIdHashInstance = JSI::Schema.new({'$id' => 'https://schemas.jsi.unth.net/test/base/named_hash_schema'}.merge(base)).jsi_schema_class
13
13
 
14
- describe JSI::BaseHash do
14
+ describe 'JSI::Base hash' do
15
15
  let(:instance) { {'foo' => {'x' => 'y'}, 'bar' => [9], 'baz' => [true]} }
16
16
  let(:schema_content) do
17
17
  {
@@ -225,43 +225,43 @@ describe JSI::BaseHash do
225
225
  end
226
226
  end
227
227
  describe '#inspect with id' do
228
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
228
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
229
229
  let(:subject) { schema.new_jsi(instance) }
230
230
  it 'inspects' do
231
- assert_equal("\#{<JSI (https://schemas.jsi.unth.net/test/withid#)> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
231
+ assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#)> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
232
232
  end
233
233
  end
234
234
  describe '#pretty_print with id' do
235
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
235
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
236
236
  let(:subject) { schema.new_jsi(instance) }
237
237
  it 'inspects' do
238
- assert_equal("\#{<JSI (https://schemas.jsi.unth.net/test/withid#)>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/withid#/properties/bar)> 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
238
+ assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#)>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
239
239
  end
240
240
  end
241
241
  describe '#inspect with id SortOfHash' do
242
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
242
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
243
243
  let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
244
244
  it 'inspects' do
245
- assert_equal("\#{<JSI (https://schemas.jsi.unth.net/test/withid#) SortOfHash> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
245
+ assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#) SortOfHash> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
246
246
  end
247
247
  end
248
248
  describe '#pretty_print with id SortOfHash' do
249
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
249
+ let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
250
250
  let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
251
251
  it 'inspects' do
252
- assert_equal("\#{<JSI (https://schemas.jsi.unth.net/test/withid#) SortOfHash>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/withid#/properties/bar)> 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
252
+ assert_equal("\#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#) SortOfHash>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/base_hash_test/withid#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
253
253
  end
254
254
  end
255
255
  describe '#inspect Node' do
256
256
  let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
257
257
  it 'inspects' do
258
- assert_equal("\#{<JSI JSI::JSON::HashNode fragment=\"#\"> \"foo\" => \#{<JSI JSI::JSON::HashNode fragment=\"#/foo\"> \"x\" => \"y\"}, \"bar\" => #[<JSI JSI::JSON::ArrayNode fragment=\"#/bar\"> 9], \"baz\" => #[<JSI::JSON::ArrayNode fragment=\"#/baz\"> true]}", subject.inspect)
258
+ assert_equal("\#{<JSI JSI::JSON::HashNode #> \"foo\" => \#{<JSI JSI::JSON::HashNode #/foo> \"x\" => \"y\"}, \"bar\" => #[<JSI JSI::JSON::ArrayNode #/bar> 9], \"baz\" => #[<JSI::JSON::ArrayNode #/baz> true]}", subject.inspect)
259
259
  end
260
260
  end
261
261
  describe '#pretty_print Node' do
262
262
  let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
263
263
  it 'pretty_prints' do
264
- assert_equal("\#{<JSI JSI::JSON::HashNode fragment=\"#\">\n \"foo\" => \#{<JSI JSI::JSON::HashNode fragment=\"#/foo\"> \"x\" => \"y\"},\n \"bar\" => #[<JSI JSI::JSON::ArrayNode fragment=\"#/bar\"> 9],\n \"baz\" => #[<JSI::JSON::ArrayNode fragment=\"#/baz\"> true]\n}\n", subject.pretty_inspect)
264
+ assert_equal("\#{<JSI JSI::JSON::HashNode #>\n \"foo\" => \#{<JSI JSI::JSON::HashNode #/foo> \"x\" => \"y\"},\n \"bar\" => #[<JSI JSI::JSON::ArrayNode #/bar> 9],\n \"baz\" => #[<JSI::JSON::ArrayNode #/baz> true]\n}\n", subject.pretty_inspect)
265
265
  end
266
266
  end
267
267
  end
@@ -41,14 +41,9 @@ describe JSI::Base do
41
41
  assert_equal('NamedSchemaInstance', NamedSchemaInstance.name)
42
42
  end
43
43
  end
44
- describe 'class for schema .schema' do
45
- it '.schema' do
46
- assert_equal(schema, JSI.class_for_schema(schema).schema)
47
- end
48
- end
49
- describe 'class for schema .schema_id' do
50
- it '.schema_id' do
51
- assert_equal(schema.schema_id, JSI.class_for_schema(schema).schema_id)
44
+ describe 'class for schema .jsi_class_schemas' do
45
+ it '.jsi_class_schemas' do
46
+ assert_equal(Set.new << schema, schema.jsi_schema_class.jsi_class_schemas)
52
47
  end
53
48
  end
54
49
  describe 'module for schema .inspect' do
@@ -61,15 +56,15 @@ describe JSI::Base do
61
56
  assert_equal(schema, JSI::SchemaClasses.module_for_schema(schema).schema)
62
57
  end
63
58
  end
64
- describe '.class_for_schema' do
59
+ describe '.class_for_schemas' do
65
60
  it 'returns a class from a schema' do
66
- class_for_schema = JSI.class_for_schema(schema)
61
+ class_for_schema = JSI.class_for_schemas([schema])
67
62
  # same class every time
68
- assert_equal(JSI.class_for_schema(schema), class_for_schema)
63
+ assert_equal(JSI.class_for_schemas([schema]), class_for_schema)
69
64
  assert_operator(class_for_schema, :<, JSI::Base)
70
65
  end
71
- it 'returns a class from a hash' do
72
- assert_equal(JSI.class_for_schema(schema), JSI.class_for_schema(schema_content))
66
+ it 'returns the same class from a hash' do
67
+ assert_equal(JSI.class_for_schemas([schema]), JSI.class_for_schemas([schema_content]))
73
68
  end
74
69
  end
75
70
  describe 'JSI::SchemaClasses.module_for_schema' do
@@ -86,7 +81,7 @@ describe JSI::Base do
86
81
  describe 'on Base' do
87
82
  it 'errors' do
88
83
  err = assert_raises(TypeError) { JSI::Base.new({}) }
89
- assert_equal('cannot instantiate JSI::Base which has no method #schema. please use JSI.class_for_schema', err.message)
84
+ assert_equal('cannot instantiate JSI::Base which has no method #jsi_schemas. it is recommended to instantiate JSIs from a schema using JSI::Schema#new_jsi.', err.message)
90
85
  end
91
86
  end
92
87
  describe 'nil' do
@@ -375,7 +370,7 @@ describe JSI::Base do
375
370
  describe 'when the instance is not hashlike' do
376
371
  let(:instance) { nil }
377
372
  it 'errors' do
378
- err = assert_raises(NoMethodError) { subject.foo }
373
+ err = assert_raises(JSI::Base::CannotSubscriptError) { subject.foo }
379
374
  assert_equal(%q(cannot subcript (using token: "foo") from instance: nil), err.message)
380
375
  end
381
376
  end
@@ -389,10 +384,10 @@ describe JSI::Base do
389
384
  'inspect' => {}, # Base
390
385
  'pretty_inspect' => {}, # Kernel
391
386
  'as_json' => {}, # Base::OverrideFromExtensions, extended on initialization
392
- 'each' => {}, # BaseHash / BaseArray
387
+ 'each' => {}, # PathedHashNode / PathedArrayNode
393
388
  'instance_exec' => {}, # BasicObject
394
389
  'jsi_instance' => {}, # Base
395
- 'schema' => {}, # module_for_schema singleton definition
390
+ 'jsi_schemas' => {}, # module_for_schema singleton definition
396
391
  },
397
392
  }
398
393
  end
@@ -406,7 +401,7 @@ describe JSI::Base do
406
401
  'each' => 'hi',
407
402
  'instance_exec' => 'hi',
408
403
  'jsi_instance' => 'hi',
409
- 'schema' => 'hi',
404
+ 'jsi_schemas' => 'hi',
410
405
  }
411
406
  end
412
407
  it 'does not define readers' do
@@ -414,14 +409,14 @@ describe JSI::Base do
414
409
 
415
410
  assert_equal(JSI::Base, subject.method(:initialize).owner)
416
411
  assert_equal('hi', subject['initialize'])
417
- assert_equal(%q(#{<JSI> "foo" => "bar", "initialize" => "hi", "inspect" => "hi", "pretty_inspect" => "hi", "as_json" => "hi", "each" => "hi", "instance_exec" => "hi", "jsi_instance" => "hi", "schema" => "hi"}), subject.inspect)
412
+ assert_equal(%q(#{<JSI> "foo" => "bar", "initialize" => "hi", "inspect" => "hi", "pretty_inspect" => "hi", "as_json" => "hi", "each" => "hi", "instance_exec" => "hi", "jsi_instance" => "hi", "jsi_schemas" => "hi"}), subject.inspect)
418
413
  assert_equal('hi', subject['inspect'])
419
- assert_equal(%Q(\#{<JSI>\n "foo" => "bar",\n "initialize" => "hi",\n "inspect" => "hi",\n "pretty_inspect" => "hi",\n "as_json" => "hi",\n "each" => "hi",\n "instance_exec" => "hi",\n "jsi_instance" => "hi",\n "schema" => "hi"\n}\n), subject.pretty_inspect)
414
+ assert_equal(%Q(\#{<JSI>\n "foo" => "bar",\n "initialize" => "hi",\n "inspect" => "hi",\n "pretty_inspect" => "hi",\n "as_json" => "hi",\n "each" => "hi",\n "instance_exec" => "hi",\n "jsi_instance" => "hi",\n "jsi_schemas" => "hi"\n}\n), subject.pretty_inspect)
420
415
  assert_equal(instance, subject.as_json)
421
416
  assert_equal(subject, subject.each { })
422
417
  assert_equal(2, subject.instance_exec { 2 })
423
418
  assert_equal(instance, subject.jsi_instance)
424
- assert_equal(schema, subject.schema)
419
+ assert_equal(Set.new << schema, subject.jsi_schemas)
425
420
  end
426
421
  end
427
422
  end
@@ -478,7 +473,7 @@ describe JSI::Base do
478
473
  end
479
474
  end
480
475
  describe 'equality between different classes of JSI::Base subclasses' do
481
- let(:subject_subclass) { Class.new(JSI.class_for_schema(schema)).new(instance) }
476
+ let(:subject_subclass) { Class.new(schema.jsi_schema_class).new(instance) }
482
477
 
483
478
  it 'considers a Base subclass (class_for_schema) and subsubclass to be equal with the same instance' do
484
479
  assert_equal(subject.hash, subject_subclass.hash)
@@ -1,10 +1,10 @@
1
1
  require_relative 'test_helper'
2
2
 
3
3
  describe JSI::JSICoder do
4
- let(:schema_object) do
4
+ let(:schema_content) do
5
5
  {properties: {foo: {}, bar: {}}}
6
6
  end
7
- let(:schema) { JSI::Schema.new(schema_object) }
7
+ let(:schema) { JSI::Schema.new(schema_content) }
8
8
  let(:options) { {} }
9
9
  let(:schema_instance_json_coder) { JSI::JSICoder.new(schema, options) }
10
10
  describe 'json' do
@@ -34,7 +34,7 @@ describe JSI::JSICoder do
34
34
  end
35
35
  end
36
36
  describe 'array schema' do
37
- let(:schema_object) { {items: {properties: {foo: {}, bar: {}}}} }
37
+ let(:schema_content) { {items: {properties: {foo: {}, bar: {}}}} }
38
38
  it 'loads an array of hashes' do
39
39
  data = [{"foo" => "bar"}, {"foo" => "baz"}]
40
40
  assert_equal(schema.new_jsi([{'foo' => 'bar'}, {'foo' => 'baz'}]), schema_instance_json_coder.load(data))
@@ -74,7 +74,7 @@ describe JSI::JSICoder do
74
74
  end
75
75
  end
76
76
  describe 'array schema' do
77
- let(:schema_object) { {items: {properties: {foo: {}, bar: {}}}} }
77
+ let(:schema_content) { {items: {properties: {foo: {}, bar: {}}}} }
78
78
  it 'dumps a schema_instance array' do
79
79
  schema_instances = schema.new_jsi([{foo: 'x', bar: 'y'}, {foo: 'z', bar: 'q'}])
80
80
  assert_equal([{"foo" => "x", "bar" => "y"}, {"foo" => "z", "bar" => "q"}], schema_instance_json_coder.dump(schema_instances))
@@ -17,7 +17,7 @@ describe JSI::JSON::Node do
17
17
  end
18
18
  it 'initializes, node_document is another Node' do
19
19
  err = assert_raises(TypeError) { JSI::JSON::Node.new(JSI::JSON::Node.new({'a' => 'b'}, node_ptr), node_ptr) }
20
- assert_equal("node_document of a Node should not be another JSI::JSON::Node: #<JSI::JSON::Node fragment=\"#\" {\"a\"=>\"b\"}>", err.message)
20
+ assert_equal("node_document of a Node should not be another JSI::JSON::Node: #<JSI::JSON::Node # {\"a\"=>\"b\"}>", err.message)
21
21
  end
22
22
  end
23
23
  describe 'initialization by .new_by_type' do
@@ -171,7 +171,7 @@ describe JSI::JSON::Node do
171
171
  err = assert_raises(JSI::JSON::Pointer::ReferenceError) do
172
172
  root_from_sub.parent_node
173
173
  end
174
- assert_match(/\Acannot access parent of root pointer: #<JSI::JSON::Pointer/, err.message)
174
+ assert_equal('cannot access parent of root pointer: JSI::JSON::Pointer[]', err.message)
175
175
  end
176
176
  end
177
177
  describe '#modified_copy' do
@@ -214,14 +214,14 @@ describe JSI::JSON::Node do
214
214
  let(:node_document) { {'a' => {'c' => ['d', 'e']}} }
215
215
  let(:path) { ['a'] }
216
216
  it 'inspects' do
217
- assert_equal(%Q(#<JSI::JSON::Node fragment="#/a" {"c"=>["d", "e"]}>), node.inspect)
217
+ assert_equal(%Q(#<JSI::JSON::Node #/a {"c"=>["d", "e"]}>), node.inspect)
218
218
  end
219
219
  end
220
220
  describe '#pretty_print' do
221
221
  let(:node_document) { {'a' => {'c' => ['d', 'e']}} }
222
222
  let(:path) { ['a'] }
223
223
  it 'pretty prints' do
224
- assert_equal(%Q(#<JSI::JSON::Node fragment="#/a" {"c"=>["d", "e"]}>), node.pretty_inspect.chomp)
224
+ assert_equal(%Q(#<JSI::JSON::Node #/a {"c"=>["d", "e"]}>), node.pretty_inspect.chomp)
225
225
  end
226
226
  end
227
227
  describe '#jsi_fingerprint' do
@@ -79,18 +79,14 @@ describe JSI::JSON::Pointer do
79
79
  '#/%20', 7,
80
80
  '#/m~0n', 8,
81
81
  ]
82
- evaluations.each_slice(2) do |fragment, value|
83
- assert_equal(value, JSI::JSON::Pointer.from_fragment(fragment).evaluate(document))
82
+ evaluations.each_slice(2) do |uri, value|
83
+ assert_equal(value, JSI::JSON::Pointer.from_fragment(Addressable::URI.parse(uri).fragment).evaluate(document))
84
84
  end
85
85
  end
86
86
 
87
87
  it 'raises for invalid syntax' do
88
88
  err = assert_raises(JSI::JSON::Pointer::PointerSyntaxError) do
89
- JSI::JSON::Pointer.from_fragment("this does not begin with #").evaluate(document)
90
- end
91
- assert_equal("Invalid fragment syntax in \"this does not begin with #\": fragment must begin with #", err.message)
92
- err = assert_raises(JSI::JSON::Pointer::PointerSyntaxError) do
93
- JSI::JSON::Pointer.from_fragment("#this does not begin with slash").evaluate(document)
89
+ JSI::JSON::Pointer.from_fragment("this does not begin with slash").evaluate(document)
94
90
  end
95
91
  assert_equal("Invalid pointer syntax in \"this does not begin with slash\": pointer must begin with /", err.message)
96
92
  end
@@ -13,7 +13,7 @@ describe JSI::MetaschemaNode do
13
13
  end
14
14
  describe 'json schema draft' do
15
15
  it 'type has a schema' do
16
- assert(JSI::JSONSchemaOrgDraft06.schema.type.schema)
16
+ assert(JSI::JSONSchemaOrgDraft06.schema.type.jsi_schemas.any?)
17
17
  end
18
18
  end
19
19
  end
@@ -22,8 +22,8 @@ describe JSI::Schema do
22
22
  end
23
23
  describe 'as an instance of metaschema' do
24
24
  let(:metaschema_jsi_module) { JSI::JSONSchemaOrgDraft04 }
25
- let(:schema_object) { {'type' => 'array', 'items' => {'description' => 'items!'}} }
26
- let(:schema) { metaschema_jsi_module.new_jsi(schema_object) }
25
+ let(:schema_content) { {'type' => 'array', 'items' => {'description' => 'items!'}} }
26
+ let(:schema) { metaschema_jsi_module.new_jsi(schema_content) }
27
27
  it '#[]' do
28
28
  schema_items = schema['items']
29
29
  assert_is_a(metaschema_jsi_module, schema_items)
@@ -35,8 +35,8 @@ describe JSI::Schema do
35
35
  assert_nil(JSI::Schema.new({}).schema_id)
36
36
  end
37
37
  it 'uses a given id with a fragment' do
38
- schema = JSI::Schema.new({id: 'https://schemas.jsi.unth.net/test/given_id#'})
39
- assert_equal('https://schemas.jsi.unth.net/test/given_id#', schema.schema_id)
38
+ schema = JSI::Schema.new({'id' => 'https://schemas.jsi.unth.net/test/given_id_with_fragment#'})
39
+ assert_equal('https://schemas.jsi.unth.net/test/given_id_with_fragment#', schema.schema_id)
40
40
  end
41
41
  it 'uses a given id (adding a fragment)' do
42
42
  schema = JSI::Schema.new({id: 'https://schemas.jsi.unth.net/test/given_id'})
@@ -44,19 +44,19 @@ describe JSI::Schema do
44
44
  end
45
45
  it 'uses a pointer in the fragment' do
46
46
  schema = JSI::Schema.new({
47
- 'id' => 'https://schemas.jsi.unth.net/test/given_id#',
47
+ 'id' => 'https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#',
48
48
  'properties' => {'foo' => {'type' => 'object'}},
49
49
  })
50
50
  subschema = schema['properties']['foo']
51
- assert_equal('https://schemas.jsi.unth.net/test/given_id#/properties/foo', subschema.schema_id)
51
+ assert_equal('https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#/properties/foo', subschema.schema_id)
52
52
  end
53
53
  it 'uses a pointer in the fragment relative to the fragment of the root' do
54
54
  schema = JSI::Schema.new({
55
- 'id' => 'https://schemas.jsi.unth.net/test/given_id#/notroot',
55
+ 'id' => 'https://schemas.jsi.unth.net/test/id_has_pointer#/notroot',
56
56
  'properties' => {'foo' => {'type' => 'object'}},
57
57
  })
58
58
  subschema = schema['properties']['foo']
59
- assert_equal('https://schemas.jsi.unth.net/test/given_id#/notroot/properties/foo', subschema.schema_id)
59
+ assert_equal('https://schemas.jsi.unth.net/test/id_has_pointer#/notroot/properties/foo', subschema.schema_id)
60
60
  end
61
61
  end
62
62
  describe '#jsi_schema_module' do
@@ -69,73 +69,95 @@ describe JSI::Schema do
69
69
  describe '#jsi_schema_class' do
70
70
  it 'returns the class for the schema' do
71
71
  schema = JSI::Schema.new({'id' => 'https://schemas.jsi.unth.net/test/schema_schema_class'})
72
- assert_equal(JSI.class_for_schema(schema), schema.jsi_schema_class)
72
+ assert_equal(JSI.class_for_schemas([schema]), schema.jsi_schema_class)
73
73
  end
74
74
  end
75
- describe '#subschema_for_property' do
75
+ describe '#subschemas_for_property_name' do
76
76
  let(:schema) do
77
77
  JSI::Schema.new({
78
- properties: {foo: {description: 'foo'}},
79
- patternProperties: {"^ba" => {description: 'ba*'}},
78
+ properties: {
79
+ foo: {description: 'foo'},
80
+ baz: {description: 'baz'},
81
+ },
82
+ patternProperties: {
83
+ "^b" => {description: 'ba*'},
84
+ },
80
85
  additionalProperties: {description: 'whatever'},
81
86
  })
82
87
  end
83
- it 'has no subschema' do
84
- assert_equal(nil, JSI::Schema.new({}).subschema_for_property('no'))
88
+ it 'has no subschemas' do
89
+ assert_empty(JSI::Schema.new({}).subschemas_for_property_name('no'))
85
90
  end
86
91
  it 'has a subschema by property' do
87
- subschema = schema.subschema_for_property('foo')
88
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschema)
89
- assert_equal('foo', subschema['description'])
90
- end
91
- it 'has a subschema by pattern property' do
92
- subschema = schema.subschema_for_property('bar')
93
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschema)
94
- assert_equal('ba*', subschema['description'])
95
- end
96
- it 'has a subschema by additional properties' do
97
- subschema = schema.subschema_for_property('anything')
98
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschema)
99
- assert_equal('whatever', subschema['description'])
92
+ subschemas = schema.subschemas_for_property_name('foo').to_a
93
+ assert_equal(1, subschemas.size)
94
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschemas[0])
95
+ assert_equal('foo', subschemas[0].description)
96
+ end
97
+ it 'has subschemas by patternProperties' do
98
+ subschemas = schema.subschemas_for_property_name('bar').to_a
99
+ assert_equal(1, subschemas.size)
100
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschemas[0])
101
+ assert_equal('ba*', subschemas[0].description)
102
+ end
103
+ it 'has subschemas by properties, patternProperties' do
104
+ subschemas = schema.subschemas_for_property_name('baz').to_a
105
+ assert_equal(2, subschemas.size)
106
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschemas[0])
107
+ assert_equal('baz', subschemas[0].description)
108
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschemas[1])
109
+ assert_equal('ba*', subschemas[1].description)
110
+ end
111
+ it 'has subschemas by additional properties' do
112
+ subschemas = schema.subschemas_for_property_name('anything').to_a
113
+ assert_equal(1, subschemas.size)
114
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, subschemas[0])
115
+ assert_equal('whatever', subschemas[0].description)
100
116
  end
101
117
  end
102
- describe '#subschema_for_index' do
103
- it 'has no subschema' do
104
- assert_equal(nil, JSI::Schema.new({}).subschema_for_index(0))
118
+ describe '#subschemas_for_index' do
119
+ it 'has no subschemas' do
120
+ assert_empty(JSI::Schema.new({}).subschemas_for_index(0))
105
121
  end
106
122
  it 'has a subschema for items' do
107
123
  schema = JSI::Schema.new({
108
124
  items: {description: 'items!'}
109
125
  })
110
- first_subschema = schema.subschema_for_index(0)
111
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, first_subschema)
112
- assert_equal('items!', first_subschema['description'])
113
- last_subschema = schema.subschema_for_index(1)
114
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, last_subschema)
115
- assert_equal('items!', last_subschema['description'])
126
+ first_subschemas = schema.subschemas_for_index(0).to_a
127
+ assert_equal(1, first_subschemas.size)
128
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, first_subschemas[0])
129
+ assert_equal('items!', first_subschemas[0].description)
130
+ last_subschemas = schema.subschemas_for_index(1).to_a
131
+ assert_equal(1, last_subschemas.size)
132
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, last_subschemas[0])
133
+ assert_equal('items!', last_subschemas[0].description)
116
134
  end
117
135
  it 'has a subschema for each item by index' do
118
136
  schema = JSI::Schema.new({
119
137
  items: [{description: 'item one'}, {description: 'item two'}]
120
138
  })
121
- first_subschema = schema.subschema_for_index(0)
122
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, first_subschema)
123
- assert_equal('item one', first_subschema['description'])
124
- last_subschema = schema.subschema_for_index(1)
125
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, last_subschema)
126
- assert_equal('item two', last_subschema['description'])
139
+ first_subschemas = schema.subschemas_for_index(0).to_a
140
+ assert_equal(1, first_subschemas.size)
141
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, first_subschemas[0])
142
+ assert_equal('item one', first_subschemas[0].description)
143
+ last_subschemas = schema.subschemas_for_index(1).to_a
144
+ assert_equal(1, last_subschemas.size)
145
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, last_subschemas[0])
146
+ assert_equal('item two', last_subschemas[0].description)
127
147
  end
128
148
  it 'has a subschema by additional items' do
129
149
  schema = JSI::Schema.new({
130
150
  items: [{description: 'item one'}],
131
151
  additionalItems: {description: "mo' crap"},
132
152
  })
133
- first_subschema = schema.subschema_for_index(0)
134
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, first_subschema)
135
- assert_equal('item one', first_subschema['description'])
136
- last_subschema = schema.subschema_for_index(1)
137
- assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, last_subschema)
138
- assert_equal("mo' crap", last_subschema['description'])
153
+ first_subschemas = schema.subschemas_for_index(0).to_a
154
+ assert_equal(1, first_subschemas.size)
155
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, first_subschemas[0])
156
+ assert_equal('item one', first_subschemas[0].description)
157
+ last_subschemas = schema.subschemas_for_index(1).to_a
158
+ assert_equal(1, last_subschemas.size)
159
+ assert_is_a(JSI::Schema.default_metaschema.jsi_schema_module, last_subschemas[0])
160
+ assert_equal("mo' crap", last_subschemas[0].description)
139
161
  end
140
162
  end
141
163
  describe 'stringification' do