spew_test.go 6.1 KB

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