Skip to content

Commit 788db39

Browse files
committed
Update dump package with better user interface and fixed dump format.
- Add ability to indent dumped messages.
1 parent 99f5a3e commit 788db39

37 files changed

Lines changed: 592 additions & 316 deletions

internal/playground/check_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ func Test_Name(t *testing.T) {
4141
},
4242
}
4343

44-
wantD := dump.DefaultDump().DumpAny(want)
45-
haveD := dump.DefaultDump().DumpAny(have)
44+
wantD := dump.Default().Any(want)
45+
haveD := dump.Default().Any(have)
4646

4747
// --- When ---
4848
edits := diff.Strings(wantD, haveD)

internal/types/types.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ func init() {
2323

2424
// /////////////////////////////////////////////////////////////////////////////
2525

26-
// IntType is type alias used in tests.
27-
type IntType int
26+
// TIntType is type alias used in tests.
27+
type TIntType int
2828

2929
// /////////////////////////////////////////////////////////////////////////////
3030

pkg/check/collection.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func Has[T comparable](want T, bag []T, opts ...Option) error {
4040
return notice.New("expected slice to have a value").
4141
Trail(ops.Trail).
4242
Want("%#v", want).
43-
Append("slice", "%s", dmp.DumpAny(bag))
43+
Append("slice", "%s", dmp.Any(bag))
4444
}
4545

4646
// HasNo checks slice does not have "want" value. Returns nil if it doesn't,
@@ -55,7 +55,7 @@ func HasNo[T comparable](want T, set []T, opts ...Option) error {
5555
Trail(ops.Trail).
5656
Want("%#v", want).
5757
Append("index", "%d", i).
58-
Append("slice", "%s", dmp.DumpAny(set))
58+
Append("slice", "%s", dmp.Any(set))
5959
}
6060
}
6161
return nil
@@ -74,7 +74,7 @@ func HasKey[K comparable, V any](key K, set map[K]V, opts ...Option) (V, error)
7474
return val, notice.New("expected map to have a key").
7575
Trail(ops.Trail).
7676
Append("key", "%#v", key).
77-
Append("map", "%s", dmp.DumpAny(set))
77+
Append("map", "%s", dmp.Any(set))
7878
}
7979

8080
// HasNoKey checks map has no key. Returns nil if it doesn't, otherwise it
@@ -90,7 +90,7 @@ func HasNoKey[K comparable, V any](key K, set map[K]V, opts ...Option) error {
9090
Trail(ops.Trail).
9191
Append("key", "%#v", key).
9292
Append("value", "%#v", val).
93-
Append("map", "%s", dmp.DumpAny(set))
93+
Append("map", "%s", dmp.Any(set))
9494
}
9595

9696
// HasKeyValue checks map has a key with given value. Returns nil if it doesn't,
@@ -138,5 +138,5 @@ func SliceSubset[V comparable](want, have []V, opts ...Option) error {
138138
const hHeader = "expected \"want\" slice to be a subset of \"have\" slice"
139139
return notice.New(hHeader).
140140
Trail(ops.Trail).
141-
Append("missing values", "%s", dmp.DumpAny(missing))
141+
Append("missing values", "%s", dmp.Any(missing))
142142
}

pkg/check/collection_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func Test_Has(t *testing.T) {
117117
affirm.NotNil(t, err)
118118
wMsg := "expected slice to have a value:\n" +
119119
"\t want: 42\n" +
120-
"\tslice: []int{\n1,\n2,\n3,\n}"
120+
"\tslice: []int{\n\t1,\n\t2,\n\t3,\n}"
121121
affirm.Equal(t, wMsg, err.Error())
122122
})
123123

@@ -134,7 +134,7 @@ func Test_Has(t *testing.T) {
134134
wMsg := "expected slice to have a value:\n" +
135135
"\ttrail: type.field\n" +
136136
"\t want: 42\n" +
137-
"\tslice: []int{\n1,\n2,\n3,\n}"
137+
"\tslice: []int{\n\t1,\n\t2,\n\t3,\n}"
138138
affirm.Equal(t, wMsg, err.Error())
139139
})
140140
}
@@ -163,7 +163,7 @@ func Test_HasNo(t *testing.T) {
163163
wMsg := "expected slice not to have value:\n" +
164164
"\t want: 2\n" +
165165
"\tindex: 1\n" +
166-
"\tslice: []int{\n1,\n2,\n3,\n}"
166+
"\tslice: []int{\n\t1,\n\t2,\n\t3,\n}"
167167
affirm.Equal(t, wMsg, err.Error())
168168
})
169169

@@ -181,7 +181,7 @@ func Test_HasNo(t *testing.T) {
181181
"\ttrail: type.field\n" +
182182
"\t want: 2\n" +
183183
"\tindex: 1\n" +
184-
"\tslice: []int{\n1,\n2,\n3,\n}"
184+
"\tslice: []int{\n\t1,\n\t2,\n\t3,\n}"
185185
affirm.Equal(t, wMsg, err.Error())
186186
})
187187

