12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- package oglematchers
- import (
- "bytes"
- "errors"
- "fmt"
- "reflect"
- )
- var byteSliceType reflect.Type = reflect.TypeOf([]byte{})
- func DeepEquals(x interface{}) Matcher {
- return &deepEqualsMatcher{x}
- }
- type deepEqualsMatcher struct {
- x interface{}
- }
- func (m *deepEqualsMatcher) Description() string {
- xDesc := fmt.Sprintf("%v", m.x)
- xValue := reflect.ValueOf(m.x)
-
-
-
- if xValue.Kind() == reflect.Slice && xValue.IsNil() {
- xDesc = "<nil slice>"
- }
- return fmt.Sprintf("deep equals: %s", xDesc)
- }
- func (m *deepEqualsMatcher) Matches(c interface{}) error {
-
- ct := reflect.TypeOf(c)
- xt := reflect.TypeOf(m.x)
- if ct != xt {
- return NewFatalError(fmt.Sprintf("which is of type %v", ct))
- }
-
- cValue := reflect.ValueOf(c)
- xValue := reflect.ValueOf(m.x)
- if ct == byteSliceType && !cValue.IsNil() && !xValue.IsNil() {
- xBytes := m.x.([]byte)
- cBytes := c.([]byte)
- if bytes.Equal(cBytes, xBytes) {
- return nil
- }
- return errors.New("")
- }
-
- if reflect.DeepEqual(m.x, c) {
- return nil
- }
-
-
- if cValue.Kind() == reflect.Slice && cValue.IsNil() {
- return errors.New("which is nil")
- }
- return errors.New("")
- }
|