update gomega godep

This commit is contained in:
David Ashpole
2019-02-12 18:34:33 -08:00
parent ee50d4784d
commit fec22bbb25
88 changed files with 155386 additions and 334 deletions

View File

@@ -6,6 +6,7 @@ go_library(
"elements.go",
"fields.go",
"ignore.go",
"keys.go",
"pointer.go",
"types.go",
],

View File

@@ -13,10 +13,14 @@ import (
//MatchAllElements succeeds if every element of a slice matches the element matcher it maps to
//through the id function, and every element matcher is matched.
// Expect([]string{"a", "b"}).To(MatchAllElements(idFn, matchers.Elements{
// "a": BeEqual("a"),
// "b": BeEqual("b"),
// })
// idFn := func(element interface{}) string {
// return fmt.Sprintf("%v", element)
// }
//
// Expect([]string{"a", "b"}).To(MatchAllElements(idFn, Elements{
// "a": Equal("a"),
// "b": Equal("b"),
// }))
func MatchAllElements(identifier Identifier, elements Elements) types.GomegaMatcher {
return &ElementsMatcher{
Identifier: identifier,
@@ -26,16 +30,27 @@ func MatchAllElements(identifier Identifier, elements Elements) types.GomegaMatc
//MatchElements succeeds if each element of a slice matches the element matcher it maps to
//through the id function. It can ignore extra elements and/or missing elements.
// Expect([]string{"a", "c"}).To(MatchElements(idFn, IgnoreMissing|IgnoreExtra, matchers.Elements{
// "a": BeEqual("a")
// "b": BeEqual("b"),
// })
// idFn := func(element interface{}) string {
// return fmt.Sprintf("%v", element)
// }
//
// Expect([]string{"a", "b", "c"}).To(MatchElements(idFn, IgnoreExtras, Elements{
// "a": Equal("a"),
// "b": Equal("b"),
// }))
// Expect([]string{"a", "c"}).To(MatchElements(idFn, IgnoreMissing, Elements{
// "a": Equal("a"),
// "b": Equal("b"),
// "c": Equal("c"),
// "d": Equal("d"),
// }))
func MatchElements(identifier Identifier, options Options, elements Elements) types.GomegaMatcher {
return &ElementsMatcher{
Identifier: identifier,
Elements: elements,
IgnoreExtras: options&IgnoreExtras != 0,
IgnoreMissing: options&IgnoreMissing != 0,
Identifier: identifier,
Elements: elements,
IgnoreExtras: options&IgnoreExtras != 0,
IgnoreMissing: options&IgnoreMissing != 0,
AllowDuplicates: options&AllowDuplicates != 0,
}
}
@@ -52,6 +67,8 @@ type ElementsMatcher struct {
IgnoreExtras bool
// Whether to ignore missing elements or consider it an error.
IgnoreMissing bool
// Whether to key duplicates when matching IDs.
AllowDuplicates bool
// State.
failures []error
@@ -88,10 +105,11 @@ func (m *ElementsMatcher) matchElements(actual interface{}) (errs []error) {
for i := 0; i < val.Len(); i++ {
element := val.Index(i).Interface()
id := m.Identifier(element)
// TODO: Add options to ignore & match duplicates.
if elements[id] {
errs = append(errs, fmt.Errorf("found duplicate element ID %s", id))
continue
if !m.AllowDuplicates {
errs = append(errs, fmt.Errorf("found duplicate element ID %s", id))
continue
}
}
elements[id] = true

View File

@@ -14,10 +14,21 @@ import (
//MatchAllFields succeeds if every field of a struct matches the field matcher associated with
//it, and every element matcher is matched.
// Expect([]string{"a", "b"}).To(MatchAllFields(idFn, gstruct.Fields{
// "a": BeEqual("a"),
// "b": BeEqual("b"),
// })
// actual := struct{
// A int
// B []bool
// C string
// }{
// A: 5,
// B: []bool{true, false},
// C: "foo",
// }
//
// Expect(actual).To(MatchAllFields(Fields{
// "A": Equal(5),
// "B": ConsistOf(true, false),
// "C": Equal("foo"),
// }))
func MatchAllFields(fields Fields) types.GomegaMatcher {
return &FieldsMatcher{
Fields: fields,
@@ -26,10 +37,26 @@ func MatchAllFields(fields Fields) types.GomegaMatcher {
//MatchFields succeeds if each element of a struct matches the field matcher associated with
//it. It can ignore extra fields and/or missing fields.
// Expect([]string{"a", "c"}).To(MatchFields(idFn, IgnoreMissing|IgnoreExtra, gstruct.Fields{
// "a": BeEqual("a")
// "b": BeEqual("b"),
// })
// actual := struct{
// A int
// B []bool
// C string
// }{
// A: 5,
// B: []bool{true, false},
// C: "foo",
// }
//
// Expect(actual).To(MatchFields(IgnoreExtras, Fields{
// "A": Equal(5),
// "B": ConsistOf(true, false),
// }))
// Expect(actual).To(MatchFields(IgnoreMissing, Fields{
// "A": Equal(5),
// "B": ConsistOf(true, false),
// "C": Equal("foo"),
// "D": Equal("extra"),
// }))
func MatchFields(options Options, fields Fields) types.GomegaMatcher {
return &FieldsMatcher{
Fields: fields,

124
vendor/github.com/onsi/gomega/gstruct/keys.go generated vendored Normal file
View File

@@ -0,0 +1,124 @@
package gstruct
import (
"errors"
"fmt"
"reflect"
"runtime/debug"
"strings"
"github.com/onsi/gomega/format"
errorsutil "github.com/onsi/gomega/gstruct/errors"
"github.com/onsi/gomega/types"
)
func MatchAllKeys(keys Keys) types.GomegaMatcher {
return &KeysMatcher{
Keys: keys,
}
}
func MatchKeys(options Options, keys Keys) types.GomegaMatcher {
return &KeysMatcher{
Keys: keys,
IgnoreExtras: options&IgnoreExtras != 0,
IgnoreMissing: options&IgnoreMissing != 0,
}
}
type KeysMatcher struct {
// Matchers for each key.
Keys Keys
// Whether to ignore extra keys or consider it an error.
IgnoreExtras bool
// Whether to ignore missing keys or consider it an error.
IgnoreMissing bool
// State.
failures []error
}
type Keys map[interface{}]types.GomegaMatcher
func (m *KeysMatcher) Match(actual interface{}) (success bool, err error) {
if reflect.TypeOf(actual).Kind() != reflect.Map {
return false, fmt.Errorf("%v is type %T, expected map", actual, actual)
}
m.failures = m.matchKeys(actual)
if len(m.failures) > 0 {
return false, nil
}
return true, nil
}
func (m *KeysMatcher) matchKeys(actual interface{}) (errs []error) {
actualValue := reflect.ValueOf(actual)
keys := map[interface{}]bool{}
for _, keyValue := range actualValue.MapKeys() {
key := keyValue.Interface()
keys[key] = true
err := func() (err error) {
// This test relies heavily on reflect, which tends to panic.
// Recover here to provide more useful error messages in that case.
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("panic checking %+v: %v\n%s", actual, r, debug.Stack())
}
}()
matcher, ok := m.Keys[key]
if !ok {
if !m.IgnoreExtras {
return fmt.Errorf("unexpected key %s: %+v", key, actual)
}
return nil
}
valValue := actualValue.MapIndex(keyValue)
match, err := matcher.Match(valValue.Interface())
if err != nil {
return err
}
if !match {
if nesting, ok := matcher.(errorsutil.NestingMatcher); ok {
return errorsutil.AggregateError(nesting.Failures())
}
return errors.New(matcher.FailureMessage(valValue))
}
return nil
}()
if err != nil {
errs = append(errs, errorsutil.Nest(fmt.Sprintf(".%#v", key), err))
}
}
for key := range m.Keys {
if !keys[key] && !m.IgnoreMissing {
errs = append(errs, fmt.Errorf("missing expected key %s", key))
}
}
return errs
}
func (m *KeysMatcher) FailureMessage(actual interface{}) (message string) {
failures := make([]string, len(m.failures))
for i := range m.failures {
failures[i] = m.failures[i].Error()
}
return format.Message(reflect.TypeOf(actual).Name(),
fmt.Sprintf("to match keys: {\n%v\n}\n", strings.Join(failures, "\n")))
}
func (m *KeysMatcher) NegatedFailureMessage(actual interface{}) (message string) {
return format.Message(actual, "not to match keys")
}
func (m *KeysMatcher) Failures() []error {
return m.failures
}

View File

@@ -8,4 +8,8 @@ const (
IgnoreExtras Options = 1 << iota
//IgnoreMissing tells the matcher to ignore missing elements or fields, rather than triggering a failure.
IgnoreMissing
//AllowDuplicates tells the matcher to permit multiple members of the slice to produce the same ID when
//considered by the indentifier function. All members that map to a given key must still match successfully
//with the matcher that is provided for that key.
AllowDuplicates
)