protip 0.10.7 → 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,36 +1,39 @@
1
1
  require 'test_helper'
2
2
 
3
+ require 'google/protobuf/wrappers'
3
4
  require 'protip/standard_converter'
4
5
 
5
6
  describe Protip::StandardConverter do
6
7
  let(:converter) { Protip::StandardConverter.new }
7
8
 
8
9
  let(:integer_types) do
9
- [Protip::Int64Value, Protip::Int32Value, Protip::UInt64Value, Protip::UInt32Value]
10
+ [Google::Protobuf::Int64Value, Google::Protobuf::Int32Value, Google::Protobuf::UInt64Value, Google::Protobuf::UInt32Value]
10
11
  end
11
12
 
12
13
  let(:float_types) do
13
- [Protip::FloatValue, Protip::DoubleValue]
14
+ [Google::Protobuf::FloatValue, Google::Protobuf::DoubleValue]
14
15
  end
15
16
 
16
17
  let(:bool_types) do
17
- [Protip::BoolValue]
18
+ [Google::Protobuf::BoolValue]
18
19
  end
19
20
 
20
21
  let(:string_types) do
21
- [Protip::StringValue, Protip::BytesValue]
22
+ [Google::Protobuf::StringValue, Google::Protobuf::BytesValue]
22
23
  end
23
24
 
24
25
 
25
26
  describe '#convertible?' do
26
27
  it 'converts all standard types' do
27
- (integer_types + float_types + string_types + bool_types + [Protip::Date]).each do |message_class|
28
- assert converter.convertible?(message_class), 'expected type not convertible'
28
+ (integer_types + float_types + string_types + bool_types + [Protip::Messages::Date]).each do |message_class|
29
+ assert converter.convertible?(message_class), "expected type #{message_class.descriptor.name} not convertible"
29
30
  end
30
31
  end
31
32
 
32
33
  it 'does not convert other message types' do
33
- refute converter.convertible?(Class.new(::Protobuf::Message))
34
+ pool = Google::Protobuf::DescriptorPool.new
35
+ pool.build { add_message('test_message') { optional :id, :string, 1 } }
36
+ refute converter.convertible?(pool.lookup('test_message').msgclass)
34
37
  end
35
38
  end
36
39
 
@@ -49,9 +52,9 @@ describe Protip::StandardConverter do
49
52
  end
50
53
 
51
54
  it 'converts dates' do
52
- date = converter.to_object(Protip::Date.new year: 2015, month: 2, day: 9)
53
- assert_instance_of Date, date
54
- assert_equal 'Mon, 09 Feb 2015', date.inspect
55
+ date = converter.to_object(::Protip::Messages::Date.new year: 2015, month: 2, day: 9)
56
+ assert_instance_of ::Date, date
57
+ assert_equal '2015-02-09', date.strftime
55
58
  end
56
59
  end
57
60
 
@@ -72,7 +75,7 @@ describe Protip::StandardConverter do
72
75
  it 'converts dates' do
73
76
  date = ::Date.new(2012, 5, 7)
74
77
  assert_equal 7, date.day # Sanity check argument order
75
- assert_equal Protip::Date.new(year: 2012, month: 5, day: 7), converter.to_message(date, Protip::Date)
78
+ assert_equal ::Protip::Messages::Date.new(year: 2012, month: 5, day: 7), converter.to_message(date, ::Protip::Messages::Date)
76
79
  end
77
80
  end
78
81
  end
@@ -1,5 +1,7 @@
1
1
  require 'test_helper'
2
2
 
3
+ require 'google/protobuf'
4
+ require 'protip/converter'
3
5
  require 'protip/wrapper'
4
6
 
5
7
  module Protip::WrapperTest # namespace for internal constants
@@ -9,18 +11,35 @@ module Protip::WrapperTest # namespace for internal constants
9
11
  include Protip::Converter
10
12
  end.new
11
13
  end
