jsi 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +8 -0
- data/LICENSE.md +613 -0
- data/README.md +62 -37
- data/jsi.gemspec +8 -12
- data/lib/jsi.rb +11 -0
- data/lib/jsi/base.rb +196 -258
- data/lib/jsi/base/to_rb.rb +2 -0
- data/lib/jsi/jsi_coder.rb +20 -15
- data/lib/jsi/json-schema-fragments.rb +2 -0
- data/lib/jsi/json.rb +2 -0
- data/lib/jsi/json/node.rb +45 -88
- data/lib/jsi/json/pointer.rb +102 -5
- data/lib/jsi/metaschema.rb +7 -0
- data/lib/jsi/metaschema_node.rb +217 -0
- data/lib/jsi/pathed_node.rb +5 -0
- data/lib/jsi/schema.rb +146 -169
- data/lib/jsi/schema_classes.rb +112 -47
- data/lib/jsi/simple_wrap.rb +8 -3
- data/lib/jsi/typelike_modules.rb +31 -39
- data/lib/jsi/util.rb +27 -47
- data/lib/jsi/version.rb +1 -1
- data/lib/schemas/json-schema.org/draft-04/schema.rb +7 -0
- data/lib/schemas/json-schema.org/draft-06/schema.rb +7 -0
- data/resources/icons/AGPL-3.0.png +0 -0
- data/test/base_array_test.rb +174 -60
- data/test/base_hash_test.rb +179 -46
- data/test/base_test.rb +163 -94
- data/test/jsi_coder_test.rb +14 -14
- data/test/jsi_json_arraynode_test.rb +10 -10
- data/test/jsi_json_hashnode_test.rb +14 -14
- data/test/jsi_json_node_test.rb +83 -136
- data/test/jsi_typelike_as_json_test.rb +1 -1
- data/test/metaschema_node_test.rb +19 -0
- data/test/schema_module_test.rb +21 -0
- data/test/schema_test.rb +40 -50
- data/test/test_helper.rb +35 -3
- data/test/util_test.rb +8 -8
- metadata +24 -16
- data/LICENSE.txt +0 -21
data/lib/jsi/version.rb
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSI
|
4
|
+
schema_id = 'http://json-schema.org/draft-04/schema'
|
5
|
+
schema_content = ::JSON.parse(File.read(::JSON::Validator.validators[schema_id].metaschema))
|
6
|
+
JSONSchemaOrgDraft04 = MetaschemaNode.new(schema_content).jsi_schema_module
|
7
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSI
|
4
|
+
schema_id = 'http://json-schema.org/draft/schema' # I don't know why this is not http://json-schema.org/draft-06/schema
|
5
|
+
schema_content = ::JSON.parse(File.read(::JSON::Validator.validators[schema_id].metaschema))
|
6
|
+
JSONSchemaOrgDraft06 = MetaschemaNode.new(schema_content).jsi_schema_module
|
7
|
+
end
|
Binary file
|
data/test/base_array_test.rb
CHANGED
@@ -1,25 +1,32 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
|
3
|
+
base = {
|
4
|
+
'description' => 'named array schema',
|
5
|
+
'type' => 'array',
|
6
|
+
'items' => [
|
7
|
+
{'type' => 'string'},
|
8
|
+
{'type' => 'object'},
|
9
|
+
{'type' => 'array', 'items' => {}},
|
10
|
+
],
|
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))
|
14
|
+
|
3
15
|
describe JSI::BaseArray do
|
4
|
-
let(:
|
5
|
-
['foo', {'lamp' => [3]}, ['q', 'r']]
|
6
|
-
end
|
7
|
-
let(:path) { [] }
|
8
|
-
let(:pointer) { JSI::JSON::Pointer.new(path) }
|
9
|
-
let(:instance) { JSI::JSON::Node.new_by_type(document, pointer) }
|
16
|
+
let(:instance) { ['foo', {'lamp' => [3]}, ['q', 'r'], {'four' => 4}] }
|
10
17
|
let(:schema_content) do
|
11
18
|
{
|
19
|
+
'description' => 'hash schema',
|
12
20
|
'type' => 'array',
|
13
21
|
'items' => [
|
14
22
|
{'type' => 'string'},
|
15
|
-
{'type' => 'object'
|
16
|
-
{'type' => 'array'},
|
23
|
+
{'type' => 'object'},
|
24
|
+
{'type' => 'array', 'items' => {}},
|
17
25
|
],
|
18
26
|
}
|
19
27
|
end
|
20
28
|
let(:schema) { JSI::Schema.new(schema_content) }
|
21
|
-
let(:
|
22
|
-
let(:subject) { class_for_schema.new(instance) }
|
29
|
+
let(:subject) { schema.new_jsi(instance) }
|
23
30
|
|
24
31
|
describe '#[] with a default that is a basic type' do
|
25
32
|
let(:schema_content) do
|
@@ -29,21 +36,21 @@ describe JSI::BaseArray do
|
|
29
36
|
}
|
30
37
|
end
|
31
38
|
describe 'default value' do
|
32
|
-
let(:
|
39
|
+
let(:instance) { [1] }
|
33
40
|
it 'returns the default value' do
|
34
41
|
assert_equal('foo', subject[2])
|
35
42
|
end
|
36
43
|
end
|
37
44
|
describe 'nondefault value (basic type)' do
|
38
|
-
let(:
|
45
|
+
let(:instance) { ['who'] }
|
39
46
|
it 'returns the nondefault value' do
|
40
47
|
assert_equal('who', subject[0])
|
41
48
|
end
|
42
49
|
end
|
43
50
|
describe 'nondefault value (nonbasic type)' do
|
44
|
-
let(:
|
51
|
+
let(:instance) { [[2]] }
|
45
52
|
it 'returns the nondefault value' do
|
46
|
-
|
53
|
+
assert_is_a(schema.items.jsi_schema_module, subject[0])
|
47
54
|
assert_equal([2], subject[0].as_json)
|
48
55
|
end
|
49
56
|
end
|
@@ -56,22 +63,22 @@ describe JSI::BaseArray do
|
|
56
63
|
}
|
57
64
|
end
|
58
65
|
describe 'default value' do
|
59
|
-
let(:
|
66
|
+
let(:instance) { [{'bar' => 3}] }
|
60
67
|
it 'returns the default value' do
|
61
|
-
|
68
|
+
assert_is_a(schema.items.jsi_schema_module, subject[1])
|
62
69
|
assert_equal({'foo' => 2}, subject[1].as_json)
|
63
70
|
end
|
64
71
|
end
|
65
72
|
describe 'nondefault value (basic type)' do
|
66
|
-
let(:
|
73
|
+
let(:instance) { [true, 'who'] }
|
67
74
|
it 'returns the nondefault value' do
|
68
75
|
assert_equal('who', subject[1])
|
69
76
|
end
|
70
77
|
end
|
71
78
|
describe 'nondefault value (nonbasic type)' do
|
72
|
-
let(:
|
79
|
+
let(:instance) { [true, [2]] }
|
73
80
|
it 'returns the nondefault value' do
|
74
|
-
|
81
|
+
assert_is_a(schema.items.jsi_schema_module, subject[1])
|
75
82
|
assert_equal([2], subject[1].as_json)
|
76
83
|
end
|
77
84
|
end
|
@@ -83,63 +90,173 @@ describe JSI::BaseArray do
|
|
83
90
|
subject[2] = {'y' => 'z'}
|
84
91
|
|
85
92
|
assert_equal({'y' => 'z'}, subject[2].as_json)
|
86
|
-
|
87
|
-
|
93
|
+
assert_is_a(schema.items[2].jsi_schema_module, orig_2)
|
94
|
+
assert_is_a(schema.items[2].jsi_schema_module, subject[2])
|
88
95
|
end
|
89
96
|
it 'modifies the instance, visible to other references to the same instance' do
|
90
|
-
orig_instance = subject.
|
97
|
+
orig_instance = subject.jsi_instance
|
91
98
|
|
92
99
|
subject[2] = {'y' => 'z'}
|
93
100
|
|
94
|
-
assert_equal(orig_instance, subject.
|
95
|
-
assert_equal({'y' => 'z'}, orig_instance[2]
|
96
|
-
assert_equal({'y' => 'z'}, subject.
|
97
|
-
assert_equal(orig_instance.class, subject.
|
101
|
+
assert_equal(orig_instance, subject.jsi_instance)
|
102
|
+
assert_equal({'y' => 'z'}, orig_instance[2])
|
103
|
+
assert_equal({'y' => 'z'}, subject.jsi_instance[2])
|
104
|
+
assert_equal(orig_instance.class, subject.jsi_instance.class)
|
98
105
|
end
|
99
106
|
describe 'when the instance is not arraylike' do
|
100
107
|
let(:instance) { nil }
|
101
108
|
it 'errors' do
|
102
109
|
err = assert_raises(NoMethodError) { subject[2] = 0 }
|
103
|
-
|
110
|
+
assert_equal("cannot assign subcript (using token: 2) to instance: nil", err.message)
|
111
|
+
end
|
112
|
+
end
|
113
|
+
describe '#inspect' do
|
114
|
+
it 'inspects' do
|
115
|
+
assert_equal("#[<JSI> \"foo\", \#{<JSI> \"lamp\" => [3]}, #[<JSI> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
describe '#pretty_print' do
|
119
|
+
it 'pretty_prints' do
|
120
|
+
assert_equal("#[<JSI> \"foo\", \#{<JSI> \"lamp\" => [3]}, #[<JSI> \"q\", \"r\"], {\"four\"=>4}]\n", subject.pretty_inspect)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
describe '#inspect SortOfArray' do
|
124
|
+
let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
|
125
|
+
it 'inspects' do
|
126
|
+
assert_equal("#[<JSI SortOfArray> \"foo\", \#{<JSI> \"lamp\" => [3]}, #[<JSI> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
|
127
|
+
end
|
128
|
+
end
|
129
|
+
describe '#pretty_print SortOfArray' do
|
130
|
+
let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
|
131
|
+
it 'pretty_prints' do
|
132
|
+
assert_equal("#[<JSI SortOfArray>\n \"foo\",\n \#{<JSI> \"lamp\" => [3]},\n #[<JSI> \"q\", \"r\"],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
describe '#inspect named' do
|
136
|
+
let(:subject) { NamedArrayInstance.new(instance) }
|
137
|
+
it 'inspects' do
|
138
|
+
assert_equal("#[<NamedArrayInstance> \"foo\", \#{<JSI> \"lamp\" => [3]}, #[<JSI> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
|
139
|
+
end
|
140
|
+
end
|
141
|
+
describe '#pretty_print named' do
|
142
|
+
let(:subject) { NamedArrayInstance.new(instance) }
|
143
|
+
it 'inspects' do
|
144
|
+
assert_equal("#[<NamedArrayInstance>\n \"foo\",\n \#{<JSI> \"lamp\" => [3]},\n #[<JSI> \"q\", \"r\"],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
describe '#inspect named SortOfArray' do
|
148
|
+
let(:subject) { NamedArrayInstance.new(SortOfArray.new(instance)) }
|
149
|
+
it 'inspects' do
|
150
|
+
assert_equal("#[<NamedArrayInstance SortOfArray> \"foo\", \#{<JSI> \"lamp\" => [3]}, #[<JSI> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
|
151
|
+
end
|
152
|
+
end
|
153
|
+
describe '#pretty_print named SortOfArray' do
|
154
|
+
let(:subject) { NamedArrayInstance.new(SortOfArray.new(instance)) }
|
155
|
+
it 'inspects' do
|
156
|
+
assert_equal("#[<NamedArrayInstance SortOfArray>\n \"foo\",\n \#{<JSI> \"lamp\" => [3]},\n #[<JSI> \"q\", \"r\"],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
describe '#inspect named with id' do
|
160
|
+
let(:subject) { NamedIdArrayInstance.new(instance) }
|
161
|
+
it 'inspects' do
|
162
|
+
assert_equal("#[<NamedIdArrayInstance> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
|
163
|
+
end
|
164
|
+
end
|
165
|
+
describe '#pretty_print named with id' do
|
166
|
+
let(:subject) { NamedIdArrayInstance.new(instance) }
|
167
|
+
it 'inspects' do
|
168
|
+
assert_equal("#[<NamedIdArrayInstance>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
describe '#inspect named with id SortOfArray' do
|
172
|
+
let(:subject) { NamedIdArrayInstance.new(SortOfArray.new(instance)) }
|
173
|
+
it 'inspects' do
|
174
|
+
assert_equal("#[<NamedIdArrayInstance SortOfArray> \"foo\", \#{<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/1)> \"lamp\" => [3]}, #[<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/2)> \"q\", \"r\"], {\"four\"=>4}]", subject.inspect)
|
175
|
+
end
|
176
|
+
end
|
177
|
+
describe '#pretty_print named with id SortOfArray' do
|
178
|
+
let(:subject) { NamedIdArrayInstance.new(SortOfArray.new(instance)) }
|
179
|
+
it 'inspects' do
|
180
|
+
assert_equal("#[<NamedIdArrayInstance SortOfArray>\n \"foo\",\n \#{<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/1)>\n \"lamp\" => [3]\n },\n #[<JSI (https://schemas.jsi.unth.net/test/base/named_array_schema#/items/2)>\n \"q\",\n \"r\"\n ],\n {\"four\"=>4}\n]\n", subject.pretty_inspect)
|
181
|
+
end
|
182
|
+
end
|
183
|
+
describe '#inspect with id' do
|
184
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
|
185
|
+
let(:subject) { schema.new_jsi(instance) }
|
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)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
describe '#pretty_print with id' do
|
191
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
|
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)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
describe '#inspect with id SortOfArray' do
|
198
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
|
199
|
+
let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
|
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)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
describe '#pretty_print with id SortOfArray' do
|
205
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'items' => [{}, {}, {}]} }
|
206
|
+
let(:subject) { schema.new_jsi(SortOfArray.new(instance)) }
|
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)
|
209
|
+
end
|
210
|
+
end
|
211
|
+
describe '#inspect Node' do
|
212
|
+
let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
|
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)
|
215
|
+
end
|
216
|
+
end
|
217
|
+
describe '#pretty_print Node' do
|
218
|
+
let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
|
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)
|
104
221
|
end
|
105
222
|
end
|
106
223
|
end
|
107
224
|
# these methods just delegate to Array so not going to test excessively
|
108
225
|
describe 'index only methods' do
|
109
|
-
it('#each_index') { assert_equal([0, 1, 2], subject.each_index.to_a) }
|
226
|
+
it('#each_index') { assert_equal([0, 1, 2, 3], subject.each_index.to_a) }
|
110
227
|
it('#empty?') { assert_equal(false, subject.empty?) }
|
111
|
-
it('#length') { assert_equal(
|
112
|
-
it('#size') { assert_equal(
|
228
|
+
it('#length') { assert_equal(4, subject.length) }
|
229
|
+
it('#size') { assert_equal(4, subject.size) }
|
113
230
|
end
|
114
231
|
describe 'index + element methods' do
|
115
|
-
it('#|') { assert_equal(['foo', subject[1], subject[2], 0], subject | [0]) }
|
232
|
+
it('#|') { assert_equal(['foo', subject[1], subject[2], subject[3], 0], subject | [0]) }
|
116
233
|
it('#&') { assert_equal(['foo'], subject & ['foo']) }
|
117
234
|
it('#*') { assert_equal(subject.to_a, subject * 1) }
|
118
235
|
it('#+') { assert_equal(subject.to_a, subject + []) }
|
119
|
-
it('#-') { assert_equal([subject[1], subject[2]], subject - ['foo']) }
|
236
|
+
it('#-') { assert_equal([subject[1], subject[2], subject[3]], subject - ['foo']) }
|
120
237
|
it('#<=>') { assert_equal(1, subject <=> []) }
|
121
238
|
it('#<=>') { assert_equal(-1, [] <=> subject) }
|
122
239
|
require 'abbrev'
|
123
|
-
it('#abbrev') { assert_equal({'a' => 'a'},
|
240
|
+
it('#abbrev') { assert_equal({'a' => 'a'}, schema.new_jsi(['a']).abbrev) }
|
124
241
|
it('#assoc') { assert_equal(['q', 'r'], subject.assoc('q')) }
|
125
242
|
it('#at') { assert_equal('foo', subject.at(0)) }
|
126
243
|
it('#bsearch') { assert_equal(nil, subject.bsearch { false }) }
|
127
244
|
it('#bsearch_index') { assert_equal(nil, subject.bsearch_index { false }) } if [].respond_to?(:bsearch_index)
|
128
|
-
it('#combination') { assert_equal([['foo'], [subject[1]], [subject[2]]], subject.combination(1).to_a) }
|
245
|
+
it('#combination') { assert_equal([['foo'], [subject[1]], [subject[2]], [subject[3]]], subject.combination(1).to_a) }
|
129
246
|
it('#count') { assert_equal(1, subject.count('foo')) }
|
130
247
|
it('#cycle') { assert_equal(subject.to_a, subject.cycle(1).to_a) }
|
131
248
|
it('#dig') { assert_equal(3, subject.dig(1, 'lamp', 0)) } if [].respond_to?(:dig)
|
132
|
-
it('#drop') { assert_equal([subject[2]], subject.drop(2)) }
|
133
|
-
it('#drop_while') { assert_equal([subject[1], subject[2]], subject.drop_while { |e| e == 'foo' }) }
|
249
|
+
it('#drop') { assert_equal([subject[2], subject[3]], subject.drop(2)) }
|
250
|
+
it('#drop_while') { assert_equal([subject[1], subject[2], subject[3]], subject.drop_while { |e| e == 'foo' }) }
|
134
251
|
it('#fetch') { assert_equal('foo', subject.fetch(0)) }
|
135
252
|
it('#find_index') { assert_equal(0, subject.find_index { true }) }
|
136
253
|
it('#first') { assert_equal('foo', subject.first) }
|
137
254
|
it('#include?') { assert_equal(true, subject.include?('foo')) }
|
138
255
|
it('#index') { assert_equal(0, subject.index('foo')) }
|
139
|
-
it('#join') { assert_equal('a b',
|
140
|
-
it('#last') { assert_equal(subject[
|
141
|
-
it('#pack') { assert_equal(' ',
|
142
|
-
it('#permutation') { assert_equal([['foo'], [subject[1]], [subject[2]]], subject.permutation(1).to_a) }
|
256
|
+
it('#join') { assert_equal('a b', schema.new_jsi(['a', 'b']).join(' ')) }
|
257
|
+
it('#last') { assert_equal(subject[3], subject.last) }
|
258
|
+
it('#pack') { assert_equal(' ', schema.new_jsi([32]).pack('c')) }
|
259
|
+
it('#permutation') { assert_equal([['foo'], [subject[1]], [subject[2]], [subject[3]]], subject.permutation(1).to_a) }
|
143
260
|
it('#product') { assert_equal([], subject.product([])) }
|
144
261
|
# due to differences in implementation between #assoc and #rassoc, the reason for which
|
145
262
|
# I cannot begin to fathom, assoc works but rassoc does not because rassoc has different
|
@@ -147,25 +264,25 @@ describe JSI::BaseArray do
|
|
147
264
|
# compare:
|
148
265
|
# assoc: https://github.com/ruby/ruby/blob/v2_5_0/array.c#L3780-L3813
|
149
266
|
# rassoc: https://github.com/ruby/ruby/blob/v2_5_0/array.c#L3815-L3847
|
150
|
-
# for this reason, rassoc is NOT defined on Arraylike and #
|
151
|
-
it('#rassoc') { assert_equal(['q', 'r'], subject.
|
267
|
+
# for this reason, rassoc is NOT defined on Arraylike and we call #jsi_instance to use it.
|
268
|
+
it('#rassoc') { assert_equal(['q', 'r'], subject.jsi_instance.rassoc('r')) }
|
152
269
|
it('#repeated_combination') { assert_equal([[]], subject.repeated_combination(0).to_a) }
|
153
270
|
it('#repeated_permutation') { assert_equal([[]], subject.repeated_permutation(0).to_a) }
|
154
|
-
it('#reverse') { assert_equal([subject[2], subject[1], 'foo'], subject.reverse) }
|
155
|
-
it('#reverse_each') { assert_equal([subject[2], subject[1], 'foo'], subject.reverse_each.to_a) }
|
271
|
+
it('#reverse') { assert_equal([subject[3], subject[2], subject[1], 'foo'], subject.reverse) }
|
272
|
+
it('#reverse_each') { assert_equal([subject[3], subject[2], subject[1], 'foo'], subject.reverse_each.to_a) }
|
156
273
|
it('#rindex') { assert_equal(0, subject.rindex('foo')) }
|
157
|
-
it('#rotate') { assert_equal([subject[1], subject[2], 'foo'], subject.rotate) }
|
158
|
-
it('#sample') { assert_equal('a',
|
159
|
-
it('#shelljoin') { assert_equal('a',
|
160
|
-
it('#shuffle') { assert_equal(
|
274
|
+
it('#rotate') { assert_equal([subject[1], subject[2], subject[3], 'foo'], subject.rotate) }
|
275
|
+
it('#sample') { assert_equal('a', schema.new_jsi(['a']).sample) }
|
276
|
+
it('#shelljoin') { assert_equal('a', schema.new_jsi(['a']).shelljoin) } if [].respond_to?(:shelljoin)
|
277
|
+
it('#shuffle') { assert_equal(4, subject.shuffle.size) }
|
161
278
|
it('#slice') { assert_equal(['foo'], subject.slice(0, 1)) }
|
162
|
-
it('#sort') { assert_equal(['a'],
|
279
|
+
it('#sort') { assert_equal(['a'], schema.new_jsi(['a']).sort) }
|
163
280
|
it('#take') { assert_equal(['foo'], subject.take(1)) }
|
164
281
|
it('#take_while') { assert_equal([], subject.take_while { false }) }
|
165
|
-
it('#transpose') { assert_equal([],
|
282
|
+
it('#transpose') { assert_equal([], schema.new_jsi([]).transpose) }
|
166
283
|
it('#uniq') { assert_equal(subject.to_a, subject.uniq) }
|
167
284
|
it('#values_at') { assert_equal(['foo'], subject.values_at(0)) }
|
168
|
-
it('#zip') { assert_equal([['foo', 'foo'], [subject[1], subject[1]], [subject[2], subject[2]]], subject.zip(subject)) }
|
285
|
+
it('#zip') { assert_equal([['foo', 'foo'], [subject[1], subject[1]], [subject[2], subject[2]], [subject[3], subject[3]]], subject.zip(subject)) }
|
169
286
|
end
|
170
287
|
describe 'with an instance that has to_ary but not other ary instance methods' do
|
171
288
|
let(:instance) { SortOfArray.new(['foo', {'lamp' => SortOfArray.new([3])}, SortOfArray.new(['q', 'r'])]) }
|
@@ -174,30 +291,27 @@ describe JSI::BaseArray do
|
|
174
291
|
it('#size') { assert_equal(3, subject.size) }
|
175
292
|
it('#count') { assert_equal(1, subject.count('foo')) }
|
176
293
|
it('#slice') { assert_equal(['foo'], subject.slice(0, 1)) }
|
177
|
-
it('#[]') { assert_equal(SortOfArray.new(['q', 'r']), subject[2].
|
294
|
+
it('#[]') { assert_equal(SortOfArray.new(['q', 'r']), subject[2].jsi_instance) }
|
178
295
|
it('#as_json') { assert_equal(['foo', {'lamp' => [3]}, ['q', 'r']], subject.as_json) }
|
179
296
|
end
|
180
297
|
end
|
181
298
|
describe 'modified copy methods' do
|
182
|
-
it('#reject') { assert_equal(
|
299
|
+
it('#reject') { assert_equal(schema.new_jsi(['foo']), subject.reject { |e| e != 'foo' }) }
|
183
300
|
it('#reject block var') do
|
184
301
|
subj_a = subject.to_a
|
185
302
|
subject.reject { |e| assert_equal(e, subj_a.shift) }
|
186
303
|
end
|
187
|
-
it('#select') { assert_equal(
|
304
|
+
it('#select') { assert_equal(schema.new_jsi(['foo']), subject.select { |e| e == 'foo' }) }
|
188
305
|
it('#select block var') do
|
189
306
|
subj_a = subject.to_a
|
190
307
|
subject.select { |e| assert_equal(e, subj_a.shift) }
|
191
308
|
end
|
192
309
|
it('#compact') { assert_equal(subject, subject.compact) }
|
193
310
|
describe 'at a depth' do
|
194
|
-
let(:document) { [['b', 'q'], {'c' => ['d', 'e']}] }
|
195
|
-
let(:path) { ['1', 'c'] }
|
196
311
|
it('#select') do
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
assert_equal(equivalent, selected)
|
312
|
+
expected = schema.new_jsi(['foo', {'lamp' => [3]}, ['r'], {'four' => 4}])[2]
|
313
|
+
actual = subject[2].select { |e| e == 'r' }
|
314
|
+
assert_equal(expected, actual)
|
201
315
|
end
|
202
316
|
end
|
203
317
|
end
|
data/test/base_hash_test.rb
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
require_relative 'test_helper'
|
2
2
|
|
3
|
+
base = {
|
4
|
+
'description' => 'named hash schema',
|
5
|
+
'type' => 'object',
|
6
|
+
'properties' => {
|
7
|
+
'foo' => {'type' => 'object'},
|
8
|
+
'bar' => {},
|
9
|
+
},
|
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))
|
13
|
+
|
3
14
|
describe JSI::BaseHash do
|
4
|
-
let(:
|
5
|
-
{'foo' => {'x' => 'y'}, 'bar' => [9], 'baz' => true}
|
6
|
-
end
|
7
|
-
let(:path) { [] }
|
8
|
-
let(:pointer) { JSI::JSON::Pointer.new(path) }
|
9
|
-
let(:instance) { JSI::JSON::Node.new_by_type(document, pointer) }
|
15
|
+
let(:instance) { {'foo' => {'x' => 'y'}, 'bar' => [9], 'baz' => [true]} }
|
10
16
|
let(:schema_content) do
|
11
17
|
{
|
18
|
+
'description' => 'hash schema',
|
12
19
|
'type' => 'object',
|
13
20
|
'properties' => {
|
14
21
|
'foo' => {'type' => 'object'},
|
@@ -17,10 +24,9 @@ describe JSI::BaseHash do
|
|
17
24
|
}
|
18
25
|
end
|
19
26
|
let(:schema) { JSI::Schema.new(schema_content) }
|
20
|
-
let(:
|
21
|
-
let(:subject) { class_for_schema.new(instance) }
|
27
|
+
let(:subject) { schema.new_jsi(instance) }
|
22
28
|
|
23
|
-
describe '#[] with a default that is a basic type' do
|
29
|
+
describe '#[] with a schema default that is a basic type' do
|
24
30
|
let(:schema_content) do
|
25
31
|
{
|
26
32
|
'type' => 'object',
|
@@ -30,26 +36,26 @@ describe JSI::BaseHash do
|
|
30
36
|
}
|
31
37
|
end
|
32
38
|
describe 'default value' do
|
33
|
-
let(:
|
39
|
+
let(:instance) { {'bar' => 3} }
|
34
40
|
it 'returns the default value' do
|
35
41
|
assert_equal('foo', subject.foo)
|
36
42
|
end
|
37
43
|
end
|
38
44
|
describe 'nondefault value (basic type)' do
|
39
|
-
let(:
|
45
|
+
let(:instance) { {'foo' => 'who'} }
|
40
46
|
it 'returns the nondefault value' do
|
41
47
|
assert_equal('who', subject.foo)
|
42
48
|
end
|
43
49
|
end
|
44
50
|
describe 'nondefault value (nonbasic type)' do
|
45
|
-
let(:
|
51
|
+
let(:instance) { {'foo' => [2]} }
|
46
52
|
it 'returns the nondefault value' do
|
47
|
-
|
53
|
+
assert_is_a(schema.properties['foo'].jsi_schema_module, subject.foo)
|
48
54
|
assert_equal([2], subject.foo.as_json)
|
49
55
|
end
|
50
56
|
end
|
51
57
|
end
|
52
|
-
describe '#[] with a default that is a nonbasic type' do
|
58
|
+
describe '#[] with a schema default that is a nonbasic type' do
|
53
59
|
let(:schema_content) do
|
54
60
|
{
|
55
61
|
'type' => 'object',
|
@@ -59,26 +65,43 @@ describe JSI::BaseHash do
|
|
59
65
|
}
|
60
66
|
end
|
61
67
|
describe 'default value' do
|
62
|
-
let(:
|
68
|
+
let(:instance) { {'bar' => 3} }
|
63
69
|
it 'returns the default value' do
|
64
|
-
|
70
|
+
assert_is_a(schema.properties['foo'].jsi_schema_module, subject.foo)
|
65
71
|
assert_equal({'foo' => 2}, subject.foo.as_json)
|
66
72
|
end
|
67
73
|
end
|
68
74
|
describe 'nondefault value (basic type)' do
|
69
|
-
let(:
|
75
|
+
let(:instance) { {'foo' => 'who'} }
|
70
76
|
it 'returns the nondefault value' do
|
71
77
|
assert_equal('who', subject.foo)
|
72
78
|
end
|
73
79
|
end
|
74
80
|
describe 'nondefault value (nonbasic type)' do
|
75
|
-
let(:
|
81
|
+
let(:instance) { {'foo' => [2]} }
|
76
82
|
it 'returns the nondefault value' do
|
77
|
-
|
83
|
+
assert_is_a(schema.properties['foo'].jsi_schema_module, subject.foo)
|
78
84
|
assert_equal([2], subject.foo.as_json)
|
79
85
|
end
|
80
86
|
end
|
81
87
|
end
|
88
|
+
describe '#[] with a hash default that is a nonbasic type' do
|
89
|
+
let(:schema_content) do
|
90
|
+
{
|
91
|
+
'type' => 'object',
|
92
|
+
'properties' => {
|
93
|
+
'foo' => {},
|
94
|
+
},
|
95
|
+
}
|
96
|
+
end
|
97
|
+
describe 'default value' do
|
98
|
+
let(:instance) { Hash.new({'foo' => 2}).merge({'bar' => 3}) }
|
99
|
+
it 'returns the default value' do
|
100
|
+
assert_is_a(Hash, subject.foo)
|
101
|
+
assert_equal({'foo' => 2}, subject.foo)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
82
105
|
describe 'hashlike []=' do
|
83
106
|
it 'sets a property' do
|
84
107
|
orig_foo = subject['foo']
|
@@ -86,8 +109,8 @@ describe JSI::BaseHash do
|
|
86
109
|
subject['foo'] = {'y' => 'z'}
|
87
110
|
|
88
111
|
assert_equal({'y' => 'z'}, subject['foo'].as_json)
|
89
|
-
|
90
|
-
|
112
|
+
assert_is_a(schema.properties['foo'].jsi_schema_module, orig_foo)
|
113
|
+
assert_is_a(schema.properties['foo'].jsi_schema_module, subject['foo'])
|
91
114
|
end
|
92
115
|
it 'sets a property to a schema instance with a different schema' do
|
93
116
|
assert(subject['foo'])
|
@@ -97,11 +120,11 @@ describe JSI::BaseHash do
|
|
97
120
|
# the content of the subscripts' instances is the same but the subscripts' classes are different
|
98
121
|
assert_equal([9], subject['foo'].as_json)
|
99
122
|
assert_equal([9], subject['bar'].as_json)
|
100
|
-
|
101
|
-
|
123
|
+
assert_is_a(schema.properties['foo'].jsi_schema_module, subject['foo'])
|
124
|
+
assert_is_a(schema.properties['bar'].jsi_schema_module, subject['bar'])
|
102
125
|
end
|
103
126
|
it 'sets a property to a schema instance with the same schema' do
|
104
|
-
other_subject =
|
127
|
+
other_subject = schema.new_jsi({'foo' => {'x' => 'y'}, 'bar' => [9], 'baz' => [true]})
|
105
128
|
# Given
|
106
129
|
assert_equal(other_subject, subject)
|
107
130
|
|
@@ -115,20 +138,130 @@ describe JSI::BaseHash do
|
|
115
138
|
refute_equal(other_subject['foo'].object_id, subject['foo'].object_id)
|
116
139
|
end
|
117
140
|
it 'modifies the instance, visible to other references to the same instance' do
|
118
|
-
orig_instance = subject.
|
141
|
+
orig_instance = subject.jsi_instance
|
119
142
|
|
120
143
|
subject['foo'] = {'y' => 'z'}
|
121
144
|
|
122
|
-
assert_equal(orig_instance, subject.
|
123
|
-
assert_equal({'y' => 'z'}, orig_instance['foo']
|
124
|
-
assert_equal({'y' => 'z'}, subject.
|
125
|
-
assert_equal(orig_instance.class, subject.
|
145
|
+
assert_equal(orig_instance, subject.jsi_instance)
|
146
|
+
assert_equal({'y' => 'z'}, orig_instance['foo'])
|
147
|
+
assert_equal({'y' => 'z'}, subject.jsi_instance['foo'])
|
148
|
+
assert_equal(orig_instance.class, subject.jsi_instance.class)
|
126
149
|
end
|
127
150
|
describe 'when the instance is not hashlike' do
|
128
151
|
let(:instance) { nil }
|
129
152
|
it 'errors' do
|
130
153
|
err = assert_raises(NoMethodError) { subject['foo'] = 0 }
|
131
|
-
|
154
|
+
assert_equal('cannot assign subcript (using token: "foo") to instance: nil', err.message)
|
155
|
+
end
|
156
|
+
end
|
157
|
+
describe '#inspect' do
|
158
|
+
it 'inspects' do
|
159
|
+
assert_equal("\#{<JSI> \"foo\" => \#{<JSI> \"x\" => \"y\"}, \"bar\" => #[<JSI> 9], \"baz\" => [true]}", subject.inspect)
|
160
|
+
end
|
161
|
+
end
|
162
|
+
describe '#pretty_print' do
|
163
|
+
it 'pretty_prints' do
|
164
|
+
assert_equal("\#{<JSI> \"foo\" => \#{<JSI> \"x\" => \"y\"}, \"bar\" => #[<JSI> 9], \"baz\" => [true]}\n", subject.pretty_inspect)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
describe '#inspect SortOfHash' do
|
168
|
+
let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
|
169
|
+
it 'inspects' do
|
170
|
+
assert_equal("\#{<JSI SortOfHash> \"foo\" => \#{<JSI> \"x\" => \"y\"}, \"bar\" => #[<JSI> 9], \"baz\" => [true]}", subject.inspect)
|
171
|
+
end
|
172
|
+
end
|
173
|
+
describe '#pretty_print SortOfHash' do
|
174
|
+
let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
|
175
|
+
it 'pretty_prints' do
|
176
|
+
assert_equal("\#{<JSI SortOfHash>\n \"foo\" => \#{<JSI> \"x\" => \"y\"},\n \"bar\" => #[<JSI> 9],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
|
177
|
+
end
|
178
|
+
end
|
179
|
+
describe '#inspect named' do
|
180
|
+
let(:subject) { NamedHashInstance.new(instance) }
|
181
|
+
it 'inspects' do
|
182
|
+
assert_equal("\#{<NamedHashInstance> \"foo\" => \#{<JSI> \"x\" => \"y\"}, \"bar\" => #[<JSI> 9], \"baz\" => [true]}", subject.inspect)
|
183
|
+
end
|
184
|
+
end
|
185
|
+
describe '#pretty_print named' do
|
186
|
+
let(:subject) { NamedHashInstance.new(instance) }
|
187
|
+
it 'inspects' do
|
188
|
+
assert_equal("\#{<NamedHashInstance>\n \"foo\" => \#{<JSI> \"x\" => \"y\"},\n \"bar\" => #[<JSI> 9],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
|
189
|
+
end
|
190
|
+
end
|
191
|
+
describe '#inspect named SortOfHash' do
|
192
|
+
let(:subject) { NamedHashInstance.new(SortOfHash.new(instance)) }
|
193
|
+
it 'inspects' do
|
194
|
+
assert_equal("\#{<NamedHashInstance SortOfHash> \"foo\" => \#{<JSI> \"x\" => \"y\"}, \"bar\" => #[<JSI> 9], \"baz\" => [true]}", subject.inspect)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
describe '#pretty_print named SortOfHash' do
|
198
|
+
let(:subject) { NamedHashInstance.new(SortOfHash.new(instance)) }
|
199
|
+
it 'inspects' do
|
200
|
+
assert_equal("\#{<NamedHashInstance SortOfHash>\n \"foo\" => \#{<JSI> \"x\" => \"y\"},\n \"bar\" => #[<JSI> 9],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
|
201
|
+
end
|
202
|
+
end
|
203
|
+
describe '#inspect named with id' do
|
204
|
+
let(:subject) { NamedIdHashInstance.new(instance) }
|
205
|
+
it 'inspects' do
|
206
|
+
assert_equal("\#{<NamedIdHashInstance> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
|
207
|
+
end
|
208
|
+
end
|
209
|
+
describe '#pretty_print named with id' do
|
210
|
+
let(:subject) { NamedIdHashInstance.new(instance) }
|
211
|
+
it 'inspects' do
|
212
|
+
assert_equal("\#{<NamedIdHashInstance>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
|
213
|
+
end
|
214
|
+
end
|
215
|
+
describe '#inspect named SortOfHash with id' do
|
216
|
+
let(:subject) { NamedIdHashInstance.new(SortOfHash.new(instance)) }
|
217
|
+
it 'inspects' do
|
218
|
+
assert_equal("\#{<NamedIdHashInstance SortOfHash> \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/foo)> \"x\" => \"y\"}, \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/bar)> 9], \"baz\" => [true]}", subject.inspect)
|
219
|
+
end
|
220
|
+
end
|
221
|
+
describe '#pretty_print named with id SortOfHash' do
|
222
|
+
let(:subject) { NamedIdHashInstance.new(SortOfHash.new(instance)) }
|
223
|
+
it 'inspects' do
|
224
|
+
assert_equal("\#{<NamedIdHashInstance SortOfHash>\n \"foo\" => \#{<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/foo)>\n \"x\" => \"y\"\n },\n \"bar\" => #[<JSI (https://schemas.jsi.unth.net/test/base/named_hash_schema#/properties/bar)>\n 9\n ],\n \"baz\" => [true]\n}\n", subject.pretty_inspect)
|
225
|
+
end
|
226
|
+
end
|
227
|
+
describe '#inspect with id' do
|
228
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
|
229
|
+
let(:subject) { schema.new_jsi(instance) }
|
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)
|
232
|
+
end
|
233
|
+
end
|
234
|
+
describe '#pretty_print with id' do
|
235
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
|
236
|
+
let(:subject) { schema.new_jsi(instance) }
|
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)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
describe '#inspect with id SortOfHash' do
|
242
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
|
243
|
+
let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
|
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)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
describe '#pretty_print with id SortOfHash' do
|
249
|
+
let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/test/withid', 'properties' => {'foo' => {}, 'bar' => {}}} }
|
250
|
+
let(:subject) { schema.new_jsi(SortOfHash.new(instance)) }
|
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)
|
253
|
+
end
|
254
|
+
end
|
255
|
+
describe '#inspect Node' do
|
256
|
+
let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
|
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)
|
259
|
+
end
|
260
|
+
end
|
261
|
+
describe '#pretty_print Node' do
|
262
|
+
let(:subject) { schema.new_jsi(JSI::JSON::Node.new_doc(instance)) }
|
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)
|
132
265
|
end
|
133
266
|
end
|
134
267
|
end
|
@@ -145,36 +278,36 @@ describe JSI::BaseHash do
|
|
145
278
|
it('#size') { assert_equal(3, subject.size) }
|
146
279
|
end
|
147
280
|
describe 'key + value methods' do
|
148
|
-
it('#<') { assert_equal(true, subject < {'foo' => subject['foo'], 'bar' => subject['bar'], 'baz' =>
|
281
|
+
it('#<') { assert_equal(true, subject < {'foo' => subject['foo'], 'bar' => subject['bar'], 'baz' => subject['baz'], 'x' => 'y'}) } if {}.respond_to?(:<)
|
149
282
|
it('#<=') { assert_equal(true, subject <= subject) } if {}.respond_to?(:<=)
|
150
283
|
it('#>') { assert_equal(true, subject > {}) } if {}.respond_to?(:>)
|
151
284
|
it('#>=') { assert_equal(false, subject >= {'foo' => 'bar'}) } if {}.respond_to?(:>=)
|
152
285
|
it('#any?') { assert_equal(false, subject.any? { |k, v| v == 3 }) }
|
153
286
|
it('#assoc') { assert_equal(['foo', subject['foo']], subject.assoc('foo')) }
|
154
287
|
it('#dig') { assert_equal(9, subject.dig('bar', 0)) } if {}.respond_to?(:dig)
|
155
|
-
it('#each_pair') { assert_equal([['foo', subject['foo']], ['bar', subject['bar']], ['baz', true]], subject.each_pair.to_a) }
|
156
|
-
it('#each_value') { assert_equal([subject['foo'], subject['bar'], true], subject.each_value.to_a) }
|
157
|
-
it('#fetch') { assert_equal(true, subject.fetch('baz')) }
|
158
|
-
it('#fetch_values') { assert_equal([true], subject.fetch_values('baz')) } if {}.respond_to?(:fetch_values)
|
159
|
-
it('#has_value?') { assert_equal(true, subject.has_value?(true)) }
|
160
|
-
it('#invert') { assert_equal({subject['foo'] => 'foo', subject['bar'] => 'bar', true => 'baz'}, subject.invert) }
|
161
|
-
it('#key') { assert_equal('baz', subject.key(true)) }
|
162
|
-
it('#rassoc') { assert_equal(['baz', true], subject.rassoc(true)) }
|
163
|
-
it('#to_h') { assert_equal({'foo' => subject['foo'], 'bar' => subject['bar'], 'baz' => true}, subject.to_h) }
|
164
|
-
it('#to_proc') { assert_equal(true, subject.to_proc.call('baz')) } if {}.respond_to?(:to_proc)
|
288
|
+
it('#each_pair') { assert_equal([['foo', subject['foo']], ['bar', subject['bar']], ['baz', [true]]], subject.each_pair.to_a) }
|
289
|
+
it('#each_value') { assert_equal([subject['foo'], subject['bar'], [true]], subject.each_value.to_a) }
|
290
|
+
it('#fetch') { assert_equal([true], subject.fetch('baz')) }
|
291
|
+
it('#fetch_values') { assert_equal([[true]], subject.fetch_values('baz')) } if {}.respond_to?(:fetch_values)
|
292
|
+
it('#has_value?') { assert_equal(true, subject.has_value?([true])) }
|
293
|
+
it('#invert') { assert_equal({subject['foo'] => 'foo', subject['bar'] => 'bar', [true] => 'baz'}, subject.invert) }
|
294
|
+
it('#key') { assert_equal('baz', subject.key([true])) }
|
295
|
+
it('#rassoc') { assert_equal(['baz', [true]], subject.rassoc([true])) }
|
296
|
+
it('#to_h') { assert_equal({'foo' => subject['foo'], 'bar' => subject['bar'], 'baz' => [true]}, subject.to_h) }
|
297
|
+
it('#to_proc') { assert_equal([true], subject.to_proc.call('baz')) } if {}.respond_to?(:to_proc)
|
165
298
|
if {}.respond_to?(:transform_values)
|
166
299
|
it('#transform_values') { assert_equal({'foo' => nil, 'bar' => nil, 'baz' => nil}, subject.transform_values { |_| nil }) }
|
167
300
|
end
|
168
301
|
it('#value?') { assert_equal(false, subject.value?('0')) }
|
169
|
-
it('#values') { assert_equal([subject['foo'], subject['bar'], true], subject.values) }
|
170
|
-
it('#values_at') { assert_equal([true], subject.values_at('baz')) }
|
302
|
+
it('#values') { assert_equal([subject['foo'], subject['bar'], [true]], subject.values) }
|
303
|
+
it('#values_at') { assert_equal([[true]], subject.values_at('baz')) }
|
171
304
|
end
|
172
305
|
describe 'with an instance that has to_hash but not other hash instance methods' do
|
173
306
|
let(:instance) { SortOfHash.new({'foo' => SortOfHash.new({'a' => 'b'})}) }
|
174
307
|
describe 'delegating instance methods to #to_hash' do
|
175
308
|
it('#each_key') { assert_equal(['foo'], subject.each_key.to_a) }
|
176
309
|
it('#each_pair') { assert_equal([['foo', subject['foo']]], subject.each_pair.to_a) }
|
177
|
-
it('#[]') { assert_equal(SortOfHash.new({'a' => 'b'}), subject['foo'].
|
310
|
+
it('#[]') { assert_equal(SortOfHash.new({'a' => 'b'}), subject['foo'].jsi_instance) }
|
178
311
|
it('#as_json') { assert_equal({'foo' => {'a' => 'b'}}, subject.as_json) }
|
179
312
|
end
|
180
313
|
end
|
@@ -182,8 +315,8 @@ describe JSI::BaseHash do
|
|
182
315
|
# I'm going to rely on the #merge test above to test the modified copy functionality and just do basic
|
183
316
|
# tests of all the modified copy methods here
|
184
317
|
it('#merge') { assert_equal(subject, subject.merge({})) }
|
185
|
-
it('#reject') { assert_equal(
|
186
|
-
it('#select') { assert_equal(
|
318
|
+
it('#reject') { assert_equal(schema.new_jsi({}), subject.reject { true }) }
|
319
|
+
it('#select') { assert_equal(schema.new_jsi({}), subject.select { false }) }
|
187
320
|
describe '#select' do
|
188
321
|
it 'yields properly too' do
|
189
322
|
subject.select do |k, v|
|