blob: e91eb7e2ba4f4e524bea2eddfe5e825ba05a5db9 [file] [log] [blame]
Joe Tsaic9899da2018-12-06 18:34:53 -08001// 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
5package defval
6
7import (
8 "math"
9 "reflect"
10 "testing"
11
12 pref "github.com/golang/protobuf/v2/reflect/protoreflect"
13)
14
15func 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}