Skip to content

Commit cc5cdd0

Browse files
authored
Utilize fmt.Stringer interface instead of ToString method (#288)
1 parent 4db71be commit cc5cdd0

File tree

4 files changed

+28
-16
lines changed

4 files changed

+28
-16
lines changed

testutils/go/parse.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ func serialize(v reflect.Value) (string, error) {
157157
case reflect.Ptr: // *TreeNode, *ListNode
158158
switch tpName := v.Type().Elem().Name(); tpName {
159159
case "TreeNode":
160-
return v.Interface().(*TreeNode).ToString(), nil
160+
return v.Interface().(*TreeNode).String(), nil
161161
case "ListNode":
162-
return v.Interface().(*ListNode).ToString(), nil
162+
return v.Interface().(*ListNode).String(), nil
163163
default:
164164
return "", fmt.Errorf("unknown type %s", tpName)
165165
}

testutils/go/predefined.go

+21-6
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,14 @@ func DeserializeListNode(s string) (*ListNode, error) {
3939
return root, nil
4040
}
4141

42-
// ToString returns a string representation of the linked list.
43-
// It panics with ErrInfiniteLoop if a cycle is detected.
42+
// ToString is deprecated, use String()
4443
func (l *ListNode) ToString() string {
44+
return l.String()
45+
}
46+
47+
// String returns a string representation of the linked list.
48+
// It panics with ErrInfiniteLoop if a cycle is detected.
49+
func (l *ListNode) String() string {
4550
seen := make(map[*ListNode]bool, 10)
4651

4752
sb := &strings.Builder{}
@@ -116,9 +121,14 @@ func DeserializeTreeNode(s string) (*TreeNode, error) {
116121
return root, nil
117122
}
118123

119-
// ToString returns a string representation of the binary tree.
120-
// It panics with ErrInfiniteLoop if a cycle is detected.
124+
// ToString is deprecated, use String()
121125
func (t *TreeNode) ToString() string {
126+
return t.String()
127+
}
128+
129+
// String returns a string representation of the binary tree.
130+
// It panics with ErrInfiniteLoop if a cycle is detected.
131+
func (t *TreeNode) String() string {
122132
nodes := []*TreeNode{}
123133
queue := []*TreeNode{t}
124134
seen := make(map[*TreeNode]bool, 10)
@@ -184,9 +194,14 @@ func DeserializeNaryTreeNode(s string) (*NaryTreeNode, error) {
184194
return root.Children[0], nil
185195
}
186196

187-
// ToString returns a string representation of the nary tree.
188-
// It panics with ErrInfiniteLoop if a cycle is detected.
197+
// ToString is deprecated, use String
189198
func (t *NaryTreeNode) ToString() string {
199+
return t.String()
200+
}
201+
202+
// String returns a string representation of the nary tree.
203+
// It panics with ErrInfiniteLoop if a cycle is detected.
204+
func (t *NaryTreeNode) String() string {
190205
nodes := []*NaryTreeNode{}
191206
q := []*NaryTreeNode{{Children: []*NaryTreeNode{t}}}
192207
seen := make(map[*NaryTreeNode]bool, 10)

testutils/go/prefdefined_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func Test_NaryTreeNodeToString(t *testing.T) {
7777
for _, test := range tests {
7878
t.Run(
7979
"", func(t *testing.T) {
80-
assert.Equal(t, test.expected, test.tree.ToString())
80+
assert.Equal(t, test.expected, test.tree.String())
8181
},
8282
)
8383
}
@@ -94,7 +94,7 @@ func Test_DeserializeNaryTree(t *testing.T) {
9494
"", func(t *testing.T) {
9595
tree, err := DeserializeNaryTreeNode(test)
9696
if assert.NoError(t, err) {
97-
assert.Equal(t, test, tree.ToString())
97+
assert.Equal(t, test, tree.String())
9898
}
9999
},
100100
)

testutils/go/serialize_test.go

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package goutils
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -17,17 +18,13 @@ func TestInfiniteLoopDetect(t *testing.T) {
1718
naryTree := &NaryTreeNode{Val: 1}
1819
naryTree.Children = []*NaryTreeNode{{Val: 2, Children: []*NaryTreeNode{naryTree}}}
1920

20-
type toStringer interface {
21-
ToString() string
22-
}
23-
24-
tests := []toStringer{
21+
tests := []fmt.Stringer{
2522
linkedList,
2623
tree,
2724
naryTree,
2825
}
2926

3027
for _, tc := range tests {
31-
assert.PanicsWithValue(t, ErrInfiniteLoop, func() { tc.ToString() })
28+
assert.PanicsWithValue(t, ErrInfiniteLoop, func() { _ = tc.String() })
3229
}
3330
}

0 commit comments

Comments
 (0)