diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 0bda154..eb8eb55 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -8,8 +8,8 @@ jobs: name: Test runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - uses: actions/setup-go@v1 + - uses: actions/checkout@v3 + - uses: actions/setup-go@v4 with: go-version: 1.17 - name: Get dependencies @@ -17,6 +17,6 @@ jobs: - name: Run tests and generate coverage report run: go test -race -coverprofile cover.out -covermode atomic ./... - name: Upload coverage to Codecov - uses: codecov/codecov-action@v1.0.7 + uses: codecov/codecov-action@v3 with: file: ./cover.out 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) + } +} diff --git a/types/tuple.go b/types/tuple.go index be90d37..a76cfd3 100644 --- a/types/tuple.go +++ b/types/tuple.go @@ -4,6 +4,11 @@ package types +import ( + "fmt" + "strings" +) + type Tuple []interface{} func NewTupleFromSlice(slice []interface{}) *Tuple { @@ -18,3 +23,22 @@ func (t *Tuple) Get(i int) interface{} { func (t *Tuple) Len() int { return len(*t) } + +func (t *Tuple) String() string { + if t == nil { + return "nil" + } + o := new(strings.Builder) + o.WriteString("(") + for i, v := range *t { + if i > 0 { + o.WriteString(", ") + } + fmt.Fprintf(o, "%v", v) + } + if t.Len() == 1 { + o.WriteString(",") + } + o.WriteString(")") + return o.String() +} diff --git a/types/tuple_test.go b/types/tuple_test.go new file mode 100644 index 0000000..34a971c --- /dev/null +++ b/types/tuple_test.go @@ -0,0 +1,28 @@ +// 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" +) + +func TestTupleStringer(t *testing.T) { + tuple := func(sli ...interface{}) *Tuple { + return NewTupleFromSlice(sli) + } + + tup := tuple(tuple(), tuple(1), tuple(2, 3), tuple(tuple(4, 5), 6)) + + buf := new(bytes.Buffer) + fmt.Fprintf(buf, "%v", tup) + got := buf.String() + want := "((), (1,), (2, 3), ((4, 5), 6))" + + if got != want { + t.Fatalf("got= %q\nwant=%q", got, want) + } +}