Documentation
¶
Overview ¶
Package notice simplifies building structured assertion messages.
Index ¶
- Constants
- Variables
- func Indent(n int, r rune, lns string) string
- func Join(err ...error) error
- func Unwrap(err error) []error
- type Notice
- func (msg *Notice) Append(name, format string, args ...any) *Notice
- func (msg *Notice) AppendRow(desc ...Row) *Notice
- func (msg *Notice) Error() string
- func (msg *Notice) GetData(key string) (any, bool)
- func (msg *Notice) Have(format string, args ...any) *Notice
- func (msg *Notice) Is(target error) bool
- func (msg *Notice) Prepend(name, format string, args ...any) *Notice
- func (msg *Notice) Remove(name string) *Notice
- func (msg *Notice) SetData(key string, val any) *Notice
- func (msg *Notice) SetHeader(header string, args ...any) *Notice
- func (msg *Notice) Trail(tr string) *Notice
- func (msg *Notice) Unwrap() error
- func (msg *Notice) Want(format string, args ...any) *Notice
- func (msg *Notice) Wrap(err error) *Notice
- type Row
Examples ¶
Constants ¶
const ContinuationHeader = " ---"
ContinuationHeader is a special [Notice.Header] separating notices with the same header.
Example:
header: want: want 0 have: have 0 --- want: want 1 have: have 1
Variables ¶
var ErrNotice = errors.New("notice error")
ErrNotice is sentinel error which is automatically wrapped by all instances of Notice unless changed with Notice.Wrap method.
Functions ¶
func Indent ¶
Indent indents lines with n number of runes. Lines are indented only if there are more than one line.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
lines := notice.Indent(4, ' ', "line1\nline2\nline3")
fmt.Println(lines)
}
Output: line1 line2 line3
func Join ¶ added in v0.6.0
Join wraps errors in an instance of multi decorator if it's an error joined with errors.Join.
Types ¶
type Notice ¶
type Notice struct {
Header string // Header message.
Rows []Row // Context rows.
Data map[string]any // Any useful data attached to the Notice.
// contains filtered or unexported fields
}
Notice represents structured notice message consisting of a header and multiple named rows giving context to it.
nolint: errname
func From ¶
From returns instance of Notice if it is in err's tree. If prefix is not empty header will be prefixed with the first element in the slice. If err is not instance of Notice it will create a new one and wrap err.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
var err error
err = notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz")
msg := notice.From(err, "optional prefix").
Append("my", "%s", "value")
fmt.Println(msg)
}
Output: [optional prefix] expected values to be equal: want: abc have: xyz my: value
func New ¶
New creates a new Notice with the specified header which is constructed using fmt.Sprintf from format and args. By default, the base error is set to ErrNotice.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz")
fmt.Println(msg)
}
Output: expected values to be equal: want: abc have: xyz
Example (FormatedHeader) ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected %s to be equal", "values").
Want("%s", "abc").
Have("%s", "xyz")
fmt.Println(msg)
}
Output: expected values to be equal: want: abc have: xyz
func (*Notice) Append ¶
Append appends a new row with the specified name and value build using fmt.Sprintf from format and args. If a row with the same name already exists, it is moved to the end of the [Notice.Order] slice. Implements fluent interface.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
Append("name", "%d", 5)
fmt.Println(msg)
}
Output: expected values to be equal: want: abc have: xyz name: 5
Example (ForceNexLine) ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("\n%s", "xyz").
Append("name", "%d", 5)
fmt.Println(msg)
}
Output: expected values to be equal: want: abc have: xyz name: 5
Example (MultiLine) ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "x\ny\nz").
Append("name", "%d", 5)
fmt.Println(msg)
}
Output: expected values to be equal: want: abc have: x y z name: 5
func (*Notice) AppendRow ¶
AppendRow appends description rows to the message.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
row0 := notice.NewRow("number", "%d", 5)
row1 := notice.NewRow("string", "%s", "abc")
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
AppendRow(row0, row1)
fmt.Println(msg)
}
Output: expected values to be equal: want: abc have: xyz number: 5 string: abc
func (*Notice) GetData ¶ added in v0.6.0
GetData returns data set by Notice.SetData. Returns nil and false if the key was never set.
func (*Notice) Have ¶
Have uses Append method to append a row with "have" name. If the "have" row already exists it will just replace its value.
func (*Notice) Prepend ¶
Prepend prepends a new row with the specified name and value built using fmt.Sprintf from format and args. If a row with the same name already exists, it is moved to the beginning of the [Notice.Order] slice. Implements fluent interface.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Trail("type.field").
Want("%s", "abc").
Have("%s", "xyz").
Prepend("name", "%d", 5)
fmt.Println(msg)
}
Output: expected values to be equal: trail: type.field name: 5 want: abc have: xyz
func (*Notice) SetData ¶ added in v0.6.0
SetData sets data. To get it back use Notice.GetData method.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
SetData("key", 123)
fmt.Println(msg.GetData("key"))
}
Output: 123 true
func (*Notice) SetHeader ¶
SetHeader sets the header message. Implements fluent interface.
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz")
_ = msg.SetHeader("some other %s", "header")
fmt.Println(msg)
}
Output: some other header: want: abc have: xyz
func (*Notice) Trail ¶
Trail adds trail row if "tr" is not empty string. If the trail row already exists it overwrites it. Implements fluent interface.
Trail examples:
- Type
- Type[1].Field
- Type["key"].Field
Example ¶
package main
import (
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
msg := notice.New("expected values to be equal").
Trail("type.field").
Want("%s", "abc").
Have("%s", "xyz")
fmt.Println(msg)
}
Output: expected values to be equal: trail: type.field want: abc have: xyz
func (*Notice) Unwrap ¶ added in v0.6.0
Unwrap returns wrapped error. By default, it returns ErrNotice unless a different error was specified using Notice.Wrap.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
ErrMy := errors.New("my error")
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
Wrap(ErrMy)
fmt.Println(msg.Unwrap())
}
Output: my error
func (*Notice) Want ¶
Want uses Append method to append a row with "want" name. If the "want" row already exists it will just replace its value.
func (*Notice) Wrap ¶
Wrap sets base error with provided one.
Example ¶
package main
import (
"errors"
"fmt"
"github.com/ctx42/testing/pkg/notice"
)
func main() {
ErrMy := errors.New("my error")
msg := notice.New("expected values to be equal").
Want("%s", "abc").
Have("%s", "xyz").
Wrap(ErrMy)
is := errors.Is(msg, ErrMy)
fmt.Println(is)
}
Output: true