Skip to content

Commit

Permalink
ut
Browse files Browse the repository at this point in the history
  • Loading branch information
guozhi.li committed Jan 18, 2024
1 parent 1b320d4 commit 89eae01
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions pkg/set/stringset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ package set

import (
"fmt"
"reflect"
"sort"
"strings"
"testing"
)
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit 89eae01

Please sign in to comment.