Damien Neil | 835b271 | 2019-08-29 14:08:28 -0700 | [diff] [blame] | 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/golang/protobuf/proto" |
| 12 | cmp "github.com/google/go-cmp/cmp" |
| 13 | testpb "google.golang.org/protobuf/internal/testprotos/test" |
| 14 | pref "google.golang.org/protobuf/reflect/protoreflect" |
| 15 | ) |
| 16 | |
| 17 | func TestExtensionType(t *testing.T) { |
| 18 | cmpOpts := cmp.Options{ |
| 19 | cmp.Comparer(func(x, y proto.Message) bool { |
| 20 | return proto.Equal(x, y) |
| 21 | }), |
| 22 | } |
| 23 | for _, test := range []struct { |
| 24 | xt pref.ExtensionType |
| 25 | value interface{} |
| 26 | }{ |
| 27 | { |
| 28 | xt: testpb.E_OptionalInt32Extension, |
| 29 | value: int32(0), |
| 30 | }, |
| 31 | { |
| 32 | xt: testpb.E_OptionalInt64Extension, |
| 33 | value: int64(0), |
| 34 | }, |
| 35 | { |
| 36 | xt: testpb.E_OptionalUint32Extension, |
| 37 | value: uint32(0), |
| 38 | }, |
| 39 | { |
| 40 | xt: testpb.E_OptionalUint64Extension, |
| 41 | value: uint64(0), |
| 42 | }, |
| 43 | { |
| 44 | xt: testpb.E_OptionalFloatExtension, |
| 45 | value: float32(0), |
| 46 | }, |
| 47 | { |
| 48 | xt: testpb.E_OptionalDoubleExtension, |
| 49 | value: float64(0), |
| 50 | }, |
| 51 | { |
| 52 | xt: testpb.E_OptionalBoolExtension, |
| 53 | value: true, |
| 54 | }, |
| 55 | { |
| 56 | xt: testpb.E_OptionalStringExtension, |
| 57 | value: "", |
| 58 | }, |
| 59 | { |
| 60 | xt: testpb.E_OptionalBytesExtension, |
| 61 | value: []byte{}, |
| 62 | }, |
| 63 | { |
| 64 | xt: testpb.E_OptionalNestedMessageExtension, |
| 65 | value: &testpb.TestAllTypes_NestedMessage{}, |
| 66 | }, |
| 67 | { |
| 68 | xt: testpb.E_OptionalNestedEnumExtension, |
| 69 | value: testpb.TestAllTypes_FOO, |
| 70 | }, |
| 71 | { |
| 72 | xt: testpb.E_RepeatedInt32Extension, |
| 73 | value: []int32{0}, |
| 74 | }, |
| 75 | { |
| 76 | xt: testpb.E_RepeatedInt64Extension, |
| 77 | value: []int64{0}, |
| 78 | }, |
| 79 | { |
| 80 | xt: testpb.E_RepeatedUint32Extension, |
| 81 | value: []uint32{0}, |
| 82 | }, |
| 83 | { |
| 84 | xt: testpb.E_RepeatedUint64Extension, |
| 85 | value: []uint64{0}, |
| 86 | }, |
| 87 | { |
| 88 | xt: testpb.E_RepeatedFloatExtension, |
| 89 | value: []float32{0}, |
| 90 | }, |
| 91 | { |
| 92 | xt: testpb.E_RepeatedDoubleExtension, |
| 93 | value: []float64{0}, |
| 94 | }, |
| 95 | { |
| 96 | xt: testpb.E_RepeatedBoolExtension, |
| 97 | value: []bool{true}, |
| 98 | }, |
| 99 | { |
| 100 | xt: testpb.E_RepeatedStringExtension, |
| 101 | value: []string{""}, |
| 102 | }, |
| 103 | { |
| 104 | xt: testpb.E_RepeatedBytesExtension, |
| 105 | value: [][]byte{nil}, |
| 106 | }, |
| 107 | { |
| 108 | xt: testpb.E_RepeatedNestedMessageExtension, |
| 109 | value: []*testpb.TestAllTypes_NestedMessage{{}}, |
| 110 | }, |
| 111 | { |
| 112 | xt: testpb.E_RepeatedNestedEnumExtension, |
| 113 | value: []testpb.TestAllTypes_NestedEnum{testpb.TestAllTypes_FOO}, |
| 114 | }, |
| 115 | } { |
| 116 | name := test.xt.TypeDescriptor().FullName() |
| 117 | t.Run(fmt.Sprint(name), func(t *testing.T) { |
| 118 | if !test.xt.IsValidInterface(test.value) { |
| 119 | t.Fatalf("IsValidInterface(%[1]T(%[1]v)) = false, want true", test.value) |
| 120 | } |
| 121 | v := test.xt.ValueOf(test.value) |
| 122 | if !test.xt.IsValidValue(v) { |
| 123 | t.Fatalf("IsValidValue(%[1]T(%[1]v)) = false, want true", v) |
| 124 | } |
| 125 | if got, want := test.xt.InterfaceOf(v), test.value; !cmp.Equal(got, want, cmpOpts) { |
| 126 | t.Fatalf("round trip InterfaceOf(ValueOf(x)) = %v, want %v", got, want) |
| 127 | } |
| 128 | }) |
| 129 | } |
| 130 | } |