@@ -392,7 +392,7 @@ func Test_SliceSubset(t *testing.T) {
392392
// --- Then ---
393393
affirm.NotNil(t, err)
394394
wMsg := "expected \"want\" slice to be a subset of \"have\" slice:\n" +
395-
"\tmissing values: []string{\n\"X\",\n\"Y\",\n}"
395+
"\tmissing values: []string{\n\t\"X\",\n\t\"Y\",\n}"
396396
affirm.Equal(t, wMsg, err.Error())
397397
})
398398

@@ -409,7 +409,7 @@ func Test_SliceSubset(t *testing.T) {
409409
affirm.NotNil(t, err)
410410
wMsg := "expected \"want\" slice to be a subset of \"have\" slice:\n" +
411411
"\t trail: type.field\n" +
412-
"\tmissing values: []string{\n\"X\",\n\"Y\",\n}"
412+
"\tmissing values: []string{\n\t\"X\",\n\t\"Y\",\n}"
413413
affirm.Equal(t, wMsg, err.Error())
414414
})
415415
}

pkg/check/nil.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func Nil(have any, opts ...Option) error {
2020
const mHeader = "expected value to be nil"
2121
return notice.New(mHeader).Want("<nil>").
2222
Trail(ops.Trail).
23-
Have("%s", dump.New(ops.DumpCfg).DumpAny(have))
23+
Have("%s", dump.New(ops.DumpCfg).Any(have))
2424
}
2525

2626
// isNil returns true if "have" is nil.

