jsi 0.0.4 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +3 -1
  3. data/CHANGELOG.md +48 -0
  4. data/LICENSE.md +613 -0
  5. data/README.md +84 -45
  6. data/jsi.gemspec +11 -14
  7. data/lib/jsi.rb +31 -12
  8. data/lib/jsi/base.rb +310 -344
  9. data/lib/jsi/base/to_rb.rb +2 -0
  10. data/lib/jsi/jsi_coder.rb +91 -0
  11. data/lib/jsi/json-schema-fragments.rb +3 -135
  12. data/lib/jsi/json.rb +3 -0
  13. data/lib/jsi/json/node.rb +72 -197
  14. data/lib/jsi/json/pointer.rb +419 -0
  15. data/lib/jsi/metaschema.rb +7 -0
  16. data/lib/jsi/metaschema_node.rb +218 -0
  17. data/lib/jsi/pathed_node.rb +118 -0
  18. data/lib/jsi/schema.rb +168 -223
  19. data/lib/jsi/schema_classes.rb +158 -0
  20. data/lib/jsi/simple_wrap.rb +12 -0
  21. data/lib/jsi/typelike_modules.rb +71 -45
  22. data/lib/jsi/util.rb +47 -57
  23. data/lib/jsi/version.rb +1 -1
  24. data/lib/schemas/json-schema.org/draft-04/schema.rb +7 -0
  25. data/lib/schemas/json-schema.org/draft-06/schema.rb +7 -0
  26. data/resources/icons/AGPL-3.0.png +0 -0
  27. data/test/base_array_test.rb +210 -84
  28. data/test/base_hash_test.rb +201 -58
  29. data/test/base_test.rb +212 -121
  30. data/test/jsi_coder_test.rb +85 -0
  31. data/test/jsi_json_arraynode_test.rb +26 -25
  32. data/test/jsi_json_hashnode_test.rb +40 -39
  33. data/test/jsi_json_node_test.rb +95 -126
  34. data/test/jsi_json_pointer_test.rb +102 -0
  35. data/test/jsi_typelike_as_json_test.rb +53 -0
  36. data/test/metaschema_node_test.rb +19 -0
  37. data/test/schema_module_test.rb +21 -0
  38. data/test/schema_test.rb +109 -97
  39. data/test/spreedly_openapi_test.rb +8 -0
  40. data/test/test_helper.rb +42 -8
  41. data/test/util_test.rb +14 -14
  42. metadata +54 -25
  43. data/LICENSE.txt +0 -21
  44. data/lib/jsi/schema_instance_json_coder.rb +0 -83
  45. data/lib/jsi/struct_json_coder.rb +0 -30
  46. data/test/schema_instance_json_coder_test.rb +0 -121
  47. data/test/struct_json_coder_test.rb +0 -130
@@ -1,121 +0,0 @@
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
- end
@@ -1,130 +0,0 @@
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