spew_test.go 5.4 KB

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