Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

types: add Stringer implementation for List and Tuple #11

Merged
merged 4 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ 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.15
go-version: 1.17
- name: Get dependencies
run: go get -v -t -d ./...
- 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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

module github.com/nlpodyssey/gopickle

go 1.15
go 1.17

require golang.org/x/text v0.14.0
21 changes: 20 additions & 1 deletion types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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()
}
23 changes: 23 additions & 0 deletions types/list_test.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand All @@ -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)
}
}
24 changes: 24 additions & 0 deletions types/tuple.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

package types

import (
"fmt"
"strings"
)

type Tuple []interface{}

func NewTupleFromSlice(slice []interface{}) *Tuple {
Expand All @@ -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()
}
28 changes: 28 additions & 0 deletions types/tuple_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}