diff --git a/testhelpers/ldtestdata/test_data_source_flag.go b/testhelpers/ldtestdata/test_data_source_flag.go index 554e7966..c36cc14c 100644 --- a/testhelpers/ldtestdata/test_data_source_flag.go +++ b/testhelpers/ldtestdata/test_data_source_flag.go @@ -178,7 +178,7 @@ func (f *FlagBuilder) VariationForAllIndex(variationIndex int) *FlagBuilder { // whether targeting is on or off. Any existing targets or rules are removed. func (f *FlagBuilder) ValueForAll(value ldvalue.Value) *FlagBuilder { f.variations = []ldvalue.Value{value} - return f.VariationForAllIndex(0) + return f.VariationForAllIndex(0).OffVariationIndex(0) } // VariationForUser sets the flag to return the specified boolean variation for a specific user key diff --git a/testhelpers/ldtestdata/test_data_source_flag_test.go b/testhelpers/ldtestdata/test_data_source_flag_test.go index d3d42546..b74242f0 100644 --- a/testhelpers/ldtestdata/test_data_source_flag_test.go +++ b/testhelpers/ldtestdata/test_data_source_flag_test.go @@ -2,17 +2,20 @@ package ldtestdata import ( "encoding/json" + "fmt" "testing" "time" "github.com/launchdarkly/go-sdk-common/v3/ldcontext" "github.com/launchdarkly/go-sdk-common/v3/ldvalue" + ldeval "github.com/launchdarkly/go-server-sdk-evaluation/v3" "github.com/launchdarkly/go-server-sdk-evaluation/v3/ldbuilders" "github.com/launchdarkly/go-server-sdk-evaluation/v3/ldmodel" "github.com/launchdarkly/go-server-sdk/v7/subsystems" "github.com/launchdarkly/go-server-sdk/v7/subsystems/ldstoreimpl" m "github.com/launchdarkly/go-test-helpers/v3/matchers" + "github.com/stretchr/testify/assert" ) const ( @@ -283,3 +286,18 @@ func TestRuleConfig(t *testing.T) { )) }) } + +func TestValueForAllReturnsValueWhetherTargetingIsOnOrOff(t *testing.T) { + for _, on := range []bool{true, false} { + t.Run(fmt.Sprintf("on=%t", on), func(t *testing.T) { + td := DataSource() + flag := td.Flag("flagkey").ValueForAll(ldvalue.String("x")).On(on).createFlag(1) + + assert.Equal(t, ldvalue.NewOptionalInt(0), flag.OffVariation) + + result := ldeval.NewEvaluator(nil).Evaluate(&flag, ldcontext.New("user-key"), nil) + assert.Equal(t, ldvalue.String("x"), result.Detail.Value) + assert.Equal(t, ldvalue.NewOptionalInt(0), result.Detail.VariationIndex) + }) + } +} diff --git a/testhelpers/ldtestdatav2/test_data_source_flag.go b/testhelpers/ldtestdatav2/test_data_source_flag.go index f0dd8655..2f8a54ad 100644 --- a/testhelpers/ldtestdatav2/test_data_source_flag.go +++ b/testhelpers/ldtestdatav2/test_data_source_flag.go @@ -178,7 +178,7 @@ func (f *FlagBuilder) VariationForAllIndex(variationIndex int) *FlagBuilder { // whether targeting is on or off. Any existing targets or rules are removed. func (f *FlagBuilder) ValueForAll(value ldvalue.Value) *FlagBuilder { f.variations = []ldvalue.Value{value} - return f.VariationForAllIndex(0) + return f.VariationForAllIndex(0).OffVariationIndex(0) } // VariationForUser sets the flag to return the specified boolean variation for a specific user key diff --git a/testhelpers/ldtestdatav2/test_data_source_flag_test.go b/testhelpers/ldtestdatav2/test_data_source_flag_test.go index 4d394160..44618449 100644 --- a/testhelpers/ldtestdatav2/test_data_source_flag_test.go +++ b/testhelpers/ldtestdatav2/test_data_source_flag_test.go @@ -8,12 +8,14 @@ import ( "github.com/launchdarkly/go-sdk-common/v3/ldcontext" "github.com/launchdarkly/go-sdk-common/v3/ldvalue" + ldeval "github.com/launchdarkly/go-server-sdk-evaluation/v3" "github.com/launchdarkly/go-server-sdk-evaluation/v3/ldbuilders" "github.com/launchdarkly/go-server-sdk-evaluation/v3/ldmodel" "github.com/launchdarkly/go-server-sdk/v7/internal/sharedtest/mocks" "github.com/launchdarkly/go-server-sdk/v7/subsystems" m "github.com/launchdarkly/go-test-helpers/v3/matchers" + "github.com/stretchr/testify/assert" ) const ( @@ -310,3 +312,18 @@ func TestRuleConfigV2(t *testing.T) { )) }) } + +func TestValueForAllReturnsValueWhetherTargetingIsOnOrOff(t *testing.T) { + for _, on := range []bool{true, false} { + t.Run(fmt.Sprintf("on=%t", on), func(t *testing.T) { + td := DataSource() + flag := td.Flag("flagkey").ValueForAll(ldvalue.String("x")).On(on).createFlag(1) + + assert.Equal(t, ldvalue.NewOptionalInt(0), flag.OffVariation) + + result := ldeval.NewEvaluator(nil).Evaluate(&flag, ldcontext.New("user-key"), nil) + assert.Equal(t, ldvalue.String("x"), result.Detail.Value) + assert.Equal(t, ldvalue.NewOptionalInt(0), result.Detail.VariationIndex) + }) + } +}