Skip to content

Commit

Permalink
Add tests on proto3 optional fields
Browse files Browse the repository at this point in the history
Let's add some tests on a message that contains proto3 optional
fields.

As I did not want to affect the compilation of all the test
protocol buffers with the --experimental_allow_proto3_optional
flag, compile the one .proto file intended to test this path
separately. Once the feature is no longer experimental (and
behind a flag), this .proto can be compiled along with the other
test fixtures.

Add a TODO on the single test that will be updated when we add
support for proto3 optional fields in the next commit.
  • Loading branch information
ezimanyi committed Jun 11, 2020
1 parent 5602e29 commit 4f7232b
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
12 changes: 12 additions & 0 deletions fixtures/Cookie.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
syntax = "proto3";

package com.example;

/**
* Represents a cookie.
*/
message Cookie {
string id = 1; // The id of the cookie.
optional string name = 2; // The name of the cookie.
repeated string ingredients = 3; // Ingredients in the cookie.
}
Binary file added fixtures/cookie.pb
Binary file not shown.
6 changes: 6 additions & 0 deletions fixtures/generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package fixtures

//go:generate protoc --descriptor_set_out=fileset.pb --include_imports --include_source_info -I. -I../thirdparty Booking.proto Vehicle.proto

// Compiling proto3 optional fields requires using protoc >=3.12.x and passing the --experimental_allow_proto3_optional flag.
// Rather than use this flag to compile all of the protocol buffers (which would eliminate test coverage for descriptors
// compiled without the flag), only pass this flag when compiling the one message explicitly testing proto3 optional fields.
// Once this feature is no longer behind an experimental flag, compilation of Cookie.proto can be moved to the above protoc command.
//go:generate protoc --experimental_allow_proto3_optional --descriptor_set_out=cookie.pb --include_imports --include_source_info -I. -I../thirdparty Cookie.proto
44 changes: 44 additions & 0 deletions template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var (
template *Template
bookingFile *File
vehicleFile *File

cookieTemplate *Template
cookieFile *File
)

func TestMain(m *testing.M) {
Expand All @@ -30,6 +33,12 @@ func TestMain(m *testing.M) {
bookingFile = template.Files[0]
vehicleFile = template.Files[1]

set, _ = utils.LoadDescriptorSet("fixtures", "cookie.pb")
req = utils.CreateGenRequest(set, "Cookie.proto")
result = protokit.ParseCodeGenRequest(req)
cookieTemplate = NewTemplate(result)
cookieFile = cookieTemplate.Files[0]

os.Exit(m.Run())
}

Expand Down Expand Up @@ -315,6 +324,41 @@ func TestFieldPropertiesProto3(t *testing.T) {
require.Empty(t, field.Options)
}

func TestFieldPropertiesProto3Optional(t *testing.T) {
msg := findMessage("Cookie", cookieFile)

field := findField("id", msg)
require.Equal(t, "id", field.Name)
require.Equal(t, "The id of the cookie.", field.Description)
require.Equal(t, "", field.Label)
require.Equal(t, "string", field.Type)
require.Equal(t, "string", field.LongType)
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)

field = findField("name", msg)
require.Equal(t, "name", field.Name)
require.Equal(t, "The name of the cookie.", field.Description)
//TODO(ezimanyi): This is a proto3 optional field, and should have label "optional"
require.Equal(t, "", field.Label)
require.Equal(t, "string", field.Type)
require.Equal(t, "string", field.LongType)
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)

field = findField("ingredients", msg)
require.Equal(t, "ingredients", field.Name)
require.Equal(t, "Ingredients in the cookie.", field.Description)
require.Equal(t, "repeated", field.Label)
require.Equal(t, "string", field.Type)
require.Equal(t, "string", field.LongType)
require.Equal(t, "string", field.FullType)
require.Empty(t, field.DefaultValue)
require.Empty(t, field.Options)
}

func TestServiceProperties(t *testing.T) {
service := findService("VehicleService", vehicleFile)
require.Equal(t, "VehicleService", service.Name)
Expand Down

0 comments on commit 4f7232b

Please sign in to comment.