Skip to content

Commit

Permalink
Merge pull request #11 from sbinet-xyz/list-stringer
Browse files Browse the repository at this point in the history
types: add Stringer implementation for List and Tuple
  • Loading branch information
matteo-grella authored Nov 24, 2023
2 parents 3f3e5fa + cd99eb4 commit 2139434
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 4 deletions.
6 changes: 3 additions & 3 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.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
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)
}
}

0 comments on commit 2139434

Please sign in to comment.