25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

273 lines
10 KiB

  1. package check_test
  2. import (
  3. "errors"
  4. "gopkg.in/check.v1"
  5. "reflect"
  6. "runtime"
  7. )
  8. type CheckersS struct{}
  9. var _ = check.Suite(&CheckersS{})
  10. func testInfo(c *check.C, checker check.Checker, name string, paramNames []string) {
  11. info := checker.Info()
  12. if info.Name != name {
  13. c.Fatalf("Got name %s, expected %s", info.Name, name)
  14. }
  15. if !reflect.DeepEqual(info.Params, paramNames) {
  16. c.Fatalf("Got param names %#v, expected %#v", info.Params, paramNames)
  17. }
  18. }
  19. func testCheck(c *check.C, checker check.Checker, result bool, error string, params ...interface{}) ([]interface{}, []string) {
  20. info := checker.Info()
  21. if len(params) != len(info.Params) {
  22. c.Fatalf("unexpected param count in test; expected %d got %d", len(info.Params), len(params))
  23. }
  24. names := append([]string{}, info.Params...)
  25. result_, error_ := checker.Check(params, names)
  26. if result_ != result || error_ != error {
  27. c.Fatalf("%s.Check(%#v) returned (%#v, %#v) rather than (%#v, %#v)",
  28. info.Name, params, result_, error_, result, error)
  29. }
  30. return params, names
  31. }
  32. func (s *CheckersS) TestComment(c *check.C) {
  33. bug := check.Commentf("a %d bc", 42)
  34. comment := bug.CheckCommentString()
  35. if comment != "a 42 bc" {
  36. c.Fatalf("Commentf returned %#v", comment)
  37. }
  38. }
  39. func (s *CheckersS) TestIsNil(c *check.C) {
  40. testInfo(c, check.IsNil, "IsNil", []string{"value"})
  41. testCheck(c, check.IsNil, true, "", nil)
  42. testCheck(c, check.IsNil, false, "", "a")
  43. testCheck(c, check.IsNil, true, "", (chan int)(nil))
  44. testCheck(c, check.IsNil, false, "", make(chan int))
  45. testCheck(c, check.IsNil, true, "", (error)(nil))
  46. testCheck(c, check.IsNil, false, "", errors.New(""))
  47. testCheck(c, check.IsNil, true, "", ([]int)(nil))
  48. testCheck(c, check.IsNil, false, "", make([]int, 1))
  49. testCheck(c, check.IsNil, false, "", int(0))
  50. }
  51. func (s *CheckersS) TestNotNil(c *check.C) {
  52. testInfo(c, check.NotNil, "NotNil", []string{"value"})
  53. testCheck(c, check.NotNil, false, "", nil)
  54. testCheck(c, check.NotNil, true, "", "a")
  55. testCheck(c, check.NotNil, false, "", (chan int)(nil))
  56. testCheck(c, check.NotNil, true, "", make(chan int))
  57. testCheck(c, check.NotNil, false, "", (error)(nil))
  58. testCheck(c, check.NotNil, true, "", errors.New(""))
  59. testCheck(c, check.NotNil, false, "", ([]int)(nil))
  60. testCheck(c, check.NotNil, true, "", make([]int, 1))
  61. }
  62. func (s *CheckersS) TestNot(c *check.C) {
  63. testInfo(c, check.Not(check.IsNil), "Not(IsNil)", []string{"value"})
  64. testCheck(c, check.Not(check.IsNil), false, "", nil)
  65. testCheck(c, check.Not(check.IsNil), true, "", "a")
  66. }
  67. type simpleStruct struct {
  68. i int
  69. }
  70. func (s *CheckersS) TestEquals(c *check.C) {
  71. testInfo(c, check.Equals, "Equals", []string{"obtained", "expected"})
  72. // The simplest.
  73. testCheck(c, check.Equals, true, "", 42, 42)
  74. testCheck(c, check.Equals, false, "", 42, 43)
  75. // Different native types.
  76. testCheck(c, check.Equals, false, "", int32(42), int64(42))
  77. // With nil.
  78. testCheck(c, check.Equals, false, "", 42, nil)
  79. // Slices
  80. testCheck(c, check.Equals, false, "runtime error: comparing uncomparable type []uint8", []byte{1, 2}, []byte{1, 2})
  81. // Struct values
  82. testCheck(c, check.Equals, true, "", simpleStruct{1}, simpleStruct{1})
  83. testCheck(c, check.Equals, false, "", simpleStruct{1}, simpleStruct{2})
  84. // Struct pointers
  85. testCheck(c, check.Equals, false, "", &simpleStruct{1}, &simpleStruct{1})
  86. testCheck(c, check.Equals, false, "", &simpleStruct{1}, &simpleStruct{2})
  87. }
  88. func (s *CheckersS) TestDeepEquals(c *check.C) {
  89. testInfo(c, check.DeepEquals, "DeepEquals", []string{"obtained", "expected"})
  90. // The simplest.
  91. testCheck(c, check.DeepEquals, true, "", 42, 42)
  92. testCheck(c, check.DeepEquals, false, "", 42, 43)
  93. // Different native types.
  94. testCheck(c, check.DeepEquals, false, "", int32(42), int64(42))
  95. // With nil.
  96. testCheck(c, check.DeepEquals, false, "", 42, nil)
  97. // Slices
  98. testCheck(c, check.DeepEquals, true, "", []byte{1, 2}, []byte{1, 2})
  99. testCheck(c, check.DeepEquals, false, "", []byte{1, 2}, []byte{1, 3})
  100. // Struct values
  101. testCheck(c, check.DeepEquals, true, "", simpleStruct{1}, simpleStruct{1})
  102. testCheck(c, check.DeepEquals, false, "", simpleStruct{1}, simpleStruct{2})
  103. // Struct pointers
  104. testCheck(c, check.DeepEquals, true, "", &simpleStruct{1}, &simpleStruct{1})
  105. testCheck(c, check.DeepEquals, false, "", &simpleStruct{1}, &simpleStruct{2})
  106. }
  107. func (s *CheckersS) TestHasLen(c *check.C) {
  108. testInfo(c, check.HasLen, "HasLen", []string{"obtained", "n"})
  109. testCheck(c, check.HasLen, true, "", "abcd", 4)
  110. testCheck(c, check.HasLen, true, "", []int{1, 2}, 2)
  111. testCheck(c, check.HasLen, false, "", []int{1, 2}, 3)
  112. testCheck(c, check.HasLen, false, "n must be an int", []int{1, 2}, "2")
  113. testCheck(c, check.HasLen, false, "obtained value type has no length", nil, 2)
  114. }
  115. func (s *CheckersS) TestErrorMatches(c *check.C) {
  116. testInfo(c, check.ErrorMatches, "ErrorMatches", []string{"value", "regex"})
  117. testCheck(c, check.ErrorMatches, false, "Error value is nil", nil, "some error")
  118. testCheck(c, check.ErrorMatches, false, "Value is not an error", 1, "some error")
  119. testCheck(c, check.ErrorMatches, true, "", errors.New("some error"), "some error")
  120. testCheck(c, check.ErrorMatches, true, "", errors.New("some error"), "so.*or")
  121. // Verify params mutation
  122. params, names := testCheck(c, check.ErrorMatches, false, "", errors.New("some error"), "other error")
  123. c.Assert(params[0], check.Equals, "some error")
  124. c.Assert(names[0], check.Equals, "error")
  125. }
  126. func (s *CheckersS) TestMatches(c *check.C) {
  127. testInfo(c, check.Matches, "Matches", []string{"value", "regex"})
  128. // Simple matching
  129. testCheck(c, check.Matches, true, "", "abc", "abc")
  130. testCheck(c, check.Matches, true, "", "abc", "a.c")
  131. // Must match fully
  132. testCheck(c, check.Matches, false, "", "abc", "ab")
  133. testCheck(c, check.Matches, false, "", "abc", "bc")
  134. // String()-enabled values accepted
  135. testCheck(c, check.Matches, true, "", reflect.ValueOf("abc"), "a.c")
  136. testCheck(c, check.Matches, false, "", reflect.ValueOf("abc"), "a.d")
  137. // Some error conditions.
  138. testCheck(c, check.Matches, false, "Obtained value is not a string and has no .String()", 1, "a.c")
  139. testCheck(c, check.Matches, false, "Can't compile regex: error parsing regexp: missing closing ]: `[c$`", "abc", "a[c")
  140. }
  141. func (s *CheckersS) TestPanics(c *check.C) {
  142. testInfo(c, check.Panics, "Panics", []string{"function", "expected"})
  143. // Some errors.
  144. testCheck(c, check.Panics, false, "Function has not panicked", func() bool { return false }, "BOOM")
  145. testCheck(c, check.Panics, false, "Function must take zero arguments", 1, "BOOM")
  146. // Plain strings.
  147. testCheck(c, check.Panics, true, "", func() { panic("BOOM") }, "BOOM")
  148. testCheck(c, check.Panics, false, "", func() { panic("KABOOM") }, "BOOM")
  149. testCheck(c, check.Panics, true, "", func() bool { panic("BOOM") }, "BOOM")
  150. // Error values.
  151. testCheck(c, check.Panics, true, "", func() { panic(errors.New("BOOM")) }, errors.New("BOOM"))
  152. testCheck(c, check.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
  153. type deep struct{ i int }
  154. // Deep value
  155. testCheck(c, check.Panics, true, "", func() { panic(&deep{99}) }, &deep{99})
  156. // Verify params/names mutation
  157. params, names := testCheck(c, check.Panics, false, "", func() { panic(errors.New("KABOOM")) }, errors.New("BOOM"))
  158. c.Assert(params[0], check.ErrorMatches, "KABOOM")
  159. c.Assert(names[0], check.Equals, "panic")
  160. // Verify a nil panic
  161. testCheck(c, check.Panics, true, "", func() { panic(nil) }, nil)
  162. testCheck(c, check.Panics, false, "", func() { panic(nil) }, "NOPE")
  163. }
  164. func (s *CheckersS) TestPanicMatches(c *check.C) {
  165. testInfo(c, check.PanicMatches, "PanicMatches", []string{"function", "expected"})
  166. // Error matching.
  167. testCheck(c, check.PanicMatches, true, "", func() { panic(errors.New("BOOM")) }, "BO.M")
  168. testCheck(c, check.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BO.M")
  169. // Some errors.
  170. testCheck(c, check.PanicMatches, false, "Function has not panicked", func() bool { return false }, "BOOM")
  171. testCheck(c, check.PanicMatches, false, "Function must take zero arguments", 1, "BOOM")
  172. // Plain strings.
  173. testCheck(c, check.PanicMatches, true, "", func() { panic("BOOM") }, "BO.M")
  174. testCheck(c, check.PanicMatches, false, "", func() { panic("KABOOM") }, "BOOM")
  175. testCheck(c, check.PanicMatches, true, "", func() bool { panic("BOOM") }, "BO.M")
  176. // Verify params/names mutation
  177. params, names := testCheck(c, check.PanicMatches, false, "", func() { panic(errors.New("KABOOM")) }, "BOOM")
  178. c.Assert(params[0], check.Equals, "KABOOM")
  179. c.Assert(names[0], check.Equals, "panic")
  180. // Verify a nil panic
  181. testCheck(c, check.PanicMatches, false, "Panic value is not a string or an error", func() { panic(nil) }, "")
  182. }
  183. func (s *CheckersS) TestFitsTypeOf(c *check.C) {
  184. testInfo(c, check.FitsTypeOf, "FitsTypeOf", []string{"obtained", "sample"})
  185. // Basic types
  186. testCheck(c, check.FitsTypeOf, true, "", 1, 0)
  187. testCheck(c, check.FitsTypeOf, false, "", 1, int64(0))
  188. // Aliases
  189. testCheck(c, check.FitsTypeOf, false, "", 1, errors.New(""))
  190. testCheck(c, check.FitsTypeOf, false, "", "error", errors.New(""))
  191. testCheck(c, check.FitsTypeOf, true, "", errors.New("error"), errors.New(""))
  192. // Structures
  193. testCheck(c, check.FitsTypeOf, false, "", 1, simpleStruct{})
  194. testCheck(c, check.FitsTypeOf, false, "", simpleStruct{42}, &simpleStruct{})
  195. testCheck(c, check.FitsTypeOf, true, "", simpleStruct{42}, simpleStruct{})
  196. testCheck(c, check.FitsTypeOf, true, "", &simpleStruct{42}, &simpleStruct{})
  197. // Some bad values
  198. testCheck(c, check.FitsTypeOf, false, "Invalid sample value", 1, interface{}(nil))
  199. testCheck(c, check.FitsTypeOf, false, "", interface{}(nil), 0)
  200. }
  201. func (s *CheckersS) TestImplements(c *check.C) {
  202. testInfo(c, check.Implements, "Implements", []string{"obtained", "ifaceptr"})
  203. var e error
  204. var re runtime.Error
  205. testCheck(c, check.Implements, true, "", errors.New(""), &e)
  206. testCheck(c, check.Implements, false, "", errors.New(""), &re)
  207. // Some bad values
  208. testCheck(c, check.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, errors.New(""))
  209. testCheck(c, check.Implements, false, "ifaceptr should be a pointer to an interface variable", 0, interface{}(nil))
  210. testCheck(c, check.Implements, false, "", interface{}(nil), &e)
  211. }