internal_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. if f == int('+') {
  35. return true
  36. }
  37. return false
  38. }
  39. func (dfs *dummyFmtState) Precision() (int, bool) {
  40. return 0, false
  41. }
  42. func (dfs *dummyFmtState) Width() (int, bool) {
  43. return 0, false
  44. }
  45. // TestInvalidReflectValue ensures the dump and formatter code handles an
  46. // invalid reflect value properly. This needs access to internal state since it
  47. // should never happen in real code and therefore can't be tested via the public
  48. // API.
  49. func TestInvalidReflectValue(t *testing.T) {
  50. i := 1
  51. // Dump invalid reflect value.
  52. v := new(reflect.Value)
  53. buf := new(bytes.Buffer)
  54. d := dumpState{w: buf, cs: &Config}
  55. d.dump(*v)
  56. s := buf.String()
  57. want := "<invalid>"
  58. if s != want {
  59. t.Errorf("InvalidReflectValue #%d\n got: %s want: %s", i, s, want)
  60. }
  61. i++
  62. // Formatter invalid reflect value.
  63. buf2 := new(dummyFmtState)
  64. f := formatState{value: *v, cs: &Config, fs: buf2}
  65. f.format(*v)
  66. s = buf2.String()
  67. want = "<invalid>"
  68. if s != want {
  69. t.Errorf("InvalidReflectValue #%d got: %s want: %s", i, s, want)
  70. }
  71. }
  72. // SortValues makes the internal sortValues function available to the test
  73. // package.
  74. func SortValues(values []reflect.Value, cs *ConfigState) {
  75. sortValues(values, cs)
  76. }