14
+ let :pool do
15
+ pool = Google::Protobuf::DescriptorPool.new
16
+ pool.build do
17
+ add_message 'inner_message' do
18
+ optional :value, :int64, 1
19
+ optional :note, :string, 2
20
+ end
21
+
22
+ add_message 'message' do
23
+ optional :inner, :message, 1, 'inner_message'
24
+ optional :string, :string, 2
12
25
 
13
- class InnerMessage < ::Protobuf::Message
14
- required :int64, :value, 1
15
- optional :string, :note, 2
26
+ repeated :inners, :message, 3, 'inner_message'
27
+ repeated :strings, :string, 4
28
+
29
+ optional :inner_blank, :message, 5, 'inner_message'
30
+ end
31
+ end
32
+ pool
16
33
  end
17
- class Message < ::Protobuf::Message
18
- optional InnerMessage, :inner, 1
19
- optional :string, :string, 2
34
+
35
+ %w(inner_message message).each do |name|
36
+ let(:"#{name}_class") do
37
+ pool.lookup(name).msgclass
38
+ end
20
39
  end
21
40
 
22
41
  let(:wrapped_message) do
23
- Message.new(inner: {value: 25}, string: 'test')
42
+ message_class.new(inner: inner_message_class.new(value: 25), string: 'test')
24
43
  end
25
44
 
26
45
  let(:wrapper) do
@@ -31,10 +50,12 @@ module Protip::WrapperTest # namespace for internal constants
31
50
  it 'adds setters for message fields' do
32
51
  assert_respond_to wrapper, :string=
33
52
  assert_respond_to wrapper, :inner=
53
+ assert_respond_to wrapper, :inner_blank=
34
54
  end
35
55
  it 'adds getters for message fields' do
36
56
  assert_respond_to wrapper, :string
37
57
  assert_respond_to wrapper, :inner
58
+ assert_respond_to wrapper, :inner_blank
38
59
  end
39
60
  it 'responds to standard defined methods' do
40
61
  assert_respond_to wrapper, :as_json
@@ -52,30 +73,38 @@ module Protip::WrapperTest # namespace for internal constants
52
73
  end
53
74
  end
54
75
 
76
+ it 'raises an error when building a repeated primitive field' do
77
+ assert_raises RuntimeError do
78
+ wrapper.build(:strings)
79
+ end
80
+ end
81
+
82
+ # TODO: How to add a new message to a repeated message field?
83
+
55
84
  it 'raises an error when building a convertible message' do
56
- converter.stubs(:convertible?).with(InnerMessage).returns(true)
85
+ converter.stubs(:convertible?).with(inner_message_class).returns(true)
57
86
  assert_raises RuntimeError do
58
87
  wrapper.build(:inner)
59
88
  end
60
89
  end
61
90
 
62
91
  describe 'with an inconvertible message field' do
63
- let(:wrapped_message) { Message.new }
92
+ let(:wrapped_message) { message_class.new }
64
93
 
65
94
  before do
66
- converter.stubs(:convertible?).with(InnerMessage).returns(false)
95
+ converter.stubs(:convertible?).with(inner_message_class).returns(false)
67
96
  end
68
97
 
69
98
  it 'builds the message when no attributes are provided' do
70
99
  assert_nil wrapped_message.inner # Sanity check
71
100
  wrapper.build(:inner)
72
- assert_equal InnerMessage.new, wrapped_message.inner
101
+ assert_equal inner_message_class.new, wrapped_message.inner
73
102
  end
74
103
 
75
104
  it 'overwrites the message if it exists' do
76
- wrapped_message.inner = InnerMessage.new(value: 4)
105
+ wrapped_message.inner = inner_message_class.new(value: 4)
77
106
  wrapper.build(:inner)
78
- assert_equal InnerMessage.new, wrapped_message.inner
107
+ assert_equal inner_message_class.new, wrapped_message.inner
79
108
  end
80
109
 
81
110
  it 'delegates to #assign_attributes if attributes are provided' do
