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,53 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- class JSONifiable
4
- def initialize(object)
5
- @object = object
6
- end
7
- def as_json
8
- @object
9
- end
10
- end
11
-
12
- describe JSI::Typelike do
13
- describe 'as_json' do
14
- it 'expresses as json' do
15
- assert_equal({}, JSI::Typelike.as_json({}))
16
- assert_equal([], JSI::Typelike.as_json([]))
17
-
18
- # symbols to string
19
- assert_equal(['a'], JSI::Typelike.as_json([:a]))
20
-
21
- # set
22
- assert_equal(['a'], JSI::Typelike.as_json(Set.new(['a'])))
23
-
24
- # responds to #to_hash / #to_ary but naught else
25
- assert_equal({'a' => 'b'}, JSI::Typelike.as_json(SortOfHash.new({'a' => 'b'})))
26
- assert_equal(['a'], JSI::Typelike.as_json(SortOfArray.new(['a'])))
27
-
28
- # symbol keys to string
29
- assert_equal({'a' => 'b'}, JSI::Typelike.as_json({a: 'b'}))
30
- # non string/symbol key
31
- err = assert_raises(TypeError) { JSI::Typelike.as_json({nil => 0}) }
32
- assert_equal('json object (hash) cannot be keyed with: nil', err.message)
33
-
34
- # schema
35
- schema = JSI::Schema.from_object({'type' => 'array'})
36
- assert_equal({'type' => 'array'}, JSI::Typelike.as_json(schema))
37
-
38
- # JSI
39
- assert_equal(['a'], JSI::Typelike.as_json(schema.new_jsi(['a'])))
40
-
41
- # JSON::Node
42
- assert_equal(['a'], JSI::Typelike.as_json(JSI::JSON::Node.new_doc(['a'])))
43
-
44
- # #as_json
45
- assert_equal(['a'], JSI::Typelike.as_json(JSONifiable.new(['a'])))
46
-
47
- # not jsonifiable
48
- object = Object.new
49
- err = assert_raises(TypeError) { JSI::Typelike.as_json(object) }
50
- assert_equal("cannot express object as json: #{object.pretty_inspect.chomp}", err.message)
51
- end
52
- end
53
- end
@@ -1,19 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe JSI::MetaschemaNode do
4
- let(:node_document) { {'properties' => {'properties' => {'additionalProperties' => {'$ref' => '#'}}}} }
5
- let(:node_ptr) { JSI::JSON::Pointer[] }
6
- let(:metaschema_root_ptr) { JSI::JSON::Pointer[] }
7
- let(:root_schema_ptr) { JSI::JSON::Pointer[] }
8
- let(:subject) { JSI::MetaschemaNode.new(node_document, node_ptr: node_ptr, metaschema_root_ptr: node_ptr, root_schema_ptr: node_ptr) }
9
- describe 'initialization' do
10
- it 'initializes' do
11
- subject
12
- end
13
- end
14
- describe 'json schema draft' do
15
- it 'type has a schema' do
16
- assert(JSI::JSONSchemaOrgDraft06.schema.type.jsi_schemas.any?)
17
- end
18
- end
19
- end
@@ -1,21 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe 'JSI::SchemaModule' do
4
- let(:schema_content) { {'properties' => {'foo' => {'items' => {'type' => 'string'}}}} }
5
- let(:schema) { JSI::Schema.new(schema_content) }
6
- let(:schema_module) { schema.jsi_schema_module }
7
- describe 'accessors and subscripts' do
8
- it 'returns schemas using accessors and subscripts' do
9
- assert_equal(schema.properties, schema_module.properties.possibly_schema_node)
10
- assert_equal(schema.properties['foo'], schema_module.properties['foo'].possibly_schema_node)
11
- assert_equal(schema.properties['foo'].jsi_schema_module, schema_module.properties['foo'])
12
- assert_equal(schema.properties['foo'].items, schema_module.properties['foo'].items.possibly_schema_node)
13
- assert_equal(schema.properties['foo'].items.jsi_schema_module, schema_module.properties['foo'].items)
14
- assert_equal('string', schema_module.properties['foo'].items.type)
15
- end
16
- it 'accessors and subscripts with a metaschema' do
17
- assert_equal(JSI::JSONSchemaOrgDraft06.schema.properties, JSI::JSONSchemaOrgDraft06.properties.possibly_schema_node)
18
- assert_equal(JSI::JSONSchemaOrgDraft06.schema.properties['properties'].additionalProperties.jsi_schema_module, JSI::JSONSchemaOrgDraft06.properties['properties'].additionalProperties)
19
- end
20
- end
21
- end
data/test/schema_test.rb DELETED
@@ -1,208 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe JSI::Schema do
4
- describe 'new' do
5
- it 'initializes from a hash' do
6
- schema = JSI::Schema.new({'type' => 'object'})
7
- assert_equal({'type' => 'object'}, schema.jsi_instance)
8
- end
9
- it 'initializes from a Node' do
10
- schema_node = JSI::JSON::Node.new_doc({'type' => 'object'})
11
- schema = JSI::Schema.new(schema_node)
12
- assert_equal(schema_node, schema.jsi_instance)
13
- end
14
- it 'cannot instantiate from some unknown object' do
15
- err = assert_raises(TypeError) { JSI::Schema.new(Object.new) }
16
- assert_match(/\Acannot instantiate Schema from: #<Object:.*>\z/m, err.message)
17
- end
18
- it 'instantiating a schema from schema returns that schema' do
19
- # this is kinda dumb, but Schema.new now just aliases Schema.from_object, so this is the behavior
20
- assert_equal(JSI::Schema.new({}), JSI::Schema.new(JSI::Schema.new({})))
21
- end
22
- end
23
- describe 'as an instance of metaschema' do
24
- let(:metaschema_jsi_module) { JSI::JSONSchemaOrgDraft04 }
25
- let(:schema_content) { {'type' => 'array', 'items' => {'description' => 'items!'}} }
26
- let(:schema) { metaschema_jsi_module.new_jsi(schema_content) }
27
- it '#[]' do
28
- schema_items = schema['items']
29
- assert_is_a(metaschema_jsi_module, schema_items)
30
- assert_equal({'description' => 'items!'}, schema_items.as_json)
31
- end
32
- end
33
- describe '#schema_id' do
34
- it "hasn't got one" do
35
- assert_nil(JSI::Schema.new({}).schema_id)
36
- end
37
- it 'uses a given id with a fragment' do
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
- end
41
- it 'uses a given id (adding a fragment)' do
42
- schema = JSI::Schema.new({id: 'https://schemas.jsi.unth.net/test/given_id'})
43
- assert_equal('https://schemas.jsi.unth.net/test/given_id#', schema.schema_id)
44
- end
45
- it 'uses a pointer in the fragment' do
46
- schema = JSI::Schema.new({
47
- 'id' => 'https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#',
48
- 'properties' => {'foo' => {'type' => 'object'}},
49
- })
50
- subschema = schema['properties']['foo']
51
- assert_equal('https://schemas.jsi.unth.net/test/uses_pointer_in_fragment#/properties/foo', subschema.schema_id)
52
- end
53
- it 'uses a pointer in the fragment relative to the fragment of the root' do
54
- schema = JSI::Schema.new({
55
- 'id' => 'https://schemas.jsi.unth.net/test/id_has_pointer#/notroot',
56
- 'properties' => {'foo' => {'type' => 'object'}},
57
- })
58
- subschema = schema['properties']['foo']
59
- assert_equal('https://schemas.jsi.unth.net/test/id_has_pointer#/notroot/properties/foo', subschema.schema_id)
60
- end
61
- end
62
- describe '#jsi_schema_module' do
63
- it 'returns the module for the schema' do
64
- schema = JSI::Schema.new({'id' => 'https://schemas.jsi.unth.net/test/jsi_schema_module'})
65
- assert_is_a(JSI::SchemaModule, schema.jsi_schema_module)
66
- assert_equal(schema, schema.jsi_schema_module.schema)
67
- end
68
- end
69
- describe '#jsi_schema_class' do
70
- it 'returns the class for the schema' do
71
- schema = JSI::Schema.new({'id' => 'https://schemas.jsi.unth.net/test/schema_schema_class'})
72
- assert_equal(JSI.class_for_schemas([schema]), schema.jsi_schema_class)
73
- end
74
- end
75
- describe '#subschemas_for_property_name' do
76
- let(:schema) do
77
- JSI::Schema.new({
78
- properties: {
79
- foo: {description: 'foo'},
80
- baz: {description: 'baz'},
81
- },
82
- patternProperties: {
83
- "^b" => {description: 'ba*'},
84
- },
85
- additionalProperties: {description: 'whatever'},
86
- })
87
- end
88
- it 'has no subschemas' do
89
- assert_empty(JSI::Schema.new({}).subschemas_for_property_name('no'))
90
- end
91
- it 'has a subschema by property' do
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)
116
- end
117
- end
118
- describe '#subschemas_for_index' do
119
- it 'has no subschemas' do
120
- assert_empty(JSI::Schema.new({}).subschemas_for_index(0))
121
- end
122
- it 'has a subschema for items' do
123
- schema = JSI::Schema.new({
124
- items: {description: 'items!'}
125
- })
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)
134
- end
135
- it 'has a subschema for each item by index' do
136
- schema = JSI::Schema.new({
137
- items: [{description: 'item one'}, {description: 'item two'}]
138
- })
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)
147
- end
148
- it 'has a subschema by additional items' do
149
- schema = JSI::Schema.new({
150
- items: [{description: 'item one'}],
151
- additionalItems: {description: "mo' crap"},
152
- })
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)
161
- end
162
- end
163
- describe 'stringification' do
164
- let(:schema) do
165
- JSI::Schema.new({id: 'https://schemas.jsi.unth.net/test/stringification', type: 'object'})
166
- end
167
-
168
- it '#inspect' do
169
- assert_equal("\#{<JSI (JSI::JSONSchemaOrgDraft06) Schema> \"id\" => \"https://schemas.jsi.unth.net/test/stringification\", \"type\" => \"object\"}", schema.inspect)
170
- end
171
- it '#pretty_print' do
172
- assert_equal("\#{<JSI (JSI::JSONSchemaOrgDraft06) Schema>
173
- \"id\" => \"https://schemas.jsi.unth.net/test/stringification\",
174
- \"type\" => \"object\"
175
- }".gsub(/^ /, ''), schema.pretty_inspect.chomp)
176
- end
177
- end
178
- describe 'validation' do
179
- let(:schema) { JSI::Schema.new({id: 'https://schemas.jsi.unth.net/test/validation', type: 'object'}) }
180
- describe 'without errors' do
181
- let(:instance) { {'foo' => 'bar'} }
182
- it '#fully_validate_instance' do
183
- assert_equal([], schema.fully_validate_instance(instance))
184
- end
185
- it '#validate_instance' do
186
- assert_equal(true, schema.validate_instance(instance))
187
- end
188
- it '#validate_instance!' do
189
- assert_equal(true, schema.validate_instance!(instance))
190
- end
191
- end
192
- describe 'with errors' do
193
- let(:instance) { ['no'] }
194
- it '#fully_validate_instance' do
195
- assert_equal(["The property '#/' of type array did not match the following type: object in schema https://schemas.jsi.unth.net/test/validation"], schema.fully_validate_instance(instance))
196
- end
197
- it '#validate_instance' do
198
- assert_equal(false, schema.validate_instance(instance))
199
- end
200
- it '#validate_instance!' do
201
- err = assert_raises(JSON::Schema::ValidationError) do
202
- schema.validate_instance!(instance)
203
- end
204
- assert_equal("The property '#/' of type array did not match the following type: object", err.message)
205
- end
206
- end
207
- end
208
- end
@@ -1,8 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- require 'spreedly_openapi'
4
- describe 'spreedly openapi' do
5
- it 'instantiates the spreedly openapi doc' do
6
- SpreedlyOpenAPI::Document.inspect
7
- end
8
- end
data/test/test_helper.rb DELETED
@@ -1,97 +0,0 @@
1
- require 'coveralls'
2
- if Coveralls.will_run?
3
- Coveralls.wear!
4
- end
5
- require 'simplecov'
6
-
7
- require 'bundler/setup'
8
-
9
- require 'byebug'
10
-
11
- $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
12
- require 'jsi'
13
-
14
- # NO EXPECTATIONS
15
- ENV["MT_NO_EXPECTATIONS"] = ''
16
-
17
- require 'minitest/autorun'
18
- require 'minitest/around/spec'
19
- require 'minitest/reporters'
20
-
21
- Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
22
-
23
- class JSISpec < Minitest::Spec
24
- if ENV['JSI_TEST_ALPHA']
25
- # :nocov:
26
- define_singleton_method(:test_order) { :alpha }
27
- # :nocov:
28
- end
29
-
30
- def assert_equal exp, act, msg = nil
31
- msg = message(msg, E) do
32
- [].tap do |ms|
33
- ms << diff(exp, act)
34
- ms << "#{ANSI.red { 'expected' }}: #{exp.inspect}"
35
- ms << "#{ANSI.green { 'actual' }}: #{act.inspect}"
36
- if exp.respond_to?(:to_str) && act.respond_to?(:to_str)
37
- ms << "#{ANSI.red { 'expected (str)' }}:"
38
- ms << exp
39
- ms << "#{ANSI.green { 'actual (str)' }}:"
40
- ms << act
41
- end
42
- end.join("\n")
43
- end
44
- assert exp == act, msg
45
- end
46
-
47
- def assert_match matcher, obj, msg = nil
48
- msg = message(msg) do
49
- [].tap do |ms|
50
- ms << "Expected match."
51
- ms << "#{ANSI.red { 'matcher' }}: #{mu_pp matcher}"
52
- ms << "#{ANSI.green { 'object' }}: #{mu_pp obj}"
53
- ms << "#{ANSI.yellow { 'escaped' }}: #{Regexp.new(Regexp.escape(obj)).inspect}" if obj.is_a?(String)
54
- end.join("\n")
55
- end
56
- assert_respond_to matcher, :"=~"
57
- matcher = Regexp.new Regexp.escape matcher if String === matcher
58
- assert matcher =~ obj, msg
59
- end
60
-
61
- def assert_is_a mod, obj, msg = nil
62
- msg = message(msg) { "Expected instance of #{mod}. received #{obj.class}: #{mu_pp(obj)}" }
63
-
64
- assert obj.is_a?(mod), msg
65
- end
66
- end
67
-
68
- # register this to be the base class for specs instead of Minitest::Spec
69
- Minitest::Spec.register_spec_type(//, JSISpec)
70
-
71
- # tests support of things that duck-type #to_hash
72
- class SortOfHash
73
- def initialize(hash)
74
- @hash = hash
75
- end
76
- def to_hash
77
- @hash
78
- end
79
- include JSI::Util::FingerprintHash
80
- def jsi_fingerprint
81
- {class: self.class, hash: @hash}
82
- end
83
- end
84
-
85
- # tests support of things that duck-type #to_ary
86
- class SortOfArray
87
- def initialize(ary)
88
- @ary = ary
89
- end
90
- def to_ary
91
- @ary
92
- end
93
- include JSI::Util::FingerprintHash
94
- def jsi_fingerprint
95
- {class: self.class, ary: @ary}
96
- end
97
- end
data/test/util_test.rb DELETED
@@ -1,62 +0,0 @@
1
- require_relative 'test_helper'
2
-
3
- describe JSI::Util do
4
- describe '.stringify_symbol_keys' do
5
- it 'stringifies symbol hash keys' do
6
- assert_equal({'a' => 'b', 'c' => 'd', nil => 3}, JSI.stringify_symbol_keys({a: 'b', 'c' => 'd', nil => 3}))
7
- end
8
- it 'stringifies HashNode keys' do
9
- actual = JSI.stringify_symbol_keys(JSI::JSON::Node.new_doc({a: 'b', 'c' => 'd', nil => 3}))
10
- expected = JSI::JSON::Node.new_doc({'a' => 'b', 'c' => 'd', nil => 3})
11
- assert_equal(expected, actual)
12
- end
13
- it 'stringifies JSI hash keys' do
14
- schema = JSI::Schema.new({type: 'object'})
15
- expected = JSI.stringify_symbol_keys(schema.new_jsi({a: 'b', 'c' => 'd', nil => 3}))
16
- actual = schema.new_jsi({'a' => 'b', 'c' => 'd', nil => 3})
17
- assert_equal(expected, actual)
18
- end
19
- describe 'non-hash-like argument' do
20
- it 'errors' do
21
- err = assert_raises(ArgumentError) { JSI.stringify_symbol_keys(nil) }
22
- assert_equal("expected argument to be a hash; got NilClass: nil", err.message)
23
- err = assert_raises(ArgumentError) { JSI.stringify_symbol_keys(JSI::JSON::Node.new_doc(3)) }
24
- assert_equal("expected argument to be a hash; got JSI::JSON::Node: #<JSI::JSON::Node # 3>", err.message)
25
- err = assert_raises(ArgumentError) { JSI.stringify_symbol_keys(JSI::Schema.new({}).new_jsi(3)) }
26
- assert_equal("expected argument to be a hash; got (JSI Schema Class: #): #<JSI 3>", err.message)
27
- end
28
- end
29
- end
30
- describe '.deep_stringify_symbol_keys' do
31
- it 'stringifies symbol hash keys' do
32
- actual = JSI.deep_stringify_symbol_keys({
33
- a: 'b',
34
- 'c' => [
35
- {d: true},
36
- [{'e' => 0}],
37
- ],
38
- nil => 3,
39
- })
40
- expected = {
41
- 'a' => 'b',
42
- 'c' => [
43
- {'d' => true},
44
- [{'e' => 0}],
45
- ],
46
- nil => 3,
47
- }
48
- assert_equal(expected, actual)
49
- end
50
- it 'deep stringifies HashNode keys' do
51
- actual = JSI.deep_stringify_symbol_keys(JSI::JSON::Node.new_doc({a: 'b', 'c' => {d: 0}, nil => 3}))
52
- expected = JSI::JSON::Node.new_doc({'a' => 'b', 'c' => {'d' => 0}, nil => 3})
53
- assert_equal(expected, actual)
54
- end
55
- it 'deep stringifies JSI instance' do
56
- schema = JSI::Schema.new(type: 'object')
57
- actual = JSI.deep_stringify_symbol_keys(schema.new_jsi(JSI::JSON::Node.new_doc({a: 'b', 'c' => {d: 0}, nil => 3})))
58
- expected = schema.new_jsi(JSI::JSON::Node.new_doc({'a' => 'b', 'c' => {'d' => 0}, nil => 3}))
59
- assert_equal(expected, actual)
60
- end
61
- end
62
- end