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{})
| 1792 | // diff returns a diff of both values as long as both are of the same type and |
| 1793 | // are a struct, map, slice, array or string. Otherwise it returns an empty string. |
| 1794 | func diff(expected interface{}, actual interface{}) string { |
| 1795 | if expected == nil || actual == nil { |
| 1796 | return "" |
| 1797 | } |
| 1798 | |
| 1799 | et, ek := typeAndKind(expected) |
| 1800 | at, _ := typeAndKind(actual) |
| 1801 | |
| 1802 | if et != at { |
| 1803 | return "" |
| 1804 | } |
| 1805 | |
| 1806 | if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String { |
| 1807 | return "" |
| 1808 | } |
| 1809 | |
| 1810 | var e, a string |
| 1811 | |
| 1812 | switch et { |
| 1813 | case reflect.TypeOf(""): |
| 1814 | e = reflect.ValueOf(expected).String() |
| 1815 | a = reflect.ValueOf(actual).String() |
| 1816 | case reflect.TypeOf(time.Time{}): |
| 1817 | e = spewConfigStringerEnabled.Sdump(expected) |
| 1818 | a = spewConfigStringerEnabled.Sdump(actual) |
| 1819 | default: |
| 1820 | e = spewConfig.Sdump(expected) |
| 1821 | a = spewConfig.Sdump(actual) |
| 1822 | } |
| 1823 | |
| 1824 | diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ |
| 1825 | A: difflib.SplitLines(e), |
| 1826 | B: difflib.SplitLines(a), |
| 1827 | FromFile: "Expected", |
| 1828 | FromDate: "", |
| 1829 | ToFile: "Actual", |
| 1830 | ToDate: "", |
| 1831 | Context: 1, |
| 1832 | }) |
| 1833 | |
| 1834 | return "\n\nDiff:\n" + diff |
| 1835 | } |
| 1836 | |
| 1837 | func isFunction(arg interface{}) bool { |
| 1838 | if arg == nil { |
searching dependent graphs…