@@ -96,11 +125,28 @@ module Protip::WrapperTest # namespace for internal constants
96
125
  assert_equal 'another thing', wrapped_message.string
97
126
  end
98
127
 
99
- it 'assigns convertible message fields directly' do
100
- converter.stubs(:convertible?).with(InnerMessage).returns(true)
101
- converter.expects(:to_message).once.with(45, InnerMessage).returns(InnerMessage.new(value: 43))
102
- wrapper.assign_attributes inner: 45
103
- assert_equal InnerMessage.new(value: 43), wrapped_message.inner
128
+ it 'assigns repeated primitive fields from an enumerator' do
129
+ wrapper.assign_attributes strings: ['one', 'two']
130
+ assert_equal ['one', 'two'], wrapped_message.strings
131
+ end
132
+
133
+ describe 'when assigning convertible message fields' do
134
+ before do
135
+ converter.stubs(:convertible?).with(inner_message_class).returns(true)
136
+ end
137
+
138
+ it 'converts Ruby values to protobuf messages' do
139
+ converter.stubs(:convertible?).with(inner_message_class).returns(true)
140
+ converter.expects(:to_message).once.with(45, inner_message_class).returns(inner_message_class.new(value: 43))
141
+ wrapper.assign_attributes inner: 45
142
+ assert_equal inner_message_class.new(value: 43), wrapped_message.inner
143
+ end
144
+
145
+ it 'allows messages to be assigned directly' do
146
+ message = inner_message_class.new
147
+ wrapper.assign_attributes inner: message
148
+ assert_same message, wrapper.message.inner
149
+ end
104
150
  end
105
151
 
106
152
  it 'returns nil' do
@@ -109,27 +155,35 @@ module Protip::WrapperTest # namespace for internal constants
109
155
 
110
156
  describe 'when assigning inconvertible message fields' do
111
157
  before do
112
- converter.stubs(:convertible?).with(InnerMessage).returns(false)
158
+ converter.stubs(:convertible?).with(inner_message_class).returns(false)
113
159
  end
114
160
 
115
161
  it 'sets multiple attributes' do
116
162
  wrapper.assign_attributes string: 'test2', inner: {value: 50}
117
163
  assert_equal 'test2', wrapped_message.string
118
- assert_equal InnerMessage.new(value: 50), wrapped_message.inner
164
+ assert_equal inner_message_class.new(value: 50), wrapped_message.inner
119
165
  end
120
166
 
121
167
  it 'updates inconvertible message fields which have already been built' do
122
- wrapped_message.inner = InnerMessage.new(value: 60)
168
+ wrapped_message.inner = inner_message_class.new(value: 60)
123
169
  wrapper.assign_attributes inner: {note: 'updated'}
124
- assert_equal InnerMessage.new(value: 60, note: 'updated'), wrapped_message.inner
170
+ assert_equal inner_message_class.new(value: 60, note: 'updated'), wrapped_message.inner
125
171
  end
126
172
 
127
- it 'delegates to itself when setting nested attributes on inconvertible message fields' do
173
+ it 'delegates to #assign_attributes on a nested wrapper when setting nested attributes on inconvertible message fields' do
128
174
  inner = mock
129
- wrapper.stubs(:get).with(wrapped_message.class.fields.detect{|f| f.name == :inner}).returns(inner)
175
+ field = wrapped_message.class.descriptor.detect{|f| f.name.to_sym == :inner}
176
+ raise 'unexpected' if !field
177
+ wrapper.stubs(:get).with(field).returns(inner)
130
178
  inner.expects(:assign_attributes).once.with(value: 50, note: 'noted')
131
179
  wrapper.assign_attributes inner: {value: 50, note: 'noted'}
132
180
  end
181
+
182
+ it 'allows messages to be assigned directly' do
183
+ message = inner_message_class.new
184
+ wrapper.assign_attributes inner: message
185
+ assert_same message, wrapper.message.inner
186
+ end
133
187
  end
134
188
  end
135
189
 
