diff --git a/.gitignore b/.gitignore index 722d5e7..aafda3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +.idea diff --git a/checker_test.go b/checker_test.go index c869a62..7e686c8 100644 --- a/checker_test.go +++ b/checker_test.go @@ -62,6 +62,7 @@ type OuterJSON struct { } type boolean bool +type myString string var checkerTests = []struct { about string @@ -179,6 +180,19 @@ got: want: "42" `, +}, { + about: "Equals: string and named string type with same value", + checker: qt.Equals, + got: "hello", + args: []interface{}{myString("hello")}, + expectedCheckFailure: ` +error: + values are not equal +got: + "hello" +want: + quicktest_test.myString("hello") +`, }, { about: "Equals: nil and nil", checker: qt.Equals, diff --git a/format.go b/format.go index a7c88b0..d0f748f 100644 --- a/format.go +++ b/format.go @@ -46,6 +46,14 @@ func Format(v interface{}) string { // (json.RawMessage for example). return fmt.Sprintf("%T(%s)", v, quoteString(string(bytes))) } + // Handle named string types (e.g. type myString string) explicitly so that + // the type name is included in the output. Without this they fall through to + // pretty.Formatter which renders them as a plain quoted string, identical to + // a plain string value, causing the deduplication in the reporter to + // incorrectly print "" when the types differ. + if rv := reflect.ValueOf(v); rv.IsValid() && rv.Kind() == reflect.String { + return fmt.Sprintf("%T(%s)", v, quoteString(rv.String())) + } // The pretty.Sprint equivalent does not quote string values. return fmt.Sprintf("%# v", pretty.Formatter(v)) }