File: types_test.go

package info (click to toggle)
golang-google-api 0.0~git20150609-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 20,232 kB
  • ctags: 21,955
  • sloc: makefile: 16
file content (44 lines) | stat: -rw-r--r-- 1,005 bytes parent folder | download | duplicates (4)
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
// Copyright 2013 Google Inc. 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 (
	"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)
	}
}