protip 0.20.3 → 0.20.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9e6ad392a69e26b1ea54bb38810e681ddc18d90b
4
- data.tar.gz: 5ce3ef3d23b68795bf8d25fb951bc67c8a974c19
3
+ metadata.gz: 13a134ef5d4a02a13ede10452e95e7971d3a983a
4
+ data.tar.gz: f00fe632579adf72c55660f62256a8277a847e2c
5
5
  SHA512:
6
- metadata.gz: 2323e59b4e659b8c61d86b8a782e1e116fead8051e3cb30b75ac57669561a845fa5c63837b04e01bb80f33252471be472a7e1a124b531c397a897bdb32deba4a
7
- data.tar.gz: 243e701afabec75aa012a3d8186e84dc36adaf36de24d960902460564c142d73734b424357e926ffc855875a415343059ec5d7c2d4a8b5d2ec374c11a949709e
6
+ metadata.gz: be84e24f6e21db21420d04a623ce090759bd56f6dd9e28d438f145dafac76944c87761c88e568da2e256e595aa01fafb7a54a9b406b51606415538f34b505fcc
7
+ data.tar.gz: 18817a7340e471b6b3ba3319bac499711b85baf114cdf86b977b2e1269afe09b8db1aaaebca091dc9c4ba7d7200dde66d029d7139e11e71dfdcac2c27fa98325
@@ -165,10 +165,9 @@ module Protip
165
165
  value
166
166
  end
167
167
 
168
- private
169
168
  def enum_for_field(field)
170
169
  Google::Protobuf::DescriptorPool.generated_pool.lookup(field.instance_variable_get(:'@_protip_enum_value'))
171
170
  end
172
171
  end
173
172
  end
174
- end
173
+ end
@@ -162,11 +162,21 @@ module Protip
162
162
  field.type == :bool || (field.type == :message && field.subtype.name == 'google.protobuf.BoolValue')
163
163
  end
164
164
 
165
+ def enum_for_field(field)
166
+ if field.type == :enum
167
+ field.subtype
168
+ elsif field.type == :message && field.subtype.name == 'protip.messages.EnumValue'
169
+ Protip::StandardConverter.enum_for_field(field)
170
+ else
171
+ nil
172
+ end
173
+ end
174
+
165
175
  # Semi-private check for whether a field should have an associated query method (e.g. +field_name?+).
166
176
  # @return [Boolean] Whether the field should have an associated query method on wrappers.
167
177
  def matchable?(field)
168
178
  return false if field.label == :repeated
169
- field.type == :enum || boolean?(field)
179
+ (nil != enum_for_field(field)) || boolean?(field)
170
180
  end
171
181
  end
172
182
 
@@ -231,7 +241,7 @@ module Protip
231
241
  end
232
242
 
233
243
  def matches?(field, value)
234
- enum = field.subtype
244
+ enum = Protip::Wrapper.enum_for_field(field)
235
245
  if value.is_a?(Fixnum)
236
246
  sym = enum.lookup_value(value)
237
247
  else
@@ -258,7 +268,7 @@ module Protip
258
268
  def method_missing_query(name, *args)
259
269
  field = message.class.descriptor.lookup(name[0, name.length - 1])
260
270
  raise NoMethodError if !field || !self.class.matchable?(field)
261
- if field.type == :enum
271
+ if nil != Protip::Wrapper.enum_for_field(field)
262
272
  raise ArgumentError unless args.length == 1
263
273
  return matches?(field, args[0])
264
274
  elsif self.class.boolean?(field)
@@ -26,6 +26,9 @@ module Protip::WrapperTest # namespace for internal constants
26
26
  add_message 'google.protobuf.BoolValue' do
27
27
  optional :value, :bool, 1
28
28
  end
29
+ add_message 'protip.messages.EnumValue' do
30
+ optional :value, :int32, 1
31
+ end
29
32
 
30
33
  add_message 'message' do
31
34
  optional :inner, :message, 1, 'inner_message'
@@ -38,16 +41,17 @@ module Protip::WrapperTest # namespace for internal constants
38
41
 
39
42
  optional :number, :enum, 6, 'number'
40
43
  repeated :numbers, :enum, 7, 'number'
44
+ optional :number_message, :message, 8, 'protip.messages.EnumValue'
41
45
 
