Skip to content

Proposal: Global setting of context/size for GetUnifiedDiffString #1878

@Radek-Pysny

Description

@Radek-Pysny

Description

  1. It would be great to have some global configuration that might affect some feature of testify package. The only one that is truly missing (at least for me) is "customisation" of one numeric magic constant used by your internal diff function (see comment in the snippet below, please).
  2. Just a small nit-pick. That function called diff introduces local variable called diff. How about calling it, e.g., result?
// diff returns a diff of both values as long as both are of the same type and
// are a struct, map, slice, array or string. Otherwise it returns an empty string.
func diff(expected interface{}, actual interface{}) string {
	if expected == nil || actual == nil {
		return ""
	}

	et, ek := typeAndKind(expected)
	at, _ := typeAndKind(actual)

	if et != at {
		return ""
	}

	if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String {
		return ""
	}

	var e, a string

	switch et {
	case reflect.TypeOf(""):
		e = reflect.ValueOf(expected).String()
		a = reflect.ValueOf(actual).String()
	case reflect.TypeOf(time.Time{}):
		e = spewConfigStringerEnabled.Sdump(expected)
		a = spewConfigStringerEnabled.Sdump(actual)
	default:
		e = spewConfig.Sdump(expected)
		a = spewConfig.Sdump(actual)
	}

	diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{
		A:        difflib.SplitLines(e),
		B:        difflib.SplitLines(a),
		FromFile: "Expected",
		FromDate: "",
		ToFile:   "Actual",
		ToDate:   "",
		Context:  3,   // <--- 1. THIS COULD BE CONFIGURABLE
	})

	return "\n\nDiff:\n" + diff  // <--- 2. SHADOWING OWN FUNCTION NAME
}

Proposed solution

No proposed solution yet.

Use case

There are use cases, where just 3 lines (one with error + one above + one below) is not sufficient. When I hit issue and need additional context to get strong enough clue to solve failing presumption, I have to change that magic constant to a higher value to get more context. This is mostly issue with struct within another struct.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions