Skip to content

Commit 5e3d38e

Browse files
committed
refactor: avoid code duplication
Signed-off-by: Maxime Soulé <btik-git@scoubidou.com>
1 parent eecf4c4 commit 5e3d38e

File tree

6 files changed

+96
-123
lines changed

6 files changed

+96
-123
lines changed

internal/color/color.go

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ package color
99
import (
1010
"fmt"
1111
"os"
12-
"reflect"
1312
"strings"
1413
"sync"
14+
15+
"github.com/maxatome/go-testdeep/internal/util"
1516
)
1617

1718
const (
@@ -212,47 +213,17 @@ func Bad(s string, args ...any) string {
212213
}
213214

214215
// BadUsage returns a string surrounded by BAD color to notice the
215-
// user he passes a bad parameter to a function. Typically used in a
216+
// user she/he passes a bad parameter to a function. Typically used in a
216217
// panic().
217218
func BadUsage(usage string, param any, pos int, kind bool) string {
218-
Init()
219-
220-
var b strings.Builder
221-
fmt.Fprintf(&b, "%susage: %s, but received ", BadOnBold, usage)
222-
223-
if param == nil {
224-
b.WriteString("nil")
225-
} else {
226-
t := reflect.TypeOf(param)
227-
if kind && t.String() != t.Kind().String() {
228-
fmt.Fprintf(&b, "%s (%s)", t, t.Kind())
229-
} else {
230-
b.WriteString(t.String())
231-
}
232-
}
233-
234-
b.WriteString(" as ")
235-
switch pos {
236-
case 1:
237-
b.WriteString("1st")
238-
case 2:
239-
b.WriteString("2nd")
240-
case 3:
241-
b.WriteString("3rd")
242-
default:
243-
fmt.Fprintf(&b, "%dth", pos)
244-
}
245-
b.WriteString(" parameter")
246-
b.WriteString(BadOff)
247-
return b.String()
219+
return Bad("usage: %s, %s", usage, util.BadParam(param, pos, kind))
248220
}
249221

250222
// TooManyParams returns a string surrounded by BAD color to notice
251-
// the user he called a variadic function with too many
223+
// the user she/he called a variadic function with too many
252224
// parameters. Typically used in a panic().
253225
func TooManyParams(usage string) string {
254-
Init()
255-
return BadOnBold + "usage: " + usage + ", too many parameters" + BadOff
226+
return Bad("usage: " + usage + ", too many parameters")
256227
}
257228

258229
// UnBad returns s with bad color prefix & suffix removed.

internal/color/color_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -139,30 +139,6 @@ func TestBadUsage(t *testing.T) {
139139
test.EqualStr(t,
140140
color.BadUsage("Zzz(STRING)", nil, 1, true),
141141
"usage: Zzz(STRING), but received nil as 1st parameter")
142-
143-
test.EqualStr(t,
144-
color.BadUsage("Zzz(STRING)", 42, 1, true),
145-
"usage: Zzz(STRING), but received int as 1st parameter")
146-
147-
test.EqualStr(t,
148-
color.BadUsage("Zzz(STRING)", []int{}, 1, true),
149-
"usage: Zzz(STRING), but received []int (slice) as 1st parameter")
150-
test.EqualStr(t,
151-
color.BadUsage("Zzz(STRING)", []int{}, 1, false),
152-
"usage: Zzz(STRING), but received []int as 1st parameter")
153-
154-
test.EqualStr(t,
155-
color.BadUsage("Zzz(STRING)", nil, 1, true),
156-
"usage: Zzz(STRING), but received nil as 1st parameter")
157-
test.EqualStr(t,
158-
color.BadUsage("Zzz(STRING)", nil, 2, true),
159-
"usage: Zzz(STRING), but received nil as 2nd parameter")
160-
test.EqualStr(t,
161-
color.BadUsage("Zzz(STRING)", nil, 3, true),
162-
"usage: Zzz(STRING), but received nil as 3rd parameter")
163-
test.EqualStr(t,
164-
color.BadUsage("Zzz(STRING)", nil, 4, true),
165-
"usage: Zzz(STRING), but received nil as 4th parameter")
166142
}
167143

168144
func TestTooManyParams(t *testing.T) {

internal/ctxerr/op_error.go

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,56 +9,31 @@ package ctxerr
99
import (
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
1927
func 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
5635
func 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

internal/ctxerr/op_error_test.go

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,6 @@ func TestOpBadUsage(t *testing.T) {
2323
test.EqualStr(t,
2424
ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 1, true).Error(),
2525
prefix+"usage: Zzz(STRING), but received nil as 1st parameter")
26-
27-
test.EqualStr(t,
28-
ctxerr.OpBadUsage("Zzz", "(STRING)", 42, 1, true).Error(),
29-
prefix+"usage: Zzz(STRING), but received int as 1st parameter")
30-
31-
test.EqualStr(t,
32-
ctxerr.OpBadUsage("Zzz", "(STRING)", []int{}, 1, true).Error(),
33-
prefix+"usage: Zzz(STRING), but received []int (slice) as 1st parameter")
34-
test.EqualStr(t,
35-
ctxerr.OpBadUsage("Zzz", "(STRING)", []int{}, 1, false).Error(),
36-
prefix+"usage: Zzz(STRING), but received []int as 1st parameter")
37-
38-
test.EqualStr(t,
39-
ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 1, true).Error(),
40-
prefix+"usage: Zzz(STRING), but received nil as 1st parameter")
41-
test.EqualStr(t,
42-
ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 2, true).Error(),
43-
prefix+"usage: Zzz(STRING), but received nil as 2nd parameter")
44-
test.EqualStr(t,
45-
ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 3, true).Error(),
46-
prefix+"usage: Zzz(STRING), but received nil as 3rd parameter")
47-
test.EqualStr(t,
48-
ctxerr.OpBadUsage("Zzz", "(STRING)", nil, 4, true).Error(),
49-
prefix+"usage: Zzz(STRING), but received nil as 4th parameter")
5026
}
5127

