Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame^] | 1 | // Copyright 2018 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 defval |
| 6 | |
| 7 | import ( |
| 8 | "math" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | |
| 12 | pref "github.com/golang/protobuf/v2/reflect/protoreflect" |
| 13 | ) |
| 14 | |
| 15 | func Test(t *testing.T) { |
| 16 | V := pref.ValueOf |
| 17 | tests := []struct { |
| 18 | val pref.Value |
| 19 | kind pref.Kind |
| 20 | strPB string |
| 21 | strGo string |
| 22 | }{ |
| 23 | {V(bool(true)), pref.BoolKind, "true", "1"}, |
| 24 | {V(int32(-0x1234)), pref.Int32Kind, "-4660", "-4660"}, |
| 25 | {V(float32(math.Pi)), pref.FloatKind, "3.1415927", "3.1415927"}, |
| 26 | {V(float64(math.Pi)), pref.DoubleKind, "3.141592653589793", "3.141592653589793"}, |
| 27 | {V(string("hello, \xde\xad\xbe\xef\n")), pref.StringKind, "hello, \xde\xad\xbe\xef\n", "hello, \xde\xad\xbe\xef\n"}, |
| 28 | {V([]byte("hello, \xde\xad\xbe\xef\n")), pref.BytesKind, "hello, \\336\\255\\276\\357\\n", "hello, \\336\\255\\276\\357\\n"}, |
| 29 | } |
| 30 | |
| 31 | for _, tt := range tests { |
| 32 | t.Run("", func(t *testing.T) { |
| 33 | gotStrPB, _ := Marshal(tt.val, tt.kind, Descriptor) |
| 34 | if gotStrPB != tt.strPB { |
| 35 | t.Errorf("Marshal(%v, %v, Descriptor) = %q, want %q", tt.val, tt.kind, gotStrPB, tt.strPB) |
| 36 | } |
| 37 | |
| 38 | gotStrGo, _ := Marshal(tt.val, tt.kind, GoTag) |
| 39 | if gotStrGo != tt.strGo { |
| 40 | t.Errorf("Marshal(%v, %v, GoTag) = %q, want %q", tt.val, tt.kind, gotStrGo, tt.strGo) |
| 41 | } |
| 42 | |
| 43 | gotValPB, _ := Unmarshal(tt.strPB, tt.kind, Descriptor) |
| 44 | if !reflect.DeepEqual(gotValPB.Interface(), tt.val.Interface()) { |
| 45 | t.Errorf("Unmarshal(%v, %v, Descriptor) = %q, want %q", tt.strPB, tt.kind, gotValPB, tt.val) |
| 46 | } |
| 47 | |
| 48 | gotValGo, _ := Unmarshal(tt.strGo, tt.kind, GoTag) |
| 49 | if !reflect.DeepEqual(gotValGo.Interface(), tt.val.Interface()) { |
| 50 | t.Errorf("Unmarshal(%v, %v, GoTag) = %q, want %q", tt.strGo, tt.kind, gotValGo, tt.val) |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | } |