Skip to content

Commit

Permalink
Add tests for protobuf map fields
Browse files Browse the repository at this point in the history
This switches to using a dedicated test.proto file, since the
descriptor.proto file previously being used doesn't have any map fields.

Signed-off-by: Nic Cope <nicc@rk0n.org>
  • Loading branch information
negz committed Oct 2, 2023
1 parent d1f4465 commit 239c3ac
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 13 deletions.
28 changes: 23 additions & 5 deletions starlark/eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"errors"
"fmt"
"math"
"os"
"os/exec"
"path/filepath"
"reflect"
Expand All @@ -19,15 +20,16 @@ import (
"go.starlark.net/internal/chunkedfile"
"go.starlark.net/lib/json"
starlarkmath "go.starlark.net/lib/math"
"go.starlark.net/lib/proto"
starlarkproto "go.starlark.net/lib/proto"
"go.starlark.net/lib/time"
"go.starlark.net/starlark"
"go.starlark.net/starlarkstruct"
"go.starlark.net/starlarktest"
"go.starlark.net/syntax"
"google.golang.org/protobuf/reflect/protoregistry"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"

_ "google.golang.org/protobuf/types/descriptorpb" // example descriptor needed for lib/proto tests
"google.golang.org/protobuf/types/descriptorpb"
)

// A test may enable non-standard options by containing (e.g.) "option:recursion".
Expand Down Expand Up @@ -115,7 +117,23 @@ func TestExecFile(t *testing.T) {
testdata := starlarktest.DataFile("starlark", ".")
thread := &starlark.Thread{Load: load}
starlarktest.SetReporter(thread, t)
proto.SetPool(thread, protoregistry.GlobalFiles)

// This proto is used for the proto.star tests. It's generated by running:
// protoc --descriptor_set_out=test.fds test.proto
data, err := os.ReadFile("testdata/proto/test.fds")
if err != nil {
t.Fatal(err)
}
fds := &descriptorpb.FileDescriptorSet{}
if err := proto.Unmarshal(data, fds); err != nil {
t.Fatal(err)
}
pool, err := protodesc.NewFiles(fds)
if err != nil {
t.Fatal(err)
}
starlarkproto.SetPool(thread, pool)

for _, file := range []string{
"testdata/assign.star",
"testdata/bool.star",
Expand Down Expand Up @@ -207,7 +225,7 @@ func load(thread *starlark.Thread, module string) (starlark.StringDict, error) {
return starlark.StringDict{"math": starlarkmath.Module}, nil
}
if module == "proto.star" {
return starlark.StringDict{"proto": proto.Module}, nil
return starlark.StringDict{"proto": starlarkproto.Module}, nil
}

// TODO(adonovan): test load() using this execution path.
Expand Down
40 changes: 32 additions & 8 deletions starlark/testdata/proto.star
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@
load("assert.star", "assert")
load("proto.star", "proto")

schema = proto.file("google/protobuf/descriptor.proto")
schema = proto.file("test.proto")

m = schema.FileDescriptorProto(name = "somename.proto", dependency = ["a", "b", "c"])
m = schema.Test(
string_field="I'm a string!",
int32_field=42,
repeated_field=["a", "b", "c"],
map_field={"a": "A", "b": "B"}
)
assert.eq(type(m), "proto.Message")
assert.eq(m.name, "somename.proto")
assert.eq(list(m.dependency), ["a", "b", "c"])
m.dependency = ["d", "e"]
assert.eq(list(m.dependency), ["d", "e"])
m.dependency.append("f")
assert.eq(list(m.dependency), ["d", "e", "f"])
assert.eq(type(m.repeated_field), "proto.repeated<string>")
assert.eq(type(m.map_field), "proto.map<string, string>")

assert.eq(m.string_field, "I'm a string!")
assert.eq(m.int32_field, 42)

assert.eq(list(m.repeated_field), ["a", "b", "c"])
m.repeated_field = ["d", "e"]
assert.eq(list(m.repeated_field), ["d", "e"])
m.repeated_field.append("f")
assert.eq(list(m.repeated_field), ["d", "e", "f"])

assert.eq(dict(m.map_field), {"a": "A", "b": "B"})
m.map_field["c"] = "C"
assert.eq(dict(m.map_field), {"a": "A", "b": "B", "c": "C"})


# m = schema.FileDescriptorProto(name = "somename.proto", dependency = ["a", "b", "c"])
# assert.eq(type(m), "proto.Message")
# assert.eq(m.name, "somename.proto")
# assert.eq(list(m.dependency), ["a", "b", "c"])
# m.dependency = ["d", "e"]
# assert.eq(list(m.dependency), ["d", "e"])
# m.dependency.append("f")
# assert.eq(list(m.dependency), ["d", "e", "f"])

13 changes: 13 additions & 0 deletions starlark/testdata/proto/test.fds
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

�

test.protogo.starlark.net.testdata"�
Test!
string_field ( R stringField
int32_field (R
int32Field%
repeated_field ( RrepeatedFieldI
map_field ( 2,.go.starlark.net.testdata.Test.MapFieldEntryRmapField;
MapFieldEntry
key ( Rkey
value ( Rvalue:8bproto3
Expand Down
11 changes: 11 additions & 0 deletions starlark/testdata/proto/test.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
syntax = "proto3";

package go.starlark.net.testdata;

message Test {
string string_field = 1;
int32 int32_field = 2;
repeated string repeated_field = 3;
map<string, string> map_field = 4;
}

0 comments on commit 239c3ac

Please sign in to comment.