gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/cmds/core/elvish/util/deepprint_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  type S struct {
     8  	I  int
     9  	S  string
    10  	Pt *T
    11  	G  G
    12  }
    13  
    14  type T struct {
    15  	M map[string]string
    16  }
    17  
    18  type G struct {
    19  }
    20  
    21  type U struct {
    22  	I int
    23  	S string
    24  }
    25  
    26  func (g G) GoString() string {
    27  	return "<G>"
    28  }
    29  
    30  var deepPrintTests = []struct {
    31  	in     interface{}
    32  	wanted string
    33  }{
    34  	{1, "1"},
    35  	{"foobar", `"foobar"`},
    36  	{[]int{42, 44}, `[]int{42, 44}`},
    37  	{[]int(nil), `nil`},
    38  	{(*int)(nil), `nil`},
    39  	{&S{42, "DON'T PANIC", &T{map[string]string{"foo": "bar"}}, G{}},
    40  		`&util.S{I: 42, S: "DON'T PANIC", Pt: &util.T{M: map[string]string{"foo": "bar"}}, G: <G>}`},
    41  	{[]interface{}{&U{42, "DON'T PANIC"}, 42, "DON'T PANIC"}, `[]interface {}{&util.U{I: 42, S: "DON'T PANIC"}, 42, "DON'T PANIC"}`},
    42  }
    43  
    44  func TestDeepPrint(t *testing.T) {
    45  	for _, tt := range deepPrintTests {
    46  		if out := DeepPrint(tt.in); out != tt.wanted {
    47  			t.Errorf("DeepPrint(%v) => %#q, want %#q", tt.in, out, tt.wanted)
    48  		}
    49  	}
    50  	// Test map.
    51  	in := map[string]int{"foo": 42, "bar": 233}
    52  	out := DeepPrint(in)
    53  	wanted1 := `map[string]int{"foo": 42, "bar": 233}`
    54  	wanted2 := `map[string]int{"bar": 233, "foo": 42}`
    55  	if out != wanted1 && out != wanted2 {
    56  		t.Errorf("DeepPrint(%v) => %#q, want %#q or %#q", in, out, wanted1, wanted2)
    57  	}
    58  }