internal_test.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright (c) 2013 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 false
  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. // TestDumpInvalidReflectValue ensures the dump code handles an invalid reflect
  43. // value properly. This needs access to internal state since it should never
  44. // happen in real code and therefore can't be tested via the public API.
  45. func TestDumpInvalidReflectValue(t *testing.T) {
  46. v := new(reflect.Value)
  47. buf := new(bytes.Buffer)
  48. d := dumpState{w: buf, cs: &Config}
  49. d.dump(*v)
  50. s := buf.String()
  51. want := "<invalid>"
  52. if s != want {
  53. t.Errorf("DumpInvalidReflectValue\n got: %s want: %s", s, want)
  54. }
  55. }
  56. // TestFormatterInvalidReflectValue ensures the formatter code handles an
  57. // invalid reflect value properly. This needs access to internal state since it
  58. // should never happen in real code and therefore can't be tested via the public
  59. // API.
  60. func TestFormatterInvalidReflectValue(t *testing.T) {
  61. v := new(reflect.Value)
  62. buf := new(dummyFmtState)
  63. f := formatState{value: *v, cs: &Config, fs: buf}
  64. f.format(*v)
  65. s := buf.String()
  66. want := "<invalid>"
  67. if s != want {
  68. t.Errorf("FormatterInvalidReflectValue\n got: %s want: %s", s, want)
  69. }
  70. }