Skip to content

Commit

Permalink
for MakeSetFromSlice
Browse files Browse the repository at this point in the history
  • Loading branch information
hyorigo committed Apr 1, 2024
1 parent d1161c5 commit 867bafa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions convert/conv.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ func FromDict(m *starlark.Dict) map[interface{}]interface{} {
}

// MakeSet makes a Set from the given map. The acceptable keys the same as ToValue.
// For nil input, it returns an empty Set.
func MakeSet(s map[interface{}]bool) (*starlark.Set, error) {
set := starlark.Set{}
for k := range s {
Expand All @@ -345,6 +346,7 @@ func MakeSet(s map[interface{}]bool) (*starlark.Set, error) {
}

// MakeSetFromSlice makes a Set from the given slice. The acceptable keys the same as ToValue.
// For nil input, it returns an empty Set.
func MakeSetFromSlice(s []interface{}) (*starlark.Set, error) {
set := starlark.Set{}
for i := range s {
Expand Down
29 changes: 28 additions & 1 deletion convert/conv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ func TestMakeTuple(t *testing.T) {
if err != nil {
t.Errorf("unexpected error 1: %v", err)
return
} else if tuple1 == nil {
t.Errorf("expected tuple1 to be non-nil")
return
}
tuple2, err := MakeTuple([]interface{}{"a", 1, true, 0.1})
if err != nil {
Expand Down Expand Up @@ -104,6 +107,9 @@ func TestMakeList(t *testing.T) {
if err != nil {
t.Errorf("unexpected error 1: %v", err)
return
} else if list1 == nil {
t.Errorf("expected list1 to be non-nil")
return
}
list2, err := MakeList([]interface{}{"a", 1, true, 0.1})
if err != nil {
Expand Down Expand Up @@ -149,9 +155,19 @@ t2d = type(list_has[3])
}

func TestMakeSet(t *testing.T) {
if _, err := MakeSet(nil); err != nil {
if s0, err := MakeSet(map[interface{}]bool{}); err != nil {
t.Errorf("unexpected error 0: %v", err)
return
} else if s0 == nil {
t.Errorf("expected s0 to be non-nil")
return
}
if s1, err := MakeSet(nil); err != nil {
t.Errorf("unexpected error 1: %v", err)
return
} else if s1 == nil {
t.Errorf("expected s1 to be non-nil")
return
}
if _, err := MakeSet(map[interface{}]bool{"a": true, 1: true, true: true, 0.1: true}); err != nil {
t.Errorf("unexpected error 2: %v", err)
Expand All @@ -164,10 +180,21 @@ func TestMakeSet(t *testing.T) {
}

func TestMakeSetFromSlice(t *testing.T) {
set0, err := MakeSetFromSlice([]interface{}{})
if err != nil {
t.Errorf("unexpected error 0: %v", err)
return
} else if set0 == nil {
t.Errorf("expected set0 to be non-nil")
return
}
set1, err := MakeSetFromSlice(nil)
if err != nil {
t.Errorf("unexpected error 1: %v", err)
return
} else if set1 == nil {
t.Errorf("expected set1 to be non-nil")
return
}
set2, err := MakeSetFromSlice([]interface{}{"a", 1, true, 0.1})
if err != nil {
Expand Down

0 comments on commit 867bafa

Please sign in to comment.