github.com/integration-system/go-cmp@v0.0.0-20190131081942-ac5582987a2f/cmp/internal/value/format_test.go (about)

     1  // Copyright 2017, 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.md file.
     4  
     5  package value
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"reflect"
    11  	"testing"
    12  )
    13  
    14  func TestFormat(t *testing.T) {
    15  	type key struct {
    16  		a int
    17  		b string
    18  		c chan bool
    19  	}
    20  
    21  	tests := []struct {
    22  		in   interface{}
    23  		want string
    24  	}{{
    25  		in:   []int{},
    26  		want: "[]int{}",
    27  	}, {
    28  		in:   []int(nil),
    29  		want: "[]int(nil)",
    30  	}, {
    31  		in:   []int{1, 2, 3, 4, 5},
    32  		want: "[]int{1, 2, 3, 4, 5}",
    33  	}, {
    34  		in:   []interface{}{1, true, "hello", struct{ A, B int }{1, 2}},
    35  		want: "[]interface {}{1, true, \"hello\", struct { A int; B int }{A: 1, B: 2}}",
    36  	}, {
    37  		in:   []struct{ A, B int }{{1, 2}, {0, 4}, {}},
    38  		want: "[]struct { A int; B int }{{A: 1, B: 2}, {B: 4}, {}}",
    39  	}, {
    40  		in:   map[*int]string{new(int): "hello"},
    41  		want: "map[*int]string{0x00: \"hello\"}",
    42  	}, {
    43  		in:   map[key]string{{}: "hello"},
    44  		want: "map[value.key]string{{}: \"hello\"}",
    45  	}, {
    46  		in:   map[key]string{{a: 5, b: "key", c: make(chan bool)}: "hello"},
    47  		want: "map[value.key]string{{a: 5, b: \"key\", c: (chan bool)(0x00)}: \"hello\"}",
    48  	}, {
    49  		in:   map[io.Reader]string{new(bytes.Reader): "hello"},
    50  		want: "map[io.Reader]string{(*bytes.Reader)(0x00): \"hello\"}",
    51  	}, {
    52  		in: func() interface{} {
    53  			var a = []interface{}{nil}
    54  			a[0] = a
    55  			return a
    56  		}(),
    57  		want: "[]interface {}{[]interface {}{*(*interface {})(0x00)}}",
    58  	}, {
    59  		in: func() interface{} {
    60  			type A *A
    61  			var a A
    62  			a = &a
    63  			return a
    64  		}(),
    65  		want: "&(value.A)(0x00)",
    66  	}, {
    67  		in: func() interface{} {
    68  			type A map[*A]A
    69  			a := make(A)
    70  			a[&a] = a
    71  			return a
    72  		}(),
    73  		want: "value.A{0x00: 0x00}",
    74  	}, {
    75  		in: func() interface{} {
    76  			var a [2]interface{}
    77  			a[0] = &a
    78  			return a
    79  		}(),
    80  		want: "[2]interface {}{&[2]interface {}{(*[2]interface {})(0x00), interface {}(nil)}, interface {}(nil)}",
    81  	}}
    82  
    83  	for i, tt := range tests {
    84  		// Intentionally retrieve the value through an unexported field to
    85  		// ensure the format logic does not depend on read-write access
    86  		// to the reflect.Value.
    87  		v := reflect.ValueOf(struct{ x interface{} }{tt.in}).Field(0)
    88  		got := formatAny(v, FormatConfig{UseStringer: true, printType: true, followPointers: true}, visited{})
    89  		if got != tt.want {
    90  			t.Errorf("test %d, Format():\ngot  %q\nwant %q", i, got, tt.want)
    91  		}
    92  	}
    93  }