github.com/psiphon-labs/goarista@v0.0.0-20160825065156-d002785f4c67/test/pretty_test.go (about)

     1  // Copyright (C) 2016  Arista Networks, Inc.
     2  // Use of this source code is governed by the Apache License 2.0
     3  // that can be found in the COPYING file.
     4  
     5  package test_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  	"unsafe"
    11  
    12  	. "github.com/aristanetworks/goarista/test"
    13  )
    14  
    15  type alias int
    16  
    17  type privateByteSlice struct {
    18  	exportme []byte
    19  }
    20  
    21  func TestPrettyPrint(t *testing.T) {
    22  	// This test doesn't need to cover all the types of input as a number of
    23  	// them are covered by other tests in this package.
    24  	ch := make(chan int, 42)
    25  	testcases := []struct {
    26  		input  interface{}
    27  		pretty string
    28  	}{
    29  		{true, "true"},
    30  		{(chan int)(nil), "(chan int)(nil)"},
    31  		{ch, fmt.Sprintf("(chan int)(%p)[42]", ch)},
    32  		{func() {}, "func(...)"},
    33  		{unsafe.Pointer(nil), "(unsafe.Pointer)(nil)"},
    34  		{unsafe.Pointer(t), fmt.Sprintf("(unsafe.Pointer)(%p)", t)},
    35  		{[]byte(nil), `[]byte(nil)`},
    36  		{[]byte{42, 0, 42}, `[]byte("*\x00*")`},
    37  		{[]int{42, 51}, "[]int{42, 51}"},
    38  		{[2]int{42, 51}, "[2]int{42, 51}"},
    39  		{[2]byte{42, 51}, "[2]uint8{42, 51}"}, // Yeah, in Go `byte' is really just `uint8'.
    40  		{alias(42), "alias(42)"},
    41  		{privateByteSlice{[]byte("a")}, `test_test.privateByteSlice{exportme:[]byte("a")}`},
    42  	}
    43  	for i, tcase := range testcases {
    44  		actual := PrettyPrint(tcase.input)
    45  		if actual != tcase.pretty {
    46  			t.Errorf("#%d: Wanted %q but got %q", i, actual, tcase.pretty)
    47  		}
    48  	}
    49  }