diff --git a/types/list.go b/types/list.go index 84325aa..826b46d 100644 --- a/types/list.go +++ b/types/list.go @@ -4,7 +4,10 @@ package types -import "fmt" +import ( + "fmt" + "strings" +) // ListAppender is implemented by any value that exhibits a list-like // behaviour, allowing arbitrary values to be appended. @@ -59,3 +62,19 @@ func (*List) Call(args ...interface{}) (interface{}, error) { } return nil, fmt.Errorf("List: invalid arguments: %#v", args) } + +func (l *List) String() string { + if l == nil { + return "nil" + } + o := new(strings.Builder) + o.WriteString("[") + for i, v := range *l { + if i > 0 { + o.WriteString(", ") + } + fmt.Fprintf(o, "%v", v) + } + o.WriteString("]") + return o.String() +} diff --git a/types/list_test.go b/types/list_test.go index 20a1e9d..3bde2dc 100644 --- a/types/list_test.go +++ b/types/list_test.go @@ -1,6 +1,12 @@ +// Copyright 2023 NLP Odyssey Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + package types import ( + "bytes" + "fmt" "testing" ) @@ -15,3 +21,20 @@ func TestCall(t *testing.T) { t.Errorf("expected %v, actual: %v", expected, actual) } } + +func TestListStringer(t *testing.T) { + pylist := func(sli ...interface{}) *List { + return NewListFromSlice(sli) + } + + lst := pylist(pylist(), pylist(1), pylist(2, 3), pylist(pylist(4, 5), 6)) + + buf := new(bytes.Buffer) + fmt.Fprintf(buf, "%v", lst) + got := buf.String() + want := "[[], [1], [2, 3], [[4, 5], 6]]" + + if got != want { + t.Fatalf("got= %q\nwant=%q", got, want) + } +}