jsi 0.2.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.yardopts +1 -1
- data/CHANGELOG.md +36 -0
- data/LICENSE.md +613 -0
- data/README.md +153 -52
- data/lib/jsi/base.rb +485 -338
- data/lib/jsi/jsi_coder.rb +24 -18
- data/lib/jsi/metaschema.rb +7 -0
- data/lib/jsi/metaschema_node/bootstrap_schema.rb +100 -0
- data/lib/jsi/metaschema_node.rb +245 -0
- data/lib/jsi/pathed_node.rb +49 -46
- data/lib/jsi/ptr.rb +292 -0
- data/lib/jsi/schema/application/child_application/contains.rb +16 -0
- data/lib/jsi/schema/application/child_application/draft04.rb +22 -0
- data/lib/jsi/schema/application/child_application/draft06.rb +29 -0
- data/lib/jsi/schema/application/child_application/draft07.rb +29 -0
- data/lib/jsi/schema/application/child_application/items.rb +18 -0
- data/lib/jsi/schema/application/child_application/properties.rb +25 -0
- data/lib/jsi/schema/application/child_application.rb +40 -0
- data/lib/jsi/schema/application/draft04.rb +8 -0
- data/lib/jsi/schema/application/draft06.rb +8 -0
- data/lib/jsi/schema/application/draft07.rb +8 -0
- data/lib/jsi/schema/application/inplace_application/dependencies.rb +28 -0
- data/lib/jsi/schema/application/inplace_application/draft04.rb +26 -0
- data/lib/jsi/schema/application/inplace_application/draft06.rb +27 -0
- data/lib/jsi/schema/application/inplace_application/draft07.rb +33 -0
- data/lib/jsi/schema/application/inplace_application/ifthenelse.rb +20 -0
- data/lib/jsi/schema/application/inplace_application/ref.rb +18 -0
- data/lib/jsi/schema/application/inplace_application/someof.rb +29 -0
- data/lib/jsi/schema/application/inplace_application.rb +46 -0
- data/lib/jsi/schema/application.rb +12 -0
- data/lib/jsi/schema/draft04.rb +14 -0
- data/lib/jsi/schema/draft06.rb +14 -0
- data/lib/jsi/schema/draft07.rb +14 -0
- data/lib/jsi/schema/issue.rb +36 -0
- data/lib/jsi/schema/ref.rb +159 -0
- data/lib/jsi/schema/schema_ancestor_node.rb +119 -0
- data/lib/jsi/schema/validation/array.rb +69 -0
- data/lib/jsi/schema/validation/const.rb +20 -0
- data/lib/jsi/schema/validation/contains.rb +25 -0
- data/lib/jsi/schema/validation/core.rb +39 -0
- data/lib/jsi/schema/validation/dependencies.rb +49 -0
- data/lib/jsi/schema/validation/draft04/minmax.rb +91 -0
- data/lib/jsi/schema/validation/draft04.rb +112 -0
- data/lib/jsi/schema/validation/draft06.rb +122 -0
- data/lib/jsi/schema/validation/draft07.rb +159 -0
- data/lib/jsi/schema/validation/enum.rb +25 -0
- data/lib/jsi/schema/validation/ifthenelse.rb +46 -0
- data/lib/jsi/schema/validation/items.rb +54 -0
- data/lib/jsi/schema/validation/not.rb +20 -0
- data/lib/jsi/schema/validation/numeric.rb +121 -0
- data/lib/jsi/schema/validation/object.rb +45 -0
- data/lib/jsi/schema/validation/pattern.rb +34 -0
- data/lib/jsi/schema/validation/properties.rb +101 -0
- data/lib/jsi/schema/validation/property_names.rb +32 -0
- data/lib/jsi/schema/validation/ref.rb +40 -0
- data/lib/jsi/schema/validation/required.rb +27 -0
- data/lib/jsi/schema/validation/someof.rb +90 -0
- data/lib/jsi/schema/validation/string.rb +47 -0
- data/lib/jsi/schema/validation/type.rb +49 -0
- data/lib/jsi/schema/validation.rb +51 -0
- data/lib/jsi/schema.rb +528 -233
- data/lib/jsi/schema_classes.rb +238 -51
- data/lib/jsi/schema_registry.rb +141 -0
- data/lib/jsi/schema_set.rb +141 -0
- data/lib/jsi/simple_wrap.rb +8 -3
- data/lib/jsi/typelike_modules.rb +75 -68
- data/lib/jsi/util/attr_struct.rb +106 -0
- data/lib/jsi/util.rb +167 -64
- data/lib/jsi/validation/error.rb +34 -0
- data/lib/jsi/validation/result.rb +210 -0
- data/lib/jsi/validation.rb +15 -0
- data/lib/jsi/version.rb +3 -1
- data/lib/jsi.rb +72 -9
- data/lib/schemas/json-schema.org/draft-04/schema.rb +12 -0
- data/lib/schemas/json-schema.org/draft-06/schema.rb +12 -0
- data/lib/schemas/json-schema.org/draft-07/schema.rb +12 -0
- data/readme.rb +138 -0
- data/{resources}/schemas/json-schema.org/draft-04/schema.json +149 -0
- data/{resources}/schemas/json-schema.org/draft-06/schema.json +154 -0
- data/{resources}/schemas/json-schema.org/draft-07/schema.json +168 -0
- metadata +80 -107
- data/.simplecov +0 -1
- data/LICENSE.txt +0 -21
- data/Rakefile.rb +0 -9
- data/jsi.gemspec +0 -31
- data/lib/jsi/base/to_rb.rb +0 -126
- data/lib/jsi/json/node.rb +0 -243
- data/lib/jsi/json/pointer.rb +0 -330
- data/lib/jsi/json-schema-fragments.rb +0 -59
- data/lib/jsi/json.rb +0 -8
- data/test/base_array_test.rb +0 -209
- data/test/base_hash_test.rb +0 -204
- data/test/base_test.rb +0 -422
- data/test/jsi_coder_test.rb +0 -85
- data/test/jsi_json_arraynode_test.rb +0 -150
- data/test/jsi_json_hashnode_test.rb +0 -132
- data/test/jsi_json_node_test.rb +0 -310
- data/test/jsi_json_pointer_test.rb +0 -106
- data/test/jsi_test.rb +0 -11
- data/test/jsi_typelike_as_json_test.rb +0 -53
- data/test/schema_test.rb +0 -196
- data/test/spreedly_openapi_test.rb +0 -8
- data/test/test_helper.rb +0 -63
- data/test/util_test.rb +0 -62
data/test/jsi_coder_test.rb
DELETED
@@ -1,85 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
describe JSI::JSICoder do
|
4
|
-
let(:schema) do
|
5
|
-
{properties: {foo: {}, bar: {}}}
|
6
|
-
end
|
7
|
-
let(:schema_instance_class) { JSI.class_for_schema(schema) }
|
8
|
-
let(:options) { {} }
|
9
|
-
let(:schema_instance_json_coder) { JSI::JSICoder.new(schema_instance_class, options) }
|
10
|
-
describe 'json' do
|
11
|
-
describe 'load' do
|
12
|
-
it 'loads nil' do
|
13
|
-
assert_nil(schema_instance_json_coder.load(nil))
|
14
|
-
end
|
15
|
-
it 'loads a hash' do
|
16
|
-
assert_equal(schema_instance_class.new('foo' => 'bar'), schema_instance_json_coder.load({"foo" => "bar"}))
|
17
|
-
end
|
18
|
-
it 'loads something else' do
|
19
|
-
assert_equal(schema_instance_class.new([[]]), schema_instance_json_coder.load([[]]))
|
20
|
-
end
|
21
|
-
describe 'array' do
|
22
|
-
let(:options) { {array: true} }
|
23
|
-
it 'loads an array of hashes' do
|
24
|
-
data = [{"foo" => "bar"}, {"foo" => "baz"}]
|
25
|
-
assert_equal([schema_instance_class.new('foo' => 'bar'), schema_instance_class.new('foo' => 'baz')], schema_instance_json_coder.load(data))
|
26
|
-
end
|
27
|
-
it 'loads an empty array' do
|
28
|
-
assert_equal([], schema_instance_json_coder.load([]))
|
29
|
-
end
|
30
|
-
it 'loads a not an array' do
|
31
|
-
assert_raises(TypeError) do
|
32
|
-
schema_instance_json_coder.load(Object.new)
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
describe 'array schema' do
|
37
|
-
let(:schema) { {items: {properties: {foo: {}, bar: {}}}} }
|
38
|
-
it 'loads an array of hashes' do
|
39
|
-
data = [{"foo" => "bar"}, {"foo" => "baz"}]
|
40
|
-
assert_equal(schema_instance_class.new([{'foo' => 'bar'}, {'foo' => 'baz'}]), schema_instance_json_coder.load(data))
|
41
|
-
end
|
42
|
-
it 'loads an empty array' do
|
43
|
-
assert_equal(schema_instance_class.new([]), schema_instance_json_coder.load([]))
|
44
|
-
end
|
45
|
-
it 'loads a not an array' do
|
46
|
-
instance = Object.new
|
47
|
-
assert_equal(schema_instance_class.new(instance), schema_instance_json_coder.load(instance))
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
describe 'dump' do
|
52
|
-
it 'dumps nil' do
|
53
|
-
assert_nil(schema_instance_json_coder.dump(nil))
|
54
|
-
end
|
55
|
-
it 'dumps a schema_instance_class' do
|
56
|
-
assert_equal({"foo" => "x", "bar" => "y"}, schema_instance_json_coder.dump(schema_instance_class.new(foo: 'x', bar: 'y')))
|
57
|
-
end
|
58
|
-
it 'dumps something else' do
|
59
|
-
assert_raises(TypeError) do
|
60
|
-
schema_instance_json_coder.dump(Object.new)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
it 'dumps some of the keys of a schema_instance_class after loading in a partial one' do
|
64
|
-
schema_instance_class = schema_instance_json_coder.load({'foo' => 'who'})
|
65
|
-
assert_equal({'foo' => 'who'}, schema_instance_json_coder.dump(schema_instance_class))
|
66
|
-
schema_instance_class.bar = 'whar'
|
67
|
-
assert_equal({'foo' => 'who', 'bar' => 'whar'}, schema_instance_json_coder.dump(schema_instance_class))
|
68
|
-
end
|
69
|
-
describe 'array' do
|
70
|
-
let(:options) { {array: true} }
|
71
|
-
it 'dumps an array of schema_instances' do
|
72
|
-
schema_instances = [schema_instance_class.new(foo: 'x', bar: 'y'), schema_instance_class.new(foo: 'z', bar: 'q')]
|
73
|
-
assert_equal([{"foo" => "x", "bar" => "y"}, {"foo" => "z", "bar" => "q"}], schema_instance_json_coder.dump(schema_instances))
|
74
|
-
end
|
75
|
-
end
|
76
|
-
describe 'array schema' do
|
77
|
-
let(:schema) { {items: {properties: {foo: {}, bar: {}}}} }
|
78
|
-
it 'dumps a schema_instance array' do
|
79
|
-
schema_instances = schema_instance_class.new([{foo: 'x', bar: 'y'}, {foo: 'z', bar: 'q'}])
|
80
|
-
assert_equal([{"foo" => "x", "bar" => "y"}, {"foo" => "z", "bar" => "q"}], schema_instance_json_coder.dump(schema_instances))
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
@@ -1,150 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
document_types = [
|
4
|
-
{
|
5
|
-
make_document: -> (d) { d },
|
6
|
-
document: ['a', ['b', 'q'], {'c' => {'d' => 'e'}}],
|
7
|
-
type_desc: 'Array',
|
8
|
-
},
|
9
|
-
{
|
10
|
-
make_document: -> (d) { SortOfArray.new(d) },
|
11
|
-
document: SortOfArray.new(['a', SortOfArray.new(['b', 'q']), SortOfHash.new({'c' => SortOfHash.new({'d' => 'e'})})]),
|
12
|
-
type_desc: 'sort of Array-like',
|
13
|
-
},
|
14
|
-
]
|
15
|
-
document_types.each do |document_type|
|
16
|
-
describe "JSI::JSON::ArrayNode with #{document_type[:type_desc]}" do
|
17
|
-
# document of the node being tested
|
18
|
-
let(:document) { document_type[:document] }
|
19
|
-
# by default the node is the whole document
|
20
|
-
let(:path) { [] }
|
21
|
-
let(:pointer) { JSI::JSON::Pointer.new(path) }
|
22
|
-
# the node being tested
|
23
|
-
let(:node) { JSI::JSON::Node.new_by_type(document, pointer) }
|
24
|
-
|
25
|
-
describe '#[] bad index' do
|
26
|
-
it 'improves TypeError for Array subsript' do
|
27
|
-
err = assert_raises(TypeError) do
|
28
|
-
node[:x]
|
29
|
-
end
|
30
|
-
assert_match(/^subscripting with :x \(Symbol\) from Array. content is: \[.*\]\z/m, err.message)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
describe '#each' do
|
34
|
-
it 'iterates, one argument' do
|
35
|
-
out = []
|
36
|
-
node.each do |arg|
|
37
|
-
out << arg
|
38
|
-
end
|
39
|
-
assert_instance_of(JSI::JSON::ArrayNode, node[1])
|
40
|
-
assert_instance_of(JSI::JSON::HashNode, node[2])
|
41
|
-
assert_equal(['a', node[1], node[2]], out)
|
42
|
-
end
|
43
|
-
it 'returns self' do
|
44
|
-
assert_equal(node.each { }.object_id, node.object_id)
|
45
|
-
end
|
46
|
-
it 'returns an enumerator when called with no block' do
|
47
|
-
enum = node.each
|
48
|
-
assert_instance_of(Enumerator, enum)
|
49
|
-
assert_equal(['a', node[1], node[2]], enum.to_a)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
describe '#to_ary' do
|
53
|
-
it 'returns a Array with Nodes in' do
|
54
|
-
assert_instance_of(Array, node.to_ary)
|
55
|
-
assert_equal(['a', node[1], node[2]], node.to_ary)
|
56
|
-
end
|
57
|
-
end
|
58
|
-
describe '#as_json' do
|
59
|
-
let(:document) { document_type[:make_document].call(['a', 'b']) }
|
60
|
-
it '#as_json' do
|
61
|
-
assert_equal(['a', 'b'], node.as_json)
|
62
|
-
assert_equal(['a', 'b'], node.as_json(some_option: false))
|
63
|
-
end
|
64
|
-
end
|
65
|
-
# these methods just delegate to Array so not going to test excessively
|
66
|
-
describe 'index only methods' do
|
67
|
-
it('#each_index') { assert_equal([0, 1, 2], node.each_index.to_a) }
|
68
|
-
it('#empty?') { assert_equal(false, node.empty?) }
|
69
|
-
it('#length') { assert_equal(3, node.length) }
|
70
|
-
it('#size') { assert_equal(3, node.size) }
|
71
|
-
end
|
72
|
-
describe 'index + element methods' do
|
73
|
-
it('#|') { assert_equal(['a', node[1], node[2], 0], node | [0]) }
|
74
|
-
it('#&') { assert_equal(['a'], node & ['a']) }
|
75
|
-
it('#*') { assert_equal(node.to_a, node * 1) }
|
76
|
-
it('#+') { assert_equal(node.to_a, node + []) }
|
77
|
-
it('#-') { assert_equal([node[1], node[2]], node - ['a']) }
|
78
|
-
it('#<=>') { assert_equal(1, node <=> []) }
|
79
|
-
it('#<=>') { assert_equal(-1, [] <=> node) }
|
80
|
-
require 'abbrev'
|
81
|
-
it('#abbrev') { assert_equal({'a' => 'a'}, JSI::JSON::Node.new_doc(['a']).abbrev) }
|
82
|
-
it('#assoc') { assert_equal(['b', 'q'], node.assoc('b')) }
|
83
|
-
it('#at') { assert_equal('a', node.at(0)) }
|
84
|
-
it('#bsearch') { assert_equal(nil, node.bsearch { false }) }
|
85
|
-
it('#bsearch_index') { assert_equal(nil, node.bsearch_index { false }) } if [].respond_to?(:bsearch_index)
|
86
|
-
it('#combination') { assert_equal([['a'], [node[1]], [node[2]]], node.combination(1).to_a) }
|
87
|
-
it('#count') { assert_equal(1, node.count('a')) }
|
88
|
-
it('#cycle') { assert_equal(node.to_a, node.cycle(1).to_a) }
|
89
|
-
it('#dig') { assert_equal('e', node.dig(2, 'c', 'd')) } if [].respond_to?(:dig)
|
90
|
-
it('#drop') { assert_equal([node[2]], node.drop(2)) }
|
91
|
-
it('#drop_while') { assert_equal([node[1], node[2]], node.drop_while { |e| e == 'a' }) }
|
92
|
-
it('#fetch') { assert_equal('a', node.fetch(0)) }
|
93
|
-
it('#find_index') { assert_equal(0, node.find_index { true }) }
|
94
|
-
it('#first') { assert_equal('a', node.first) }
|
95
|
-
it('#include?') { assert_equal(true, node.include?('a')) }
|
96
|
-
it('#index') { assert_equal(0, node.index('a')) }
|
97
|
-
it('#join') { assert_equal('a b', JSI::JSON::Node.new_doc(['a', 'b']).join(' ')) }
|
98
|
-
it('#last') { assert_equal(node[2], node.last) }
|
99
|
-
it('#pack') { assert_equal(' ', JSI::JSON::Node.new_doc([32]).pack('c')) }
|
100
|
-
it('#permutation') { assert_equal([['a'], [node[1]], [node[2]]], node.permutation(1).to_a) }
|
101
|
-
it('#product') { assert_equal([], node.product([])) }
|
102
|
-
# due to differences in implementation between #assoc and #rassoc, the reason for which
|
103
|
-
# I cannot begin to fathom, assoc works but rassoc does not because rassoc has different
|
104
|
-
# type checking than assoc for the array(like) array elements.
|
105
|
-
# compare:
|
106
|
-
# assoc: https://github.com/ruby/ruby/blob/v2_5_0/array.c#L3780-L3813
|
107
|
-
# rassoc: https://github.com/ruby/ruby/blob/v2_5_0/array.c#L3815-L3847
|
108
|
-
# for this reason, rassoc is NOT defined on Arraylike. it's here with as_json.
|
109
|
-
#
|
110
|
-
# I've never even seen anybody use rassoc. of all the methods to put into the standard library ...
|
111
|
-
it('#rassoc') { assert_equal(['b', 'q'], node.as_json.rassoc('q')) }
|
112
|
-
it('#repeated_combination') { assert_equal([[]], node.repeated_combination(0).to_a) }
|
113
|
-
it('#repeated_permutation') { assert_equal([[]], node.repeated_permutation(0).to_a) }
|
114
|
-
it('#reverse') { assert_equal([node[2], node[1], 'a'], node.reverse) }
|
115
|
-
it('#reverse_each') { assert_equal([node[2], node[1], 'a'], node.reverse_each.to_a) }
|
116
|
-
it('#rindex') { assert_equal(0, node.rindex('a')) }
|
117
|
-
it('#rotate') { assert_equal([node[1], node[2], 'a'], node.rotate) }
|
118
|
-
it('#sample') { assert_equal('a', JSI::JSON::Node.new_doc(['a']).sample) }
|
119
|
-
it('#shelljoin') { assert_equal('a', JSI::JSON::Node.new_doc(['a']).shelljoin) } if [].respond_to?(:shelljoin)
|
120
|
-
it('#shuffle') { assert_equal(3, node.shuffle.size) }
|
121
|
-
it('#slice') { assert_equal(['a'], node.slice(0, 1)) }
|
122
|
-
it('#sort') { assert_equal(['a'], JSI::JSON::Node.new_doc(['a']).sort) }
|
123
|
-
it('#take') { assert_equal(['a'], node.take(1)) }
|
124
|
-
it('#take_while') { assert_equal([], node.take_while { false }) }
|
125
|
-
it('#transpose') { assert_equal([], JSI::JSON::Node.new_doc([]).transpose) }
|
126
|
-
it('#uniq') { assert_equal(node.to_a, node.uniq) }
|
127
|
-
it('#values_at') { assert_equal(['a'], node.values_at(0)) }
|
128
|
-
it('#zip') { assert_equal([['a', 'a'], [node[1], node[1]], [node[2], node[2]]], node.zip(node)) }
|
129
|
-
end
|
130
|
-
describe 'modified copy methods' do
|
131
|
-
it('#reject') { assert_equal(JSI::JSON::Node.new_doc(['a']), node.reject { |e| e != 'a' }) }
|
132
|
-
it('#select') { assert_equal(JSI::JSON::Node.new_doc(['a']), node.select { |e| e == 'a' }) }
|
133
|
-
it('#compact') { assert_equal(JSI::JSON::Node.new_doc(node.content.to_ary), node.compact) }
|
134
|
-
describe 'at a depth' do
|
135
|
-
let(:document) { document_type[:make_document].call([['b', 'q'], {'c' => ['d', 'e']}]) }
|
136
|
-
let(:path) { ['1', 'c'] }
|
137
|
-
it('#select') do
|
138
|
-
selected = node.select { |e| e == 'd' }
|
139
|
-
equivalent = JSI::JSON::Node.new_by_type([['b', 'q'], {'c' => ['d']}], pointer)
|
140
|
-
assert_equal(equivalent, selected)
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
144
|
-
JSI::Arraylike::DESTRUCTIVE_METHODS.each do |destructive_method_name|
|
145
|
-
it("does not respond to destructive method #{destructive_method_name}") do
|
146
|
-
assert(!node.respond_to?(destructive_method_name))
|
147
|
-
end
|
148
|
-
end
|
149
|
-
end
|
150
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require_relative 'test_helper'
|
2
|
-
|
3
|
-
document_types = [
|
4
|
-
{
|
5
|
-
make_document: -> (d) { d },
|
6
|
-
document: {'a' => 'b', 'c' => {'d' => 'e'}},
|
7
|
-
type_desc: 'Hash',
|
8
|
-
},
|
9
|
-
{
|
10
|
-
make_document: -> (d) { SortOfHash.new(d) },
|
11
|
-
document: SortOfHash.new({'a' => 'b', 'c' => SortOfHash.new({'d' => 'e'})}),
|
12
|
-
type_desc: 'sort of Hash-like',
|
13
|
-
},
|
14
|
-
]
|
15
|
-
document_types.each do |document_type|
|
16
|
-
describe "JSI::JSON::HashNode with #{document_type[:type_desc]}" do
|
17
|
-
# document of the node being tested
|
18
|
-
let(:document) { document_type[:document] }
|
19
|
-
# by default the node is the whole document
|
20
|
-
let(:path) { [] }
|
21
|
-
let(:pointer) { JSI::JSON::Pointer.new(path) }
|
22
|
-
# the node being tested
|
23
|
-
let(:node) { JSI::JSON::Node.new_by_type(document, pointer) }
|
24
|
-
|
25
|
-
describe '#each' do
|
26
|
-
it 'iterates, one argument' do
|
27
|
-
out = []
|
28
|
-
node.each do |arg|
|
29
|
-
out << arg
|
30
|
-
end
|
31
|
-
assert_instance_of(JSI::JSON::HashNode, node['c'])
|
32
|
-
assert_equal([['a', 'b'], ['c', node['c']]], out)
|
33
|
-
end
|
34
|
-
it 'iterates, two arguments' do
|
35
|
-
out = []
|
36
|
-
node.each do |k, v|
|
37
|
-
out << [k, v]
|
38
|
-
end
|
39
|
-
assert_instance_of(JSI::JSON::HashNode, node['c'])
|
40
|
-
assert_equal([['a', 'b'], ['c', node['c']]], out)
|
41
|
-
end
|
42
|
-
it 'returns self' do
|
43
|
-
assert_equal(node.each { }.object_id, node.object_id)
|
44
|
-
end
|
45
|
-
it 'returns an enumerator when called with no block' do
|
46
|
-
enum = node.each
|
47
|
-
assert_instance_of(Enumerator, enum)
|
48
|
-
assert_equal([['a', 'b'], ['c', node['c']]], enum.to_a)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
describe '#to_hash' do
|
52
|
-
it 'returns a Hash with Nodes in' do
|
53
|
-
assert_instance_of(Hash, node.to_hash)
|
54
|
-
assert_equal({'a' => 'b', 'c' => node['c']}, node.to_hash)
|
55
|
-
end
|
56
|
-
end
|
57
|
-
describe '#merge' do
|
58
|
-
let(:document) { document_type[:make_document].call({'a' => {'b' => 0}, 'c' => {'d' => 'e'}}) }
|
59
|
-
# testing the node at 'c' here, merging a hash at a path within a document.
|
60
|
-
let(:path) { ['c'] }
|
61
|
-
it 'merges' do
|
62
|
-
merged = node.merge('x' => 'y')
|
63
|
-
# check the content at 'c' was merged with the remainder of the document intact (at 'a')
|
64
|
-
assert_equal({'a' => {'b' => 0}, 'c' => {'d' => 'e', 'x' => 'y'}}, merged.document)
|
65
|
-
# check the original node retains its original document
|
66
|
-
assert_equal(document_type[:make_document].call({'a' => {'b' => 0}, 'c' => {'d' => 'e'}}), node.document)
|
67
|
-
# check that unnecessary copies of unaffected parts of the document were not made
|
68
|
-
assert_equal(node.document.to_hash['a'].object_id, merged.document['a'].object_id)
|
69
|
-
end
|
70
|
-
end
|
71
|
-
describe '#as_json' do
|
72
|
-
let(:document) { document_type[:make_document].call({'a' => 'b'}) }
|
73
|
-
it '#as_json' do
|
74
|
-
assert_equal({'a' => 'b'}, node.as_json)
|
75
|
-
assert_equal({'a' => 'b'}, node.as_json(this_option: 'what?'))
|
76
|
-
end
|
77
|
-
end
|
78
|
-
# these methods just delegate to Hash so not going to test excessively
|
79
|
-
describe 'key only methods' do
|
80
|
-
it('#each_key') { assert_equal(['a', 'c'], node.each_key.to_a) }
|
81
|
-
it('#empty?') { assert_equal(false, node.empty?) }
|
82
|
-
it('#has_key?') { assert_equal(true, node.has_key?('a')) }
|
83
|
-
it('#include?') { assert_equal(false, node.include?('q')) }
|
84
|
-
it('#key?') { assert_equal(true, node.key?('c')) }
|
85
|
-
it('#keys') { assert_equal(['a', 'c'], node.keys) }
|
86
|
-
it('#length') { assert_equal(2, node.length) }
|
87
|
-
it('#member?') { assert_equal(false, node.member?(0)) }
|
88
|
-
it('#size') { assert_equal(2, node.size) }
|
89
|
-
end
|
90
|
-
describe 'key + value methods' do
|
91
|
-
it('#<') { assert_equal(true, node < {'a' => 'b', 'c' => node['c'], 'x' => 'y'}) } if {}.respond_to?(:<)
|
92
|
-
it('#<=') { assert_equal(true, node <= node) } if {}.respond_to?(:<=)
|
93
|
-
it('#>') { assert_equal(true, node > {}) } if {}.respond_to?(:>)
|
94
|
-
it('#>=') { assert_equal(false, node >= {'foo' => 'bar'}) } if {}.respond_to?(:>=)
|
95
|
-
it('#any?') { assert_equal(false, node.any? { |k, v| v == 3 }) }
|
96
|
-
it('#assoc') { assert_equal(['a', 'b'], node.assoc('a')) }
|
97
|
-
it('#dig') { assert_equal('e', node.dig('c', 'd')) } if {}.respond_to?(:dig)
|
98
|
-
it('#each_pair') { assert_equal([['a', 'b'], ['c', node['c']]], node.each_pair.to_a) }
|
99
|
-
it('#each_value') { assert_equal(['b', node['c']], node.each_value.to_a) }
|
100
|
-
it('#fetch') { assert_equal('b', node.fetch('a')) }
|
101
|
-
it('#fetch_values') { assert_equal(['b'], node.fetch_values('a')) } if {}.respond_to?(:fetch_values)
|
102
|
-
it('#has_value?') { assert_equal(true, node.has_value?('b')) }
|
103
|
-
it('#invert') { assert_equal({'b' => 'a', node['c'] => 'c'}, node.invert) }
|
104
|
-
it('#key') { assert_equal('a', node.key('b')) }
|
105
|
-
it('#rassoc') { assert_equal(['a', 'b'], node.rassoc('b')) }
|
106
|
-
it('#to_h') { assert_equal({'a' => 'b', 'c' => node['c']}, node.to_h) }
|
107
|
-
it('#to_proc') { assert_equal('b', node.to_proc.call('a')) } if {}.respond_to?(:to_proc)
|
108
|
-
if {}.respond_to?(:transform_values)
|
109
|
-
it('#transform_values') { assert_equal({'a' => nil, 'c' => nil}, node.transform_values { |_| nil }) }
|
110
|
-
end
|
111
|
-
it('#value?') { assert_equal(false, node.value?('0')) }
|
112
|
-
it('#values') { assert_equal(['b', node['c']], node.values) }
|
113
|
-
it('#values_at') { assert_equal(['b'], node.values_at('a')) }
|
114
|
-
end
|
115
|
-
describe 'modified copy methods' do
|
116
|
-
# I'm going to rely on the #merge test above to test the modified copy functionality and just do basic
|
117
|
-
# tests of all the modified copy methods here
|
118
|
-
it('#merge') { assert_equal(JSI::JSON::Node.new_doc(node.content), node.merge({})) }
|
119
|
-
it('#reject') { assert_equal(JSI::JSON::Node.new_doc({}), node.reject { true }) }
|
120
|
-
it('#select') { assert_equal(JSI::JSON::Node.new_doc({}), node.select { false }) }
|
121
|
-
# Hash#compact only available as of ruby 2.5.0
|
122
|
-
if {}.respond_to?(:compact)
|
123
|
-
it('#compact') { assert_equal(JSI::JSON::Node.new_doc({"a" => "b", "c" => node.content.to_hash["c"]}), node.compact) }
|
124
|
-
end
|
125
|
-
end
|
126
|
-
JSI::Hashlike::DESTRUCTIVE_METHODS.each do |destructive_method_name|
|
127
|
-
it("does not respond to destructive method #{destructive_method_name}") do
|
128
|
-
assert(!node.respond_to?(destructive_method_name))
|
129
|
-
end
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|