jsi 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/jsi_test.rb ADDED
@@ -0,0 +1,11 @@
1
+ require_relative 'test_helper'
2
+
3
+ class JSITest < Minitest::Test
4
+ def test_that_it_has_a_version_number
5
+ refute_nil ::JSI::VERSION
6
+ end
7
+
8
+ def test_it_does_something_useful
9
+ assert true
10
+ end
11
+ end
@@ -0,0 +1,122 @@
1
+ require_relative 'test_helper'
2
+
3
+ describe JSI::SchemaInstanceJSONCoder do
4
+ let(:schema_instance_class) { JSI.class_for_schema(properties: {foo: {}, bar: {}}) }
5
+ let(:options) { {} }
6
+ let(:schema_instance_json_coder) { JSI::SchemaInstanceJSONCoder.new(schema_instance_class, options) }
7
+ describe 'json' do
8
+ describe 'load' do
9
+ it 'loads nil' do
10
+ assert_nil(schema_instance_json_coder.load(nil))
11
+ end
12
+ it 'loads a hash' do
13
+ assert_equal(schema_instance_class.new(foo: 'bar'), schema_instance_json_coder.load({"foo" => "bar"}))
14
+ end
15
+ it 'loads something else' do
16
+ assert_equal(schema_instance_class.new([[]]), schema_instance_json_coder.load([[]]))
17
+ end
18
+ describe 'array' do
19
+ let(:options) { {array: true} }
20
+ it 'loads an array of hashes' do
21
+ data = [{"foo" => "bar"}, {"foo" => "baz"}]
22
+ assert_equal([schema_instance_class.new(foo: 'bar'), schema_instance_class.new(foo: 'baz')], schema_instance_json_coder.load(data))
23
+ end
24
+ it 'loads an empty array' do
25
+ assert_equal([], schema_instance_json_coder.load([]))
26
+ end
27
+ it 'loads a not an array' do
28
+ assert_raises(TypeError) do
29
+ schema_instance_json_coder.load(Object.new)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ describe 'dump' do
35
+ it 'dumps nil' do
36
+ assert_nil(schema_instance_json_coder.dump(nil))
37
+ end
38
+ it 'dumps a schema_instance_class' do
39
+ assert_equal({"foo" => "x","bar" => "y"}, schema_instance_json_coder.dump(schema_instance_class.new(foo: 'x', bar: 'y')))
40
+ end
41
+ it 'dumps something else' do
42
+ assert_raises(TypeError) do
43
+ schema_instance_json_coder.dump(Object.new)
44
+ end
45
+ end
46
+ it 'dumps some of the keys of a schema_instance_class after loading in a partial one' do
47
+ schema_instance_class = schema_instance_json_coder.load({'foo' => 'who'})
48
+ assert_equal({'foo' => 'who'}, schema_instance_json_coder.dump(schema_instance_class))
49
+ schema_instance_class.bar = 'whar'
50
+ assert_equal({'foo' => 'who', 'bar' => 'whar'}, schema_instance_json_coder.dump(schema_instance_class))
51
+ end
52
+ describe 'array' do
53
+ let(:options) { {array: true} }
54
+ it 'dumps an array of schema_instances' do
55
+ schema_instances = [schema_instance_class.new(foo: 'x', bar: 'y'), schema_instance_class.new(foo: 'z', bar: 'q')]
56
+ assert_equal([{"foo" => "x","bar" => "y"},{"foo" => "z","bar" => "q"}], schema_instance_json_coder.dump(schema_instances))
57
+ end
58
+ end
59
+ end
60
+ end
61
+ describe 'string' do
62
+ let(:options) { {string: true} }
63
+ describe 'load' do
64
+ it 'loads nil' do
65
+ assert_nil(schema_instance_json_coder.load(nil))
66
+ end
67
+ it 'loads a hash' do
68
+ assert_equal(schema_instance_class.new(foo: 'bar'), schema_instance_json_coder.load('{"foo": "bar"}'))
69
+ end
70
+ it 'loads something else' do
71
+ assert_equal(schema_instance_class.new([[]]), schema_instance_json_coder.load('[[]]'))
72
+ end
73
+ it 'loads something that is not a json string' do
74
+ assert_raises(::JSON::ParserError) do
75
+ schema_instance_json_coder.load('??')
76
+ end
77
+ end
78
+ describe 'array' do
79
+ let(:options) { {string: true, array: true} }
80
+ it 'loads an array of hashes' do
81
+ data = '[{"foo": "bar"}, {"foo": "baz"}]'
82
+ assert_equal([schema_instance_class.new(foo: 'bar'), schema_instance_class.new(foo: 'baz')], schema_instance_json_coder.load(data))
83
+ end
84
+ it 'loads an empty array' do
85
+ assert_equal([], schema_instance_json_coder.load('[]'))
86
+ end
87
+ it 'loads a not an array' do
88
+ assert_raises(TypeError) do
89
+ schema_instance_json_coder.load('{}')
90
+ end
91
+ end
92
+ end
93
+ end
94
+ describe 'dump' do
95
+ it 'dumps nil' do
96
+ assert_nil(schema_instance_json_coder.dump(nil))
97
+ end
98
+ it 'dumps a schema_instance_class' do
99
+ assert_equal('{"foo":"x","bar":"y"}', schema_instance_json_coder.dump(schema_instance_class.new(foo: 'x', bar: 'y')))
100
+ end
101
+ it 'dumps something else' do
102
+ assert_raises(TypeError) do
103
+ schema_instance_json_coder.dump(Object.new)
104
+ end
105
+ end
106
+ it 'dumps some of the keys of a schema_instance_class after loading in a partial one' do
107
+ schema_instance_class = schema_instance_json_coder.load('{"foo": "who"}')
108
+ assert_equal("{\"foo\":\"who\"}", schema_instance_json_coder.dump(schema_instance_class))
109
+ schema_instance_class.bar = 'whar'
110
+ assert_equal("{\"foo\":\"who\",\"bar\":\"whar\"}", schema_instance_json_coder.dump(schema_instance_class))
111
+ end
112
+ describe 'array' do
113
+ let(:options) { {string: true, array: true} }
114
+ it 'dumps an array of schema_instances' do
115
+ schema_instances = [schema_instance_class.new(foo: 'x', bar: 'y'), schema_instance_class.new(foo: 'z', bar: 'q')]
116
+ assert_equal('[{"foo":"x","bar":"y"},{"foo":"z","bar":"q"}]', schema_instance_json_coder.dump(schema_instances))
117
+ end
118
+ end
119
+ end
120
+ end
121
+
122
+ end
@@ -0,0 +1,130 @@
1
+ require_relative 'test_helper'
2
+
3
+ describe JSI::StructJSONCoder do
4
+ let(:struct) { Struct.new(:foo, :bar) }
5
+ let(:options) { {} }
6
+ let(:struct_json_coder) { JSI::StructJSONCoder.new(struct, options) }
7
+ describe 'json' do
8
+ describe 'load' do
9
+ it 'loads nil' do
10
+ assert_nil(struct_json_coder.load(nil))
11
+ end
12
+ it 'loads a hash' do
13
+ assert_equal(struct.new('bar'), struct_json_coder.load({"foo" => "bar"}))
14
+ end
15
+ it 'loads something else' do
16
+ assert_raises(JSI::StructJSONCoder::LoadError) do
17
+ struct_json_coder.load([[]])
18
+ end
19
+ end
20
+ it 'loads unrecognized keys' do
21
+ assert_raises(JSI::StructJSONCoder::LoadError) do
22
+ struct_json_coder.load({"uhoh" => "spaghettio"})
23
+ end
24
+ end
25
+ describe 'array' do
26
+ let(:options) { {array: true} }
27
+ it 'loads an array of hashes' do
28
+ data = [{"foo" => "bar"}, {"foo" => "baz"}]
29
+ assert_equal([struct.new('bar'), struct.new('baz')], struct_json_coder.load(data))
30
+ end
31
+ it 'loads an empty array' do
32
+ assert_equal([], struct_json_coder.load([]))
33
+ end
34
+ end
35
+ end
36
+ describe 'dump' do
37
+ it 'dumps nil' do
38
+ assert_nil(struct_json_coder.dump(nil))
39
+ end
40
+ it 'dumps a struct' do
41
+ assert_equal({"foo" => "x","bar" => "y"}, struct_json_coder.dump(struct.new('x', 'y')))
42
+ end
43
+ it 'dumps something else' do
44
+ assert_raises(TypeError) do
45
+ struct_json_coder.dump(Object.new)
46
+ end
47
+ end
48
+ it 'dumps all the keys of a struct after loading in a partial one' do
49
+ struct = struct_json_coder.load({'foo' => 'who'})
50
+ assert_equal({'foo' => 'who', 'bar' => nil}, struct_json_coder.dump(struct))
51
+ struct.bar = 'whar'
52
+ assert_equal({'foo' => 'who', 'bar' => 'whar'}, struct_json_coder.dump(struct))
53
+ end
54
+ describe 'array' do
55
+ let(:options) { {array: true} }
56
+ it 'dumps an array of structs' do
57
+ structs = [struct.new('x', 'y'), struct.new('z', 'q')]
58
+ assert_equal([{"foo" => "x","bar" => "y"},{"foo" => "z","bar" => "q"}], struct_json_coder.dump(structs))
59
+ end
60
+ end
61
+ end
62
+ end
63
+ describe 'string' do
64
+ let(:options) { {string: true} }
65
+ describe 'load' do
66
+ it 'loads nil' do
67
+ assert_nil(struct_json_coder.load(nil))
68
+ end
69
+ it 'loads a hash' do
70
+ assert_equal(struct.new('bar'), struct_json_coder.load('{"foo": "bar"}'))
71
+ end
72
+ it 'loads something else' do
73
+ assert_raises(JSI::StructJSONCoder::LoadError) do
74
+ struct_json_coder.load('[[]]')
75
+ end
76
+ end
77
+ it 'loads something that is not a json string' do
78
+ assert_raises(JSON::ParserError) do
79
+ struct_json_coder.load('??')
80
+ end
81
+ end
82
+ it 'loads unrecognized keys' do
83
+ assert_raises(JSI::StructJSONCoder::LoadError) do
84
+ struct_json_coder.load('{"uhoh": "spaghettio"}')
85
+ end
86
+ end
87
+ describe 'array' do
88
+ let(:options) { {string: true, array: true} }
89
+ it 'loads an array of hashes' do
90
+ data = '[{"foo": "bar"}, {"foo": "baz"}]'
91
+ assert_equal([struct.new('bar'), struct.new('baz')], struct_json_coder.load(data))
92
+ end
93
+ it 'loads an empty array' do
94
+ assert_equal([], struct_json_coder.load('[]'))
95
+ end
96
+ it 'loads a not an array' do
97
+ assert_raises(TypeError) do
98
+ struct_json_coder.load('{}')
99
+ end
100
+ end
101
+ end
102
+ end
103
+ describe 'dump' do
104
+ it 'dumps nil' do
105
+ assert_nil(struct_json_coder.dump(nil))
106
+ end
107
+ it 'dumps a struct' do
108
+ assert_equal('{"foo":"x","bar":"y"}', struct_json_coder.dump(struct.new('x', 'y')))
109
+ end
110
+ it 'dumps something else' do
111
+ assert_raises(TypeError) do
112
+ struct_json_coder.dump(Object.new)
113
+ end
114
+ end
115
+ it 'dumps all the keys of a struct after loading in a partial one' do
116
+ struct = struct_json_coder.load('{"foo": "who"}')
117
+ assert_equal("{\"foo\":\"who\",\"bar\":null}", struct_json_coder.dump(struct))
118
+ struct.bar = 'whar'
119
+ assert_equal("{\"foo\":\"who\",\"bar\":\"whar\"}", struct_json_coder.dump(struct))
120
+ end
121
+ describe 'array' do
122
+ let(:options) { {string: true, array: true} }
123
+ it 'dumps an array of structs' do
124
+ structs = [struct.new('x', 'y'), struct.new('z', 'q')]
125
+ assert_equal('[{"foo":"x","bar":"y"},{"foo":"z","bar":"q"}]', struct_json_coder.dump(structs))
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,29 @@
1
+ require 'coveralls'
2
+ if Coveralls.will_run?
3
+ Coveralls.wear!
4
+ end
5
+
6
+ require 'simplecov'
7
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
8
+ require 'jsi'
9
+
10
+ # NO EXPECTATIONS
11
+ ENV["MT_NO_EXPECTATIONS"] = ''
12
+
13
+ require 'minitest/autorun'
14
+ require 'minitest/around/spec'
15
+ require 'minitest/reporters'
16
+
17
+ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
18
+
19
+ require 'byebug'
20
+
21
+ class JSISpec < Minitest::Spec
22
+ def assert_equal exp, act, msg = nil
23
+ msg = message(msg, E) { diff exp, act }
24
+ assert exp == act, msg
25
+ end
26
+ end
27
+
28
+ # register this to be the base class for specs instead of Minitest::Spec
29
+ Minitest::Spec.register_spec_type(//, JSISpec)
data/test/util_test.rb ADDED
@@ -0,0 +1,62 @@
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::HashNode.new({a: 'b', 'c' => 'd', nil => 3}, []))
10
+ expected = JSI::JSON::HashNode.new({'a' => 'b', 'c' => 'd', nil => 3}, [])
11
+ assert_equal(expected, actual)
12
+ end
13
+ it 'stringifies SchemaObjectBase hash keys' do
14
+ klass = JSI.class_for_schema(type: 'object')
15
+ expected = JSI.stringify_symbol_keys(klass.new(JSI::JSON::HashNode.new({a: 'b', 'c' => 'd', nil => 3}, [])))
16
+ actual = klass.new(JSI::JSON::HashNode.new({'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(3, [])) }
24
+ assert_equal("expected argument to be a hash; got JSI::JSON::Node: #<JSI::JSON::Node fragment=\"#\" 3>", err.message)
25
+ err = assert_raises(ArgumentError) { JSI.stringify_symbol_keys(JSI.class_for_schema({}).new(JSI::JSON::Node.new(3, []))) }
26
+ assert_match(%r(\Aexpected argument to be a hash; got JSI::SchemaClasses\["[^"]+#"\]: #<JSI::SchemaClasses\["[^"]+#"\]\n #<JSI::JSON::Node fragment="#" 3>\n>\z)m, 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::HashNode.new({a: 'b', 'c' => {d: 0}, nil => 3}, []))
52
+ expected = JSI::JSON::HashNode.new({'a' => 'b', 'c' => {'d' => 0}, nil => 3}, [])
53
+ assert_equal(expected, actual)
54
+ end
55
+ it 'deep stringifies SchemaObjectBase instance on initialize' do
56
+ klass = JSI.class_for_schema(type: 'object')
57
+ expected = klass.new(JSI::JSON::HashNode.new({a: 'b', 'c' => {d: 0}, nil => 3}, []))
58
+ actual = klass.new(JSI::JSON::HashNode.new({'a' => 'b', 'c' => {'d' => 0}, nil => 3}, []))
59
+ assert_equal(expected, actual)
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ethan
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json-schema
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest-around
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest-reporters
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: JSI represents json-schemas as ruby classes and json-schema instances
84
+ as instances of those classes
85
+ email:
86
+ - ethan@unth
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".simplecov"
92
+ - CHANGELOG.md
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile.rb
96
+ - jsi.gemspec
97
+ - lib/jsi.rb
98
+ - lib/jsi/base.rb
99
+ - lib/jsi/base/to_rb.rb
100
+ - lib/jsi/json-schema-fragments.rb
101
+ - lib/jsi/json.rb
102
+ - lib/jsi/json/node.rb
103
+ - lib/jsi/schema.rb
104
+ - lib/jsi/schema_instance_json_coder.rb
105
+ - lib/jsi/struct_json_coder.rb
106
+ - lib/jsi/typelike_modules.rb
107
+ - lib/jsi/util.rb
108
+ - lib/jsi/version.rb
109
+ - test/base_array_test.rb
110
+ - test/base_hash_test.rb
111
+ - test/base_test.rb
112
+ - test/jsi_json_arraynode_test.rb
113
+ - test/jsi_json_hashnode_test.rb
114
+ - test/jsi_json_node_test.rb
115
+ - test/jsi_test.rb
116
+ - test/schema_instance_json_coder_test.rb
117
+ - test/struct_json_coder_test.rb
118
+ - test/test_helper.rb
119
+ - test/util_test.rb
120
+ homepage: https://github.com/notEthan/jsi
121
+ licenses:
122
+ - MIT
123
+ metadata: {}
124
+ post_install_message:
125
+ rdoc_options: []
126
+ require_paths:
127
+ - lib
128
+ required_ruby_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ requirements:
135
+ - - ">="
136
+ - !ruby/object:Gem::Version
137
+ version: '0'
138
+ requirements: []
139
+ rubyforge_project:
140
+ rubygems_version: 2.7.7
141
+ signing_key:
142
+ specification_version: 4
143
+ summary: 'JSI: JSON-Schema instantiation'
144
+ test_files:
145
+ - test/base_array_test.rb
146
+ - test/base_hash_test.rb
147
+ - test/base_test.rb
148
+ - test/jsi_json_arraynode_test.rb
149
+ - test/jsi_json_hashnode_test.rb
150
+ - test/jsi_json_node_test.rb
151
+ - test/jsi_test.rb
152
+ - test/schema_instance_json_coder_test.rb
153
+ - test/struct_json_coder_test.rb
154
+ - test/test_helper.rb
155
+ - test/util_test.rb