spew_test.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. }
  141. }
  142. // TestSpew executes all of the tests described by spewTests.
  143. func TestSpew(t *testing.T) {
  144. initSpewTests()
  145. t.Logf("Running %d tests", len(spewTests))
  146. for i, test := range spewTests {
  147. buf := new(bytes.Buffer)
  148. switch test.f {
  149. case fCSFdump:
  150. test.cs.Fdump(buf, test.in)
  151. case fCSFprint:
  152. test.cs.Fprint(buf, test.in)
  153. case fCSFprintf:
  154. test.cs.Fprintf(buf, test.format, test.in)
  155. case fCSFprintln:
  156. test.cs.Fprintln(buf, test.in)
  157. case fCSPrint:
  158. b, err := redirStdout(func() { test.cs.Print(test.in) })
  159. if err != nil {
  160. t.Errorf("%v #%d %v", test.f, i, err)
  161. continue
  162. }
  163. buf.Write(b)
  164. case fCSPrintln:
  165. b, err := redirStdout(func() { test.cs.Println(test.in) })
  166. if err != nil {
  167. t.Errorf("%v #%d %v", test.f, i, err)
  168. continue
  169. }
  170. buf.Write(b)
  171. case fCSErrorf:
  172. err := test.cs.Errorf(test.format, test.in)
  173. buf.WriteString(err.Error())
  174. case fCSNewFormatter:
  175. fmt.Fprintf(buf, test.format, test.cs.NewFormatter(test.in))
  176. case fErrorf:
  177. err := spew.Errorf(test.format, test.in)
  178. buf.WriteString(err.Error())
  179. case fFprint:
  180. spew.Fprint(buf, test.in)
  181. case fFprintln:
  182. spew.Fprintln(buf, test.in)
  183. case fPrint:
  184. b, err := redirStdout(func() { spew.Print(test.in) })
  185. if err != nil {
  186. t.Errorf("%v #%d %v", test.f, i, err)
  187. continue
  188. }
  189. buf.Write(b)
  190. case fPrintln:
  191. b, err := redirStdout(func() { spew.Println(test.in) })
  192. if err != nil {
  193. t.Errorf("%v #%d %v", test.f, i, err)
  194. continue
  195. }
  196. buf.Write(b)
  197. default:
  198. t.Errorf("%v #%d unrecognized function", test.f, i)
  199. continue
  200. }
  201. s := buf.String()
  202. if test.want != s {
  203. t.Errorf("ConfigState #%d\n got: %s want: %s", i, s, test.want)
  204. continue
  205. }
  206. }
  207. }