not.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2011 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. "errors"
  18. "fmt"
  19. )
  20. // Not returns a matcher that inverts the set of values matched by the wrapped
  21. // matcher. It does not transform the result for values for which the wrapped
  22. // matcher returns a fatal error.
  23. func Not(m Matcher) Matcher {
  24. return &notMatcher{m}
  25. }
  26. type notMatcher struct {
  27. wrapped Matcher
  28. }
  29. func (m *notMatcher) Matches(c interface{}) (err error) {
  30. err = m.wrapped.Matches(c)
  31. // Did the wrapped matcher say yes?
  32. if err == nil {
  33. return errors.New("")
  34. }
  35. // Did the wrapped matcher return a fatal error?
  36. if _, isFatal := err.(*FatalError); isFatal {
  37. return err
  38. }
  39. // The wrapped matcher returned a non-fatal error.
  40. return nil
  41. }
  42. func (m *notMatcher) Description() string {
  43. return fmt.Sprintf("not(%s)", m.wrapped.Description())
  44. }