...

Source file src/google.golang.org/api/googleapi/types_test.go

Documentation: google.golang.org/api/googleapi

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

View as plain text