has_same_type_as.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // Copyright 2015 Aaron Jacobs. All Rights Reserved.
  2. // Author: aaronjjacobs@gmail.com (Aaron Jacobs)
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. package oglematchers
  16. import (
  17. "fmt"
  18. "reflect"
  19. )
  20. // HasSameTypeAs returns a matcher that matches values with exactly the same
  21. // type as the supplied prototype.
  22. func HasSameTypeAs(p interface{}) Matcher {
  23. expected := reflect.TypeOf(p)
  24. pred := func(c interface{}) error {
  25. actual := reflect.TypeOf(c)
  26. if actual != expected {
  27. return fmt.Errorf("which has type %v", actual)
  28. }
  29. return nil
  30. }
  31. return NewMatcher(pred, fmt.Sprintf("has type %v", expected))
  32. }