...

Source file src/google.golang.org/protobuf/internal/impl/extension_test.go

Documentation: google.golang.org/protobuf/internal/impl

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package impl_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"github.com/google/go-cmp/cmp"
    12  
    13  	"google.golang.org/protobuf/proto"
    14  	"google.golang.org/protobuf/reflect/protoreflect"
    15  
    16  	testpb "google.golang.org/protobuf/internal/testprotos/test"
    17  )
    18  
    19  func TestExtensionType(t *testing.T) {
    20  	cmpOpts := cmp.Options{
    21  		cmp.Comparer(func(x, y proto.Message) bool {
    22  			return proto.Equal(x, y)
    23  		}),
    24  	}
    25  	for _, test := range []struct {
    26  		xt    protoreflect.ExtensionType
    27  		value interface{}
    28  	}{
    29  		{
    30  			xt:    testpb.E_OptionalInt32,
    31  			value: int32(0),
    32  		},
    33  		{
    34  			xt:    testpb.E_OptionalInt64,
    35  			value: int64(0),
    36  		},
    37  		{
    38  			xt:    testpb.E_OptionalUint32,
    39  			value: uint32(0),
    40  		},
    41  		{
    42  			xt:    testpb.E_OptionalUint64,
    43  			value: uint64(0),
    44  		},
    45  		{
    46  			xt:    testpb.E_OptionalFloat,
    47  			value: float32(0),
    48  		},
    49  		{
    50  			xt:    testpb.E_OptionalDouble,
    51  			value: float64(0),
    52  		},
    53  		{
    54  			xt:    testpb.E_OptionalBool,
    55  			value: true,
    56  		},
    57  		{
    58  			xt:    testpb.E_OptionalString,
    59  			value: "",
    60  		},
    61  		{
    62  			xt:    testpb.E_OptionalBytes,
    63  			value: []byte{},
    64  		},
    65  		{
    66  			xt:    testpb.E_OptionalNestedMessage,
    67  			value: &testpb.TestAllExtensions_NestedMessage{},
    68  		},
    69  		{
    70  			xt:    testpb.E_OptionalNestedEnum,
    71  			value: testpb.TestAllTypes_FOO,
    72  		},
    73  		{
    74  			xt:    testpb.E_RepeatedInt32,
    75  			value: []int32{0},
    76  		},
    77  		{
    78  			xt:    testpb.E_RepeatedInt64,
    79  			value: []int64{0},
    80  		},
    81  		{
    82  			xt:    testpb.E_RepeatedUint32,
    83  			value: []uint32{0},
    84  		},
    85  		{
    86  			xt:    testpb.E_RepeatedUint64,
    87  			value: []uint64{0},
    88  		},
    89  		{
    90  			xt:    testpb.E_RepeatedFloat,
    91  			value: []float32{0},
    92  		},
    93  		{
    94  			xt:    testpb.E_RepeatedDouble,
    95  			value: []float64{0},
    96  		},
    97  		{
    98  			xt:    testpb.E_RepeatedBool,
    99  			value: []bool{true},
   100  		},
   101  		{
   102  			xt:    testpb.E_RepeatedString,
   103  			value: []string{""},
   104  		},
   105  		{
   106  			xt:    testpb.E_RepeatedBytes,
   107  			value: [][]byte{nil},
   108  		},
   109  		{
   110  			xt:    testpb.E_RepeatedNestedMessage,
   111  			value: []*testpb.TestAllExtensions_NestedMessage{{}},
   112  		},
   113  		{
   114  			xt:    testpb.E_RepeatedNestedEnum,
   115  			value: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO},
   116  		},
   117  	} {
   118  		name := test.xt.TypeDescriptor().FullName()
   119  		t.Run(fmt.Sprint(name), func(t *testing.T) {
   120  			if !test.xt.IsValidInterface(test.value) {
   121  				t.Fatalf("IsValidInterface(%[1]T(%[1]v)) = false, want true", test.value)
   122  			}
   123  			v := test.xt.ValueOf(test.value)
   124  			if !test.xt.IsValidValue(v) {
   125  				t.Fatalf("IsValidValue(%[1]T(%[1]v)) = false, want true", v)
   126  			}
   127  			if got, want := test.xt.InterfaceOf(v), test.value; !cmp.Equal(got, want, cmpOpts) {
   128  				t.Fatalf("round trip InterfaceOf(ValueOf(x)) = %v, want %v", got, want)
   129  			}
   130  		})
   131  	}
   132  }
   133  

View as plain text