From 89eae01f815a1d26e984c2208775b916aa216c5d Mon Sep 17 00:00:00 2001 From: "guozhi.li" Date: Thu, 18 Jan 2024 14:43:27 +0800 Subject: [PATCH] ut --- pkg/set/stringset_test.go | 87 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/pkg/set/stringset_test.go b/pkg/set/stringset_test.go index ec6dfbe63..442c774ad 100644 --- a/pkg/set/stringset_test.go +++ b/pkg/set/stringset_test.go @@ -19,6 +19,8 @@ package set import ( "fmt" + "reflect" + "sort" "strings" "testing" ) @@ -346,3 +348,88 @@ func TestStringSetToSlice(t *testing.T) { } } } + +func TestStringSet_UnmarshalJSON(t *testing.T) { + type args struct { + data []byte + expectResult []string + } + tests := []struct { + name string + set StringSet + args args + wantErr bool + }{ + { + name: "test strings", + set: NewStringSet(), + args: args{ + data: []byte(`["foo","bar"]`), + expectResult: []string{"foo", "bar"}, + }, + wantErr: false, + }, + { + name: "test string", + set: NewStringSet(), + args: args{ + data: []byte(`"foo"`), + expectResult: []string{"foo"}, + }, + wantErr: false, + }, + { + name: "test bools", + set: NewStringSet(), + args: args{ + data: []byte(`[false,true]`), + expectResult: []string{"false", "true"}, + }, + wantErr: false, + }, + { + name: "test bool", + set: NewStringSet(), + args: args{ + data: []byte(`false`), + expectResult: []string{"false"}, + }, + wantErr: false, + }, + { + name: "test ints", + set: NewStringSet(), + args: args{ + data: []byte(`[1,2]`), + expectResult: []string{"1", "2"}, + }, + wantErr: false, + }, + { + name: "test int", + set: NewStringSet(), + args: args{ + data: []byte(`1`), + expectResult: []string{"1"}, + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := tt.set.UnmarshalJSON(tt.args.data); (err != nil) != tt.wantErr { + t.Errorf("UnmarshalJSON() error = %v, wantErr %v", err, tt.wantErr) + } + slice := tt.set.ToSlice() + sort.Slice(slice, func(i, j int) bool { + return slice[i] < slice[j] + }) + sort.Slice(tt.args.expectResult, func(i, j int) bool { + return tt.args.expectResult[i] < tt.args.expectResult[j] + }) + if !reflect.DeepEqual(slice, tt.args.expectResult) { + t.Errorf("StringSet() get %v, want %v", tt.set.ToSlice(), tt.args.expectResult) + } + }) + } +}