2013-01-20 13:31:01 -06:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Dave Collins <dave@davec.name>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package spew_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-11-14 21:46:27 -06:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
2013-01-20 13:31:01 -06:00
|
|
|
)
|
|
|
|
|
2013-01-20 17:53:55 -06:00
|
|
|
// custom type to test Stinger interface on non-pointer receiver.
|
|
|
|
type stringer string
|
|
|
|
|
|
|
|
// String implements the Stringer interface for testing invocation of custom
|
|
|
|
// stringers on types with non-pointer receivers.
|
|
|
|
func (s stringer) String() string {
|
|
|
|
return "stringer " + string(s)
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:31:01 -06:00
|
|
|
// custom type to test Stinger interface on pointer receiver.
|
|
|
|
type pstringer string
|
|
|
|
|
|
|
|
// String implements the Stringer interface for testing invocation of custom
|
|
|
|
// stringers on types with only pointer receivers.
|
|
|
|
func (s *pstringer) String() string {
|
|
|
|
return "stringer " + string(*s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// xref1 and xref2 are cross referencing structs for testing circular reference
|
2013-02-03 01:33:35 -06:00
|
|
|
// detection.
|
2013-01-20 13:31:01 -06:00
|
|
|
type xref1 struct {
|
|
|
|
ps2 *xref2
|
|
|
|
}
|
|
|
|
type xref2 struct {
|
|
|
|
ps1 *xref1
|
|
|
|
}
|
|
|
|
|
|
|
|
// indirCir1, indirCir2, and indirCir3 are used to generate an indirect circular
|
|
|
|
// reference for testing detection.
|
|
|
|
type indirCir1 struct {
|
|
|
|
ps2 *indirCir2
|
|
|
|
}
|
|
|
|
type indirCir2 struct {
|
|
|
|
ps3 *indirCir3
|
|
|
|
}
|
|
|
|
type indirCir3 struct {
|
|
|
|
ps1 *indirCir1
|
|
|
|
}
|
|
|
|
|
2013-01-20 19:55:55 -06:00
|
|
|
// embed is used to test embedded structures.
|
|
|
|
type embed struct {
|
|
|
|
a string
|
|
|
|
}
|
|
|
|
|
|
|
|
// embedwrap is used to test embedded structures.
|
|
|
|
type embedwrap struct {
|
|
|
|
*embed
|
|
|
|
e *embed
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:31:01 -06:00
|
|
|
// panicer is used to intentionally cause a panic for testing spew properly
|
|
|
|
// handles them
|
|
|
|
type panicer int
|
|
|
|
|
|
|
|
func (p panicer) String() string {
|
|
|
|
panic("test panic")
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:41:53 -06:00
|
|
|
// customError is used to test custom error interface invocation.
|
|
|
|
type customError int
|
|
|
|
|
|
|
|
func (e customError) Error() string {
|
|
|
|
return fmt.Sprintf("error: %d", int(e))
|
|
|
|
}
|
|
|
|
|
2013-01-20 13:31:01 -06:00
|
|
|
// stringizeWants converts a slice of wanted test output into a format suitable
|
2013-02-07 18:42:39 -06:00
|
|
|
// for a test error message.
|
2013-01-20 13:31:01 -06:00
|
|
|
func stringizeWants(wants []string) string {
|
|
|
|
s := ""
|
|
|
|
for i, want := range wants {
|
|
|
|
if i > 0 {
|
|
|
|
s += fmt.Sprintf("want%d: %s", i+1, want)
|
|
|
|
} else {
|
|
|
|
s += "want: " + want
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// testFailed returns whether or not a test failed by checking if the result
|
|
|
|
// of the test is in the slice of wanted strings.
|
|
|
|
func testFailed(result string, wants []string) bool {
|
|
|
|
for _, want := range wants {
|
|
|
|
if result == want {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2013-11-14 21:44:13 -06:00
|
|
|
|
|
|
|
// TestSortValues ensures the sort functionality for relect.Value based sorting
|
|
|
|
// works as intended.
|
|
|
|
func TestSortValues(t *testing.T) {
|
2013-12-01 09:38:22 -06:00
|
|
|
getInterfaces := func(values []reflect.Value) []interface{} {
|
|
|
|
interfaces := []interface{}{}
|
|
|
|
for _, v := range values {
|
|
|
|
interfaces = append(interfaces, v.Interface())
|
|
|
|
}
|
|
|
|
return interfaces
|
|
|
|
}
|
|
|
|
|
2013-11-14 21:44:13 -06:00
|
|
|
v := reflect.ValueOf
|
|
|
|
|
|
|
|
a := v("a")
|
|
|
|
b := v("b")
|
|
|
|
c := v("c")
|
2013-11-14 22:22:17 -06:00
|
|
|
embedA := v(embed{"a"})
|
|
|
|
embedB := v(embed{"b"})
|
|
|
|
embedC := v(embed{"c"})
|
2013-11-14 21:44:13 -06:00
|
|
|
tests := []struct {
|
|
|
|
input []reflect.Value
|
|
|
|
expected []reflect.Value
|
|
|
|
}{
|
2013-11-14 22:22:17 -06:00
|
|
|
// No values.
|
|
|
|
{
|
|
|
|
[]reflect.Value{},
|
|
|
|
[]reflect.Value{},
|
|
|
|
},
|
|
|
|
// Bools.
|
|
|
|
{
|
|
|
|
[]reflect.Value{v(false), v(true), v(false)},
|
|
|
|
[]reflect.Value{v(false), v(false), v(true)},
|
|
|
|
},
|
|
|
|
// Ints.
|
2013-11-14 21:44:13 -06:00
|
|
|
{
|
|
|
|
[]reflect.Value{v(2), v(1), v(3)},
|
|
|
|
[]reflect.Value{v(1), v(2), v(3)},
|
|
|
|
},
|
2013-11-14 22:22:17 -06:00
|
|
|
// Uints.
|
2013-11-14 21:44:13 -06:00
|
|
|
{
|
2013-11-14 22:22:17 -06:00
|
|
|
[]reflect.Value{v(uint8(2)), v(uint8(1)), v(uint8(3))},
|
|
|
|
[]reflect.Value{v(uint8(1)), v(uint8(2)), v(uint8(3))},
|
2013-11-14 21:44:13 -06:00
|
|
|
},
|
2013-11-14 22:22:17 -06:00
|
|
|
// Floats.
|
2013-11-14 21:44:13 -06:00
|
|
|
{
|
2013-11-14 22:22:17 -06:00
|
|
|
[]reflect.Value{v(2.0), v(1.0), v(3.0)},
|
|
|
|
[]reflect.Value{v(1.0), v(2.0), v(3.0)},
|
2013-11-14 21:44:13 -06:00
|
|
|
},
|
2013-11-14 22:22:17 -06:00
|
|
|
// Strings.
|
2013-11-14 21:44:13 -06:00
|
|
|
{
|
|
|
|
[]reflect.Value{b, a, c},
|
|
|
|
[]reflect.Value{a, b, c},
|
|
|
|
},
|
2013-11-14 22:22:17 -06:00
|
|
|
// Uintptrs.
|
|
|
|
{
|
|
|
|
[]reflect.Value{v(uintptr(2)), v(uintptr(1)), v(uintptr(3))},
|
|
|
|
[]reflect.Value{v(uintptr(1)), v(uintptr(2)), v(uintptr(3))},
|
|
|
|
},
|
|
|
|
// Invalid.
|
|
|
|
{
|
|
|
|
[]reflect.Value{embedB, embedA, embedC},
|
|
|
|
[]reflect.Value{embedB, embedA, embedC},
|
|
|
|
},
|
2013-11-14 21:44:13 -06:00
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
spew.SortValues(test.input)
|
2013-12-01 09:38:22 -06:00
|
|
|
// reflect.DeepEqual cannot really make sense of reflect.Value,
|
|
|
|
// probably because of all the pointer tricks. For instance,
|
|
|
|
// v(2.0) != v(2.0) on a 32-bits system. Turn them into interface{}
|
|
|
|
// instead.
|
|
|
|
input := getInterfaces(test.input)
|
|
|
|
expected := getInterfaces(test.expected)
|
|
|
|
if !reflect.DeepEqual(input, expected) {
|
|
|
|
t.Errorf("Sort mismatch:\n %v != %v", input, expected)
|
2013-11-14 21:44:13 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|