From b4ddbb467d2cdbeb27efa4a20fdbe47def3abab3 Mon Sep 17 00:00:00 2001 From: shan-96 Date: Mon, 6 Apr 2026 18:40:51 +0400 Subject: [PATCH 1/2] give type mismatch hint when checker fails for same value but wrong type --- checker.go | 11 +++++++++++ checker_test.go | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/checker.go b/checker.go index e378680..ea71042 100644 --- a/checker.go +++ b/checker.go @@ -100,6 +100,17 @@ func (c *equalsChecker) Check(got interface{}, args []interface{}, note func(key } } + // Show type hint when string values are equal but have different types (e.g., a string type alias). + gotVal := reflect.ValueOf(got) + wantVal := reflect.ValueOf(want) + if gotVal.IsValid() && wantVal.IsValid() && + gotVal.Kind() == reflect.String && wantVal.Kind() == reflect.String && + reflect.TypeOf(got) != reflect.TypeOf(want) && + gotVal.String() == wantVal.String() { + note("got type", Unquoted(reflect.TypeOf(got).String())) + note("want type", Unquoted(reflect.TypeOf(want).String())) + } + return errors.New("values are not equal") } diff --git a/checker_test.go b/checker_test.go index c869a62..b3d5839 100644 --- a/checker_test.go +++ b/checker_test.go @@ -63,6 +63,8 @@ type OuterJSON struct { type boolean bool +type stringAlias string + var checkerTests = []struct { about string checker qt.Checker @@ -179,6 +181,23 @@ got: want: "42" `, +}, { + about: "Equals: string alias with same value shows type hint", + checker: qt.Equals, + got: stringAlias("CONSTANT"), + args: []interface{}{"CONSTANT"}, + expectedCheckFailure: ` +error: + values are not equal +got type: + quicktest_test.stringAlias +want type: + string +got: + "CONSTANT" +want: + +`, }, { about: "Equals: nil and nil", checker: qt.Equals, From f63fe08e8c5cfefb77bc9d3581311d3ee5403f9e Mon Sep 17 00:00:00 2001 From: shan-96 Date: Thu, 9 Apr 2026 13:28:55 +0400 Subject: [PATCH 2/2] better fix --- .gitignore | 1 + checker.go | 11 ----------- checker_test.go | 17 ++++++----------- format.go | 8 ++++++++ 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 722d5e7..aafda3a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +.idea diff --git a/checker.go b/checker.go index ea71042..e378680 100644 --- a/checker.go +++ b/checker.go @@ -100,17 +100,6 @@ func (c *equalsChecker) Check(got interface{}, args []interface{}, note func(key } } - // Show type hint when string values are equal but have different types (e.g., a string type alias). - gotVal := reflect.ValueOf(got) - wantVal := reflect.ValueOf(want) - if gotVal.IsValid() && wantVal.IsValid() && - gotVal.Kind() == reflect.String && wantVal.Kind() == reflect.String && - reflect.TypeOf(got) != reflect.TypeOf(want) && - gotVal.String() == wantVal.String() { - note("got type", Unquoted(reflect.TypeOf(got).String())) - note("want type", Unquoted(reflect.TypeOf(want).String())) - } - return errors.New("values are not equal") } diff --git a/checker_test.go b/checker_test.go index b3d5839..7e686c8 100644 --- a/checker_test.go +++ b/checker_test.go @@ -62,8 +62,7 @@ type OuterJSON struct { } type boolean bool - -type stringAlias string +type myString string var checkerTests = []struct { about string @@ -182,21 +181,17 @@ want: "42" `, }, { - about: "Equals: string alias with same value shows type hint", + about: "Equals: string and named string type with same value", checker: qt.Equals, - got: stringAlias("CONSTANT"), - args: []interface{}{"CONSTANT"}, + got: "hello", + args: []interface{}{myString("hello")}, expectedCheckFailure: ` error: values are not equal -got type: - quicktest_test.stringAlias -want type: - string got: - "CONSTANT" + "hello" want: - + quicktest_test.myString("hello") `, }, { about: "Equals: nil and nil", 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)) }