jsi 0.4.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/.yardopts +1 -1
  3. data/CHANGELOG.md +33 -0
  4. data/LICENSE.md +1 -1
  5. data/README.md +114 -42
  6. data/jsi.gemspec +14 -12
  7. data/lib/jsi/base/node.rb +183 -0
  8. data/lib/jsi/base.rb +388 -220
  9. data/lib/jsi/jsi_coder.rb +8 -7
  10. data/lib/jsi/metaschema.rb +0 -1
  11. data/lib/jsi/metaschema_node/bootstrap_schema.rb +101 -0
  12. data/lib/jsi/metaschema_node.rb +159 -135
  13. data/lib/jsi/ptr.rb +303 -0
  14. data/lib/jsi/schema/application/child_application/contains.rb +25 -0
  15. data/lib/jsi/schema/application/child_application/draft04.rb +22 -0
  16. data/lib/jsi/schema/application/child_application/draft06.rb +29 -0
  17. data/lib/jsi/schema/application/child_application/draft07.rb +29 -0
  18. data/lib/jsi/schema/application/child_application/items.rb +18 -0
  19. data/lib/jsi/schema/application/child_application/properties.rb +25 -0
  20. data/lib/jsi/schema/application/child_application.rb +38 -0
  21. data/lib/jsi/schema/application/draft04.rb +8 -0
  22. data/lib/jsi/schema/application/draft06.rb +8 -0
  23. data/lib/jsi/schema/application/draft07.rb +8 -0
  24. data/lib/jsi/schema/application/inplace_application/dependencies.rb +28 -0
  25. data/lib/jsi/schema/application/inplace_application/draft04.rb +26 -0
  26. data/lib/jsi/schema/application/inplace_application/draft06.rb +27 -0
  27. data/lib/jsi/schema/application/inplace_application/draft07.rb +33 -0
  28. data/lib/jsi/schema/application/inplace_application/ifthenelse.rb +20 -0
  29. data/lib/jsi/schema/application/inplace_application/ref.rb +18 -0
  30. data/lib/jsi/schema/application/inplace_application/someof.rb +44 -0
  31. data/lib/jsi/schema/application/inplace_application.rb +41 -0
  32. data/lib/jsi/schema/application.rb +12 -0
  33. data/lib/jsi/schema/draft04.rb +14 -0
  34. data/lib/jsi/schema/draft06.rb +14 -0
  35. data/lib/jsi/schema/draft07.rb +14 -0
  36. data/lib/jsi/schema/issue.rb +36 -0
  37. data/lib/jsi/schema/ref.rb +160 -0
  38. data/lib/jsi/schema/schema_ancestor_node.rb +113 -0
  39. data/lib/jsi/schema/validation/array.rb +69 -0
  40. data/lib/jsi/schema/validation/const.rb +20 -0
  41. data/lib/jsi/schema/validation/contains.rb +25 -0
  42. data/lib/jsi/schema/validation/core.rb +39 -0
  43. data/lib/jsi/schema/validation/dependencies.rb +49 -0
  44. data/lib/jsi/schema/validation/draft04/minmax.rb +91 -0
  45. data/lib/jsi/schema/validation/draft04.rb +112 -0
  46. data/lib/jsi/schema/validation/draft06.rb +122 -0
  47. data/lib/jsi/schema/validation/draft07.rb +159 -0
  48. data/lib/jsi/schema/validation/enum.rb +25 -0
  49. data/lib/jsi/schema/validation/ifthenelse.rb +46 -0
  50. data/lib/jsi/schema/validation/items.rb +54 -0
  51. data/lib/jsi/schema/validation/not.rb +20 -0
  52. data/lib/jsi/schema/validation/numeric.rb +121 -0
  53. data/lib/jsi/schema/validation/object.rb +45 -0
  54. data/lib/jsi/schema/validation/pattern.rb +34 -0
  55. data/lib/jsi/schema/validation/properties.rb +101 -0
  56. data/lib/jsi/schema/validation/property_names.rb +32 -0
  57. data/lib/jsi/schema/validation/ref.rb +40 -0
  58. data/lib/jsi/schema/validation/required.rb +27 -0
  59. data/lib/jsi/schema/validation/someof.rb +90 -0
  60. data/lib/jsi/schema/validation/string.rb +47 -0
  61. data/lib/jsi/schema/validation/type.rb +49 -0
  62. data/lib/jsi/schema/validation.rb +51 -0
  63. data/lib/jsi/schema.rb +508 -149
  64. data/lib/jsi/schema_classes.rb +199 -59
  65. data/lib/jsi/schema_registry.rb +151 -0
  66. data/lib/jsi/schema_set.rb +181 -0
  67. data/lib/jsi/simple_wrap.rb +23 -4
  68. data/lib/jsi/util/private/attr_struct.rb +127 -0
  69. data/lib/jsi/util/private.rb +204 -0
  70. data/lib/jsi/util/typelike.rb +229 -0
  71. data/lib/jsi/util.rb +89 -53
  72. data/lib/jsi/validation/error.rb +34 -0
  73. data/lib/jsi/validation/result.rb +210 -0
  74. data/lib/jsi/validation.rb +15 -0
  75. data/lib/jsi/version.rb +3 -1
  76. data/lib/jsi.rb +44 -14
  77. data/lib/schemas/json-schema.org/draft-04/schema.rb +10 -3
  78. data/lib/schemas/json-schema.org/draft-06/schema.rb +10 -3
  79. data/lib/schemas/json-schema.org/draft-07/schema.rb +14 -0
  80. data/readme.rb +138 -0
  81. data/{resources}/schemas/json-schema.org/draft-04/schema.json +149 -0
  82. data/{resources}/schemas/json-schema.org/draft-06/schema.json +154 -0
  83. data/{resources}/schemas/json-schema.org/draft-07/schema.json +168 -0
  84. metadata +75 -122
  85. data/.simplecov +0 -3
  86. data/Rakefile.rb +0 -9
  87. data/lib/jsi/base/to_rb.rb +0 -128
  88. data/lib/jsi/json/node.rb +0 -203
  89. data/lib/jsi/json/pointer.rb +0 -419
  90. data/lib/jsi/json-schema-fragments.rb +0 -61
  91. data/lib/jsi/json.rb +0 -10
  92. data/lib/jsi/pathed_node.rb +0 -118
  93. data/lib/jsi/typelike_modules.rb +0 -240
  94. data/resources/icons/AGPL-3.0.png +0 -0
  95. data/test/base_array_test.rb +0 -323
  96. data/test/base_hash_test.rb +0 -337
  97. data/test/base_test.rb +0 -486
  98. data/test/jsi_coder_test.rb +0 -85
  99. data/test/jsi_json_arraynode_test.rb +0 -150
  100. data/test/jsi_json_hashnode_test.rb +0 -132
  101. data/test/jsi_json_node_test.rb +0 -257
  102. data/test/jsi_json_pointer_test.rb +0 -102
  103. data/test/jsi_test.rb +0 -11
  104. data/test/jsi_typelike_as_json_test.rb +0 -53
  105. data/test/metaschema_node_test.rb +0 -19
  106. data/test/schema_module_test.rb +0 -21
  107. data/test/schema_test.rb +0 -208
  108. data/test/spreedly_openapi_test.rb +0 -8
  109. data/test/test_helper.rb +0 -97
  110. data/test/util_test.rb +0 -62