@@ -140,9 +194,9 @@ module Protip::WrapperTest # namespace for internal constants
140
194
  end
141
195
 
142
196
  it 'returns false when messages are not equal' do
143
- alternate_message = Message.new
144
- refute_equal alternate_message, wrapped_message # Sanity check
145
- refute_equal wrapper, Protip::Wrapper.new(alternate_message, converter)
197
+ alternate_message = message_class.new
198
+ refute_equal alternate_message, wrapper.message # Sanity check
199
+ refute_equal wrapper, Protip::Wrapper.new(alternate_message, wrapper.converter)
146
200
  end
147
201
 
148
202
  it 'returns false when converters are not equal' do
@@ -169,15 +223,21 @@ module Protip::WrapperTest # namespace for internal constants
169
223
  end
170
224
 
171
225
  it 'converts convertible messages' do
172
- converter.expects(:convertible?).with(InnerMessage).once.returns(true)
173
- converter.expects(:to_object).with(InnerMessage.new(value: 25)).returns 40
226
+ converter.expects(:convertible?).with(inner_message_class).once.returns(true)
227
+ converter.expects(:to_object).once.with(inner_message_class.new(value: 25)).returns 40
174
228
  assert_equal 40, wrapper.inner
175
229
  end
176
230
 
177
231
  it 'wraps inconvertible messages' do
178
- converter.expects(:convertible?).with(InnerMessage).once.returns(false)
232
+ converter.expects(:convertible?).with(inner_message_class).once.returns(false)
179
233
  converter.expects(:to_object).never
180
- assert_equal Protip::Wrapper.new(InnerMessage.new(value: 25), converter), wrapper.inner
234
+ assert_equal Protip::Wrapper.new(inner_message_class.new(value: 25), converter), wrapper.inner
235
+ end
236
+
237
+ it 'returns nil for messages that have not been set' do
238
+ converter.expects(:convertible?).never
239
+ converter.expects(:to_object).never
240
+ assert_equal nil, wrapper.inner_blank
181
241
  end
182
242
  end
183
243
 
@@ -191,15 +251,23 @@ module Protip::WrapperTest # namespace for internal constants
191
251
  end
192
252
 
193
253
  it 'converts convertible messages' do
194
- converter.expects(:convertible?).with(InnerMessage).once.returns(true)
195
- converter.expects(:to_message).with(40, InnerMessage).returns(InnerMessage.new(value: 30))
254
+ converter.expects(:convertible?).with(inner_message_class).once.returns(true)
255
+ converter.expects(:to_message).with(40, inner_message_class).returns(inner_message_class.new(value: 30))
196
256
 
197
257
  wrapper.inner = 40
198
- assert_equal InnerMessage.new(value: 30), wrapper.message.inner
258
+ assert_equal inner_message_class.new(value: 30), wrapper.message.inner
259
+ end
260
+
261
+ it 'removes message fields when assigning nil' do
262
+ converter.expects(:convertible?).never
263
+ converter.expects(:to_message).never
264
+
265
+ wrapper.inner = nil
266
+ assert_equal nil, wrapper.message.inner
199
267
  end
200
268
 
201
269
  it 'raises an error when setting inconvertible messages' do
202
- converter.expects(:convertible?).with(InnerMessage).once.returns(false)
270
+ converter.expects(:convertible?).with(inner_message_class).once.returns(false)
203
271
  converter.expects(:to_message).never
204
272
  assert_raises ArgumentError do
205
273
  wrapper.inner = 'cannot convert me'
@@ -210,8 +278,8 @@ module Protip::WrapperTest # namespace for internal constants
210
278
  converter.expects(:convertible?).never
211
279
  converter.expects(:to_message).never
212
280
 
213
- wrapper.inner = InnerMessage.new(value: 50)
214
- assert_equal InnerMessage.new(value: 50), wrapper.message.inner
281
+ wrapper.inner = inner_message_class.new(value: 50)
282
+ assert_equal inner_message_class.new(value: 50), wrapper.message.inner
215
283
  end
