You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

69 rivejä
1.6 KiB

  1. // Copyright 2013 Google Inc. 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 googleapi
  5. import (
  6. "bytes"
  7. "encoding/json"
  8. "reflect"
  9. "testing"
  10. )
  11. func TestTypes(t *testing.T) {
  12. type T struct {
  13. I32 Int32s
  14. I64 Int64s
  15. U32 Uint32s
  16. U64 Uint64s
  17. F64 Float64s
  18. }
  19. v := &T{
  20. I32: Int32s{-1, 2, 3},
  21. I64: Int64s{-1, 2, 1 << 33},
  22. U32: Uint32s{1, 2},
  23. U64: Uint64s{1, 2, 1 << 33},
  24. F64: Float64s{1.5, 3.33},
  25. }
  26. got, err := json.Marshal(v)
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. want := `{"I32":["-1","2","3"],"I64":["-1","2","8589934592"],"U32":["1","2"],"U64":["1","2","8589934592"],"F64":["1.5","3.33"]}`
  31. if string(got) != want {
  32. t.Fatalf("Marshal mismatch.\n got: %s\nwant: %s\n", got, want)
  33. }
  34. v2 := new(T)
  35. if err := json.Unmarshal(got, v2); err != nil {
  36. t.Fatalf("Unmarshal: %v", err)
  37. }
  38. if !reflect.DeepEqual(v, v2) {
  39. t.Fatalf("Unmarshal didn't produce same results.\n got: %#v\nwant: %#v\n", v, v2)
  40. }
  41. }
  42. func TestRawMessageMarshal(t *testing.T) {
  43. // https://golang.org/issue/14493
  44. const want = "{}"
  45. b, err := json.Marshal(RawMessage(want))
  46. if err != nil {
  47. t.Fatalf("Marshal: %v", err)
  48. }
  49. if !bytes.Equal(b, []byte(want)) {
  50. t.Errorf("Marshal(RawMessage(%q)) = %q; want %q", want, b, want)
  51. }
  52. }
  53. func TestRawMessageUnmarshal(t *testing.T) {
  54. const want = "{}"
  55. var m RawMessage
  56. if err := json.Unmarshal([]byte(want), &m); err != nil {
  57. t.Fatalf("Unmarshal: %v", err)
  58. }
  59. if !bytes.Equal([]byte(m), []byte(want)) {
  60. t.Errorf("Unmarshal([]byte(%q), &m); m = %q; want %q", want, string(m), want)
  61. }
  62. }