Skip to content

Commit

Permalink
Add unitests for packed arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarp committed Jun 12, 2019
1 parent 3f8a1a5 commit 4c62fb7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/google/protobuf/decoding.d
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,32 @@ unittest
assert(buff.empty);
}

unittest
{
import std.array : array;
import std.typecons : Yes;

import google.protobuf.encoding : toProtobuf;

struct Foo
{
@Proto(1) int[] bar = protoDefaultValue!(int[]);
@Proto(2, Wire.none, Yes.packed) int[] baz = protoDefaultValue!(int[]);
}

Foo foo;
foo.bar = [1, 2];
foo.baz = [3, 4];
auto buff = foo.toProtobuf.array;

foo = Foo.init;
assert(foo.bar.empty);
assert(foo.baz.empty);
foo = buff.fromProtobuf!Foo;
assert(foo.bar == [1, 2]);
assert(foo.baz == [3, 4]);
}

private static void fromProtobufByField(alias field, T, R)(ref R inputRange, ref T message)
if (isInputRange!R)
{
Expand Down
18 changes: 18 additions & 0 deletions src/google/protobuf/encoding.d
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,24 @@ unittest
assert(emptyMessage.toProtobuf.empty);
}

unittest
{
import std.array : array;
import std.typecons : Yes;

struct Foo
{
@Proto(1) int[] bar = protoDefaultValue!(int[]);
@Proto(2, Wire.none, Yes.packed) int[] baz = protoDefaultValue!(int[]);
}

Foo foo;
assert(foo.toProtobuf.empty);
foo.bar = [1, 2];
foo.baz = [3, 4];
assert(foo.toProtobuf.array == [0x08, 0x01, 0x08, 0x02, 0x12, 0x02, 0x03, 0x04]);
}

unittest
{
struct Foo
Expand Down

0 comments on commit 4c62fb7

Please sign in to comment.