216
284
  end
217
285
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.7
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - AngelList
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-16 00:00:00.000000000 Z
11
+ date: 2015-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -51,19 +51,19 @@ dependencies:
51
51
  - !ruby/object:Gem::Version
52
52
  version: '5.0'
53
53
  - !ruby/object:Gem::Dependency
54
- name: protobuf
54
+ name: google-protobuf
55
55
  requirement: !ruby/object:Gem::Requirement
56
56
  requirements:
57
- - - "~>"
57
+ - - '='
58
58
  - !ruby/object:Gem::Version
59
- version: 3.5.0
59
+ version: 3.0.0.alpha.3
60
60
  type: :runtime
61
61
  prerelease: false
62
62
  version_requirements: !ruby/object:Gem::Requirement
63
63
  requirements:
64
- - - "~>"
64
+ - - '='
65
65
  - !ruby/object:Gem::Version
66
- version: 3.5.0
66
+ version: 3.0.0.alpha.3
67
67
  - !ruby/object:Gem::Dependency
68
68
  name: minitest
69
69
  requirement: !ruby/object:Gem::Requirement
@@ -142,18 +142,18 @@ executables: []
142
142
  extensions: []
143
143
  extra_rdoc_files: []
144
144
  files:
145
+ - definitions/google/protobuf/wrappers.proto
145
146
  - definitions/protip/messages/array.proto
146
147
  - definitions/protip/messages/errors.proto
147
148
  - definitions/protip/messages/types.proto
148
- - definitions/protip/messages/wrappers.proto
149
+ - lib/google/protobuf/wrappers.rb
149
150
  - lib/protip.rb
150
151
  - lib/protip/client.rb
151
152
  - lib/protip/converter.rb
152
153
  - lib/protip/error.rb
153
- - lib/protip/messages/array.pb.rb
154
- - lib/protip/messages/errors.pb.rb
155
- - lib/protip/messages/types.pb.rb
156
- - lib/protip/messages/wrappers.pb.rb
154
+ - lib/protip/messages/array.rb
155
+ - lib/protip/messages/errors.rb
156
+ - lib/protip/messages/types.rb
157
157
  - lib/protip/resource.rb
158
158
  - lib/protip/standard_converter.rb
159
159
  - lib/protip/wrapper.rb
@@ -1,60 +0,0 @@
1
- package protip;
2
-
3
- /////
4
- // Wrappers, copied from https://github.com/google/protobuf/blob/master/src/google/protobuf/wrappers.proto
5
- // These should exactly match the structure of those built-in types, so that users can safely swap them in
6
- // when upgrading to proto3.
7
-
8
- // Wrapper message for double.
9
- message DoubleValue {
10
- // The double value.
11
- required double value = 1;
12
- }
13
-
14
- // Wrapper message for float.
15
- message FloatValue {
16
- // The float value.
17
- required float value = 1;
18
- }
19
-
20
- // Wrapper message for int64.
21
- message Int64Value {
22
- // The int64 value.
23
- required int64 value = 1;
24
- }
25
-
26
- // Wrapper message for uint64.
27
- message UInt64Value {
28
- // The uint64 value.
29
- required uint64 value = 1;
30
- }
31
-
32
- // Wrapper message for int32.
33
- message Int32Value {
34
- // The int32 value.
35
- required int32 value = 1;
36
- }
37
-
38
- // Wrapper message for uint32.
39
- message UInt32Value {
40
- // The uint32 value.
41
- required uint32 value = 1;
42
- }
43
-
44
- // Wrapper message for bool.
45
- message BoolValue {
46
- // The bool value.
47
- required bool value = 1;
48
- }
49
-
50
- // Wrapper message for string.
51
- message StringValue {
52
- // The string value.
53
- required string value = 1;
54
- }
55
-
56
- // Wrapper message for bytes.
57
- message BytesValue {
58
- // The bytes value.
59
- required bytes value = 1;
60
- }