5228
func TestOpTooManyParams(t *testing.T) {

internal/util/utils.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,51 @@
66

77
package util
88

9+
import (
10+
"fmt"
11+
"reflect"
12+
"strings"
13+
)
14+
15+
// BadParam returns a string noticing a misuse of a function parameter.
16+
//
17+
// If kind and param's kind name ≠ param's type name:
18+
//
19+
// but received {param type} ({param kind}) as {pos}th parameter
20+
//
21+
// else
22+
//
23+
// but received {param type} as {pos}th parameter
24+
func BadParam(param any, pos int, kind bool) string {
25+
var b strings.Builder
26+
b.WriteString("but received ")
27+
28+
if param == nil {
29+
b.WriteString("nil")
30+
} else {
31+
t := reflect.TypeOf(param)
32+
if kind && t.String() != t.Kind().String() {
33+
fmt.Fprintf(&b, "%s (%s)", t, t.Kind())
34+
} else {
35+
b.WriteString(t.String())
36+
}
37+
}
38+
39+
b.WriteString(" as ")
40+
switch pos {
41+
case 1:
42+
b.WriteString("1st")
43+
case 2:
44+
b.WriteString("2nd")
45+
case 3:
46+
b.WriteString("3rd")
47+
default:
48+
fmt.Fprintf(&b, "%dth", pos)
49+
}
50+
b.WriteString(" parameter")
51+
return b.String()
52+
}
53+
954
// TernRune returns a if cond is true, b otherwise.
1055
func TernRune(cond bool, a, b rune) rune {
1156
if cond {

internal/util/utils_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,36 @@ import (
1313
"github.com/maxatome/go-testdeep/internal/util"
1414
)
1515

16+
func TestBadParam(t *testing.T) {
17+
test.EqualStr(t,
18+
util.BadParam(nil, 1, true),
19+
"but received nil as 1st parameter")
20+
21+
test.EqualStr(t,
22+
util.BadParam(42, 1, true),
23+
"but received int as 1st parameter")
24+
25+
test.EqualStr(t,
26+
util.BadParam([]int{}, 1, true),
27+
"but received []int (slice) as 1st parameter")
28+
test.EqualStr(t,
29+
util.BadParam([]int{}, 1, false),
30+
"but received []int as 1st parameter")
31+
32+
test.EqualStr(t,
33+
util.BadParam(nil, 1, true),
34+
"but received nil as 1st parameter")
35+
test.EqualStr(t,
36+
util.BadParam(nil, 2, true),
37+
"but received nil as 2nd parameter")
38+
test.EqualStr(t,
39+
util.BadParam(nil, 3, true),
40+
"but received nil as 3rd parameter")
41+
test.EqualStr(t,
42+
util.BadParam(nil, 4, true),
43+
"but received nil as 4th parameter")
44+
}
45+
1646
func TestTern(t *testing.T) {
1747
test.EqualInt(t, int(util.TernRune(true, 'A', 'B')), int('A'))
1848
test.EqualInt(t, int(util.TernRune(false, 'A', 'B')), int('B'))

0 commit comments

Comments
 (0)