spew_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. package spew_test
  17. import (
  18. "bytes"
  19. "fmt"
  20. "github.com/davecgh/go-spew/spew"
  21. "io/ioutil"
  22. "os"
  23. "testing"
  24. )
  25. // spewFunc is used to identify which public function of the spew package or
  26. // ConfigState a test applies to.
  27. type spewFunc int
  28. const (
  29. fCSFdump spewFunc = iota
  30. fCSFprint
  31. fCSFprintf
  32. fCSFprintln
  33. fCSPrint
  34. fCSPrintln
  35. fCSErrorf
  36. fCSNewFormatter
  37. fErrorf
  38. fFprint
  39. fFprintln
  40. fPrint
  41. fPrintln
  42. )
  43. // Map of spewFunc values to names for pretty printing.
  44. var spewFuncStrings = map[spewFunc]string{
  45. fCSFdump: "ConfigState.Fdump",
  46. fCSFprint: "ConfigState.Fprint",
  47. fCSFprintf: "ConfigState.Fprintf",
  48. fCSFprintln: "ConfigState.Fprintln",
  49. fCSPrint: "ConfigState.Print",
  50. fCSPrintln: "ConfigState.Println",
  51. fCSErrorf: "ConfigState.Errorf",
  52. fCSNewFormatter: "ConfigState.NewFormatter",
  53. fErrorf: "spew.Errorf",
  54. fFprint: "spew.Fprint",
  55. fFprintln: "spew.Fprintln",
  56. fPrint: "spew.Print",
  57. fPrintln: "spew.Println",
  58. }
  59. func (f spewFunc) String() string {
  60. if s, ok := spewFuncStrings[f]; ok {
  61. return s
  62. }
  63. return fmt.Sprintf("Unknown spewFunc (%d)", int(f))
  64. }
  65. // spewTest is used to describe a test to be performed against the public
  66. // functions of the spew package or ConfigState.
  67. type spewTest struct {
  68. cs *spew.ConfigState
  69. f spewFunc
  70. format string
  71. in interface{}
  72. want string
  73. }
  74. // spewTests houses the tests to be performed against the public functions of
  75. // the spew package and ConfigState.
  76. //
  77. // These tests are only intended to ensure the public functions are exercised
  78. // and are intentionally not exhaustive of types. The exhaustive type
  79. // tests are handled in the dump and format tests.
  80. var spewTests []spewTest
  81. // redirStdout is a helper function to return the standard output from f as a
  82. // byte slice.
  83. func redirStdout(f func()) ([]byte, error) {
  84. tempFile, err := ioutil.TempFile("", "ss-test")
  85. if err != nil {
  86. return nil, err
  87. }
  88. fileName := tempFile.Name()
  89. defer os.Remove(fileName) // Ignore error
  90. origStdout := os.Stdout
  91. os.Stdout = tempFile
  92. f()
  93. os.Stdout = origStdout
  94. tempFile.Close()
  95. return ioutil.ReadFile(fileName)
  96. }
  97. func initSpewTests() {
  98. // Config states with various settings.
  99. scsDefault := spew.NewDefaultConfig()
  100. scsNoMethods := &spew.ConfigState{Indent: " ", DisableMethods: true}
  101. scsNoPmethods := &spew.ConfigState{Indent: " ", DisablePointerMethods: true}
  102. scsMaxDepth := &spew.ConfigState{Indent: " ", MaxDepth: 1}
  103. // Variables for tests on types which implement Stringer interface with and
  104. // without a pointer receiver.
  105. ts := stringer("test")
  106. tps := pstringer("test")
  107. // depthTester is used to test max depth handling for structs, array, slices
  108. // and maps.
  109. type depthTester struct {
  110. ic indirCir1
  111. arr [1]string
  112. slice []string
  113. m map[string]int
  114. }
  115. dt := depthTester{indirCir1{nil}, [1]string{"arr"}, []string{"slice"},
  116. map[string]int{"one": 1}}
  117. spewTests = []spewTest{
  118. {scsDefault, fCSFdump, "", int8(127), "(int8) 127\n"},
  119. {scsDefault, fCSFprint, "", int16(32767), "32767"},
  120. {scsDefault, fCSFprintf, "%v", int32(2147483647), "2147483647"},
  121. {scsDefault, fCSFprintln, "", int(2147483647), "2147483647\n"},
  122. {scsDefault, fCSPrint, "", int64(9223372036854775807), "9223372036854775807"},
  123. {scsDefault, fCSPrintln, "", uint8(255), "255\n"},
  124. {scsDefault, fCSErrorf, "%#v", uint16(65535), "(uint16)65535"},
  125. {scsDefault, fCSNewFormatter, "%v", uint32(4294967295), "4294967295"},
  126. {scsDefault, fErrorf, "%v", uint64(18446744073709551615), "18446744073709551615"},
  127. {scsDefault, fFprint, "", float32(3.14), "3.14"},
  128. {scsDefault, fFprintln, "", float64(6.28), "6.28\n"},
  129. {scsDefault, fPrint, "", true, "true"},
  130. {scsDefault, fPrintln, "", false, "false\n"},
  131. {scsNoMethods, fCSFprint, "", ts, "test"},
  132. {scsNoMethods, fCSFprint, "", &ts, "<*>test"},
  133. {scsNoMethods, fCSFprint, "", tps, "test"},
  134. {scsNoMethods, fCSFprint, "", &tps, "<*>test"},
  135. {scsNoPmethods, fCSFprint, "", ts, "stringer test"},
  136. {scsNoPmethods, fCSFprint, "", &ts, "<*>stringer test"},
  137. {scsNoPmethods, fCSFprint, "", tps, "test"},
  138. {scsNoPmethods, fCSFprint, "", &tps, "<*>stringer test"},
  139. {scsMaxDepth, fCSFprint, "", dt, "{{<max>} [<max>] [<max>] map[<max>]}"},
  140. {scsMaxDepth, fCSFdump, "", dt, "(spew_test.depthTester) {\n" +
  141. " ic: (spew_test.indirCir1) {\n <max depth reached>\n },\n" +
  142. " arr: ([1]string) {\n <max depth reached>\n },\n" +
  143. " slice: ([]string) {\n <max depth reached>\n },\n" +
  144. " m: (map[string]int) {\n <max depth reached>\n }\n}\n"},
  145. }
  146. }
  147. // TestSpew executes all of the tests described by spewTests.
  148. func TestSpew(t *testing.T) {
  149. initSpewTests()
  150. t.Logf("Running %d tests", len(spewTests))
  151. for i, test := range spewTests {
  152. buf := new(bytes.Buffer)
  153. switch test.f {
  154. case fCSFdump:
  155. test.cs.Fdump(buf, test.in)
  156. case fCSFprint:
  157. test.cs.Fprint(buf, test.in)
  158. case fCSFprintf:
  159. test.cs.Fprintf(buf, test.format, test.in)
  160. case fCSFprintln:
  161. test.cs.Fprintln(buf, test.in)
  162. case fCSPrint:
  163. b, err := redirStdout(func() { test.cs.Print(test.in) })
  164. if err != nil {
  165. t.Errorf("%v #%d %v", test.f, i, err)
  166. continue
  167. }
  168. buf.Write(b)
  169. case fCSPrintln:
  170. b, err := redirStdout(func() { test.cs.Println(test.in) })
  171. if err != nil {
  172. t.Errorf("%v #%d %v", test.f, i, err)
  173. continue
  174. }
  175. buf.Write(b)
  176. case fCSErrorf:
  177. err := test.cs.Errorf(test.format, test.in)
  178. buf.WriteString(err.Error())
  179. case fCSNewFormatter:
  180. fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in))
  181. case fErrorf:
  182. err := spew.Errorf(test.format, test.in)
  183. buf.WriteString(err.Error())
  184. case fFprint:
  185. spew.Fprint(buf, test.in)
  186. case fFprintln:
  187. spew.Fprintln(buf, test.in)
  188. case fPrint:
  189. b, err := redirStdout(func() { spew.Print(test.in) })
  190. if err != nil {
  191. t.Errorf("%v #%d %v", test.f, i, err)
  192. continue
  193. }
  194. buf.Write(b)
  195. case fPrintln:
  196. b, err := redirStdout(func() { spew.Println(test.in) })
  197. if err != nil {
  198. t.Errorf("%v #%d %v", test.f, i, err)
  199. continue
  200. }
  201. buf.Write(b)
  202. default:
  203. t.Errorf("%v #%d unrecognized function", test.f, i)
  204. continue
  205. }
  206. s := buf.String()
  207. if test.want != s {
  208. t.Errorf("ConfigState #%d\n got: %s want: %s", i, s, test.want)
  209. continue
  210. }
  211. }
  212. }