Skip to content

Consider add assertion ShouldMatchRegex #700

@1ncludeSteven

Description

@1ncludeSteven

add below code in assertions/strings.go

func ShouldMatchRegex(actual interface{}, expected ...interface{}) string {
	if fail := need(1, expected); fail != success {
		return fail
	}

	obs, valueIsString := actual.(string)
	pattern, expecIsString := expected[0].(string)

	if !valueIsString || !expecIsString {
		return fmt.Sprintf(shouldBothBeStrings, reflect.TypeOf(actual), reflect.TypeOf(expected[0]))
	}
	matched, err := regexp.MatchString(pattern, obs)
	if err != nil {
		return fmt.Sprintf("error from regex.MatchString: '%v' in trying to match observed '%s' against expected pattern '%s'", err, obs, pattern)
	}
	if !matched {
		return fmt.Sprintf("failed to match observed '%s' against expected pattern '%s'", obs, pattern)
	}
	return success
}

then add ShouldMatchRegex in convey/assertions.go

ShouldMatchRegex  = assertions.ShouldMatchRegex

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions