default_test.go 1.8 KB

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