2 * Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 This test file is part of the spew package rather than than the spew_test
19 package because it needs access to internals to properly test certain cases
20 which are not possible via the public interface since they should never happen.
31 // dummyFmtState implements a fake fmt.State to use for testing invalid
32 // reflect.Value handling. This is necessary because the fmt package catches
33 // invalid values before invoking the formatter on them.
34 type dummyFmtState struct {
38 func (dfs *dummyFmtState) Flag(f int) bool {
42 func (dfs *dummyFmtState) Precision() (int, bool) {
46 func (dfs *dummyFmtState) Width() (int, bool) {
50 // TestInvalidReflectValue ensures the dump and formatter code handles an
51 // invalid reflect value properly. This needs access to internal state since it
52 // should never happen in real code and therefore can't be tested via the public
54 func TestInvalidReflectValue(t *testing.T) {
57 // Dump invalid reflect value.
58 v := new(reflect.Value)
59 buf := new(bytes.Buffer)
60 d := dumpState{w: buf, cs: &Config}
65 t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
69 // Formatter invalid reflect value.
70 buf2 := new(dummyFmtState)
71 f := formatState{value: *v, cs: &Config, fs: buf2}
76 t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
80 // SortValues makes the internal sortValues function available to the test
82 func SortValues(values []reflect.Value, cs *ConfigState) {
83 sortValues(values, cs)