fix(forge): make stringers nil-safe
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
7e8340a3d4
commit
edbf3f7088
4 changed files with 60 additions and 0 deletions
|
|
@ -165,6 +165,9 @@ func (c *Client) HTTPClient() *http.Client {
|
|||
//
|
||||
// s := client.String()
|
||||
func (c *Client) String() string {
|
||||
if c == nil {
|
||||
return "forge.Client{<nil>}"
|
||||
}
|
||||
tokenState := "unset"
|
||||
if c.HasToken() {
|
||||
tokenState = "set"
|
||||
|
|
|
|||
6
forge.go
6
forge.go
|
|
@ -117,6 +117,12 @@ func (f *Forge) HasToken() bool { return f.client.HasToken() }
|
|||
//
|
||||
// s := f.String()
|
||||
func (f *Forge) String() string {
|
||||
if f == nil {
|
||||
return "forge.Forge{<nil>}"
|
||||
}
|
||||
if f.client == nil {
|
||||
return "forge.Forge{client=<nil>}"
|
||||
}
|
||||
return "forge.Forge{" + f.client.String() + "}"
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -27,6 +27,9 @@ type Resource[T any, C any, U any] struct {
|
|||
//
|
||||
// s := res.String()
|
||||
func (r *Resource[T, C, U]) String() string {
|
||||
if r == nil {
|
||||
return "forge.Resource{<nil>}"
|
||||
}
|
||||
return core.Concat(
|
||||
"forge.Resource{path=",
|
||||
strconv.Quote(r.path),
|
||||
|
|
|
|||
48
stringer_nil_test.go
Normal file
48
stringer_nil_test.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package forge
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestClient_String_NilSafe(t *testing.T) {
|
||||
var c *Client
|
||||
want := "forge.Client{<nil>}"
|
||||
if got := c.String(); got != want {
|
||||
t.Fatalf("got String()=%q, want %q", got, want)
|
||||
}
|
||||
if got := fmt.Sprint(c); got != want {
|
||||
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
|
||||
}
|
||||
if got := fmt.Sprintf("%#v", c); got != want {
|
||||
t.Fatalf("got GoString=%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestForge_String_NilSafe(t *testing.T) {
|
||||
var f *Forge
|
||||
want := "forge.Forge{<nil>}"
|
||||
if got := f.String(); got != want {
|
||||
t.Fatalf("got String()=%q, want %q", got, want)
|
||||
}
|
||||
if got := fmt.Sprint(f); got != want {
|
||||
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
|
||||
}
|
||||
if got := fmt.Sprintf("%#v", f); got != want {
|
||||
t.Fatalf("got GoString=%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResource_String_NilSafe(t *testing.T) {
|
||||
var r *Resource[int, struct{}, struct{}]
|
||||
want := "forge.Resource{<nil>}"
|
||||
if got := r.String(); got != want {
|
||||
t.Fatalf("got String()=%q, want %q", got, want)
|
||||
}
|
||||
if got := fmt.Sprint(r); got != want {
|
||||
t.Fatalf("got fmt.Sprint=%q, want %q", got, want)
|
||||
}
|
||||
if got := fmt.Sprintf("%#v", r); got != want {
|
||||
t.Fatalf("got GoString=%q, want %q", got, want)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue