-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
67 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,22 @@ | ||
package jaws | ||
|
||
type Stringer interface { | ||
import "fmt" | ||
|
||
type stringer interface { | ||
String() string | ||
} | ||
|
||
type stringizer struct { | ||
v *any | ||
} | ||
|
||
func (s stringizer) String() string { | ||
if s.v == nil { | ||
return "<nil>" | ||
} | ||
return fmt.Sprint(*s.v) | ||
} | ||
|
||
func Stringer(v *any) stringer { | ||
return stringizer{v} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package jaws | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestStringer(t *testing.T) { | ||
var pnil any | ||
txt := any("text") | ||
num := any(int(123)) | ||
tests := []struct { | ||
name string | ||
arg *any | ||
want string | ||
}{ | ||
{ | ||
name: "nil", | ||
arg: nil, | ||
want: "<nil>", | ||
}, | ||
{ | ||
name: "pointer to nil", | ||
arg: &pnil, | ||
want: "<nil>", | ||
}, | ||
{ | ||
name: "text", | ||
arg: &txt, | ||
want: "text", | ||
}, | ||
{ | ||
name: "num", | ||
arg: &num, | ||
want: "123", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := Stringer(tt.arg).String(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Stringer() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters