Skip to content

Commit

Permalink
Add proto2 default test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pcj committed Jun 21, 2023
1 parent 8100b7e commit 49e0dcc
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions hasher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,68 @@ func TestHashStringFields(t *testing.T) {
}
}

// TestHashProto2DefaultFields checks that proto2 default field values are properly hashed.
func TestHashProto2DefaultFields(t *testing.T) {
for name, tc := range map[string]hashTestCase{
"simple bool": {
fieldNamesAsKeys: true,
protos: []proto.Message{
&pb2_latest.Simple{BoolField: proto.Bool(false)},
},
obj: map[string]bool{"bool_field": false},
json: "{\"bool_field\":false}",
want: "1ab5ecdbe4176473024f7efd080593b740d22d076d06ea6edd8762992b484a12",
},
"simple bytes": {
fieldNamesAsKeys: true,
protos: []proto.Message{
&pb2_latest.Simple{BytesField: []byte{}},
},
obj: map[string][]byte{"bytes_field": {}},
// json: skipped - JSON does not have 'bytes' representation
want: "10a0dbbfa097b731c7a505246ffa96a82f997b8c25892d76d3b8b1355e529e05",
},
"simple string": {
fieldNamesAsKeys: true,
protos: []proto.Message{
&pb2_latest.Simple{StringField: proto.String("")},
},
obj: map[string]string{"string_field": ""},
json: "{\"string_field\":\"\"}",
want: "2d60c2941830ef4bb14424e47c6cd010f2b95e5e34291f429998288a60ac8c22",
},
"ints": {
fieldNamesAsKeys: true,
protos: []proto.Message{
&pb2_latest.Fixed32Message{Value: proto.Uint32(0)},
&pb2_latest.Fixed64Message{Value: proto.Uint64(0)},
&pb2_latest.Int32Message{Value: proto.Int32(0)},
&pb2_latest.Int64Message{Value: proto.Int64(0)},
&pb2_latest.Sfixed32Message{Value: proto.Int32(0)},
&pb2_latest.Sfixed64Message{Value: proto.Int64(0)},
&pb2_latest.Sint32Message{Value: proto.Int32(0)},
&pb2_latest.Sint64Message{Value: proto.Int64(0)},
&pb2_latest.Uint32Message{Value: proto.Uint32(0)},
&pb2_latest.Uint64Message{Value: proto.Uint64(0)},
},
obj: map[string]int64{"value": 0},
want: "49f031b73dad26859ffeea8a2bb170aaf7358d2277b00c7fc7ea8edcd37e53a1",
},
"floats": {
fieldNamesAsKeys: true,
protos: []proto.Message{
&pb2_latest.DoubleMessage{Value: proto.Float64(0.0)},
&pb2_latest.FloatMessage{Value: proto.Float32(0.0)},
},
obj: map[string]float64{"value": 0.0},
json: "{\"value\":0.0}",
want: "94136b0850db069dfd7bee090fc7ede48aa7da53ae3cc8514140a493818c3b91",
},
} {
tc.Check(name, t)
}
}

func TestHashMapFields(t *testing.T) {
for name, tc := range map[string]hashTestCase{
"boolean maps": {
Expand Down

0 comments on commit 49e0dcc

Please sign in to comment.