protobuffy 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (192) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +5 -0
  5. data/CHANGES.md +261 -0
  6. data/CONTRIBUTING.md +16 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE.txt +14 -0
  9. data/README.md +58 -0
  10. data/Rakefile +61 -0
  11. data/bin/protoc-gen-ruby +17 -0
  12. data/bin/rpc_server +4 -0
  13. data/examples/bin/reverse-client-http +4 -0
  14. data/examples/bin/reverse-client-socket +4 -0
  15. data/examples/bin/reverse-client-zmq +4 -0
  16. data/examples/config.ru +6 -0
  17. data/examples/definitions/example/reverse.proto +12 -0
  18. data/examples/lib/example/reverse-client.rb +23 -0
  19. data/examples/lib/example/reverse-service.rb +9 -0
  20. data/examples/lib/example/reverse.pb.rb +36 -0
  21. data/lib/protobuf.rb +106 -0
  22. data/lib/protobuf/cli.rb +249 -0
  23. data/lib/protobuf/code_generator.rb +41 -0
  24. data/lib/protobuf/decoder.rb +74 -0
  25. data/lib/protobuf/deprecator.rb +42 -0
  26. data/lib/protobuf/descriptors.rb +3 -0
  27. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +52 -0
  28. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +249 -0
  29. data/lib/protobuf/encoder.rb +62 -0
  30. data/lib/protobuf/enum.rb +319 -0
  31. data/lib/protobuf/exceptions.rb +9 -0
  32. data/lib/protobuf/field.rb +74 -0
  33. data/lib/protobuf/field/base_field.rb +280 -0
  34. data/lib/protobuf/field/bool_field.rb +53 -0
  35. data/lib/protobuf/field/bytes_field.rb +81 -0
  36. data/lib/protobuf/field/double_field.rb +26 -0
  37. data/lib/protobuf/field/enum_field.rb +57 -0
  38. data/lib/protobuf/field/field_array.rb +86 -0
  39. data/lib/protobuf/field/fixed32_field.rb +25 -0
  40. data/lib/protobuf/field/fixed64_field.rb +29 -0
  41. data/lib/protobuf/field/float_field.rb +38 -0
  42. data/lib/protobuf/field/int32_field.rb +22 -0
  43. data/lib/protobuf/field/int64_field.rb +22 -0
  44. data/lib/protobuf/field/integer_field.rb +24 -0
  45. data/lib/protobuf/field/message_field.rb +66 -0
  46. data/lib/protobuf/field/sfixed32_field.rb +28 -0
  47. data/lib/protobuf/field/sfixed64_field.rb +29 -0
  48. data/lib/protobuf/field/signed_integer_field.rb +30 -0
  49. data/lib/protobuf/field/sint32_field.rb +22 -0
  50. data/lib/protobuf/field/sint64_field.rb +22 -0
  51. data/lib/protobuf/field/string_field.rb +35 -0
  52. data/lib/protobuf/field/uint32_field.rb +22 -0
  53. data/lib/protobuf/field/uint64_field.rb +22 -0
  54. data/lib/protobuf/field/varint_field.rb +68 -0
  55. data/lib/protobuf/generators/base.rb +71 -0
  56. data/lib/protobuf/generators/enum_generator.rb +42 -0
  57. data/lib/protobuf/generators/extension_generator.rb +28 -0
  58. data/lib/protobuf/generators/field_generator.rb +132 -0
  59. data/lib/protobuf/generators/file_generator.rb +140 -0
  60. data/lib/protobuf/generators/group_generator.rb +113 -0
  61. data/lib/protobuf/generators/message_generator.rb +99 -0
  62. data/lib/protobuf/generators/printable.rb +161 -0
  63. data/lib/protobuf/generators/service_generator.rb +27 -0
  64. data/lib/protobuf/http.rb +20 -0
  65. data/lib/protobuf/lifecycle.rb +46 -0
  66. data/lib/protobuf/logger.rb +86 -0
  67. data/lib/protobuf/message.rb +182 -0
  68. data/lib/protobuf/message/fields.rb +122 -0
  69. data/lib/protobuf/message/serialization.rb +84 -0
  70. data/lib/protobuf/optionable.rb +23 -0
  71. data/lib/protobuf/rpc/buffer.rb +79 -0
  72. data/lib/protobuf/rpc/client.rb +168 -0
  73. data/lib/protobuf/rpc/connector.rb +21 -0
  74. data/lib/protobuf/rpc/connectors/base.rb +54 -0
  75. data/lib/protobuf/rpc/connectors/common.rb +172 -0
  76. data/lib/protobuf/rpc/connectors/http.rb +90 -0
  77. data/lib/protobuf/rpc/connectors/socket.rb +73 -0
  78. data/lib/protobuf/rpc/connectors/zmq.rb +205 -0
  79. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +47 -0
  80. data/lib/protobuf/rpc/env.rb +58 -0
  81. data/lib/protobuf/rpc/error.rb +28 -0
  82. data/lib/protobuf/rpc/error/client_error.rb +31 -0
  83. data/lib/protobuf/rpc/error/server_error.rb +43 -0
  84. data/lib/protobuf/rpc/middleware.rb +25 -0
  85. data/lib/protobuf/rpc/middleware/exception_handler.rb +36 -0
  86. data/lib/protobuf/rpc/middleware/logger.rb +91 -0
  87. data/lib/protobuf/rpc/middleware/request_decoder.rb +83 -0
  88. data/lib/protobuf/rpc/middleware/response_encoder.rb +88 -0
  89. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  90. data/lib/protobuf/rpc/rpc.pb.rb +53 -0
  91. data/lib/protobuf/rpc/server.rb +39 -0
  92. data/lib/protobuf/rpc/servers/http/server.rb +101 -0
  93. data/lib/protobuf/rpc/servers/http_runner.rb +34 -0
  94. data/lib/protobuf/rpc/servers/socket/server.rb +113 -0
  95. data/lib/protobuf/rpc/servers/socket/worker.rb +56 -0
  96. data/lib/protobuf/rpc/servers/socket_runner.rb +34 -0
  97. data/lib/protobuf/rpc/servers/zmq/broker.rb +155 -0
  98. data/lib/protobuf/rpc/servers/zmq/server.rb +313 -0
  99. data/lib/protobuf/rpc/servers/zmq/util.rb +47 -0
  100. data/lib/protobuf/rpc/servers/zmq/worker.rb +105 -0
  101. data/lib/protobuf/rpc/servers/zmq_runner.rb +51 -0
  102. data/lib/protobuf/rpc/service.rb +179 -0
  103. data/lib/protobuf/rpc/service_directory.rb +245 -0
  104. data/lib/protobuf/rpc/service_dispatcher.rb +46 -0
  105. data/lib/protobuf/rpc/service_filters.rb +273 -0
  106. data/lib/protobuf/rpc/stat.rb +148 -0
  107. data/lib/protobuf/socket.rb +22 -0
  108. data/lib/protobuf/tasks.rb +1 -0
  109. data/lib/protobuf/tasks/compile.rake +61 -0
  110. data/lib/protobuf/version.rb +3 -0
  111. data/lib/protobuf/wire_type.rb +10 -0
  112. data/lib/protobuf/zmq.rb +21 -0
  113. data/proto/dynamic_discovery.proto +44 -0
  114. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  115. data/proto/google/protobuf/descriptor.proto +620 -0
  116. data/proto/rpc.proto +62 -0
  117. data/protobuffy.gemspec +37 -0
  118. data/spec/benchmark/tasks.rb +113 -0
  119. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  120. data/spec/data/data.bin +3 -0
  121. data/spec/data/types.bin +0 -0
  122. data/spec/encoding/all_types_spec.rb +91 -0
  123. data/spec/encoding/extreme_values_spec.rb +0 -0
  124. data/spec/functional/socket_server_spec.rb +59 -0
  125. data/spec/functional/zmq_server_spec.rb +103 -0
  126. data/spec/lib/protobuf/cli_spec.rb +267 -0
  127. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  128. data/spec/lib/protobuf/enum_spec.rb +239 -0
  129. data/spec/lib/protobuf/field/int32_field_spec.rb +7 -0
  130. data/spec/lib/protobuf/field/string_field_spec.rb +46 -0
  131. data/spec/lib/protobuf/field_spec.rb +194 -0
  132. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  133. data/spec/lib/protobuf/generators/enum_generator_spec.rb +68 -0
  134. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  135. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  136. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  137. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  138. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  139. data/spec/lib/protobuf/lifecycle_spec.rb +89 -0
  140. data/spec/lib/protobuf/logger_spec.rb +136 -0
  141. data/spec/lib/protobuf/message_spec.rb +368 -0
  142. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  143. data/spec/lib/protobuf/rpc/client_spec.rb +66 -0
  144. data/spec/lib/protobuf/rpc/connector_spec.rb +26 -0
  145. data/spec/lib/protobuf/rpc/connectors/base_spec.rb +50 -0
  146. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +170 -0
  147. data/spec/lib/protobuf/rpc/connectors/connector_spec.rb +13 -0
  148. data/spec/lib/protobuf/rpc/connectors/http_spec.rb +61 -0
  149. data/spec/lib/protobuf/rpc/connectors/socket_spec.rb +24 -0
  150. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +129 -0
  151. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  152. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  153. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  154. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +75 -0
  155. data/spec/lib/protobuf/rpc/servers/http/server_spec.rb +104 -0
  156. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +38 -0
  157. data/spec/lib/protobuf/rpc/servers/zmq/server_spec.rb +41 -0
  158. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +55 -0
  159. data/spec/lib/protobuf/rpc/servers/zmq/worker_spec.rb +35 -0
  160. data/spec/lib/protobuf/rpc/service_directory_spec.rb +295 -0
  161. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +52 -0
  162. data/spec/lib/protobuf/rpc/service_filters_spec.rb +484 -0
  163. data/spec/lib/protobuf/rpc/service_spec.rb +161 -0
  164. data/spec/lib/protobuf/rpc/stat_spec.rb +151 -0
  165. data/spec/lib/protobuf_spec.rb +78 -0
  166. data/spec/spec_helper.rb +57 -0
  167. data/spec/support/all.rb +7 -0
  168. data/spec/support/packed_field.rb +22 -0
  169. data/spec/support/server.rb +94 -0
  170. data/spec/support/test/all_types.data.bin +0 -0
  171. data/spec/support/test/all_types.data.txt +119 -0
  172. data/spec/support/test/defaults.pb.rb +25 -0
  173. data/spec/support/test/defaults.proto +9 -0
  174. data/spec/support/test/enum.pb.rb +59 -0
  175. data/spec/support/test/enum.proto +34 -0
  176. data/spec/support/test/extended.pb.rb +22 -0
  177. data/spec/support/test/extended.proto +10 -0
  178. data/spec/support/test/extreme_values.data.bin +0 -0
  179. data/spec/support/test/google_unittest.pb.rb +543 -0
  180. data/spec/support/test/google_unittest.proto +713 -0
  181. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  182. data/spec/support/test/google_unittest_import.proto +64 -0
  183. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  184. data/spec/support/test/google_unittest_import_public.proto +38 -0
  185. data/spec/support/test/multi_field_extensions.pb.rb +56 -0
  186. data/spec/support/test/multi_field_extensions.proto +33 -0
  187. data/spec/support/test/resource.pb.rb +117 -0
  188. data/spec/support/test/resource.proto +94 -0
  189. data/spec/support/test/resource_service.rb +26 -0
  190. data/spec/support/test_app_file.rb +2 -0
  191. data/spec/support/tolerance_matcher.rb +40 -0
  192. metadata +367 -0
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Protobuf::Field::Int32Field do
4
+
5
+ it_behaves_like :packable_field, described_class
6
+
7
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+
3
+ require 'spec_helper'
4
+
5
+ describe ::Protobuf::Field::StringField do
6
+
7
+ describe '#encode' do
8
+ context 'when a repeated string field contains frozen strings' do
9
+ it 'does not raise an encoding error' do
10
+ expect {
11
+ frozen_strings = [ "foo".freeze, "bar".freeze, "baz".freeze ]
12
+ ::Test::ResourceFindRequest.encode(:name => 'resource', :widgets => frozen_strings)
13
+ }.not_to raise_error
14
+ end
15
+ end
16
+
17
+ context 'when a repeated bytes field contains frozen strings' do
18
+ it 'does not raise an encoding error' do
19
+ expect {
20
+ frozen_strings = [ "foo".freeze, "bar".freeze, "baz".freeze ]
21
+ ::Test::ResourceFindRequest.encode(:name => 'resource', :widget_bytes => frozen_strings)
22
+ }.not_to raise_error
23
+ end
24
+ end
25
+
26
+ it 'does not alter string values after encoding multiple times' do
27
+ source_string = "foo"
28
+ proto = ::Test::Resource.new(:name => source_string)
29
+ proto.encode
30
+ proto.name.should eq source_string
31
+ proto.encode
32
+ proto.name.should eq source_string
33
+ end
34
+
35
+ it 'does not alter unicode string values after encoding multiple times' do
36
+ source_string = "¢"
37
+ proto = ::Test::Resource.new(:name => source_string)
38
+ proto.encode
39
+ proto.name.should eq source_string
40
+ proto.encode
41
+ proto.name.should eq source_string
42
+ end
43
+ end
44
+
45
+
46
+ end
@@ -0,0 +1,194 @@
1
+ require 'spec_helper'
2
+ require 'protobuf/field'
3
+
4
+ describe ::Protobuf::Field do
5
+
6
+ describe '.build' do
7
+ pending
8
+ end
9
+
10
+ describe '.field_class' do
11
+ context 'when type is an enum class' do
12
+ it 'returns an enum field' do
13
+ expect(subject.field_class(::Test::EnumTestType)).to eq(::Protobuf::Field::EnumField)
14
+ expect(subject.field_type(::Test::EnumTestType)).to eq(::Test::EnumTestType)
15
+ end
16
+ end
17
+
18
+ context 'when type is a message class' do
19
+ it 'returns a message field' do
20
+ expect(subject.field_class(::Test::Resource)).to eq(::Protobuf::Field::MessageField)
21
+ expect(subject.field_type(::Test::Resource)).to eq(::Test::Resource)
22
+ end
23
+ end
24
+
25
+ context 'when type is a base field class' do
26
+ it 'returns that class' do
27
+ expect(subject.field_class(::Protobuf::Field::BoolField)).to eq(::Protobuf::Field::BoolField)
28
+ end
29
+ end
30
+
31
+ context 'when type is a double field class or symbol' do
32
+ it 'returns that class' do
33
+ expected_field = ::Protobuf::Field::DoubleField
34
+ expect(subject.field_class(expected_field)).to eq(expected_field)
35
+ expect(subject.field_class(:double)).to eq(expected_field)
36
+ expect(subject.field_type(expected_field)).to eq(expected_field)
37
+ expect(subject.field_type(:double)).to eq(expected_field)
38
+ end
39
+ end
40
+
41
+ context 'when type is a float field class or symbol' do
42
+ it 'returns that class' do
43
+ expected_field = ::Protobuf::Field::FloatField
44
+ expect(subject.field_class(expected_field)).to eq(expected_field)
45
+ expect(subject.field_class(:float)).to eq(expected_field)
46
+ expect(subject.field_type(expected_field)).to eq(expected_field)
47
+ expect(subject.field_type(:float)).to eq(expected_field)
48
+ end
49
+ end
50
+
51
+ context 'when type is a int32 field class or symbol' do
52
+ it 'returns that class' do
53
+ expected_field = ::Protobuf::Field::Int32Field
54
+ expect(subject.field_class(expected_field)).to eq(expected_field)
55
+ expect(subject.field_class(:int32)).to eq(expected_field)
56
+ expect(subject.field_type(expected_field)).to eq(expected_field)
57
+ expect(subject.field_type(:int32)).to eq(expected_field)
58
+ end
59
+ end
60
+
61
+ context 'when type is a int64 field class or symbol' do
62
+ it 'returns that class' do
63
+ expected_field = ::Protobuf::Field::Int64Field
64
+ expect(subject.field_class(expected_field)).to eq(expected_field)
65
+ expect(subject.field_class(:int64)).to eq(expected_field)
66
+ expect(subject.field_type(expected_field)).to eq(expected_field)
67
+ expect(subject.field_type(:int64)).to eq(expected_field)
68
+ end
69
+ end
70
+
71
+ context 'when type is a uint32 field class or symbol' do
72
+ it 'returns that class' do
73
+ expected_field = ::Protobuf::Field::Uint32Field
74
+ expect(subject.field_class(expected_field)).to eq(expected_field)
75
+ expect(subject.field_class(:uint32)).to eq(expected_field)
76
+ expect(subject.field_type(expected_field)).to eq(expected_field)
77
+ expect(subject.field_type(:uint32)).to eq(expected_field)
78
+ end
79
+ end
80
+
81
+ context 'when type is a uint64 field class or symbol' do
82
+ it 'returns that class' do
83
+ expected_field = ::Protobuf::Field::Uint64Field
84
+ expect(subject.field_class(expected_field)).to eq(expected_field)
85
+ expect(subject.field_class(:uint64)).to eq(expected_field)
86
+ expect(subject.field_type(expected_field)).to eq(expected_field)
87
+ expect(subject.field_type(:uint64)).to eq(expected_field)
88
+ end
89
+ end
90
+
91
+ context 'when type is a sint32 field class or symbol' do
92
+ it 'returns that class' do
93
+ expected_field = ::Protobuf::Field::Sint32Field
94
+ expect(subject.field_class(expected_field)).to eq(expected_field)
95
+ expect(subject.field_class(:sint32)).to eq(expected_field)
96
+ expect(subject.field_type(expected_field)).to eq(expected_field)
97
+ expect(subject.field_type(:sint32)).to eq(expected_field)
98
+ end
99
+ end
100
+
101
+ context 'when type is a sint64 field class or symbol' do
102
+ it 'returns that class' do
103
+ expected_field = ::Protobuf::Field::Sint64Field
104
+ expect(subject.field_class(expected_field)).to eq(expected_field)
105
+ expect(subject.field_class(:sint64)).to eq(expected_field)
106
+ expect(subject.field_type(expected_field)).to eq(expected_field)
107
+ expect(subject.field_type(:sint64)).to eq(expected_field)
108
+ end
109
+ end
110
+
111
+ context 'when type is a fixed32 field class or symbol' do
112
+ it 'returns that class' do
113
+ expected_field = ::Protobuf::Field::Fixed32Field
114
+ expect(subject.field_class(expected_field)).to eq(expected_field)
115
+ expect(subject.field_class(:fixed32)).to eq(expected_field)
116
+ expect(subject.field_type(expected_field)).to eq(expected_field)
117
+ expect(subject.field_type(:fixed32)).to eq(expected_field)
118
+ end
119
+ end
120
+
121
+ context 'when type is a fixed64 field class or symbol' do
122
+ it 'returns that class' do
123
+ expected_field = ::Protobuf::Field::Fixed64Field
124
+ expect(subject.field_class(expected_field)).to eq(expected_field)
125
+ expect(subject.field_class(:fixed64)).to eq(expected_field)
126
+ expect(subject.field_type(expected_field)).to eq(expected_field)
127
+ expect(subject.field_type(:fixed64)).to eq(expected_field)
128
+ end
129
+ end
130
+
131
+ context 'when type is a sfixed32 field class or symbol' do
132
+ it 'returns that class' do
133
+ expected_field = ::Protobuf::Field::Sfixed32Field
134
+ expect(subject.field_class(expected_field)).to eq(expected_field)
135
+ expect(subject.field_class(:sfixed32)).to eq(expected_field)
136
+ expect(subject.field_type(expected_field)).to eq(expected_field)
137
+ expect(subject.field_type(:sfixed32)).to eq(expected_field)
138
+ end
139
+ end
140
+
141
+ context 'when type is a sfixed64 field class or symbol' do
142
+ it 'returns that class' do
143
+ expected_field = ::Protobuf::Field::Sfixed64Field
144
+ expect(subject.field_class(expected_field)).to eq(expected_field)
145
+ expect(subject.field_class(:sfixed64)).to eq(expected_field)
146
+ expect(subject.field_type(expected_field)).to eq(expected_field)
147
+ expect(subject.field_type(:sfixed64)).to eq(expected_field)
148
+ end
149
+ end
150
+
151
+
152
+ context 'when type is a string field class or symbol' do
153
+ it 'returns that class' do
154
+ expected_field = ::Protobuf::Field::StringField
155
+ expect(subject.field_class(expected_field)).to eq(expected_field)
156
+ expect(subject.field_class(:string)).to eq(expected_field)
157
+ expect(subject.field_type(expected_field)).to eq(expected_field)
158
+ expect(subject.field_type(:string)).to eq(expected_field)
159
+ end
160
+ end
161
+
162
+
163
+ context 'when type is a bytes field class or symbol' do
164
+ it 'returns that class' do
165
+ expected_field = ::Protobuf::Field::BytesField
166
+ expect(subject.field_class(expected_field)).to eq(expected_field)
167
+ expect(subject.field_class(:bytes)).to eq(expected_field)
168
+ expect(subject.field_type(expected_field)).to eq(expected_field)
169
+ expect(subject.field_type(:bytes)).to eq(expected_field)
170
+ end
171
+ end
172
+
173
+
174
+ context 'when type is a bool field class or symbol' do
175
+ it 'returns that class' do
176
+ expected_field = ::Protobuf::Field::BoolField
177
+ expect(subject.field_class(expected_field)).to eq(expected_field)
178
+ expect(subject.field_class(:bool)).to eq(expected_field)
179
+ expect(subject.field_type(expected_field)).to eq(expected_field)
180
+ expect(subject.field_type(:bool)).to eq(expected_field)
181
+ end
182
+ end
183
+
184
+ context 'when type is not mapped' do
185
+ it 'raises an ArgumentError' do
186
+ expect {
187
+ subject.field_class("boom")
188
+ }.to raise_error
189
+ end
190
+ end
191
+
192
+ end
193
+
194
+ end
@@ -0,0 +1,87 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+ require 'protobuf/generators/base'
5
+
6
+ describe ::Protobuf::Generators::Base do
7
+
8
+ subject { described_class.new(double) }
9
+
10
+ context 'namespaces' do
11
+ let(:descriptor) { double(:name => 'Baz') }
12
+ subject { described_class.new(descriptor, 0, :namespace => [ :foo, :bar ]) }
13
+ its(:type_namespace) { should eq [ :foo, :bar, 'Baz' ] }
14
+ its(:fully_qualified_type_namespace) { should eq '.foo.bar.Baz' }
15
+ end
16
+
17
+ describe '#run_once' do
18
+ it 'protects the block from being entered more than once' do
19
+ foo = 0
20
+ bar = 0
21
+
22
+ test_run_once = lambda do
23
+ bar += 1
24
+ subject.run_once(:foo_test) do
25
+ foo += 1
26
+ end
27
+ end
28
+
29
+ 10.times { test_run_once.call }
30
+ expect(foo).to eq(1)
31
+ expect(bar).to eq(10)
32
+ end
33
+
34
+ it 'always returns the same object' do
35
+ rv = subject.run_once(:foo_test) do
36
+ "foo bar"
37
+ end
38
+ expect(rv).to eq("foo bar")
39
+
40
+ rv = subject.run_once(:foo_test) do
41
+ "baz quux"
42
+ end
43
+ expect(rv).to eq("foo bar")
44
+ end
45
+ end
46
+
47
+ describe '#to_s' do
48
+ before do
49
+ class ToStringTest < ::Protobuf::Generators::Base
50
+ def compile
51
+ run_once(:compile) do
52
+ puts "this is a test"
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ subject { ToStringTest.new(double) }
59
+
60
+ it 'compiles and returns the contents' do
61
+ 10.times do
62
+ expect(subject.to_s).to eq("this is a test\n")
63
+ end
64
+ end
65
+ end
66
+
67
+ describe '#validate_tags' do
68
+ context 'when tags are duplicated' do
69
+ it 'fails with a GeneratorFatalError' do
70
+ ::Protobuf::CodeGenerator.should_receive(:fatal)
71
+ .with(/FooBar object has duplicate tags\. Expected 3 tags, but got 4/)
72
+
73
+ described_class.validate_tags("FooBar", [1,2,2,3])
74
+ end
75
+ end
76
+
77
+ context 'when tags are missing in the range' do
78
+ it 'prints a warning' do
79
+ ::Protobuf::CodeGenerator.should_receive(:warn)
80
+ .with(/FooBar object should have 5 tags \(1\.\.5\), but found 4 tags/)
81
+ described_class.validate_tags("FooBar", [1,2,4,5])
82
+ end
83
+ end
84
+ end
85
+
86
+ end
87
+
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/generators/enum_generator'
4
+
5
+ describe ::Protobuf::Generators::EnumGenerator do
6
+
7
+ let(:values) {
8
+ [
9
+ { :name => 'FOO', :number => 1 },
10
+ { :name => 'BAR', :number => 2 },
11
+ { :name => 'BAZ', :number => 3 }
12
+ ]
13
+ }
14
+ let(:options) { nil }
15
+ let(:enum_fields) { { :name => 'TestEnum',
16
+ :value => values,
17
+ :options => options } }
18
+
19
+ let(:enum) { ::Google::Protobuf::EnumDescriptorProto.new(enum_fields) }
20
+
21
+ subject { described_class.new(enum) }
22
+
23
+ describe '#compile' do
24
+ let(:compiled) {
25
+ %q{class TestEnum < ::Protobuf::Enum
26
+ define :FOO, 1
27
+ define :BAR, 2
28
+ define :BAZ, 3
29
+ end
30
+
31
+ }
32
+ }
33
+
34
+ it 'compiles the enum and it\'s field values' do
35
+ subject.compile
36
+ subject.to_s.should eq(compiled)
37
+ end
38
+
39
+ context 'when allow_alias option is set' do
40
+ let(:compiled) {
41
+ %q{class TestEnum < ::Protobuf::Enum
42
+ set_option :allow_alias
43
+
44
+ define :FOO, 1
45
+ define :BAR, 2
46
+ define :BAZ, 3
47
+ end
48
+
49
+ }
50
+ }
51
+
52
+ let(:options) { { :allow_alias => true } }
53
+
54
+ it 'sets the allow_alias option' do
55
+ subject.compile
56
+ subject.to_s.should eq(compiled)
57
+ end
58
+ end
59
+ end
60
+
61
+ describe '#build_value' do
62
+ it 'returns a string identifying the given enum value' do
63
+ subject.build_value(enum.value.first).should eq("define :FOO, 1")
64
+ end
65
+ end
66
+
67
+ end
68
+
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ require 'protobuf/code_generator'
4
+ require 'protobuf/generators/extension_generator'
5
+
6
+ describe ::Protobuf::Generators::ExtensionGenerator do
7
+
8
+ let(:field_descriptors) {
9
+ [
10
+ double('field descriptor 1', :to_s => " field 1\n"),
11
+ double('field descriptor 2', :to_s => " field 2\n"),
12
+ double('field descriptor 3', :to_s => " field 3\n")
13
+ ]
14
+ }
15
+ let(:message_type) { 'FooBar' }
16
+
17
+ before do
18
+ ::Protobuf::Generators::FieldGenerator.should_receive(:new).with(field_descriptors[0], 1).and_return(field_descriptors[0])
19
+ ::Protobuf::Generators::FieldGenerator.should_receive(:new).with(field_descriptors[1], 1).and_return(field_descriptors[1])
20
+ ::Protobuf::Generators::FieldGenerator.should_receive(:new).with(field_descriptors[2], 1).and_return(field_descriptors[2])
21
+ end
22
+
23
+ subject { described_class.new(message_type, field_descriptors, 0) }
24
+
25
+ describe '#compile' do
26
+ let(:compiled) {
27
+ %q{class FooBar < ::Protobuf::Message
28
+ field 1
29
+ field 2
30
+ field 3
31
+ end
32
+
33
+ }
34
+ }
35
+
36
+ it 'compiles the a class with the extension fields' do
37
+ subject.compile
38
+ subject.to_s.should eq(compiled)
39
+ end
40
+ end
41
+
42
+ end
43
+