Skip to content

Commit b843884

Browse files
bschaatsbergenapparentlymart
authored andcommitted
function/stdlib: unit tests for Distinct function
1 parent 0b7ccb8 commit b843884

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

Diff for: cty/function/stdlib/collection_test.go

+130
Original file line numberDiff line numberDiff line change
@@ -2836,3 +2836,133 @@ func TestSlice(t *testing.T) {
28362836
})
28372837
}
28382838
}
2839+
2840+
func TestDistinct(t *testing.T) {
2841+
tests := []struct {
2842+
List cty.Value
2843+
Want cty.Value
2844+
Err string
2845+
}{
2846+
// Empty list (string type)
2847+
{
2848+
cty.ListValEmpty(cty.String),
2849+
cty.ListValEmpty(cty.String),
2850+
"",
2851+
},
2852+
// Empty list (number type)
2853+
{
2854+
cty.ListValEmpty(cty.Number),
2855+
cty.ListValEmpty(cty.Number),
2856+
"",
2857+
},
2858+
// List with single element
2859+
{
2860+
cty.ListVal([]cty.Value{
2861+
cty.StringVal("single"),
2862+
}),
2863+
cty.ListVal([]cty.Value{
2864+
cty.StringVal("single"),
2865+
}),
2866+
"",
2867+
},
2868+
// List where all elements are identical
2869+
{
2870+
cty.ListVal([]cty.Value{
2871+
cty.NumberIntVal(42),
2872+
cty.NumberIntVal(42),
2873+
cty.NumberIntVal(42),
2874+
}),
2875+
cty.ListVal([]cty.Value{
2876+
cty.NumberIntVal(42),
2877+
}),
2878+
"",
2879+
},
2880+
// List that is already distinct
2881+
{
2882+
cty.ListVal([]cty.Value{
2883+
cty.StringVal("a"),
2884+
cty.StringVal("b"),
2885+
cty.StringVal("c"),
2886+
}),
2887+
cty.ListVal([]cty.Value{
2888+
cty.StringVal("a"),
2889+
cty.StringVal("b"),
2890+
cty.StringVal("c"),
2891+
}),
2892+
"",
2893+
},
2894+
// List with nested lists
2895+
{
2896+
cty.ListVal([]cty.Value{
2897+
cty.ListVal([]cty.Value{
2898+
cty.StringVal("a"),
2899+
cty.StringVal("a"),
2900+
}),
2901+
cty.ListVal([]cty.Value{
2902+
cty.StringVal("b"),
2903+
}),
2904+
cty.ListVal([]cty.Value{
2905+
cty.StringVal("a"),
2906+
cty.StringVal("a"),
2907+
}),
2908+
}),
2909+
cty.ListVal([]cty.Value{
2910+
cty.ListVal([]cty.Value{
2911+
cty.StringVal("a"),
2912+
cty.StringVal("a"),
2913+
}),
2914+
cty.ListVal([]cty.Value{
2915+
cty.StringVal("b"),
2916+
}),
2917+
}),
2918+
"",
2919+
},
2920+
// List with unknown values
2921+
{
2922+
cty.ListVal([]cty.Value{
2923+
cty.UnknownVal(cty.String),
2924+
cty.StringVal("a"),
2925+
cty.StringVal("b"),
2926+
cty.UnknownVal(cty.String),
2927+
}),
2928+
cty.UnknownVal(cty.List(cty.String)).RefineNotNull(),
2929+
"",
2930+
},
2931+
// List with null values
2932+
{
2933+
cty.ListVal([]cty.Value{
2934+
cty.NullVal(cty.String),
2935+
cty.StringVal("a"),
2936+
cty.NullVal(cty.String),
2937+
cty.StringVal("b"),
2938+
}),
2939+
cty.ListVal([]cty.Value{
2940+
cty.NullVal(cty.String),
2941+
cty.StringVal("a"),
2942+
cty.StringVal("b"),
2943+
}),
2944+
"",
2945+
},
2946+
}
2947+
2948+
for _, test := range tests {
2949+
t.Run(fmt.Sprintf("Distinct(%#v)", test.List), func(t *testing.T) {
2950+
got, err := Distinct(test.List)
2951+
if test.Err != "" {
2952+
if err == nil {
2953+
t.Fatal("succeeded; want error")
2954+
}
2955+
if got, want := err.Error(), test.Err; got != want {
2956+
t.Fatalf("wrong error\ngot: %s\nwant: %s", got, want)
2957+
}
2958+
return
2959+
} else if err != nil {
2960+
t.Fatalf("unexpected error: %s", err)
2961+
}
2962+
2963+
if !got.RawEquals(test.Want) {
2964+
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
2965+
}
2966+
})
2967+
}
2968+
}

0 commit comments

Comments
 (0)