json2ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,130 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSON2Ruby::Collection do
4
+ describe '#initialize' do
5
+ it 'should set up correct attrs' do
6
+ x = JSON2Ruby::Collection.new('hello',{ :test => 1, :test2 => 2})
7
+ expect(x.name).to eq("hello")
8
+ expect(x.ruby_types).to eq({ :test => 1, :test2 => 2})
9
+ end
10
+
11
+ it 'should have the correct defaults' do
12
+ x = JSON2Ruby::Collection.new('hello')
13
+ expect(x.name).to eq("hello")
14
+ expect(x.ruby_types).to eq({})
15
+ end
16
+ end
17
+
18
+ describe '#short_name' do
19
+ it 'should have the correct short name' do
20
+ expect(JSON2Ruby::Collection.short_name).to eq('Collection')
21
+ end
22
+ end
23
+
24
+ describe '#self.parse_from' do
25
+ it 'should parse' do
26
+ obj = [
27
+ "one",
28
+ 2,
29
+ 3.14,
30
+ true,
31
+ {
32
+ "five" => 5
33
+ },
34
+ [
35
+ "six"
36
+ ]
37
+ ]
38
+
39
+ col = JSON2Ruby::Collection.parse_from("Test",obj)
40
+
41
+ expect(col).to be_a(JSON2Ruby::Collection)
42
+ expect(col.name).to eq("Test")
43
+ expect(col.ruby_types.length).to eq(6)
44
+ expect(col.attr_hash).to eq("6c7447d1288a16012372de22075fdc19")
45
+ end
46
+
47
+ it 'should use numeric instead of int/float when told to' do
48
+ obj = [
49
+ "one",
50
+ 2,
51
+ 3.14,
52
+ true,
53
+ {
54
+ "five" => 5
55
+ },
56
+ [
57
+ "six"
58
+ ]
59
+ ]
60
+
61
+ col = JSON2Ruby::Collection.parse_from("Test",obj, {:forcenumeric=>true})
62
+
63
+ expect(col).to be_a(JSON2Ruby::Collection)
64
+ expect(col.name).to eq("Test")
65
+ expect(col.ruby_types.length).to eq(4)
66
+ expect(col.attr_hash).to eq("e38d96234bfbf21545ed4ab2128b9ab4")
67
+ end
68
+ end
69
+
70
+ describe '#attr_hash' do
71
+ it 'should return correct hash' do
72
+ types = {
73
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
74
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
75
+ }
76
+ x = JSON2Ruby::Collection.new('hello',types)
77
+ expect(x.attr_hash).to eq("b99a7214ee0aa2cb312890eb5374aa20")
78
+
79
+ x.ruby_types = {
80
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
81
+ }
82
+ expect(x.attr_hash).to eq("5392fad6bee373074dbd3edfae6506f2")
83
+ end
84
+ end
85
+
86
+ describe '#==' do
87
+ it 'should return false if class mismatch' do
88
+ x = JSON2Ruby::Collection.new('hello','world')
89
+
90
+ expect(x=={}).to be(false)
91
+ expect(x==[]).to be(false)
92
+ expect(x==1).to be(false)
93
+ expect(x=="hello").to be(false)
94
+ expect(x==nil).to be(false)
95
+ end
96
+
97
+ it 'should return true if and only if attr_hash matches' do
98
+ types = {
99
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
100
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
101
+ }
102
+ x = JSON2Ruby::Collection.new('hello',types)
103
+
104
+ types = {
105
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
106
+ }
107
+ y = JSON2Ruby::Collection.new('hello',types)
108
+
109
+ types = {
110
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
111
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
112
+ }
113
+ z = JSON2Ruby::Collection.new('hello',types)
114
+
115
+ expect(x==x).to be(true)
116
+ expect(x==y).to be(false)
117
+ expect(x==z).to be(true)
118
+ end
119
+ end
120
+
121
+ describe '#comment' do
122
+ it 'should return correct values' do
123
+ x = JSON2Ruby::Collection.new('hello')
124
+ expect(x.comment).to eq("hello[]")
125
+
126
+ x.original_name = "test!"
127
+ expect(x.comment).to eq("hello[] (test!)")
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,178 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSON2Ruby::Entity do
4
+ describe '#initialize' do
5
+ it 'should set up correct attrs' do
6
+ x = JSON2Ruby::Entity.new('hello',{ :test => 1, :test2 => 2})
7
+ expect(x.name).to eq("hello")
8
+ expect(x.attributes).to eq({ :test => 1, :test2 => 2})
9
+ end
10
+
11
+ it 'should have the correct defaults' do
12
+ x = JSON2Ruby::Entity.new('hello')
13
+ expect(x.name).to eq("hello")
14
+ expect(x.attributes).to eq({})
15
+ end
16
+ end
17
+
18
+ describe '#short_name' do
19
+ it 'should have the correct short name' do
20
+ expect(JSON2Ruby::Entity.short_name).to eq('Entity')
21
+ end
22
+ end
23
+
24
+ describe '#self.parse_from' do
25
+ it 'should parse' do
26
+ obj = {
27
+ "one" => "1",
28
+ "two" => 2,
29
+ "three" => 3.14,
30
+ "four" => true,
31
+ "five" => {
32
+ "5.1"=>5.1
33
+ },
34
+ "six" => [ 1,2,3 ]
35
+ }
36
+
37
+ ent = JSON2Ruby::Entity.parse_from("Test",obj)
38
+
39
+ expect(ent).to be_a(JSON2Ruby::Entity)
40
+ expect(ent.name).to eq("Test")
41
+ expect(ent.attributes.length).to eq(6)
42
+ expect(ent.attr_hash).to eq("e17024aea3f6f3623dda7170697a72dd")
43
+ end
44
+
45
+ it 'should use numeric instead of int/float when told to' do
46
+ obj = {
47
+ "one" => "1",
48
+ "two" => 2,
49
+ "three" => 3.14,
50
+ "four" => true,
51
+ "five" => {
52
+ "5.1"=>5.1
53
+ },
54
+ "six" => [ 1,2,3 ]
55
+ }
56
+
57
+ ent = JSON2Ruby::Entity.parse_from("Test",obj, {:forcenumeric=>true})
58
+
59
+ expect(ent).to be_a(JSON2Ruby::Entity)
60
+ expect(ent.name).to eq("Test")
61
+ expect(ent.attributes.length).to eq(6)
62
+ expect(ent.attr_hash).to eq("592884f7d98c4a31b7d641b10636d546")
63
+ end
64
+ end
65
+
66
+ describe '#attr_hash' do
67
+ it 'should return correct hash' do
68
+ attrs = {
69
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
70
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
71
+ }
72
+ x = JSON2Ruby::Entity.new('hello',attrs)
73
+ expect(x.attr_hash).to eq("91c9d28d138816b43934d63c0a4b443f")
74
+
75
+ x.attributes = {
76
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
77
+ }
78
+ expect(x.attr_hash).to eq("1bf08c8584361b8b12e1278e0e095340")
79
+ end
80
+ end
81
+
82
+ describe '#==' do
83
+ it 'should return false if class mismatch' do
84
+ x = JSON2Ruby::Entity.new('hello','world')
85
+
86
+ expect(x=={}).to be(false)
87
+ expect(x==[]).to be(false)
88
+ expect(x==1).to be(false)
89
+ expect(x=="hello").to be(false)
90
+ expect(x==nil).to be(false)
91
+ end
92
+
93
+ it 'should return true if and only if attr_hash matches' do
94
+ attrs = {
95
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
96
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
97
+ }
98
+ x = JSON2Ruby::Entity.new('hello',attrs)
99
+
100
+ attrs = {
101
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
102
+ }
103
+ y = JSON2Ruby::Entity.new('hello',attrs)
104
+
105
+ attrs = {
106
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
107
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
108
+ }
109
+ z = JSON2Ruby::Entity.new('hello',attrs)
110
+
111
+
112
+ expect(x==x).to be(true)
113
+ expect(x==y).to be(false)
114
+ expect(x==z).to be(true)
115
+ end
116
+ end
117
+
118
+ describe '#self.get_next_unknown' do
119
+
120
+ it 'should return different results after each call' do
121
+
122
+ x = JSON2Ruby::Entity.get_next_unknown
123
+ y = JSON2Ruby::Entity.get_next_unknown
124
+ z = JSON2Ruby::Entity.get_next_unknown
125
+
126
+ expect(x==y).to be(false)
127
+ expect(y==z).to be(false)
128
+ expect(z==x).to be(false)
129
+ end
130
+ end
131
+
132
+ describe '#self.reset_parse' do
133
+ it 'should be standard after reset' do
134
+ JSON2Ruby::Entity.class_variable_set('@@objs',[1,2,3])
135
+
136
+ JSON2Ruby::Entity.reset_parse
137
+
138
+ expect(JSON2Ruby::Entity.entities).to eq({
139
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
140
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
141
+ JSON2Ruby::RUBYFLOAT.attr_hash => JSON2Ruby::RUBYFLOAT,
142
+ JSON2Ruby::RUBYBOOLEAN.attr_hash => JSON2Ruby::RUBYBOOLEAN,
143
+ JSON2Ruby::RUBYNUMERIC.attr_hash => JSON2Ruby::RUBYNUMERIC,
144
+ })
145
+ end
146
+ end
147
+
148
+ describe '#self.entities' do
149
+ it 'should return @@objs' do
150
+ JSON2Ruby::Entity.class_variable_set('@@objs',[1,2,3])
151
+ expect(JSON2Ruby::Entity.entities).to eq([1,2,3])
152
+ end
153
+
154
+ it 'should be standard after reset' do
155
+ JSON2Ruby::Entity.class_variable_set('@@objs',[1,2,3])
156
+
157
+ JSON2Ruby::Entity.reset_parse
158
+
159
+ expect(JSON2Ruby::Entity.entities).to eq({
160
+ JSON2Ruby::RUBYSTRING.attr_hash => JSON2Ruby::RUBYSTRING,
161
+ JSON2Ruby::RUBYINTEGER.attr_hash => JSON2Ruby::RUBYINTEGER,
162
+ JSON2Ruby::RUBYFLOAT.attr_hash => JSON2Ruby::RUBYFLOAT,
163
+ JSON2Ruby::RUBYBOOLEAN.attr_hash => JSON2Ruby::RUBYBOOLEAN,
164
+ JSON2Ruby::RUBYNUMERIC.attr_hash => JSON2Ruby::RUBYNUMERIC,
165
+ })
166
+ end
167
+ end
168
+
169
+ describe '#comment' do
170
+ it 'should return correct values' do
171
+ x = JSON2Ruby::Entity.new('hello')
172
+ expect(x.comment).to eq("hello")
173
+
174
+ x.original_name = "test!"
175
+ expect(x.comment).to eq("hello (test!)")
176
+ end
177
+ end
178
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ describe JSON2Ruby::Primitive do
4
+ describe '#initialize' do
5
+ it 'should set up correct attrs' do
6
+ x = JSON2Ruby::Primitive.new('hello','world')
7
+ expect(x.name).to eq("hello")
8
+ expect(x.attr_hash).to eq("world")
9
+ end
10
+ end
11
+
12
+ describe '#short_name' do
13
+ it 'should have the correct short name' do
14
+ expect(JSON2Ruby::Primitive.short_name).to eq('Primitive')
15
+ end
16
+ end
17
+
18
+ describe '#attr_hash' do
19
+ it 'should return hash' do
20
+ x = JSON2Ruby::Primitive.new('hello','world')
21
+ expect(x.attr_hash).to eq("world")
22
+ end
23
+ end
24
+
25
+ describe '#comment' do
26
+ it 'should return hash' do
27
+ x = JSON2Ruby::Primitive.new('hello','world')
28
+ expect(x.comment).to eq("hello")
29
+ end
30
+ end
31
+
32
+ describe 'Standard Primitives' do
33
+ it 'should have standard primitives correctly defined' do
34
+
35
+ expect(JSON2Ruby::RUBYSTRING.name).to eq('String')
36
+ expect(JSON2Ruby::RUBYSTRING.attr_hash).to eq('0123456789ABCDEF0123456789ABCDEF')
37
+
38
+ expect(JSON2Ruby::RUBYINTEGER.name).to eq('Integer')
39
+ expect(JSON2Ruby::RUBYINTEGER.attr_hash).to eq('0123456789ABCDEF0123456789ABCDE0')
40
+
41
+ expect(JSON2Ruby::RUBYFLOAT.name).to eq('Float')
42
+ expect(JSON2Ruby::RUBYFLOAT.attr_hash).to eq('0123456789ABCDEF0123456789ABCDE1')
43
+
44
+ expect(JSON2Ruby::RUBYBOOLEAN.name).to eq('Boolean')
45
+ expect(JSON2Ruby::RUBYBOOLEAN.attr_hash).to eq('0123456789ABCDEF0123456789ABCDE2')
46
+
47
+ expect(JSON2Ruby::RUBYNUMERIC.name).to eq('Numeric')
48
+ expect(JSON2Ruby::RUBYNUMERIC.attr_hash).to eq('0123456789ABCDEF0123456789ABCDE2')
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+ require 'ostruct'
3
+
4
+ describe JSON2Ruby::RubyWriter do
5
+
6
+ describe '#to_code' do
7
+ it 'should return correct code for classes' do
8
+ entity = OpenStruct.new({
9
+ :name => "TestEntitiy",
10
+ })
11
+
12
+ options = {
13
+ :require => [ 'recone', 'rectwo', 'recthree' ],
14
+ :include => [ 'incone', 'inctwo', 'incthree' ],
15
+ :extend => [ 'excone', 'exctwo', 'excthree' ],
16
+ :superclass_name => 'Superclass',
17
+ :modules => false,
18
+ }
19
+
20
+ expect(JSON2Ruby::RubyWriter).to receive(:attributes_to_ruby).with(entity, 2, options).and_return("")
21
+
22
+ expect(JSON2Ruby::RubyWriter.to_code(entity,0,options)).to eq("require 'recone'\r\nrequire 'rectwo'\r\nrequire 'recthree'\r\n\r\nclass TestEntitiy < Superclass\r\n extend excone\r\n extend exctwo\r\n extend excthree\r\n\r\n include incone\r\n include inctwo\r\n include incthree\r\n\r\nend\r\n")
23
+ end
24
+
25
+ it 'should return correct code for modules' do
26
+ entity = OpenStruct.new({
27
+ :name => "TestEntitiy",
28
+ })
29
+
30
+ options = {
31
+ :require => [ 'recone', 'rectwo', 'recthree' ],
32
+ :include => [ 'incone', 'inctwo', 'incthree' ],
33
+ :extend => [ 'excone', 'exctwo', 'excthree' ],
34
+ :superclass_name => 'Superclass',
35
+ :modules => true,
36
+ }
37
+
38
+ expect(JSON2Ruby::RubyWriter).to receive(:attributes_to_ruby).with(entity, 2, options).and_return("")
39
+
40
+ expect(JSON2Ruby::RubyWriter.to_code(entity,0,options)).to eq("require 'recone'\r\nrequire 'rectwo'\r\nrequire 'recthree'\r\n\r\nmodule TestEntitiy < Superclass\r\n extend excone\r\n extend exctwo\r\n extend excthree\r\n\r\n include incone\r\n include inctwo\r\n include incthree\r\n\r\nend\r\n")
41
+ end
42
+ end
43
+
44
+ describe '#attributes_to_ruby' do
45
+ it 'should return correct code for simple attrs' do
46
+ entity = OpenStruct.new({
47
+ :name => "TestEntitiy",
48
+ :attributes => {
49
+ "test1" => OpenStruct.new({:comment => 'test1 comment'}),
50
+ "test2" => OpenStruct.new({:comment => 'test2 comment'}),
51
+ }
52
+ })
53
+
54
+ options = {
55
+ :collectionmethod => 'collectionmethod',
56
+ :attributemethod => 'attributemethod',
57
+ }
58
+
59
+ expect(JSON2Ruby::RubyWriter.attributes_to_ruby(entity,0,options)).to eq("attributemethod :test1 # test1 comment\r\nattributemethod :test2 # test2 comment\r\n")
60
+ end
61
+
62
+ it 'should return correct code for attrs that are primitives' do
63
+ entity = OpenStruct.new({
64
+ :name => "TestEntitiy",
65
+ :attributes => {
66
+ "test1" => JSON2Ruby::RUBYSTRING,
67
+ "test2" => OpenStruct.new({:comment => 'test2 comment'}),
68
+ }
69
+ })
70
+
71
+ options = {
72
+ :collectionmethod => 'collectionmethod',
73
+ :attributemethod => 'attributemethod',
74
+ }
75
+
76
+ expect(JSON2Ruby::RubyWriter.attributes_to_ruby(entity,0,options)).to eq("attributemethod :test1 # String\r\nattributemethod :test2 # test2 comment\r\n")
77
+ end
78
+
79
+ it 'should return correct code for collections' do
80
+ entity = OpenStruct.new({
81
+ :name => "TestEntitiy",
82
+ :attributes => {
83
+ "testCollection" => JSON2Ruby::Collection.new("testCol"),
84
+ "test2" => OpenStruct.new({:comment => 'test2 comment'}),
85
+ }
86
+ })
87
+
88
+ options = {
89
+ :collectionmethod => 'collectionmethod',
90
+ :attributemethod => 'attributemethod',
91
+ }
92
+
93
+ expect(JSON2Ruby::RubyWriter.attributes_to_ruby(entity,0,options)).to eq("collectionmethod :testCollection # testCol[]\r\nattributemethod :test2 # test2 comment\r\n")
94
+ end
95
+
96
+ it 'should return correct code for attrs and include type as second param' do
97
+ entity = OpenStruct.new({
98
+ :name => "TestEntitiy",
99
+ :attributes => {
100
+ "test1" => OpenStruct.new({:comment => 'test1 comment'}),
101
+ "test2" => OpenStruct.new({:comment => 'test2 comment'}),
102
+ }
103
+ })
104
+
105
+ options = {
106
+ :collectionmethod => 'collectionmethod',
107
+ :attributemethod => 'attributemethod',
108
+ :includetypes => true,
109
+ }
110
+
111
+ expect(JSON2Ruby::RubyWriter.attributes_to_ruby(entity,0,options)).to eq("attributemethod :test1, '' # test1 comment\r\nattributemethod :test2, '' # test2 comment\r\n")
112
+ end
113
+
114
+
115
+ end
116
+
117
+ end