You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

120 line
3.8 KiB

  1. // Copyright 2010 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //go:generate mockgen -destination internal/mock_matcher/mock_matcher.go github.com/golang/mock/gomock Matcher
  15. package gomock_test
  16. import (
  17. "errors"
  18. "testing"
  19. "github.com/golang/mock/gomock"
  20. mock_matcher "github.com/golang/mock/gomock/internal/mock_matcher"
  21. )
  22. func TestMatchers(t *testing.T) {
  23. type e interface{}
  24. type testCase struct {
  25. matcher gomock.Matcher
  26. yes, no []e
  27. }
  28. tests := []testCase{
  29. {gomock.Any(), []e{3, nil, "foo"}, nil},
  30. {gomock.Eq(4), []e{4}, []e{3, "blah", nil, int64(4)}},
  31. {gomock.Nil(),
  32. []e{nil, (error)(nil), (chan bool)(nil), (*int)(nil)},
  33. []e{"", 0, make(chan bool), errors.New("err"), new(int)}},
  34. {gomock.Not(gomock.Eq(4)), []e{3, "blah", nil, int64(4)}, []e{4}},
  35. }
  36. for i, test := range tests {
  37. for _, x := range test.yes {
  38. if !test.matcher.Matches(x) {
  39. t.Errorf(`test %d: "%v %s" should be true.`, i, x, test.matcher)
  40. }
  41. }
  42. for _, x := range test.no {
  43. if test.matcher.Matches(x) {
  44. t.Errorf(`test %d: "%v %s" should be false.`, i, x, test.matcher)
  45. }
  46. }
  47. }
  48. }
  49. // A more thorough test of notMatcher
  50. func TestNotMatcher(t *testing.T) {
  51. ctrl := gomock.NewController(t)
  52. defer ctrl.Finish()
  53. mockMatcher := mock_matcher.NewMockMatcher(ctrl)
  54. notMatcher := gomock.Not(mockMatcher)
  55. mockMatcher.EXPECT().Matches(4).Return(true)
  56. if match := notMatcher.Matches(4); match {
  57. t.Errorf("notMatcher should not match 4")
  58. }
  59. mockMatcher.EXPECT().Matches(5).Return(false)
  60. if match := notMatcher.Matches(5); !match {
  61. t.Errorf("notMatcher should match 5")
  62. }
  63. }
  64. type Dog struct {
  65. Breed, Name string
  66. }
  67. // A thorough test of assignableToTypeOfMatcher
  68. func TestAssignableToTypeOfMatcher(t *testing.T) {
  69. ctrl := gomock.NewController(t)
  70. defer ctrl.Finish()
  71. aStr := "def"
  72. anotherStr := "ghi"
  73. if match := gomock.AssignableToTypeOf("abc").Matches(4); match {
  74. t.Errorf(`AssignableToTypeOf("abc") should not match 4`)
  75. }
  76. if match := gomock.AssignableToTypeOf("abc").Matches(&aStr); match {
  77. t.Errorf(`AssignableToTypeOf("abc") should not match &aStr (*string)`)
  78. }
  79. if match := gomock.AssignableToTypeOf("abc").Matches("def"); !match {
  80. t.Errorf(`AssignableToTypeOf("abc") should match "def"`)
  81. }
  82. if match := gomock.AssignableToTypeOf(&aStr).Matches("abc"); match {
  83. t.Errorf(`AssignableToTypeOf(&aStr) should not match "abc"`)
  84. }
  85. if match := gomock.AssignableToTypeOf(&aStr).Matches(&anotherStr); !match {
  86. t.Errorf(`AssignableToTypeOf(&aStr) should match &anotherStr`)
  87. }
  88. if match := gomock.AssignableToTypeOf(0).Matches(4); !match {
  89. t.Errorf(`AssignableToTypeOf(0) should match 4`)
  90. }
  91. if match := gomock.AssignableToTypeOf(0).Matches("def"); match {
  92. t.Errorf(`AssignableToTypeOf(0) should not match "def"`)
  93. }
  94. if match := gomock.AssignableToTypeOf(Dog{}).Matches(&Dog{}); match {
  95. t.Errorf(`AssignableToTypeOf(Dog{}) should not match &Dog{}`)
  96. }
  97. if match := gomock.AssignableToTypeOf(Dog{}).Matches(Dog{Breed: "pug", Name: "Fido"}); !match {
  98. t.Errorf(`AssignableToTypeOf(Dog{}) should match Dog{Breed: "pug", Name: "Fido"}`)
  99. }
  100. if match := gomock.AssignableToTypeOf(&Dog{}).Matches(Dog{}); match {
  101. t.Errorf(`AssignableToTypeOf(&Dog{}) should not match Dog{}`)
  102. }
  103. if match := gomock.AssignableToTypeOf(&Dog{}).Matches(&Dog{Breed: "pug", Name: "Fido"}); !match {
  104. t.Errorf(`AssignableToTypeOf(&Dog{}) should match &Dog{Breed: "pug", Name: "Fido"}`)
  105. }
  106. }