@@ -9,56 +9,31 @@ package ctxerr
99import (
1010 "fmt"
1111 "reflect"
12- "strings"
1312
1413 "github.com/maxatome/go-testdeep/internal/types"
14+ "github.com/maxatome/go-testdeep/internal/util"
1515)
1616
17- // OpBadUsage returns a string to notice the user he passed a bad
17+ // OpBadUsage returns an [*Error] to notice the user she/ he passed a bad
1818// parameter to an operator constructor.
19+ //
20+ // If kind and param's kind name ≠ param's type name:
21+ //
22+ // usage: {op}{usage}, but received {param type} ({param kind}) as {pos}th parameter
23+ //
24+ // else
25+ //
26+ // usage: {op}{usage}, but received {param type} as {pos}th parameter
1927func OpBadUsage (op , usage string , param any , pos int , kind bool ) * Error {
20- var b strings.Builder
21- fmt .Fprintf (& b , "usage: %s%s, but received " , op , usage )
22-
23- if param == nil {
24- b .WriteString ("nil" )
25- } else {
26- t := reflect .TypeOf (param )
27- if kind && t .String () != t .Kind ().String () {
28- fmt .Fprintf (& b , "%s (%s)" , t , t .Kind ())
29- } else {
30- b .WriteString (t .String ())
31- }
32- }
33-
34- b .WriteString (" as " )
35- switch pos {
36- case 1 :
37- b .WriteString ("1st" )
38- case 2 :
39- b .WriteString ("2nd" )
40- case 3 :
41- b .WriteString ("3rd" )
42- default :
43- fmt .Fprintf (& b , "%dth" , pos )
44- }
45- b .WriteString (" parameter" )
46-
47- return & Error {
48- Message : "bad usage of " + op + " operator" ,
49- Summary : NewSummary (b .String ()),
50- User : true ,
51- }
28+ return OpBad (op , "usage: %s%s, %s" , op , usage , util .BadParam (param , pos , kind ))
5229}
5330
54- // OpTooManyParams returns an [*Error] to notice the user he called a
31+ // OpTooManyParams returns an [*Error] to notice the user she/ he called a
5532// variadic operator constructor with too many parameters.
33+ //
34+ // usage: {op}{usage}, too many parameters
5635func OpTooManyParams (op , usage string ) * Error {
57- return & Error {
58- Message : "bad usage of " + op + " operator" ,
59- Summary : NewSummary ("usage: " + op + usage + ", too many parameters" ),
60- User : true ,
61- }
36+ return OpBad (op , "usage: %s%s, too many parameters" , op , usage )
6237}
6338
6439// OpBad returns an [*Error] to notice the user a bad operator
0 commit comments