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 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 5 | package defval_test |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "math" |
| 9 | "reflect" |
| 10 | "testing" |
| 11 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 12 | "google.golang.org/protobuf/internal/encoding/defval" |
| 13 | fdesc "google.golang.org/protobuf/internal/filedesc" |
Damien Neil | e89e624 | 2019-05-13 23:55:40 -0700 | [diff] [blame] | 14 | pref "google.golang.org/protobuf/reflect/protoreflect" |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 15 | ) |
| 16 | |
| 17 | func Test(t *testing.T) { |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 18 | evs := fdesc.EnumValues{List: []fdesc.EnumValue{{}}} |
| 19 | evs.List[0].L0.ParentFile = fdesc.SurrogateProto2 |
| 20 | evs.List[0].L0.FullName = "ALPHA" |
| 21 | evs.List[0].L1.Number = 1 |
| 22 | |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 23 | V := pref.ValueOf |
| 24 | tests := []struct { |
| 25 | val pref.Value |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 26 | enum pref.EnumValueDescriptor |
| 27 | enums pref.EnumValueDescriptors |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 28 | kind pref.Kind |
| 29 | strPB string |
| 30 | strGo string |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 31 | }{{ |
| 32 | val: V(bool(true)), |
| 33 | enum: nil, |
| 34 | enums: nil, |
| 35 | kind: pref.BoolKind, |
| 36 | strPB: "true", |
| 37 | strGo: "1", |
| 38 | }, { |
| 39 | val: V(int32(-0x1234)), |
| 40 | enum: nil, |
| 41 | enums: nil, |
| 42 | kind: pref.Int32Kind, |
| 43 | strPB: "-4660", |
| 44 | strGo: "-4660", |
| 45 | }, { |
| 46 | val: V(float32(math.Pi)), |
| 47 | enum: nil, |
| 48 | enums: nil, |
| 49 | kind: pref.FloatKind, |
| 50 | strPB: "3.1415927", |
| 51 | strGo: "3.1415927", |
| 52 | }, { |
| 53 | val: V(float64(math.Pi)), |
| 54 | enum: nil, |
| 55 | enums: nil, |
| 56 | kind: pref.DoubleKind, |
| 57 | strPB: "3.141592653589793", |
| 58 | strGo: "3.141592653589793", |
| 59 | }, { |
| 60 | val: V(string("hello, \xde\xad\xbe\xef\n")), |
| 61 | enum: nil, |
| 62 | enums: nil, |
| 63 | kind: pref.StringKind, |
| 64 | strPB: "hello, \xde\xad\xbe\xef\n", |
| 65 | strGo: "hello, \xde\xad\xbe\xef\n", |
| 66 | }, { |
| 67 | val: V([]byte("hello, \xde\xad\xbe\xef\n")), |
| 68 | enum: nil, |
| 69 | enums: nil, |
| 70 | kind: pref.BytesKind, |
| 71 | strPB: "hello, \\336\\255\\276\\357\\n", |
| 72 | strGo: "hello, \\336\\255\\276\\357\\n", |
| 73 | }, { |
| 74 | val: V(pref.EnumNumber(1)), |
| 75 | enum: &evs.List[0], |
| 76 | enums: &evs, |
| 77 | kind: pref.EnumKind, |
| 78 | strPB: "ALPHA", |
| 79 | strGo: "1", |
| 80 | }} |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 81 | |
| 82 | for _, tt := range tests { |
| 83 | t.Run("", func(t *testing.T) { |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 84 | gotStr, _ := defval.Marshal(tt.val, tt.enum, tt.kind, defval.Descriptor) |
| 85 | if gotStr != tt.strPB { |
| 86 | t.Errorf("Marshal(%v, %v, Descriptor) = %q, want %q", tt.val, tt.kind, gotStr, tt.strPB) |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 89 | gotStr, _ = defval.Marshal(tt.val, tt.enum, tt.kind, defval.GoTag) |
| 90 | if gotStr != tt.strGo { |
| 91 | t.Errorf("Marshal(%v, %v, GoTag) = %q, want %q", tt.val, tt.kind, gotStr, tt.strGo) |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 94 | gotVal, gotEnum, _ := defval.Unmarshal(tt.strPB, tt.kind, tt.enums, defval.Descriptor) |
| 95 | if !reflect.DeepEqual(gotVal.Interface(), tt.val.Interface()) || gotEnum != tt.enum { |
| 96 | t.Errorf("Unmarshal(%v, %v, Descriptor) = (%q, %v), want (%q, %v)", tt.strPB, tt.kind, gotVal, gotEnum, tt.val, tt.enum) |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Joe Tsai | d888139 | 2019-06-06 13:01:53 -0700 | [diff] [blame^] | 99 | gotVal, gotEnum, _ = defval.Unmarshal(tt.strGo, tt.kind, tt.enums, defval.GoTag) |
| 100 | if !reflect.DeepEqual(gotVal.Interface(), tt.val.Interface()) || gotEnum != tt.enum { |
| 101 | t.Errorf("Unmarshal(%v, %v, GoTag) = (%q, %v), want (%q, %v)", tt.strGo, tt.kind, gotVal, gotEnum, tt.val, tt.enum) |
Joe Tsai | c9899da | 2018-12-06 18:34:53 -0800 | [diff] [blame] | 102 | } |
| 103 | }) |
| 104 | } |
| 105 | } |