當前位置: 首頁>>代碼示例>>Golang>>正文


Golang base.NewResultf函數代碼示例

本文整理匯總了Golang中github.com/rdrdr/hamcrest/base.NewResultf函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewResultf函數的具體用法?Golang NewResultf怎麽用?Golang NewResultf使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewResultf函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: 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

示例2: 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

示例3: 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

示例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: 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

示例6: 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

示例7: 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

示例8: 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

示例9: 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

示例10: 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

示例11: _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

示例12: 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

示例13: 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

示例14: 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

示例15: 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.NewResultf函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。