@@ -1,337 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- base = {
4
- 'description' => 'named hash schema',
5
- 'type' => 'object',
6
- 'properties' => {
7
- 'foo' => {'type' => 'object'},
8
- 'bar' => {},
9
- },
10
- }
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
-
14
- describe 'JSI::Base hash' do
15
- let(:instance) { {'foo' => {'x' => 'y'}, 'bar' => [9], 'baz' => [true]} }
16
- let(:schema_content) do
17
- {
18
- 'description' => 'hash schema',
19
- 'type' => 'object',
20
- 'properties' => {
21
- 'foo' => {'type' => 'object'},
22
- 'bar' => {},
23
- },
24
- }
25
- end
26
- let(:schema) { JSI::Schema.new(schema_content) }
27
- let(:subject) { schema.new_jsi(instance) }
28
-
29
- describe '#[] with a schema default that is a basic type' do
30
- let(:schema_content) do
31
- {
32
- 'type' => 'object',
33
- 'properties' => {
34
- 'foo' => {'default' => 'foo'},
35
- },
36
- }
37
- end
38
- describe 'default value' do
39
- let(:instance) { {'bar' => 3} }
40
- it 'returns the default value' do
41
- assert_equal('foo', subject.foo)
42
- end
43
- end
44
- describe 'nondefault value (basic type)' do
45
- let(:instance) { {'foo' => 'who'} }
46
- it 'returns the nondefault value' do
47
- assert_equal('who', subject.foo)
48
- end
49
- end
50
- describe 'nondefault value (nonbasic type)' do
51
- let(:instance) { {'foo' => [2]} }
52
- it 'returns the nondefault value' do
53
- assert_is_a(schema.properties['foo'].jsi_schema_module, subject.foo)
54
- assert_equal([2], subject.foo.as_json)
55
- end
56
- end
57
- end
58
- describe '#[] with a schema default that is a nonbasic type' do
59
- let(:schema_content) do
60
- {
61
- 'type' => 'object',
62
- 'properties' => {
63
- 'foo' => {'default' => {'foo' => 2}},
64
- },
65
- }
66
- end
67
- describe 'default value' do
68
- let(:instance) { {'bar' => 3} }
69
- it 'returns the default value' do
70
- assert_is_a(schema.properties['foo'].jsi_schema_module, subject.foo)
71
- assert_equal({'foo' => 2}, subject.foo.as_json)
72
- end
73
- end
74
- describe 'nondefault value (basic type)' do
75
- let(:instance) { {'foo' => 'who'} }
76
- it 'returns the nondefault value' do
77
- assert_equal('who', subject.foo)
78
- end
79
- end
80
- describe 'nondefault value (nonbasic type)' do
81
- let(:instance) { {'foo' => [2]} }
82
- it 'returns the nondefault value' do
83
- assert_is_a(schema.properties['foo'].jsi_schema_module, subject.foo)
84
- assert_equal([2], subject.foo.as_json)
85
- end
86
- end
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
105
- describe 'hashlike []=' do
106
- it 'sets a property' do
107
- orig_foo = subject['foo']
108
-
109
- subject['foo'] = {'y' => 'z'}
110
-
111
- assert_equal({'y' => 'z'}, subject['foo'].as_json)
112
- assert_is_a(schema.properties['foo'].jsi_schema_module, orig_foo)
113
- assert_is_a(schema.properties['foo'].jsi_schema_module, subject['foo'])
114
- end
115
- it 'sets a property to a schema instance with a different schema' do
116
- assert(subject['foo'])
117
-
118
- subject['foo'] = subject['bar']
119
-
120
- # the content of the subscripts' instances is the same but the subscripts' classes are different
121
- assert_equal([9], subject['foo'].as_json)
122
- assert_equal([9], subject['bar'].as_json)
123
- assert_is_a(schema.properties['foo'].jsi_schema_module, subject['foo'])
124
- assert_is_a(schema.properties['bar'].jsi_schema_module, subject['bar'])
125
- end
126
- it 'sets a property to a schema instance with the same schema' do
127
- other_subject = schema.new_jsi({'foo' => {'x' => 'y'}, 'bar' => [9], 'baz' => [true]})
128
- # Given
129
- assert_equal(other_subject, subject)
130
-
131
- # When:
132
- subject['foo'] = other_subject['foo']
133
-
134
- # Then:
135
- # still equal
136
- assert_equal(other_subject, subject)
137
- # but different instances
138
- refute_equal(other_subject['foo'].object_id, subject['foo'].object_id)
139
- end
140
- it 'modifies the instance, visible to other references to the same instance' do
141
- orig_instance = subject.jsi_instance
142
-
143
- subject['foo'] = {'y' => 'z'}
144
-
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)
149
- end
150
- describe 'when the instance is not hashlike' do
151
- let(:instance) { nil }
152
- it 'errors' do
153
- err = assert_raises(NoMethodError) { subject['foo'] = 0 }
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/base_hash_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/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
- end
233
- end
234
- describe '#pretty_print with id' do
235
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_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/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
- end
240
- end
241
- describe '#inspect with id SortOfHash' do
242
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_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/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
- end
247
- end
248
- describe '#pretty_print with id SortOfHash' do
249
- let(:schema_content) { {'$id' => 'https://schemas.jsi.unth.net/base_hash_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/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
- 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 #> \"foo\" => \#{<JSI JSI::JSON::HashNode #/foo> \"x\" => \"y\"}, \"bar\" => #[<JSI JSI::JSON::ArrayNode #/bar> 9], \"baz\" => #[<JSI::JSON::ArrayNode #/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 #>\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
- end
266
- end
267
- end
268
- # these methods just delegate to Hash so not going to test excessively
269
- describe 'key only methods' do
270
- it('#each_key') { assert_equal(['foo', 'bar', 'baz'], subject.each_key.to_a) }
271
- it('#empty?') { assert_equal(false, subject.empty?) }
272
- it('#has_key?') { assert_equal(true, subject.has_key?('bar')) }
273
- it('#include?') { assert_equal(false, subject.include?('q')) }
274
- it('#key?') { assert_equal(true, subject.key?('baz')) }
275
- it('#keys') { assert_equal(['foo', 'bar', 'baz'], subject.keys) }
276
- it('#length') { assert_equal(3, subject.length) }
277
- it('#member?') { assert_equal(false, subject.member?(0)) }
278
- it('#size') { assert_equal(3, subject.size) }
279
- end
280
- describe 'key + value methods' do
281
- it('#<') { assert_equal(true, subject < {'foo' => subject['foo'], 'bar' => subject['bar'], 'baz' => subject['baz'], 'x' => 'y'}) } if {}.respond_to?(:<)
282
- it('#<=') { assert_equal(true, subject <= subject) } if {}.respond_to?(:<=)
283
- it('#>') { assert_equal(true, subject > {}) } if {}.respond_to?(:>)
284
- it('#>=') { assert_equal(false, subject >= {'foo' => 'bar'}) } if {}.respond_to?(:>=)
285
- it('#any?') { assert_equal(false, subject.any? { |k, v| v == 3 }) }
286
- it('#assoc') { assert_equal(['foo', subject['foo']], subject.assoc('foo')) }
287
- it('#dig') { assert_equal(9, subject.dig('bar', 0)) } if {}.respond_to?(:dig)
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)
298
- if {}.respond_to?(:transform_values)
299
- it('#transform_values') { assert_equal({'foo' => nil, 'bar' => nil, 'baz' => nil}, subject.transform_values { |_| nil }) }
300
- end
301
- it('#value?') { assert_equal(false, subject.value?('0')) }
302
- it('#values') { assert_equal([subject['foo'], subject['bar'], [true]], subject.values) }
303
- it('#values_at') { assert_equal([[true]], subject.values_at('baz')) }
304
- end
305
- describe 'with an instance that has to_hash but not other hash instance methods' do
306
- let(:instance) { SortOfHash.new({'foo' => SortOfHash.new({'a' => 'b'})}) }
307
- describe 'delegating instance methods to #to_hash' do
308
- it('#each_key') { assert_equal(['foo'], subject.each_key.to_a) }
309
- it('#each_pair') { assert_equal([['foo', subject['foo']]], subject.each_pair.to_a) }
310
- it('#[]') { assert_equal(SortOfHash.new({'a' => 'b'}), subject['foo'].jsi_instance) }
311
- it('#as_json') { assert_equal({'foo' => {'a' => 'b'}}, subject.as_json) }
312
- end
313
- end
314
- describe 'modified copy methods' do
315
- # I'm going to rely on the #merge test above to test the modified copy functionality and just do basic
316
- # tests of all the modified copy methods here
317
- it('#merge') { assert_equal(subject, subject.merge({})) }
318
- it('#reject') { assert_equal(schema.new_jsi({}), subject.reject { true }) }
319
- it('#select') { assert_equal(schema.new_jsi({}), subject.select { false }) }
320
- describe '#select' do
321
- it 'yields properly too' do
322
- subject.select do |k, v|
323
- assert_equal(subject[k], v)
324
- end
325
- end
326
- end
327
- # Hash#compact only available as of ruby 2.5.0
328
- if {}.respond_to?(:compact)
329
- it('#compact') { assert_equal(subject, subject.compact) }
330
- end
331
- end
332
- JSI::Hashlike::DESTRUCTIVE_METHODS.each do |destructive_method_name|
333
- it("does not respond to destructive method #{destructive_method_name}") do
334
- assert(!subject.respond_to?(destructive_method_name))
335
- end
336
- end
337
- end