From 867bafa9394bd158637e4d07228e907fc992c7d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hyori=20=ED=9A=A8=EB=A6=AC?= Date: Mon, 1 Apr 2024 19:24:05 +0800 Subject: [PATCH] for MakeSetFromSlice --- convert/conv.go | 2 ++ convert/conv_test.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/convert/conv.go b/convert/conv.go index 80c59d1..297a269 100644 --- a/convert/conv.go +++ b/convert/conv.go @@ -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 { @@ -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 { diff --git a/convert/conv_test.go b/convert/conv_test.go index 6c24fab..6366fc1 100644 --- a/convert/conv_test.go +++ b/convert/conv_test.go @@ -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 { @@ -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 { @@ -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) @@ -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 {