internal_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. This test file is part of the spew package rather than than the spew_test
  18. package because it needs access to internals to properly test certain cases
  19. which are not possible via the public interface since they should never happen.
  20. */
  21. package spew
  22. import (
  23. "bytes"
  24. "reflect"
  25. "testing"
  26. )
  27. // dummyFmtState implements a fake fmt.State to use for testing invalid
  28. // reflect.Value handling. This is necessary because the fmt package catches
  29. // invalid values before invoking the formatter on them.
  30. type dummyFmtState struct {
  31. bytes.Buffer
  32. }
  33. func (dfs *dummyFmtState) Flag(f int) bool {
  34. return f == int('+')
  35. }
  36. func (dfs *dummyFmtState) Precision() (int, bool) {
  37. return 0, false
  38. }
  39. func (dfs *dummyFmtState) Width() (int, bool) {
  40. return 0, false
  41. }
  42. // TestInvalidReflectValue ensures the dump and formatter code handles an
  43. // invalid reflect value properly. This needs access to internal state since it
  44. // should never happen in real code and therefore can't be tested via the public
  45. // API.
  46. func TestInvalidReflectValue(t *testing.T) {
  47. i := 1
  48. // Dump invalid reflect value.
  49. v := new(reflect.Value)
  50. buf := new(bytes.Buffer)
  51. d := dumpState{w: buf, cs: &Config}
  52. d.dump(*v)
  53. s := buf.String()
  54. want := "<invalid>"
  55. if s != want {
  56. t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
  57. }
  58. i++
  59. // Formatter invalid reflect value.
  60. buf2 := new(dummyFmtState)
  61. f := formatState{value: *v, cs: &Config, fs: buf2}
  62. f.format(*v)
  63. s = buf2.String()
  64. want = "<invalid>"
  65. if s != want {
  66. t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
  67. }
  68. }
  69. // SortValues makes the internal sortValues function available to the test
  70. // package.
  71. func SortValues(values []reflect.Value, cs *ConfigState) {
  72. sortValues(values, cs)
  73. }