42
- optional :boolean, :bool, 8
43
- repeated :booleans, :bool, 9
46
+ optional :boolean, :bool, 9
47
+ repeated :booleans, :bool, 10
44
48
 
45
- optional :google_bool_value, :message, 10, 'google.protobuf.BoolValue'
46
- repeated :google_bool_values, :message, 11, 'google.protobuf.BoolValue'
49
+ optional :google_bool_value, :message, 11, 'google.protobuf.BoolValue'
50
+ repeated :google_bool_values, :message, 12, 'google.protobuf.BoolValue'
47
51
 
48
52
  oneof :oneof_group do
49
- optional :oneof_string1, :string, 12
50
- optional :oneof_string2, :string, 13
53
+ optional :oneof_string1, :string, 13
54
+ optional :oneof_string2, :string, 14
51
55
  end
52
56
  end
53
57
  end
@@ -59,6 +63,9 @@ module Protip::WrapperTest # namespace for internal constants
59
63
  pool.lookup(name).msgclass
60
64
  end
61
65
  end
66
+ let(:enum_message_class) do
67
+ pool.lookup('protip.messages.EnumValue').msgclass
68
+ end
62
69
  let (:inner_message_field) { message_class.descriptor.lookup('inner') }
63
70
 
64
71
  # Stubbed API client
@@ -87,6 +94,13 @@ module Protip::WrapperTest # namespace for internal constants
87
94
  Protip::Wrapper.new(wrapped_message, converter)
88
95
  end
89
96
 
97
+ # Stub the wrapped-enum fetcher method - probably rethink this once the
98
+ # instance variable hack is no longer necessary
99
+ before do
100
+ Protip::StandardConverter.stubs(:enum_for_field)
101
+ .returns(pool.lookup('number') || raise('unexpected - no enum field found'))
102
+ end
103
+
90
104
  describe '#respond_to?' do
91
105
  it 'adds setters for message fields' do
92
106
  assert_respond_to wrapper, :string=
@@ -103,6 +117,7 @@ module Protip::WrapperTest # namespace for internal constants
103
117
  end
104
118
  it 'adds queries for scalar matchable fields' do
105
119
  assert_respond_to wrapper, :number?, 'enum field should respond to query'
120
+ assert_respond_to wrapper, :number_message?, 'enum message field should respond to query'
106
121
  assert_respond_to wrapper, :boolean?, 'bool field should respond to query'
107
122
  assert_respond_to wrapper, :google_bool_value?, 'google.protobuf.BoolValue field should respond to query'
108
123
  end
@@ -346,7 +361,7 @@ module Protip::WrapperTest # namespace for internal constants
346
361
  end
347
362
 
348
363
  it 'contains keys for all fields of the parent message' do
349
- keys = %i(string strings inner inners inner_blank number numbers boolean booleans
364
+ keys = %i(string strings inner inners inner_blank number numbers number_message boolean booleans
350
365
  google_bool_value google_bool_values oneof_string1 oneof_string2)
351
366
  assert_equal keys.sort, wrapper.to_h.keys.sort
352
367
  end
@@ -596,6 +611,36 @@ module Protip::WrapperTest # namespace for internal constants
596
611
  end
597
612
  end
598
613
  end
614
+
615
+ describe 'for a wrapped enum' do
616
+ let :enum_message do
617
+ enum_message_class.new value: 1
618
+ end
619
+
620
+ before do
621
+ converter.stubs(:convertible?).with(enum_message_class).returns true
622
+ converter.stubs(:to_object).with(enum_message, anything).returns :ONE
623
+ wrapper.message.number_message = enum_message
624
+ end
625
+
626
+ it 'returns true when its symbolized argument matches the value' do
627
+ m = mock
628
+ m.expects(:to_sym).returns :ONE
629
+ assert wrapper.number_message?(m)
630
+ end
631
+
632
+ it 'returns false when its symbolized argument does not match the value' do
633
+ m = mock
634
+ m.expects(:to_sym).returns :TWO
635
+ refute wrapper.number_message?(m)
636
+ end
637
+
638
+ it 'returns true when a Fixnum argument matches the value' do
639
+ assert wrapper.number_message?(1)
640
+ end
641
+
642
+ end
643
+
599
644
  end
600
645
  end
601
646
  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.20.3
4
+ version: 0.20.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - AngelList
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-10 00:00:00.000000000 Z
11
+ date: 2016-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel