Diff gets a string describing the differences between the arguments and the specified objects. Returns the diff string and number of differences found.
(objects []interface{})
| 953 | // |
| 954 | // Returns the diff string and number of differences found. |
| 955 | func (args Arguments) Diff(objects []interface{}) (string, int) { |
| 956 | // TODO: could return string as error and nil for No difference |
| 957 | |
| 958 | output := "\n" |
| 959 | var differences int |
| 960 | |
| 961 | maxArgCount := len(args) |
| 962 | if len(objects) > maxArgCount { |
| 963 | maxArgCount = len(objects) |
| 964 | } |
| 965 | |
| 966 | for i := 0; i < maxArgCount; i++ { |
| 967 | var actual, expected interface{} |
| 968 | var actualFmt, expectedFmt string |
| 969 | |
| 970 | if len(objects) <= i { |
| 971 | actual = "(Missing)" |
| 972 | actualFmt = "(Missing)" |
| 973 | } else { |
| 974 | actual = objects[i] |
| 975 | actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual) |
| 976 | } |
| 977 | |
| 978 | if len(args) <= i { |
| 979 | expected = "(Missing)" |
| 980 | expectedFmt = "(Missing)" |
| 981 | } else { |
| 982 | expected = args[i] |
| 983 | expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected) |
| 984 | } |
| 985 | |
| 986 | if matcher, ok := expected.(argumentMatcher); ok { |
| 987 | var matches bool |
| 988 | func() { |
| 989 | defer func() { |
| 990 | if r := recover(); r != nil { |
| 991 | actualFmt = fmt.Sprintf("panic in argument matcher: %v", r) |
| 992 | } |
| 993 | }() |
| 994 | matches = matcher.Matches(actual) |
| 995 | }() |
| 996 | if matches { |
| 997 | output = fmt.Sprintf("%s\t%d: PASS: %s matched by %s\n", output, i, actualFmt, matcher) |
| 998 | } else { |
| 999 | differences++ |
| 1000 | output = fmt.Sprintf("%s\t%d: FAIL: %s not matched by %s\n", output, i, actualFmt, matcher) |
| 1001 | } |
| 1002 | } else { |
| 1003 | switch expected := expected.(type) { |
| 1004 | case anythingOfTypeArgument: |
| 1005 | // type checking |
| 1006 | if reflect.TypeOf(actual).Name() != string(expected) && reflect.TypeOf(actual).String() != string(expected) { |
| 1007 | // not match |
| 1008 | differences++ |
| 1009 | output = fmt.Sprintf("%s\t%d: FAIL: type %s != type %s - %s\n", output, i, expected, reflect.TypeOf(actual).Name(), actualFmt) |
| 1010 | } |
| 1011 | case *IsTypeArgument: |
| 1012 | actualT := reflect.TypeOf(actual) |