|
| 1 | +package collection |
| 2 | + |
| 3 | +import ( |
| 4 | + "iter" |
| 5 | + "slices" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + |
| 10 | + "github.com/ARM-software/golang-utils/utils/commonerrors" |
| 11 | +) |
| 12 | + |
| 13 | +func TestInSequence(t *testing.T) { |
| 14 | + values := []string{"alpha", "Beta", "gamma"} |
| 15 | + |
| 16 | + assert.True(t, InSequence(slices.Values(values), " beta ", StringCleanCaseInsensitiveMatch)) |
| 17 | + assert.False(t, InSequence(slices.Values(values), "beta", StringCleanMatch)) |
| 18 | + assert.False(t, InSequence(slices.Values(values), "delta", StringCleanCaseInsensitiveMatch)) |
| 19 | + |
| 20 | + var nilSeq iter.Seq[string] |
| 21 | + assert.False(t, InSequence(nilSeq, "alpha", StringMatch)) |
| 22 | + |
| 23 | + errMatch := func(pattern, value string) (bool, error) { |
| 24 | + return true, commonerrors.ErrUnexpected |
| 25 | + } |
| 26 | + assert.False(t, InSequence(slices.Values(values), "alpha", errMatch)) |
| 27 | +} |
| 28 | + |
| 29 | +func TestInSequenceRef(t *testing.T) { |
| 30 | + values := []int{1, 2, 3} |
| 31 | + target := 2 |
| 32 | + |
| 33 | + assert.True(t, InSequenceRef(slices.Values(values), &target, StrictRefMatch)) |
| 34 | + |
| 35 | + missing := 4 |
| 36 | + assert.False(t, InSequenceRef(slices.Values(values), &missing, StrictRefMatch)) |
| 37 | + assert.False(t, InSequenceRef(slices.Values(values), &target, func(a, b *int) (bool, error) { |
| 38 | + return true, commonerrors.ErrUnexpected |
| 39 | + })) |
| 40 | + |
| 41 | + var nilSeq iter.Seq[int] |
| 42 | + assert.False(t, InSequenceRef(nilSeq, &target, StrictRefMatch)) |
| 43 | +} |
| 44 | + |
| 45 | +func TestIn(t *testing.T) { |
| 46 | + values := []string{"alpha", "beta", "gamma"} |
| 47 | + |
| 48 | + assert.True(t, In(values, "^be", StringRegexMatch)) |
| 49 | + assert.False(t, In(values, "^de", StringRegexMatch)) |
| 50 | + assert.True(t, In(values, "BETA", StringCaseInsensitiveMatch)) |
| 51 | + assert.True(t, In(values, ".+ta.*", StringRegexMatch)) |
| 52 | +} |
| 53 | + |
| 54 | +func TestInRef(t *testing.T) { |
| 55 | + values := []string{"alpha", "beta", "gamma"} |
| 56 | + target := "beta" |
| 57 | + |
| 58 | + assert.True(t, InRef(values, &target, StrictRefMatch)) |
| 59 | + |
| 60 | + missing := "delta" |
| 61 | + assert.False(t, InRef(values, &missing, StrictRefMatch)) |
| 62 | + assert.False(t, InRef(values, &target, func(a, b *string) (bool, error) { |
| 63 | + return true, commonerrors.ErrUnexpected |
| 64 | + })) |
| 65 | +} |
0 commit comments