spew_test.go 5.2 KB

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