本文整理汇总了Golang中github.com/rdrdr/hamcrest/base.NewMatcherf函数的典型用法代码示例。如果您正苦于以下问题:Golang NewMatcherf函数的具体用法?Golang NewMatcherf怎么用?Golang NewMatcherf使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewMatcherf函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: OnPatternGroup
// Variant of OnPattern() that uses the given subgroup of the pattern.
func OnPatternGroup(pattern string, group int) func(matcher *base.Matcher) *base.Matcher {
re := regexp.MustCompile(pattern)
if num := re.NumSubexp(); num < group {
println("Illegal group #", group, ": there are only ", num, "groups.")
panic("Group index out of bounds.")
}
return func(matcher *base.Matcher) *base.Matcher {
match := func(s string) *base.Result {
loc := re.FindStringSubmatchIndex(s)
if loc == nil {
return base.NewResultf(false,
"No occurrences of pattern \"%v\"", pattern)
}
start, end := loc[0], loc[1]
substart, subend := loc[group*2], loc[group*2+1]
prefix := s[start:substart]
substring := s[substart:subend]
suffix := s[subend:end]
result := matcher.Match(substring)
return base.NewResultf(result.Matched(),
"Found substring[%v:%v], [%v:%v]=\"%v[%v]%v\" for pattern \"%v\"",
substart, subend, start, end,
prefix, substring, suffix, pattern).WithCauses(result)
}
return base.NewMatcherf(match,
"OnPatternGroup[\"%v\", %v][%v]", pattern, group, matcher)
}
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:29,代码来源:strings.go
示例2: Test_IfThen
func Test_IfThen(t *testing.T) {
yes, no := Anything(), Not(Anything())
calledSnoop := false
snoop := base.NewMatcherf(func(v interface{}) *base.Result {
calledSnoop = true
return base.NewResultf(false, "snooped!")
}, "Snoop")
if result := If(yes).Then(yes).Match(0); !result.Matched() {
t.Errorf("if yes then yes should match, was [%v]", result)
}
if result := If(yes).Then(no).Match(0); result.Matched() {
t.Errorf("if yes then no should not match, was [%v]", result)
}
result := If(no).Then(snoop).Match(0)
if calledSnoop {
t.Errorf("If-no-then-snoop should short-circuit before calling snoop")
}
if !result.Matched() {
t.Errorf("if-no-then-snoop should match on failing antecedent, was [%v]",
result)
}
logSamples(t, If(yes).Then(yes))
logSamples(t, If(yes).Then(no))
logSamples(t, If(no).Then(yes))
logSamples(t, If(no).Then(no))
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:27,代码来源:logic_test.go
示例3: Contains
// Matches strings that contain the given substring.
func Contains(substring string) *base.Matcher {
match := func(s string) *base.Result {
extra := 8
if foundStart := strings.Index(s, substring); foundStart >= 0 {
foundEnd := foundStart + len(substring)
start, end := foundStart-extra, foundEnd+extra
prefix, suffix := "", ""
if start <= 0 {
start = 0
} else {
prefix = "..."
}
if end >= len(s) {
end = len(s)
} else {
suffix = "..."
}
return base.NewResultf(true,
"substring \"%v\" appears in \"%v%v[%v]%v%v\"", substring,
prefix, s[start:foundStart], substring, s[foundEnd:end], suffix)
}
return base.NewResultf(false,
"substring \"%v\" does not appear in \"%v\"",
substring, s)
}
return base.NewMatcherf(match, "Contains(\"%v\")", substring)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:28,代码来源:strings.go
示例4: Xor
// Second part of a builder for an either/xor matcher:
// matcher := Either(matcher1).Xor(matcher2)
// This matcher matches when exactly one of the two matchers matches
// a given value; if both or neither of the matchers is successful,
// xor fails to match. Note that this is *never* a short-circuiting
// operation.
func (self *EitherClause) Xor(matcher2 *base.Matcher) *base.Matcher {
matcher1 := self.matcher
match := func(actual interface{}) *base.Result {
result1 := matcher1.Match(actual)
result2 := matcher2.Match(actual)
if result1.Matched() {
if result2.Matched() {
return base.NewResultf(false,
"both parts of 'Either/Xor' matched [%v]", actual).
WithCauses(result1, result2)
}
return base.NewResultf(true,
"only the first part of 'Either/Xor' matched [%v]", actual).
WithCauses(result1, result2)
}
if result2.Matched() {
return base.NewResultf(true,
"only the second part of 'Either/Xor' matched [%v]", actual).
WithCauses(result1, result2)
}
return base.NewResultf(false,
"neither part of 'Either/Xor' matched [%v]", actual).
WithCauses(result1, result2)
}
return base.NewMatcherf(match, "either [%v] xor [%v]", matcher1, matcher2)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:32,代码来源:logic.go
示例5: Test_BothAnd
// Check Matchers
func Test_BothAnd(t *testing.T) {
yes, no := Anything(), Not(Anything())
calledSnoop := false
snoop := base.NewMatcherf(func(v interface{}) *base.Result {
calledSnoop = true
return base.NewResultf(false, "snooped!")
}, "Snoop")
if result := Both(yes).And(yes).Match(0); !result.Matched() {
t.Errorf("yes and yes should match, was [%v]", result)
}
if result := Both(yes).And(no).Match(0); result.Matched() {
t.Errorf("yes and no should not match, was [%v]", result)
}
result := Both(no).And(snoop).Match(0)
if calledSnoop {
t.Errorf("no and snoop should short-circuit before calling snoop")
}
if result.Matched() {
t.Errorf("no and snoop should not match, was [%v]", result)
}
logSamples(t, Both(yes).And(yes))
logSamples(t, Both(yes).And(no))
logSamples(t, Both(no).And(yes))
logSamples(t, Both(no).And(no))
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:27,代码来源:logic_test.go
示例6: Then
// Constructs an if-and-only-if/then matcher:
// matcher := IfAndOnlyIf(AntecedentMatcher).Then(ConsequentMatcher)
// that matches when both or neither of the Antecedent and the
// Consequent match. Note that this is logically equivalent to:
// Either(Not(AntecedentMatcher)).Xor(ConsequentMatcher)
// But may be more readable in practice.
func (self *IfAndOnlyIfClause) Then(consequent *base.Matcher) *base.Matcher {
antecedent := self.antecedent
match := func(actual interface{}) *base.Result {
result1 := antecedent.Match(actual)
result2 := consequent.Match(actual)
if result1.Matched() {
if result2.Matched() {
return base.NewResultf(true,
"Matched because both parts of 'Iff/Then' matched on [%v]", actual).
WithCauses(result1, result2)
}
return base.NewResultf(false,
"Failed because only the first part of 'Iff/Then' matched on [%v]", actual).
WithCauses(result1, result2)
}
if result2.Matched() {
return base.NewResultf(false,
"Failed because only the second part of 'IFf/Then' matched on [%v]", actual).
WithCauses(result1, result2)
}
return base.NewResultf(true,
"Matched because neither part of 'Iff/Then' matched on [%v]", actual).
WithCauses(result1, result2)
}
return base.NewMatcherf(match, "if and only if [%v] then [%v]", antecedent, consequent)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:32,代码来源:logic.go
示例7: EachElem
// Returns a matcher that matches on any array or slice input value
// if the given matcher matches every element of that array or slice.
//
// The returned matcher does not match any non-array-or-slice value.
func EachElem(matcher *base.Matcher) *base.Matcher {
match := func(actual interface{}) *base.Result {
v := reflect.NewValue(actual)
var value _ElemAndLen
var ok bool
value, ok = v.(*reflect.ArrayValue)
if !ok {
value, ok = v.(*reflect.SliceValue)
}
if !ok {
return base.NewResultf(false,
"Was not array or slice: was type %T", actual)
}
n := value.Len()
for i := 0; i < n; i++ {
elem := value.Elem(i).Interface()
result := matcher.Match(elem)
if !result.Matched() {
return base.NewResultf(false,
"Failed to match element %v of %v: %v",
i+1, n, elem).
WithCauses(result)
}
}
return base.NewResultf(true,
"Matched all of the %v elements", n)
}
return base.NewMatcherf(match, "EveryElement[%v]", matcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:33,代码来源:slices.go
示例8: AnyPattern
// Returns a short-circuiting function that applies the matcher to each
// occurrence of the pattern in an input string, until a matching pattern
// is found (in which case the matcher successfully matches) or all
// matching instances are exhausted (in which case the output matcher
// fails to match).
//
// For example:
// AnyPattern("x.")(EqualTo("xy"))
// would match:
// "six sax are sexy" (three matches of "x.", third is "xy")
// but not:
// "pox pix are pixelated" (three matches of "x.", none is "xy")
func AnyPattern(pattern string) func(matcher *base.Matcher) *base.Matcher {
re := regexp.MustCompile(pattern)
return func(matcher *base.Matcher) *base.Matcher {
match := func(s string) *base.Result {
matches := re.FindAllStringIndex(s, -1)
if matches == nil {
return base.NewResultf(false,
"No occurrences of pattern \"%v\"", pattern)
}
for index, loc := range matches {
start, end := loc[0], loc[1]
substring := s[start:end]
result := matcher.Match(substring)
if result.Matched() {
return base.NewResultf(true,
"matched substring[%v:%v]=\"%v\", occurrence #%v (of %v) of pattern \"%v\"",
start, end, substring, index+1, len(matches), pattern)
}
}
return base.NewResultf(false,
"Did not match any occurrence (of %v) of pattern \"%v\"",
len(matches), pattern)
}
return base.NewMatcherf(match,
"AnyPattern[\"%v\"][%v]", pattern, matcher)
}
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:40,代码来源:strings.go
示例9: AnyPatternGroup
// Variant of AnyPattern() that uses the given subgroup of the pattern.
func AnyPatternGroup(pattern string, group int) func(matcher *base.Matcher) *base.Matcher {
re := regexp.MustCompile(pattern)
if num := re.NumSubexp(); num < group {
println("Illegal group #", group, ": there are only ", num, "groups.")
panic("Group index out of bounds.")
}
return func(matcher *base.Matcher) *base.Matcher {
match := func(s string) *base.Result {
matches := re.FindAllStringSubmatchIndex(s, -1)
if matches == nil {
return base.NewResultf(false,
"No occurrences of pattern \"%v\"", pattern)
}
for index, loc := range matches {
substart, subend := loc[2*group], loc[2*group+1]
substring := s[substart:subend]
result := matcher.Match(substring)
if result.Matched() {
start, end := loc[0], loc[1]
prefix, suffix := s[start:substart], s[subend:end]
return base.NewResultf(true,
"matched substring[%v:%v], [%v:%v]=\"%v[%v]%v\", occurrence #%v (of %v) of pattern \"%v\"",
substart, subend, start, end,
prefix, substring, suffix, index+1, len(matches), pattern)
}
}
return base.NewResultf(false,
"Did not match any occurrence (of %v) of pattern \"%v\"",
len(matches), pattern)
}
return base.NewMatcherf(match,
"AnyPatternGroup[\"%v\", %v][%v]", pattern, group, matcher)
}
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:36,代码来源:strings.go
示例10: ToLen
// Creates a new matcher that applies the given matcher to the result of
// converting an input string its length. (using the `len()` builtin).
// If the input value is not a string, the matcher fails to match.
func ToLen(matcher *base.Matcher) *base.Matcher {
match := func(s string) *base.Result {
length := len(s)
result := matcher.Match(length)
return base.NewResultf(result.Matched(),
"length is %v", length).
WithCauses(result)
}
return base.NewMatcherf(match, "ToLen(%v)", matcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:13,代码来源:strings.go
示例11: ToUpper
// Creates a new matcher that applies the given matcher to the result of
// converting an input string to uppercase (using strings.ToUpper).
// If the input value is not a string, the matcher fails to match.
func ToUpper(matcher *base.Matcher) *base.Matcher {
match := func(s string) *base.Result {
upper := strings.ToUpper(s)
result := matcher.Match(upper)
return base.NewResultf(result.Matched(),
"ToUpper is %v", upper).
WithCauses(result)
}
return base.NewMatcherf(match, "ToUpper(%v)", matcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:13,代码来源:strings.go
示例12: ToType
// Returns a new matcher that applies the type of its input
// element to the given matcher.
func ToType(matcher *base.Matcher) *base.Matcher {
match := func(actual interface{}) *base.Result {
actualType := reflect.Typeof(actual)
result := matcher.Match(actualType)
return base.NewResultf(result.Matched(),
"reflect.Typeof() returned %v", actualType).
WithCauses(result)
}
return base.NewMatcherf(match, "ToType(%v)", matcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:12,代码来源:reflect.go
示例13: Empty
// Matches any input element that is an empty array, slice, or map.
func Empty() *base.Matcher {
match := func(actual interface{}) *base.Result {
value := reflect.NewValue(actual)
if hasLen, ok := value.(_HasLen); ok {
length := hasLen.Len()
return base.NewResultf(length == 0,
"Len() returned %v", length)
}
return base.NewResultf(false, "Can't determine length of type %T", actual)
}
return base.NewMatcherf(match, "Empty")
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:13,代码来源:slices.go
示例14: ToLen
// Applies the given matcher to the length of the input element.
func ToLen(matcher *base.Matcher) *base.Matcher {
match := func(actual interface{}) *base.Result {
value := reflect.NewValue(actual)
if hasLen, ok := value.(_HasLen); ok {
length := hasLen.Len()
result := matcher.Match(length)
return base.NewResultf(result.Matched(), "Len() returned %v", length)
}
return base.NewResultf(false,
"Can't determine Len() for %T", actual)
}
return base.NewMatcherf(match, "ToLen[%v]", matcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:14,代码来源:slices.go
示例15: _TypeMatcher
func _TypeMatcher(name string, expectedType reflect.Type) *base.Matcher {
match := func(actual interface{}) *base.Result {
if actual == nil {
return base.NewResultf(false, "was nil")
}
actualType := reflect.Typeof(actual)
if reflect.DeepEqual(actualType, expectedType) {
return base.NewResultf(true, "was of type %v", expectedType)
}
return base.NewResultf(false,
"was a %v, not a %v", actualType, expectedType)
}
return base.NewMatcherf(match, "Typeof[%v]", expectedType)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:14,代码来源:reflect.go
示例16: HasPattern
// Matches strings that contain the given regexp pattern, using
// the same syntax as the standard regexp package.
func HasPattern(pattern string) *base.Matcher {
re := regexp.MustCompile(pattern)
match := func(s string) *base.Result {
if found := re.FindStringIndex(s); found != nil {
start, end := found[0], found[1]
return base.NewResultf(true,
"pattern \"%v\" matched substring[%v:%v]=\"%v\"",
pattern, start, end, s[start:end])
}
return base.NewResultf(false,
"pattern \"%v\" not found in \"%v\"", pattern, s)
}
return base.NewMatcherf(match, "HasPattern[\"%v\"]", pattern)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:16,代码来源:strings.go
示例17: SliceTypeOf
// Returns a new matcher that, on any input that is a *reflect.SliceType,
// extracts the type of element and matches it against the given matcher.
//
// If the given input is not an *reflect.SliceType, this fails to match.
// Note: this matches slice *types*, not slices. (See SliceOf.)
func SliceTypeOf(elementTypeMatcher *base.Matcher) *base.Matcher {
match := func(actual interface{}) *base.Result {
if sliceType, ok := actual.(*reflect.SliceType); ok {
elementType := sliceType.Elem()
result := elementTypeMatcher.Match(elementType)
return base.NewResultf(
result.Matched(),
"was SliceType with elements of type %v", elementType.Name()).
WithCauses(result)
}
return base.NewResultf(false, "was of type %T, not a slice", actual)
}
return base.NewMatcherf(match, "SliceTypeOf(%v)", elementTypeMatcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:19,代码来源:reflect.go
示例18: EqualToIgnoringCase
func EqualToIgnoringCase(expected string) *base.Matcher {
expectedToLower := strings.ToLower(expected)
match := func(actual string) *base.Result {
actualToLower := strings.ToLower(actual)
if actualToLower == expectedToLower {
return base.NewResultf(true,
"\"%v\" matches \"%v\" (ignoring case)",
actual, expected)
}
return base.NewResultf(false,
"\"%v\" differs from \"%v\" (ignoring case)",
actual, expected)
}
return base.NewMatcherf(match, "EqualToIgnoringCase(\"%v\")", expected)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:15,代码来源:strings.go
示例19: ArrayOf
// Returns a new matcher that, on any input that is an array, extracts
// its type and matches it against the given matcher.
//
// If the given input is not an array, this fails to match.
// Note: this matches *arrays*, not array *types*. (See ArrayTypeOf.)
func ArrayOf(elementTypeMatcher *base.Matcher) *base.Matcher {
match := func(actual interface{}) *base.Result {
actualType := reflect.Typeof(actual)
if arrayType, ok := actualType.(*reflect.ArrayType); ok {
elementType := arrayType.Elem()
result := elementTypeMatcher.Match(elementType)
return base.NewResultf(
result.Matched(),
"was array with elements of type %v", elementType).
WithCauses(result)
}
return base.NewResultf(false, "was of type %T, not an array", actual)
}
return base.NewMatcherf(match, "ArrayOf(%v)", elementTypeMatcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:20,代码来源:reflect.go
示例20: ChannelOf
// Returns a new matcher that, on any input that is a channel, extracts
// its type and matches it against the given matcher.
//
// If the given input is not a channel, this fails to match.
// Note: this matches *channels*, not channel *types*. (See ChannelTypeOf.)
func ChannelOf(elementTypeMatcher *base.Matcher) *base.Matcher {
match := func(actual interface{}) *base.Result {
actualType := reflect.Typeof(actual)
if channelType, ok := actualType.(*reflect.ChanType); ok {
elementType := channelType.Elem()
result := elementTypeMatcher.Match(elementType)
return base.NewResultf(result.Matched(),
"was channel with elements of type %v",
elementType).
WithCauses(result)
}
return base.NewResultf(false, "was of type %T, not a channel", actual)
}
return base.NewMatcherf(match, "ChannelOf(%v)", elementTypeMatcher)
}
开发者ID:rdrdr,项目名称:hamcrest,代码行数:20,代码来源:reflect.go
注:本文中的github.com/rdrdr/hamcrest/base.NewMatcherf函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论