protojson 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,179 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'examples')
5
+
6
+ require 'protojson'
7
+
8
+ require 'simple.pb'
9
+ require 'repeated.pb'
10
+ require 'addressbook.pb'
11
+
12
+ describe Protojson::Codec::JsonTagMap do
13
+ before(:all) do
14
+ Protojson.set_default_codec(Protojson::Codec::JsonTagMap)
15
+ end
16
+
17
+ context "simple message" do
18
+
19
+ before(:each) do
20
+ @simple = Examples::Simple.new
21
+ @simple.foo = "Foo"
22
+ @simple.bar = 1000
23
+ @encoded = Protojson.encode(@simple)
24
+ end
25
+
26
+ it "should encode properly when two required attributes provided" do
27
+ @encoded.should eql('{"1":"Foo","2":1000}')
28
+ end
29
+
30
+ it "should decode properly" do
31
+ decoded = Protojson.decode(Examples::Simple, @encoded)
32
+ decoded.foo.should eql(@simple.foo)
33
+ decoded.bar.should eql(@simple.bar)
34
+ end
35
+
36
+ end
37
+
38
+ context "simple message (II)" do
39
+
40
+ before(:each) do
41
+ @simple = Examples::Simple.new
42
+ @simple.foo = "Foo"
43
+ @simple.bar = 1000
44
+ @simple.baz = "Bazz"
45
+ @encoded = Protojson.encode(@simple)
46
+ end
47
+
48
+ it "should encode properly when two required attributes and one optional attribute provided" do
49
+ @encoded.should eql('{"1":"Foo","2":1000,"3":"Bazz"}')
50
+ end
51
+
52
+ it "should decode properly" do
53
+ decoded = Protojson.decode(Examples::Simple, @encoded)
54
+ decoded.foo.should eql(@simple.foo)
55
+ decoded.bar.should eql(@simple.bar)
56
+ decoded.baz.should eql(@simple.baz)
57
+ end
58
+
59
+ end
60
+
61
+ context "repeated message" do
62
+
63
+ it "should encode properly when the string field has more than one value" do
64
+ repeated = Examples::Repeated.new
65
+ repeated.string << "one"
66
+ repeated.string << "two"
67
+ repeated.string << "three"
68
+ Protojson.encode(repeated).should eql('{"1":["one","two","three"]}')
69
+ end
70
+
71
+ it "should encode properly when the int field has more than one value" do
72
+ repeated = Examples::Repeated.new
73
+ repeated.int << 1
74
+ repeated.int << 2
75
+ repeated.int << 3
76
+ Protojson.encode(repeated).should eql('{"2":[1,2,3]}')
77
+ end
78
+
79
+ it "should encode properly when both int and string fields has more than one value" do
80
+ repeated = Examples::Repeated.new
81
+ repeated.string << "one"
82
+ repeated.string << "two"
83
+ repeated.string << "three"
84
+ repeated.int << 1
85
+ repeated.int << 2
86
+ repeated.int << 3
87
+ Protojson.encode(repeated).should eql('{"1":["one","two","three"],"2":[1,2,3]}')
88
+ end
89
+
90
+ it "should encode properly when the nested field has more than one value" do
91
+ repeated = Examples::Repeated.new
92
+ (1..3).each { |id|
93
+ nested = Examples::Repeated::Nested.new
94
+ nested.id = id
95
+ repeated.nested << nested
96
+ }
97
+ Protojson.encode(repeated).should eql('{"3":[{"1":1},{"1":2},{"1":3}]}')
98
+ end
99
+
100
+ end
101
+
102
+ context "complex message" do
103
+ it "should encode and decode the address_book example" do
104
+ book = Examples::AddressBook.new
105
+ person = Examples::Person.new
106
+ person.name = 'John Doe'
107
+ person.id = 2051
108
+ person.email = "john.doe@gmail.com"
109
+ phone = Examples::Person::PhoneNumber.new
110
+ phone.number = '1231231212'
111
+ phone.type = Examples::Person::PhoneType::HOME
112
+ person.phone << phone
113
+ phone = Examples::Person::PhoneNumber.new
114
+ phone.number = '55512321312'
115
+ phone.type = Examples::Person::PhoneType::MOBILE
116
+ person.phone << phone
117
+ book.person << person
118
+
119
+ person = Examples::Person.new
120
+ person.name = "Ivan Montes"
121
+ person.id = 23
122
+ person.email = "drslump@pollinimini.net"
123
+ phone = Examples::Person::PhoneNumber.new
124
+ phone.number = '3493123123'
125
+ phone.type = Examples::Person::PhoneType::WORK
126
+ person.phone << phone
127
+ book.person << person
128
+
129
+ person = Examples::Person.new
130
+ person.name = "Juan de Bravo"
131
+ person.id = 24
132
+ person.email = "juan@pollinimini.net"
133
+ phone = Examples::Person::PhoneNumber.new
134
+ phone.number = '3493123124'
135
+ phone.type = Examples::Person::PhoneType::WORK
136
+ person.phone << phone
137
+ book.person << person
138
+
139
+ expected =<<END
140
+ {
141
+ "1":[
142
+ {
143
+ "1":"John Doe",
144
+ "2":2051,
145
+ "3":"john.doe@gmail.com",
146
+ "4":[
147
+ {"1":"1231231212","2":1},
148
+ {"1":"55512321312","2":0}
149
+ ]
150
+ },
151
+ {
152
+ "1":"Ivan Montes",
153
+ "2":23,
154
+ "3":"drslump@pollinimini.net",
155
+ "4":[{"1":"3493123123","2":2}]
156
+ },
157
+ {
158
+ "1":"Juan de Bravo",
159
+ "2":24,
160
+ "3":"juan@pollinimini.net",
161
+ "4":[{"1":"3493123124","2":2}]
162
+ }
163
+ ]
164
+ }
165
+ END
166
+
167
+ encoded = Protojson.encode(book)
168
+ encoded.should eql(expected.gsub(/\n\s*/, ''))
169
+ decoded = Protojson.decode(Examples::AddressBook, encoded)
170
+ decoded.person.length.should eql 3
171
+ decoded.person[0].name.should eql "John Doe"
172
+ decoded.person[1].name.should eql "Ivan Montes"
173
+ decoded.person[2].name.should eql "Juan de Bravo"
174
+ decoded.person[0].phone[1].number.should eql "55512321312"
175
+ end
176
+
177
+ end
178
+
179
+ end
@@ -0,0 +1,182 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'examples')
5
+
6
+ require 'protojson'
7
+
8
+ require 'simple.pb'
9
+ require 'repeated.pb'
10
+ require 'addressbook.pb'
11
+
12
+ describe Protojson::Codec::JsonTagMap do
13
+ before(:all) do
14
+ Protojson.set_default_codec(Protojson::Codec::JsonIndexed)
15
+ end
16
+
17
+ context "simple message" do
18
+
19
+ before(:each) do
20
+ @simple = Examples::Simple.new
21
+ @simple.foo = "Foo"
22
+ @simple.bar = 1000
23
+ @encoded = Protojson.encode(@simple)
24
+ end
25
+
26
+ it "should encode properly when two required attributes provided" do
27
+ @encoded.to_s.should eql('["12", "Foo", 1000]')
28
+ end
29
+
30
+ it "should decode properly" do
31
+ decoded = Protojson.decode(Examples::Simple, @encoded)
32
+ decoded.foo.should eql(@simple.foo)
33
+ decoded.bar.should eql(@simple.bar)
34
+ end
35
+
36
+ end
37
+
38
+ context "simple message (II)" do
39
+
40
+ before(:each) do
41
+ @simple = Examples::Simple.new
42
+ @simple.foo = "Foo"
43
+ @simple.bar = 1000
44
+ @simple.baz = "Bazz"
45
+ @encoded = Protojson.encode(@simple)
46
+ end
47
+
48
+ it "should encode properly when two required attributes and one optional attribute provided" do
49
+ @encoded.to_s.should eql('["123", "Foo", 1000, "Bazz"]')
50
+ end
51
+
52
+ it "should decode properly" do
53
+ decoded = Protojson.decode(Examples::Simple, @encoded)
54
+ decoded.foo.should eql(@simple.foo)
55
+ decoded.bar.should eql(@simple.bar)
56
+ decoded.baz.should eql(@simple.baz)
57
+ end
58
+
59
+ end
60
+
61
+ context "repeated message" do
62
+
63
+ it "should encode properly when the string field has more than one value" do
64
+ repeated = Examples::Repeated.new
65
+ repeated.string << "one"
66
+ repeated.string << "two"
67
+ repeated.string << "three"
68
+ Protojson.encode(repeated).to_s.should eql('["1", ["one", "two", "three"]]')
69
+ end
70
+
71
+ it "should encode properly when the int field has more than one value" do
72
+ repeated = Examples::Repeated.new
73
+ repeated.int << 1
74
+ repeated.int << 2
75
+ repeated.int << 3
76
+ Protojson.encode(repeated).to_s.should eql('["2", [1, 2, 3]]')
77
+ end
78
+
79
+ it "should encode properly when both int and string fields has more than one value" do
80
+ repeated = Examples::Repeated.new
81
+ repeated.string << "one"
82
+ repeated.string << "two"
83
+ repeated.string << "three"
84
+ repeated.int << 1
85
+ repeated.int << 2
86
+ repeated.int << 3
87
+ Protojson.encode(repeated).to_s.should eql('["12", ["one", "two", "three"], [1, 2, 3]]')
88
+ end
89
+
90
+ it "should encode properly when the nested field has more than one value" do
91
+ repeated = Examples::Repeated.new
92
+ (1..3).each { |id|
93
+ nested = Examples::Repeated::Nested.new
94
+ nested.id = id
95
+ repeated.nested << nested
96
+ }
97
+ Protojson.encode(repeated).to_s.should eql('["3", [["1", 1], ["1", 2], ["1", 3]]]')
98
+ end
99
+
100
+ end
101
+
102
+ context "complex message" do
103
+
104
+ it "should encode and decode the address_book example" do
105
+ book = Examples::AddressBook.new
106
+ person = Examples::Person.new
107
+ person.name = 'John Doe'
108
+ person.id = 2051
109
+ person.email = "john.doe@gmail.com"
110
+ phone = Examples::Person::PhoneNumber.new
111
+ phone.number = '1231231212'
112
+ phone.type = Examples::Person::PhoneType::HOME
113
+ person.phone << phone
114
+ phone = Examples::Person::PhoneNumber.new
115
+ phone.number = '55512321312'
116
+ phone.type = Examples::Person::PhoneType::MOBILE
117
+ person.phone << phone
118
+ book.person << person
119
+
120
+ person = Examples::Person.new
121
+ person.name = "Ivan Montes"
122
+ person.id = 23
123
+ person.email = "drslump@pollinimini.net"
124
+ phone = Examples::Person::PhoneNumber.new
125
+ phone.number = '3493123123'
126
+ phone.type = Examples::Person::PhoneType::WORK
127
+ person.phone << phone
128
+ book.person << person
129
+
130
+ person = Examples::Person.new
131
+ person.name = "Juan de Bravo"
132
+ person.id = 24
133
+ person.email = "juan@pollinimini.net"
134
+ phone = Examples::Person::PhoneNumber.new
135
+ phone.number = '3493123124'
136
+ phone.type = Examples::Person::PhoneType::WORK
137
+ person.phone << phone
138
+ book.person << person
139
+
140
+ expected =<<END
141
+ [
142
+ "1",[
143
+ [
144
+ "1234",
145
+ "John Doe",
146
+ 2051,
147
+ "john.doe@gmail.com",
148
+ [
149
+ ["12", "1231231212", 1],
150
+ ["12", "55512321312", 0]
151
+ ]
152
+ ],
153
+ [
154
+ "1234",
155
+ Ivan Montes",
156
+ 23,
157
+ "drslump@pollinimini.net",
158
+ [["12", "3493123123", 2]]
159
+ ],
160
+ {
161
+ "1234",
162
+ Juan de Bravo",
163
+ 24,
164
+ "juan@pollinimini.net",
165
+ ["12", "3493123124", 2]]
166
+ ]
167
+ ]
168
+ ]
169
+ END
170
+
171
+ encoded = Protojson.encode(book)
172
+ decoded = Protojson.decode(Examples::AddressBook, encoded)
173
+ decoded.person.length.should eql 3
174
+ decoded.person[0].name.should eql "John Doe"
175
+ decoded.person[1].name.should eql "Ivan Montes"
176
+ decoded.person[2].name.should eql "Juan de Bravo"
177
+ decoded.person[0].phone[1].number.should eql "55512321312"
178
+ end
179
+
180
+ end
181
+
182
+ end
@@ -0,0 +1,153 @@
1
+ # -*- coding: UTF-8 -*-
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'examples')
5
+
6
+ require 'protojson'
7
+
8
+ require 'simple.pb'
9
+ require 'repeated.pb'
10
+ require 'addressbook.pb'
11
+
12
+ describe Protojson::Codec::Json do
13
+ before(:all) do
14
+ Protojson.set_default_codec(Protojson::Codec::Json)
15
+ end
16
+
17
+ context "simple message" do
18
+
19
+ it "should encode properly when two required attributes provided" do
20
+ simple = Examples::Simple.new
21
+ simple.foo = "Foo"
22
+ simple.bar = 1000
23
+ Protojson.encode(simple).should eql('{"foo":"Foo","bar":1000}')
24
+ end
25
+
26
+ it "should encode properly when two required attributes and one optional attribute provided" do
27
+ simple = Examples::Simple.new
28
+ simple.foo = "Foo"
29
+ simple.bar = 1000
30
+ simple.baz = "Bazz"
31
+ Protojson.encode(simple).should eql('{"foo":"Foo","bar":1000,"baz":"Bazz"}')
32
+ end
33
+ end
34
+
35
+ context "repeated message" do
36
+
37
+ it "should encode properly when the string field has more than one value" do
38
+ repeated = Examples::Repeated.new
39
+ repeated.string << "one"
40
+ repeated.string << "two"
41
+ repeated.string << "three"
42
+ Protojson.encode(repeated).should eql('{"string":["one","two","three"]}')
43
+ end
44
+
45
+ it "should encode properly when the int field has more than one value" do
46
+ repeated = Examples::Repeated.new
47
+ repeated.int << 1
48
+ repeated.int << 2
49
+ repeated.int << 3
50
+ Protojson.encode(repeated).should eql('{"int":[1,2,3]}')
51
+ end
52
+
53
+ it "should encode properly when both int and string fields has more than one value" do
54
+ repeated = Examples::Repeated.new
55
+ repeated.string << "one"
56
+ repeated.string << "two"
57
+ repeated.string << "three"
58
+ repeated.int << 1
59
+ repeated.int << 2
60
+ repeated.int << 3
61
+ Protojson.encode(repeated).should eql('{"string":["one","two","three"],"int":[1,2,3]}')
62
+ end
63
+
64
+ it "should encode properly when the nested field has more than one value" do
65
+ repeated = Examples::Repeated.new
66
+ (1..3).each { |id|
67
+ nested = Examples::Repeated::Nested.new
68
+ nested.id = id
69
+ repeated.nested << nested
70
+ }
71
+ Protojson.encode(repeated).should eql('{"nested":[{"id":1},{"id":2},{"id":3}]}')
72
+ end
73
+
74
+ end
75
+
76
+ context "complex message" do
77
+ it "should work!! :)" do
78
+ book = Examples::AddressBook.new
79
+ person = Examples::Person.new
80
+ person.name = 'John Doe'
81
+ person.id = 2051
82
+ person.email = "john.doe@gmail.com"
83
+ phone = Examples::Person::PhoneNumber.new
84
+ phone.number = '1231231212'
85
+ phone.type = Examples::Person::PhoneType::HOME
86
+ person.phone << phone
87
+ phone = Examples::Person::PhoneNumber.new
88
+ phone.number = '55512321312'
89
+ phone.type = Examples::Person::PhoneType::MOBILE
90
+ person.phone << phone
91
+ book.person << person
92
+
93
+ person = Examples::Person.new
94
+ person.name = "Ivan Montes"
95
+ person.id = 23
96
+ person.email = "drslump@pollinimini.net"
97
+ phone = Examples::Person::PhoneNumber.new
98
+ phone.number = '3493123123'
99
+ phone.type = Examples::Person::PhoneType::WORK
100
+ person.phone << phone
101
+ book.person << person
102
+
103
+ person = Examples::Person.new
104
+ person.name = "Juan de Bravo"
105
+ person.id = 24
106
+ person.email = "juan@pollinimini.net"
107
+ phone = Examples::Person::PhoneNumber.new
108
+ phone.number = '3493123124'
109
+ phone.type = Examples::Person::PhoneType::WORK
110
+ person.phone << phone
111
+ book.person << person
112
+
113
+ expected =<<END
114
+ {
115
+ "person":[
116
+ {
117
+ "name":"John Doe",
118
+ "id":2051,
119
+ "email":"john.doe@gmail.com",
120
+ "phone":[
121
+ {"number":"1231231212","type":1},
122
+ {"number":"55512321312","type":0}
123
+ ]
124
+ },
125
+ {
126
+ "name":"Ivan Montes",
127
+ "id":23,
128
+ "email":"drslump@pollinimini.net",
129
+ "phone":[{"number":"3493123123","type":2}]
130
+ },
131
+ {
132
+ "name":"Juan de Bravo",
133
+ "id":24,
134
+ "email":"juan@pollinimini.net",
135
+ "phone":[{"number":"3493123124","type":2}]
136
+ }
137
+ ]
138
+ }
139
+ END
140
+
141
+ encoded = Protojson.encode(book)
142
+ encoded.should eql(expected.gsub(/\n\s*/, ''))
143
+ decoded = Protojson.decode(Examples::AddressBook, encoded)
144
+ decoded.person.length.should eql 3
145
+ decoded.person[0].name.should eql "John Doe"
146
+ decoded.person[1].name.should eql "Ivan Montes"
147
+ decoded.person[2].name.should eql "Juan de Bravo"
148
+ decoded.person[0].phone[1].number.should eql "55512321312"
149
+ end
150
+
151
+ end
152
+
153
+ end