pkg/check/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func DefaultOptions() Options {
112112
return Options{
113113
DumpCfg: dump.NewConfig(
114114
dump.WithTimeFormat(DumpTimeFormat),
115-
dump.WithDepth(DumpDepth),
115+
dump.WithMaxDepth(DumpDepth),
116116
),
117117
Recent: RecentDuration,
118118
TimeFormat: ParseTimeFormat,

pkg/check/options_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ func Test_WithDump(t *testing.T) {
5252
ops := Options{}
5353

5454
// --- When ---
55-
have := WithDump(dump.WithDepth(100))(ops)
55+
have := WithDump(dump.WithMaxDepth(100))(ops)
5656

5757
// --- Then ---
58-
affirm.Equal(t, 0, ops.DumpCfg.Depth)
59-
affirm.Equal(t, 100, have.DumpCfg.Depth)
58+
affirm.Equal(t, 0, ops.DumpCfg.MaxDepth)
59+
affirm.Equal(t, 100, have.DumpCfg.MaxDepth)
6060
}
6161

6262
func Test_DefaultOptions(t *testing.T) {

pkg/dump/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ val := types.TA{
3838
TAp: nil,
3939
}
4040

41-
have := dump.DefaultDump().DumpAny(val)
41+
have := dump.Default().Any(val)
4242

4343
fmt.Println(have)
4444
// Output:
@@ -73,7 +73,7 @@ val := map[string]any{
7373
}
7474

7575
cfg := dump.NewConfig(dump.Flat)
76-
have := dump.New(cfg).DumpAny(val)
76+
have := dump.New(cfg).Any(val)
7777

7878
fmt.Println(have)
7979
// Output:
@@ -91,7 +91,7 @@ You can customize how `time.Time` values are displayed using the
9191
val := map[time.Time]int{time.Date(2000, 1, 2, 3, 4, 5, 0, time.UTC): 42}
9292

9393
cfg := dump.NewConfig(dump.Flat, dump.TimeFormat(time.Kitchen))
94-
have := dump.New(cfg).DumpAny(val)
94+
have := dump.New(cfg).Any(val)
9595

9696
fmt.Println(have)
9797
// Output:
@@ -110,7 +110,7 @@ val := map[string]any{
110110
}
111111

112112
cfg := NewConfig(PtrAddr)
113-
have := New(cfg).DumpAny(val)
113+
have := New(cfg).Any(val)
114114

115115
fmt.Println(have)
116116
// Output:
@@ -144,7 +144,7 @@ customIntDumper := func(dmp Dump, lvl int, val reflect.Value) string {
144144
}
145145

146146
cfg := NewConfig(Flat, Compact, WithDumper(i, customIntDumper))
147-
have := New(cfg).DumpAny(42)
147+
have := New(cfg).Any(42)
148148

149149
fmt.Println(have)
150150
// Output:
@@ -174,7 +174,7 @@ val := &Node{
174174
},
175175
}
176176

177-
have := dump.DefaultDump().DumpAny(val)
177+
have := dump.Default().Any(val)
178178
fmt.Println(have)
179179
// Output:
180180
// {
@@ -211,7 +211,7 @@ val := &Node{
211211
},
212212
}
213213

214-
have := dump.DefaultDump().DumpAny(val)
214+
have := dump.Default().Any(val)
215215
fmt.Println(have)
216216
// Output:
217217
// {

pkg/dump/config.go

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ const (
1515

1616
// DefaultDepth is default depth when dumping values recursively.
1717
DefaultDepth = 6
18+
19+
// DefaultIndent is default additional indent when dumping values.
20+
DefaultIndent = 0
1821
)
1922

2023
// Package wide configuration.
@@ -24,6 +27,9 @@ var (
2427

2528
// Depth is configurable depth when dumping values recursively.
2629
Depth = DefaultDepth
30+
31+
// Indent is configurable additional indent when dumping values.
32+
Indent = DefaultIndent
2733
)
2834

2935
// Option represents [NewConfig] option.
@@ -47,18 +53,21 @@ func WithTimeFormat(format string) func(cfg *Config) {
4753
return func(cfg *Config) { cfg.TimeFormat = format }
4854
}
4955

50-
// WithPrintType is option for [NewConfig] which makes [Dump] print types.
51-
func WithPrintType(cfg *Config) { cfg.PrintType = true }
52-
5356
// WithDumper adds custom [Dumper] to the config.
5457
func WithDumper(typ any, dumper Dumper) func(cfg *Config) {
5558
return func(cfg *Config) { cfg.Dumpers[reflect.TypeOf(typ)] = dumper }
5659
}
5760

58-
// WithDepth is option for [NewConfig] which controls maximum nesting when bumping
59-
// recursive types.
60-
func WithDepth(maximum int) func(cfg *Config) {
61-
return func(cfg *Config) { cfg.Depth = maximum }
61+
// WithMaxDepth is option for [NewConfig] which controls maximum nesting when
62+
// bumping recursive types.
63+
func WithMaxDepth(maximum int) func(cfg *Config) {
64+
return func(cfg *Config) { cfg.MaxDepth = maximum }
65+
}
66+
67+
// WithIndent is option for [NewConfig] which how much additional indentation
68+
// to apply to dumped values.
69+
func WithIndent(n int) func(cfg *Config) {
70+
return func(cfg *Config) { cfg.Indent = n }
6271
}
6372

6473
// Config represents [Dump] configuration.
@@ -107,7 +116,11 @@ type Config struct {
107116
Dumpers map[reflect.Type]Dumper
108117

109118
// Controls maximum nesting when dumping recursive types.
110-
Depth int
119+
// The depth is also used to properly indent values being dumped.
120+
MaxDepth int
121+
122+
// How much additional indentation to apply to values being dumped.
123+
Indent int
111124
}
112125

113126
// NewConfig returns new instance of [Config] with default values.
@@ -117,7 +130,8 @@ func NewConfig(opts ...Option) Config {
117130
PrintType: true,
118131
UseAny: true,
119132
Dumpers: make(map[reflect.Type]Dumper),
120-
Depth: Depth,
133+
MaxDepth: Depth,
134+
Indent: Indent,
121135
}
122136
for _, opt := range opts {
123137
opt(&cfg)

pkg/dump/config_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,28 @@ func Test_WithTimeFormat(t *testing.T) {
5656
affirm.Equal(t, TimeAsUnix, cfg.TimeFormat)
5757
}
5858

59-
func Test_WithDepth(t *testing.T) {
59+
func Test_WithMaxDepth(t *testing.T) {
6060
// --- Given ---
6161
cfg := &Config{}
6262

6363
// --- When ---
64-
opt := WithDepth(10)
64+
opt := WithMaxDepth(10)
6565

6666
// --- Then ---
6767
opt(cfg)
68-
affirm.Equal(t, 10, cfg.Depth)
68+
affirm.Equal(t, 10, cfg.MaxDepth)
6969
}
7070

71-
func Test_WithPrintType(t *testing.T) {
71+
func Test_WithIndent(t *testing.T) {
7272
// --- Given ---
7373
cfg := &Config{}
7474

7575
// --- When ---
76-
WithPrintType(cfg)
76+
opt := WithIndent(10)
7777

7878
// --- Then ---
79-
affirm.True(t, cfg.PrintType)
79+
opt(cfg)
80+
affirm.Equal(t, 10, cfg.Indent)
8081
}
8182

8283
func Test_WithDumper(t *testing.T) {
@@ -87,7 +88,7 @@ func Test_WithDumper(t *testing.T) {
8788
WithDumper(time.Time{}, GetTimeDumper(time.Kitchen))(&cfg)
8889

8990
// --- Then ---
90-
dmp := New(cfg).DumpAny(time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC))
91+
dmp := New(cfg).Any(time.Date(2020, 1, 2, 3, 4, 5, 0, time.UTC))
9192
affirm.Equal(t, `"3:04AM"`, dmp)
9293
}
9394

@@ -103,7 +104,8 @@ func Test_NewConfig(t *testing.T) {
103104
affirm.False(t, have.PtrAddr)
104105
affirm.True(t, have.UseAny)
105106
affirm.True(t, len(have.Dumpers) == 3)
106-
affirm.Equal(t, DefaultDepth, have.Depth)
107+
affirm.Equal(t, DefaultDepth, have.MaxDepth)
108+
affirm.Equal(t, DefaultIndent, have.Indent)
107109

108110
val, ok := have.Dumpers[typDur]
109111
affirm.True(t, ok)

0 commit comments

Comments
 (0)