Skip to content

Commit

Permalink
function/stdlib: unit tests for Distinct function
Browse files Browse the repository at this point in the history
  • Loading branch information
bschaatsbergen authored and apparentlymart committed Oct 13, 2024
1 parent 0b7ccb8 commit b843884
Showing 1 changed file with 130 additions and 0 deletions.
130 changes: 130 additions & 0 deletions cty/function/stdlib/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2836,3 +2836,133 @@ func TestSlice(t *testing.T) {
})
}
}

func TestDistinct(t *testing.T) {
tests := []struct {
List cty.Value
Want cty.Value
Err string
}{
// Empty list (string type)
{
cty.ListValEmpty(cty.String),
cty.ListValEmpty(cty.String),
"",
},
// Empty list (number type)
{
cty.ListValEmpty(cty.Number),
cty.ListValEmpty(cty.Number),
"",
},
// List with single element
{
cty.ListVal([]cty.Value{
cty.StringVal("single"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("single"),
}),
"",
},
// List where all elements are identical
{
cty.ListVal([]cty.Value{
cty.NumberIntVal(42),
cty.NumberIntVal(42),
cty.NumberIntVal(42),
}),
cty.ListVal([]cty.Value{
cty.NumberIntVal(42),
}),
"",
},
// List that is already distinct
{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("b"),
cty.StringVal("c"),
}),
"",
},
// List with nested lists
{
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("b"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("a"),
}),
}),
cty.ListVal([]cty.Value{
cty.ListVal([]cty.Value{
cty.StringVal("a"),
cty.StringVal("a"),
}),
cty.ListVal([]cty.Value{
cty.StringVal("b"),
}),
}),
"",
},
// List with unknown values
{
cty.ListVal([]cty.Value{
cty.UnknownVal(cty.String),
cty.StringVal("a"),
cty.StringVal("b"),
cty.UnknownVal(cty.String),
}),
cty.UnknownVal(cty.List(cty.String)).RefineNotNull(),
"",
},
// List with null values
{
cty.ListVal([]cty.Value{
cty.NullVal(cty.String),
cty.StringVal("a"),
cty.NullVal(cty.String),
cty.StringVal("b"),
}),
cty.ListVal([]cty.Value{
cty.NullVal(cty.String),
cty.StringVal("a"),
cty.StringVal("b"),
}),
"",
},
}

for _, test := range tests {
t.Run(fmt.Sprintf("Distinct(%#v)", test.List), func(t *testing.T) {
got, err := Distinct(test.List)
if test.Err != "" {
if err == nil {
t.Fatal("succeeded; want error")
}
if got, want := err.Error(), test.Err; got != want {
t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want)
}
return
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}

0 comments on commit b843884

Please sign in to comment.