github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/test/go-spew/spew/internal_test.go (about)

     1  /*
     2   * Copyright (c) 2013-2016 Dave Collins <dave@davec.name>
     3   *
     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.
     7   *
     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.
    15   */
    16  
    17  /*
    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.
    21  */
    22  
    23  package spew
    24  
    25  import (
    26  	"bytes"
    27  	"reflect"
    28  	"testing"
    29  )
    30  
    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 {
    35  	bytes.Buffer
    36  }
    37  
    38  func (dfs *dummyFmtState) Flag(f int) bool {
    39  	if f == int('+') {
    40  		return true
    41  	}
    42  	return false
    43  }
    44  
    45  func (dfs *dummyFmtState) Precision() (int, bool) {
    46  	return 0, false
    47  }
    48  
    49  func (dfs *dummyFmtState) Width() (int, bool) {
    50  	return 0, false
    51  }
    52  
    53  // TestInvalidReflectValue ensures the dump and formatter code handles an
    54  // invalid reflect value properly.  This needs access to internal state since it
    55  // should never happen in real code and therefore can't be tested via the public
    56  // API.
    57  func TestInvalidReflectValue(t *testing.T) {
    58  	i := 1
    59  
    60  	// Dump invalid reflect value.
    61  	v := new(reflect.Value)
    62  	buf := new(bytes.Buffer)
    63  	d := dumpState{w: buf, cs: &Config}
    64  	d.dump(*v)
    65  	s := buf.String()
    66  	want := "<invalid>"
    67  	if s != want {
    68  		t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
    69  	}
    70  	i++
    71  
    72  	// Formatter invalid reflect value.
    73  	buf2 := new(dummyFmtState)
    74  	f := formatState{value: *v, cs: &Config, fs: buf2}
    75  	f.format(*v)
    76  	s = buf2.String()
    77  	want = "<invalid>"
    78  	if s != want {
    79  		t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
    80  	}
    81  }
    82  
    83  // SortValues makes the internal sortValues function available to the test
    84  // package.
    85  func SortValues(values []reflect.Value, cs *ConfigState) {
    86  	sortValues(values, cs)
    87  }