Skip to content

Commit

Permalink
Utilize fmt.Stringer interface instead of ToString method (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-telpis authored Mar 22, 2024
1 parent 4db71be commit cc5cdd0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions testutils/go/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ func serialize(v reflect.Value) (string, error) {
case reflect.Ptr: // *TreeNode, *ListNode
switch tpName := v.Type().Elem().Name(); tpName {
case "TreeNode":
return v.Interface().(*TreeNode).ToString(), nil
return v.Interface().(*TreeNode).String(), nil
case "ListNode":
return v.Interface().(*ListNode).ToString(), nil
return v.Interface().(*ListNode).String(), nil
default:
return "", fmt.Errorf("unknown type %s", tpName)
}
Expand Down
27 changes: 21 additions & 6 deletions testutils/go/predefined.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,14 @@ func DeserializeListNode(s string) (*ListNode, error) {
return root, nil
}

// ToString returns a string representation of the linked list.
// It panics with ErrInfiniteLoop if a cycle is detected.
// ToString is deprecated, use String()
func (l *ListNode) ToString() string {
return l.String()
}

// String returns a string representation of the linked list.
// It panics with ErrInfiniteLoop if a cycle is detected.
func (l *ListNode) String() string {
seen := make(map[*ListNode]bool, 10)

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

// ToString returns a string representation of the binary tree.
// It panics with ErrInfiniteLoop if a cycle is detected.
// ToString is deprecated, use String()
func (t *TreeNode) ToString() string {
return t.String()
}

// String returns a string representation of the binary tree.
// It panics with ErrInfiniteLoop if a cycle is detected.
func (t *TreeNode) String() string {
nodes := []*TreeNode{}
queue := []*TreeNode{t}
seen := make(map[*TreeNode]bool, 10)
Expand Down Expand Up @@ -184,9 +194,14 @@ func DeserializeNaryTreeNode(s string) (*NaryTreeNode, error) {
return root.Children[0], nil
}

// ToString returns a string representation of the nary tree.
// It panics with ErrInfiniteLoop if a cycle is detected.
// ToString is deprecated, use String
func (t *NaryTreeNode) ToString() string {
return t.String()
}

// String returns a string representation of the nary tree.
// It panics with ErrInfiniteLoop if a cycle is detected.
func (t *NaryTreeNode) String() string {
nodes := []*NaryTreeNode{}
q := []*NaryTreeNode{{Children: []*NaryTreeNode{t}}}
seen := make(map[*NaryTreeNode]bool, 10)
Expand Down
4 changes: 2 additions & 2 deletions testutils/go/prefdefined_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func Test_NaryTreeNodeToString(t *testing.T) {
for _, test := range tests {
t.Run(
"", func(t *testing.T) {
assert.Equal(t, test.expected, test.tree.ToString())
assert.Equal(t, test.expected, test.tree.String())
},
)
}
Expand All @@ -94,7 +94,7 @@ func Test_DeserializeNaryTree(t *testing.T) {
"", func(t *testing.T) {
tree, err := DeserializeNaryTreeNode(test)
if assert.NoError(t, err) {
assert.Equal(t, test, tree.ToString())
assert.Equal(t, test, tree.String())
}
},
)
Expand Down
9 changes: 3 additions & 6 deletions testutils/go/serialize_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goutils

import (
"fmt"
"testing"

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

type toStringer interface {
ToString() string
}

tests := []toStringer{
tests := []fmt.Stringer{
linkedList,
tree,
naryTree,
}

for _, tc := range tests {
assert.PanicsWithValue(t, ErrInfiniteLoop, func() { tc.ToString() })
assert.PanicsWithValue(t, ErrInfiniteLoop, func() { _ = tc.String() })
}
}

0 comments on commit cc5cdd0

Please sign in to comment.