From e41d77db0cbf91a6775a5b7ea9adea1ed0e84d7d Mon Sep 17 00:00:00 2001 From: Matthew Keeler Date: Thu, 5 Nov 2026 18:35:22 +0000 Subject: [PATCH] fix: Make TestData ValueForAll return its value when targeting is off Generated-by: termag --- .../ldtestdata/test_data_source_flag.go | 2 +- .../ldtestdata/test_data_source_flag_test.go | 18 ++++++++++++++++++ .../ldtestdatav2/test_data_source_flag.go | 2 +- .../ldtestdatav2/test_data_source_flag_test.go | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/testhelpers/ldtestdata/test_data_source_flag.go b/testhelpers/ldtestdata/test_data_source_flag.go index 554e79660..c36cc14c6 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 d3d42546d..b74242f09 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 f0dd86557..2f8a54adb 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 4d3941601..44618449b 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) + }) + } +}