File: types_test.go

package info (click to toggle)
golang-google-api 0.61.0-6
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 209,156 kB
  • sloc: sh: 183; makefile: 22; python: 4
file content (68 lines) | stat: -rw-r--r-- 1,619 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2013 Google LLC. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package googleapi

import (
	"bytes"
	"encoding/json"
	"reflect"
	"testing"
)

func TestTypes(t *testing.T) {
	type T struct {
		I32 Int32s
		I64 Int64s
		U32 Uint32s
		U64 Uint64s
		F64 Float64s
	}
	v := &T{
		I32: Int32s{-1, 2, 3},
		I64: Int64s{-1, 2, 1 << 33},
		U32: Uint32s{1, 2},
		U64: Uint64s{1, 2, 1 << 33},
		F64: Float64s{1.5, 3.33},
	}
	got, err := json.Marshal(v)
	if err != nil {
		t.Fatal(err)
	}
	want := `{"I32":["-1","2","3"],"I64":["-1","2","8589934592"],"U32":["1","2"],"U64":["1","2","8589934592"],"F64":["1.5","3.33"]}`
	if string(got) != want {
		t.Fatalf("Marshal mismatch.\n got: %s\nwant: %s\n", got, want)
	}

	v2 := new(T)
	if err := json.Unmarshal(got, v2); err != nil {
		t.Fatalf("Unmarshal: %v", err)
	}
	if !reflect.DeepEqual(v, v2) {
		t.Fatalf("Unmarshal didn't produce same results.\n got: %#v\nwant: %#v\n", v, v2)
	}
}

func TestRawMessageMarshal(t *testing.T) {
	// https://golang.org/issue/14493
	const want = "{}"
	b, err := json.Marshal(RawMessage(want))
	if err != nil {
		t.Fatalf("Marshal: %v", err)
	}
	if !bytes.Equal(b, []byte(want)) {
		t.Errorf("Marshal(RawMessage(%q)) = %q; want %q", want, b, want)
	}
}

func TestRawMessageUnmarshal(t *testing.T) {
	const want = "{}"
	var m RawMessage
	if err := json.Unmarshal([]byte(want), &m); err != nil {
		t.Fatalf("Unmarshal: %v", err)
	}
	if !bytes.Equal([]byte(m), []byte(want)) {
		t.Errorf("Unmarshal([]byte(%q), &m); m = %q; want %q", want, string(m), want)
	}
}