aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/davecgh/go-spew/spew/internal_test.go
blob: b583bfdef3c5552d1dbf6a1fac18e656d66016f8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * 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.
 */

/*
This test file is part of the spew package rather than than the spew_test
package because it needs access to internals to properly test certain cases
which are not possible via the public interface since they should never happen.
*/

package spew

import (
    "bytes"
    "reflect"
    "testing"
    "unsafe"
)

// dummyFmtState implements a fake fmt.State to use for testing invalid
// reflect.Value handling.  This is necessary because the fmt package catches
// invalid values before invoking the formatter on them.
type dummyFmtState struct {
    bytes.Buffer
}

func (dfs *dummyFmtState) Flag(f int) bool {
    if f == int('+') {
        return true
    }
    return false
}

func (dfs *dummyFmtState) Precision() (int, bool) {
    return 0, false
}

func (dfs *dummyFmtState) Width() (int, bool) {
    return 0, false
}

// TestInvalidReflectValue ensures the dump and formatter code handles an
// invalid reflect value properly.  This needs access to internal state since it
// should never happen in real code and therefore can't be tested via the public
// API.
func TestInvalidReflectValue(t *testing.T) {
    i := 1

    // Dump invalid reflect value.
    v := new(reflect.Value)
    buf := new(bytes.Buffer)
    d := dumpState{w: buf, cs: &Config}
    d.dump(*v)
    s := buf.String()
    want := "<invalid>"
    if s != want {
        t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
    }
    i++

    // Formatter invalid reflect value.
    buf2 := new(dummyFmtState)
    f := formatState{value: *v, cs: &Config, fs: buf2}
    f.format(*v)
    s = buf2.String()
    want = "<invalid>"
    if s != want {
        t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
    }
}

// changeKind uses unsafe to intentionally change the kind of a reflect.Value to
// the maximum kind value which does not exist.  This is needed to test the
// fallback code which punts to the standard fmt library for new types that
// might get added to the language.
func changeKind(v *reflect.Value, readOnly bool) {
    rvf := (*uintptr)(unsafe.Pointer(uintptr(unsafe.Pointer(v)) + offsetFlag))
    *rvf = *rvf | ((1<<flagKindWidth - 1) << flagKindShift)
    if readOnly {
        *rvf |= flagRO
    } else {
        *rvf &= ^uintptr(flagRO)
    }
}

// TestAddedReflectValue tests functionaly of the dump and formatter code which
// falls back to the standard fmt library for new types that might get added to
// the language.
func TestAddedReflectValue(t *testing.T) {
    i := 1

    // Dump using a reflect.Value that is exported.
    v := reflect.ValueOf(int8(5))
    changeKind(&v, false)
    buf := new(bytes.Buffer)
    d := dumpState{w: buf, cs: &Config}
    d.dump(v)
    s := buf.String()
    want := "(int8) 5"
    if s != want {
        t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
    }
    i++

    // Dump using a reflect.Value that is not exported.
    changeKind(&v, true)
    buf.Reset()
    d.dump(v)
    s = buf.String()
    want = "(int8) <int8 Value>"
    if s != want {
        t.Errorf("TestAddedReflectValue #%d\n got: %s want: %s", i, s, want)
    }
    i++

    // Formatter using a reflect.Value that is exported.
    changeKind(&v, false)
    buf2 := new(dummyFmtState)
    f := formatState{value: v, cs: &Config, fs: buf2}
    f.format(v)
    s = buf2.String()
    want = "5"
    if s != want {
        t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
    }
    i++

    // Formatter using a reflect.Value that is not exported.
    changeKind(&v, true)
    buf2.Reset()
    f = formatState{value: v, cs: &Config, fs: buf2}
    f.format(v)
    s = buf2.String()
    want = "<int8 Value>"
    if s != want {
        t.Errorf("TestAddedReflectValue #%d got: %s want: %s", i, s, want)
    }
}

// SortValues makes the internal sortValues function available to the test
// package.
func SortValues(values []reflect.Value, cs *ConfigState) {
    sortValues(values, cs)
}