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.
(expected interface{}, actual interface{})
| 1853 | // diff returns a diff of both values as long as both are of the same type and |
| 1854 | // are a struct, map, slice, array or string. Otherwise it returns an empty string. |
| 1855 | func diff(expected interface{}, actual interface{}) string { |
| 1856 | if expected == nil || actual == nil { |
| 1857 | return "" |
| 1858 | } |
| 1859 | |
| 1860 | et, ek := typeAndKind(expected) |
| 1861 | at, _ := typeAndKind(actual) |
| 1862 | |
| 1863 | if et != at { |
| 1864 | return "" |
| 1865 | } |
| 1866 | |
| 1867 | if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String { |
| 1868 | return "" |
| 1869 | } |
| 1870 | |
| 1871 | var e, a string |
| 1872 | |
| 1873 | switch et { |
| 1874 | case reflect.TypeOf(""): |
| 1875 | e = reflect.ValueOf(expected).String() |
| 1876 | a = reflect.ValueOf(actual).String() |
| 1877 | case reflect.TypeOf(time.Time{}): |
| 1878 | e = spewConfigStringerEnabled.Sdump(expected) |
| 1879 | a = spewConfigStringerEnabled.Sdump(actual) |
| 1880 | default: |
| 1881 | e = spewConfig.Sdump(expected) |
| 1882 | a = spewConfig.Sdump(actual) |
| 1883 | } |
| 1884 | |
| 1885 | diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ |
| 1886 | A: difflib.SplitLines(e), |
| 1887 | B: difflib.SplitLines(a), |
| 1888 | FromFile: "Expected", |
| 1889 | FromDate: "", |
| 1890 | ToFile: "Actual", |
| 1891 | ToDate: "", |
| 1892 | Context: 1, |
| 1893 | }) |
| 1894 | |
| 1895 | return "\n\nDiff:\n" + diff |
| 1896 | } |
| 1897 | |
| 1898 | func isFunction(arg interface{}) bool { |
| 1899 | if arg == nil { |