|
1 | 1 | defmodule Protobuf.DSLTest do |
2 | 2 | use ExUnit.Case, async: true |
3 | 3 |
|
4 | | - import ExUnit.CaptureIO |
5 | | - |
6 | 4 | alias Protobuf.{FieldProps, MessageProps} |
7 | 5 | alias TestMsg.{Foo, Foo2, Proto3Optional} |
8 | 6 |
|
@@ -205,88 +203,92 @@ defmodule Protobuf.DSLTest do |
205 | 203 | assert TestMsg.WithTransformModule.transform_module() == TestMsg.TransformModule |
206 | 204 | end |
207 | 205 |
|
208 | | - test "emits a warning if there is already a definition for the t/0 type for an enum" do |
209 | | - output = |
210 | | - capture_io(:stderr, fn -> |
211 | | - Code.eval_quoted( |
212 | | - quote do |
213 | | - defmodule MessageWithWarning do |
214 | | - use Protobuf, syntax: :proto3, enum: true |
215 | | - |
216 | | - @type t() :: integer() | :FOO |
217 | | - |
218 | | - field :FOO, 0, type: :bool |
219 | | - end |
220 | | - end |
221 | | - ) |
222 | | - end) |
223 | | - |
224 | | - assert output =~ "the t/0 type in Protobuf enum modules is automatically generated" |
| 206 | + test "raises a compilation error if there is already a definition for the t/0 type for an enum" do |
| 207 | + assert_raise Protobuf.InvalidError, |
| 208 | + ~r{t/0 type and the struct are automatically generated}, |
| 209 | + fn -> |
| 210 | + Code.eval_quoted( |
| 211 | + quote do |
| 212 | + defmodule MessageWithWarning do |
| 213 | + use Protobuf, syntax: :proto3, enum: true |
| 214 | + |
| 215 | + @type t() :: integer() | :FOO |
| 216 | + |
| 217 | + field :FOO, 0, type: :bool |
| 218 | + end |
| 219 | + end |
| 220 | + ) |
| 221 | + end |
225 | 222 | end |
226 | 223 |
|
227 | | - test "emits a warning if there is already a call to defstruct/1 and a definition for the t/0 type" do |
228 | | - output = |
229 | | - capture_io(:stderr, fn -> |
230 | | - Code.eval_quoted( |
231 | | - quote do |
232 | | - defmodule MessageWithWarning do |
233 | | - use Protobuf, syntax: :proto3 |
| 224 | + test "raises a compilation error if there is already a call to defstruct/1 and a definition for the t/0 type" do |
| 225 | + assert_raise Protobuf.InvalidError, |
| 226 | + ~r{t/0 type and the struct are automatically generated}, |
| 227 | + fn -> |
| 228 | + Code.eval_quoted( |
| 229 | + quote do |
| 230 | + defmodule MessageWithWarning do |
| 231 | + use Protobuf, syntax: :proto3 |
234 | 232 |
|
235 | | - @type t() :: %__MODULE__{foo: boolean()} |
| 233 | + @type t() :: %__MODULE__{foo: boolean()} |
236 | 234 |
|
237 | | - defstruct [:foo] |
| 235 | + defstruct [:foo] |
238 | 236 |
|
239 | | - field :foo, 1, type: :bool |
240 | | - end |
241 | | - end |
242 | | - ) |
243 | | - end) |
244 | | - |
245 | | - assert output =~ "t/0 type and the struct are automatically generated" |
| 237 | + field :foo, 1, type: :bool |
| 238 | + end |
| 239 | + end |
| 240 | + ) |
| 241 | + end |
246 | 242 | end |
247 | 243 |
|
248 | 244 | test "raises a compilation error if there is already a call to defstruct/1 but no definition for the t/0 type" do |
249 | | - assert_raise RuntimeError, ~r{t/0 type and the struct are automatically generated}, fn -> |
250 | | - Code.eval_quoted( |
251 | | - quote do |
252 | | - defmodule MessageWithDefstructError do |
253 | | - use Protobuf, syntax: :proto3 |
254 | | - |
255 | | - defstruct [:foo] |
256 | | - |
257 | | - field :foo, 1, type: :bool |
258 | | - end |
259 | | - end |
260 | | - ) |
261 | | - end |
| 245 | + assert_raise Protobuf.InvalidError, |
| 246 | + ~r{t/0 type and the struct are automatically generated}, |
| 247 | + fn -> |
| 248 | + Code.eval_quoted( |
| 249 | + quote do |
| 250 | + defmodule MessageWithDefstructError do |
| 251 | + use Protobuf, syntax: :proto3 |
| 252 | + |
| 253 | + defstruct [:foo] |
| 254 | + |
| 255 | + field :foo, 1, type: :bool |
| 256 | + end |
| 257 | + end |
| 258 | + ) |
| 259 | + end |
262 | 260 | end |
263 | 261 |
|
264 | 262 | test "raises a compilation error if there is already a definition for the t/0 type but no defstruct" do |
265 | | - assert_raise RuntimeError, ~r{the t/0 type and the struct are automatically generated}, fn -> |
266 | | - Code.eval_quoted( |
267 | | - quote do |
268 | | - defmodule MessageWithTTypeError do |
269 | | - use Protobuf, syntax: :proto3 |
270 | | - |
271 | | - @type t() :: %__MODULE__{foo: boolean()} |
272 | | - |
273 | | - field :foo, 1, type: :bool |
274 | | - end |
275 | | - end |
276 | | - ) |
277 | | - end |
| 263 | + assert_raise Protobuf.InvalidError, |
| 264 | + ~r{the t/0 type and the struct are automatically generated}, |
| 265 | + fn -> |
| 266 | + Code.eval_quoted( |
| 267 | + quote do |
| 268 | + defmodule MessageWithTTypeError do |
| 269 | + use Protobuf, syntax: :proto3 |
| 270 | + |
| 271 | + @type t() :: %__MODULE__{foo: boolean()} |
| 272 | + |
| 273 | + field :foo, 1, type: :bool |
| 274 | + end |
| 275 | + end |
| 276 | + ) |
| 277 | + end |
278 | 278 | end |
279 | 279 |
|
280 | 280 | test "raises a compilation error if syntax is proto3 and the first enum has tag other than 0" do |
281 | | - assert_raise RuntimeError, "the first enum value must have tag 0 in proto3, got: 1", fn -> |
282 | | - Code.eval_quoted( |
283 | | - quote do |
284 | | - defmodule MessageWithProto3BadEnumTag do |
285 | | - use Protobuf, syntax: :proto3, enum: true |
286 | | - field :NOT_ZERO, 1 |
287 | | - end |
288 | | - end |
289 | | - ) |
290 | | - end |
| 281 | + assert_raise RuntimeError, |
| 282 | + "the first enum value must have tag 0 in proto3, got: 1", |
| 283 | + fn -> |
| 284 | + Code.eval_quoted( |
| 285 | + quote do |
| 286 | + defmodule MessageWithProto3BadEnumTag do |
| 287 | + use Protobuf, syntax: :proto3, enum: true |
| 288 | + field :NOT_ZERO, 1 |
| 289 | + end |
| 290 | + end |
| 291 | + ) |
| 292 | + end |
291 | 293 | end |
292 | 294 | end |
0 commit comments