Skip to content

Commit

Permalink
update list and dict to work with loadReduce
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Belitz committed Mar 2, 2023
1 parent 81a228d commit 1ad3998
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
12 changes: 12 additions & 0 deletions pickle/pickle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
package pickle

import (
"fmt"
"github.com/nlpodyssey/gopickle/types"
"math/big"
"reflect"
"strings"
"testing"
)
Expand Down Expand Up @@ -637,6 +639,16 @@ func TestByteArrayP5(t *testing.T) {
}
}

func TestFindClass(t *testing.T) {
u := &Unpickler{}
v, _ := u.findClass("builtins", "list")
actual, _ := fmt.Println(reflect.TypeOf(v))
expected, _ := fmt.Println(reflect.TypeOf(&types.List{}))
if actual != expected {
t.Errorf("expected %v, actual: %v", expected, actual)
}
}

// TODO: test BinPersId
// TODO: test Get
// TODO: test BinGet
Expand Down
9 changes: 6 additions & 3 deletions types/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ func (d *Dict) Keys() []interface{} {
}

func (*Dict) Call(args ...interface{}) (interface{}, error) {
if len(args) != 0 {
return args, nil
if len(args) == 0 {
return NewDict(), nil
}
return NewDict(), nil
if len(args) == 1 {
return args[0], nil
}
return nil, fmt.Errorf("Dict: invalid arguments: %#v", args)
}
16 changes: 16 additions & 0 deletions types/dict_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package types

import "testing"

func TestDictCall(t *testing.T) {
d := NewDict()
d.Set("foo", "bar")
args := []interface{}{d}
result, _ := d.Call(args)
resultdict := *result.([]interface{})[0].(*Dict)
actual, _ := resultdict.Get("foo")
expected := "bar"
if actual != expected {
t.Errorf("expected %v, actual: %v", expected, actual)
}
}
11 changes: 8 additions & 3 deletions types/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package types

import "fmt"

// ListAppender is implemented by any value that exhibits a list-like
// behaviour, allowing arbitrary values to be appended.
type ListAppender interface {
Expand Down Expand Up @@ -49,8 +51,11 @@ func (l *List) Len() int {
}

func (*List) Call(args ...interface{}) (interface{}, error) {
if len(args) != 0 {
return args, nil
if len(args) == 0 {
return NewList(), nil
}
if len(args) == 1 {
return args[0], nil
}
return NewList(), nil
return nil, fmt.Errorf("List: invalid arguments: %#v", args)
}
17 changes: 17 additions & 0 deletions types/list_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package types

import (
"testing"
)

func TestCall(t *testing.T) {
list := NewList()
list.Append("foo")
args := []interface{}{list}
result, _ := list.Call(args)
actual := (*result.([]interface{})[0].(*List))[0]
expected := "foo"
if actual != expected {
t.Errorf("expected %v, actual: %v", expected, actual)
}
}

0 comments on commit 1ad3998

Please sign in to comment.