Skip to content

Commit 193dddc

Browse files
committed
Test from the external package, dot-imported, with the examples beside their tests
1 parent 8a38d8c commit 193dddc

4 files changed

Lines changed: 171 additions & 179 deletions

File tree

‎error_test.go‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
package errors
1+
package errors_test
22

33
import (
44
"fmt"
55
"io"
66
"log/slog"
7+
"os"
78
"runtime"
89
"slices"
910
"strings"
1011
"testing"
1112

1213
"github.com/gravitton/assert"
14+
. "github.com/gravitton/errors"
1315
)
1416

1517
func TestError(t *testing.T) {
@@ -473,6 +475,98 @@ func TestError_LogValue(t *testing.T) {
473475
})
474476
}
475477

478+
func ExampleNew() {
479+
err := New("boom")
480+
481+
fmt.Printf("%v\n", err)
482+
fmt.Printf("%q\n", err)
483+
// Output:
484+
// boom
485+
// "boom"
486+
}
487+
488+
func ExampleNewf() {
489+
err := Newf("load user %d: %w", 42, ErrNotFound)
490+
491+
fmt.Println(err)
492+
// Output:
493+
// load user 42: not found
494+
}
495+
496+
func ExampleWrap() {
497+
inner := New("connection refused")
498+
err := Wrap(fmt.Errorf("load config: %w", inner))
499+
500+
fmt.Println(err)
501+
fmt.Println(slices.Equal(err.StackTrace(), inner.StackTrace()))
502+
// Output:
503+
// load config: connection refused
504+
// true
505+
}
506+
507+
func ExampleError_Fields() {
508+
err := Wrap(ErrNotFound).WithField("id", 42).WithField("table", "users")
509+
510+
fmt.Println(err)
511+
for key, value := range err.Fields() {
512+
fmt.Println(key, value)
513+
}
514+
// Output:
515+
// not found
516+
// id 42
517+
// table users
518+
}
519+
520+
func ExampleError_WithCause() {
521+
cause := New("connection refused").WithField("port", 5432)
522+
err := New("could not load config").WithCause(cause).WithCause(io.ErrClosedPipe)
523+
524+
fmt.Println(err)
525+
fmt.Println(Is(err, io.ErrClosedPipe))
526+
// Output:
527+
// could not load config
528+
// true
529+
}
530+
531+
func ExampleError_Format() {
532+
err := New("could not load config").WithField("path", "config.yml").WithCause(io.EOF)
533+
534+
fmt.Printf("%+v\n", err)
535+
// The stack trace holds file paths that depend on where the module is built, so this
536+
// example has no checked output. It prints something like:
537+
//
538+
// could not load config
539+
// path=config.yml
540+
// github.com/gravitton/errors_test.ExampleError_Format
541+
// /app/error_test.go:532
542+
// ...
543+
// caused by: EOF
544+
}
545+
546+
func ExampleError_GoString() {
547+
var zero Error
548+
err := New("boom")
549+
550+
fmt.Printf("%#v\n", &zero)
551+
fmt.Printf("%#v\n", err)
552+
fmt.Printf("%#v\n", err.WithField("id", 42))
553+
fmt.Printf("%#v\n", err.WithCause(io.EOF))
554+
// Output:
555+
// &errors.Error{}
556+
// &errors.Error{err:&errors.errorString{s:"boom"}}
557+
// &errors.Error{err:&errors.errorString{s:"boom"}, fields:errors.fieldList{errors.field{key:"id", value:42}}}
558+
// &errors.Error{err:&errors.errorString{s:"boom"}, cause:&errors.errorString{s:"EOF"}}
559+
}
560+
561+
func ExampleError_LogValue() {
562+
cause := New("connection refused").WithField("port", 5432)
563+
err := Wrap(ErrNotFound).WithField("id", 42).WithCause(cause)
564+
565+
newLogger().Error("load user", "err", err)
566+
// Output:
567+
// level=ERROR msg="load user" err.msg="not found" err.fields.id=42 err.cause.msg="connection refused" err.cause.fields.port=5432
568+
}
569+
476570
func here() string {
477571
pc, _, _, _ := runtime.Caller(1)
478572

@@ -511,6 +605,18 @@ func logged(err error) string {
511605
return strings.TrimSpace(b.String())
512606
}
513607

608+
func newLogger() *slog.Logger {
609+
return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
610+
ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr {
611+
if len(groups) == 0 && attr.Key == slog.TimeKey {
612+
return slog.Attr{}
613+
}
614+
615+
return attr
616+
},
617+
}))
618+
}
619+
514620
func stackAt(err *Error, level int) string {
515621
b := &strings.Builder{}
516622
indent := strings.Repeat("\t", level)
@@ -547,6 +653,7 @@ var (
547653
valueSink any
548654

549655
errSentinel = Sentinel("sentinel")
656+
ErrNotFound = Sentinel("not found")
550657
errInit = New("init")
551658

552659
initFunction = here()

‎example_test.go‎

Lines changed: 0 additions & 176 deletions
This file was deleted.

‎multi_test.go‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package errors
1+
package errors_test
22

33
import (
44
"errors"
@@ -8,6 +8,7 @@ import (
88
"testing"
99

1010
"github.com/gravitton/assert"
11+
. "github.com/gravitton/errors"
1112
)
1213

1314
func TestMultiError(t *testing.T) {
@@ -304,3 +305,62 @@ func TestMultiError_LogValue(t *testing.T) {
304305
assert.Equal(t, logged(NewMulti()), "")
305306
})
306307
}
308+
309+
func ExampleMultiError() {
310+
errs := NewMulti()
311+
fmt.Println(errs.ErrorOrNil())
312+
313+
errs.Add(nil, io.EOF)
314+
errs.Add(Join(io.ErrUnexpectedEOF, io.ErrClosedPipe))
315+
316+
fmt.Printf("%v\n", errs)
317+
// Output:
318+
// <nil>
319+
// EOF
320+
// unexpected EOF
321+
// io: read/write on closed pipe
322+
}
323+
324+
func ExampleJoin() {
325+
fmt.Println(Join(nil, nil))
326+
fmt.Println(Join(io.EOF, nil))
327+
fmt.Println(Join(io.EOF, New("line one\nline two"), io.ErrClosedPipe))
328+
// Output:
329+
// <nil>
330+
// EOF
331+
// EOF
332+
// line one
333+
// line two
334+
// io: read/write on closed pipe
335+
}
336+
337+
func ExampleMultiError_Format() {
338+
errs := Join(New("connection refused").WithField("port", 5432), io.EOF)
339+
340+
fmt.Printf("%+v\n", errs)
341+
// The stack trace holds file paths that depend on where the module is built, so this
342+
// example has no checked output. It prints something like:
343+
//
344+
// connection refused
345+
// port=5432
346+
// github.com/gravitton/errors_test.ExampleMultiError_Format
347+
// /app/multi_test.go:338
348+
// ...
349+
// EOF
350+
}
351+
352+
func ExampleMultiError_GoString() {
353+
fmt.Printf("%#v\n", NewMulti())
354+
fmt.Printf("%#v\n", Join(io.EOF, New("boom").WithField("id", 42)))
355+
// Output:
356+
// &errors.MultiError{}
357+
// &errors.MultiError{errs:[]error{&errors.errorString{s:"EOF"}, &errors.Error{err:&errors.errorString{s:"boom"}, fields:errors.fieldList{errors.field{key:"id", value:42}}}}}
358+
}
359+
360+
func ExampleMultiError_LogValue() {
361+
errs := Join(New("connection refused").WithField("port", 5432), io.EOF)
362+
363+
newLogger().Error("load users", "err", errs)
364+
// Output:
365+
// level=ERROR msg="load users" err.0.msg="connection refused" err.0.fields.port=5432 err.1=EOF
366+
}

0 commit comments

Comments
 (0)