Skip to content

Commit

Permalink
handled feature flags in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ascandone committed Dec 6, 2024
1 parent 6e5d5fb commit 571e01c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 9 deletions.
10 changes: 10 additions & 0 deletions internal/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ func (s *programState) parseVars(varDeclrs []parser.VarDeclaration, rawVars map[
return nil
}

type FeatureFlag = string

const ExperimentalOverdraftFunctionFeatureFlag FeatureFlag = "experimental-overdraft-function"

func RunProgram(
ctx context.Context,
program parser.Program,
Expand All @@ -196,6 +200,10 @@ func RunProgram(
ctx: ctx,
}

if _, ok := featureFlags[ExperimentalOverdraftFunctionFeatureFlag]; ok {
st.OverdraftFunctionFeatureFlag = true
}

err := st.parseVars(program.Vars, vars)
if err != nil {
return nil, err
Expand Down Expand Up @@ -252,6 +260,8 @@ type programState struct {
CachedBalances Balances

CurrentBalanceQuery BalanceQuery

OverdraftFunctionFeatureFlag bool
}

func (st *programState) pushSender(name string, monetary *big.Int) {
Expand Down
9 changes: 9 additions & 0 deletions internal/interpreter/interpreter_error.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,12 @@ type QueryMetadataError struct {
func (e QueryMetadataError) Error() string {
return e.WrappedError.Error()
}

type ExperimentalFeature struct {
parser.Range
FlagName string
}

func (e ExperimentalFeature) Error() string {
return fmt.Sprintf("this feature is experimental. You need the '%s' feature flag to enable it", e.FlagName)
}
51 changes: 42 additions & 9 deletions internal/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,50 @@ func (c *TestCase) setBalance(account string, asset string, amount int64) {
}

func test(t *testing.T, testCase TestCase) {
testWithFeatureFlag(t, testCase, "")
}

// A version of test() which tests code under a feature flag
// if the feature flag is the empty string, it behaves as test()
// otherwise, it tests the program under that feature flag and also tests that
// the same script, without the flag, yields the ExperimentalFeature{} error
func testWithFeatureFlag(t *testing.T, testCase TestCase, flagName string) {
t.Parallel()

prog := testCase.program

require.NotNil(t, prog)

execResult, err := machine.RunProgram(context.Background(), *prog, testCase.vars, machine.StaticStore{
testCase.balances,
testCase.meta,
}, nil)
featureFlags := map[string]struct{}{}
if flagName != "" {
featureFlags[flagName] = struct{}{}

_, err := machine.RunProgram(
context.Background(),
*prog,
testCase.vars,
machine.StaticStore{
testCase.balances,
testCase.meta,
},
nil,
)

require.Equal(t, machine.ExperimentalFeature{
FlagName: flagName,
}, removeRange(err))
}

execResult, err := machine.RunProgram(
context.Background(),
*prog,
testCase.vars,
machine.StaticStore{
testCase.balances,
testCase.meta,
},
featureFlags,
)

expected := testCase.expected
if expected.Error != nil {
Expand Down Expand Up @@ -3266,7 +3300,6 @@ func TestSaveFromAccount(t *testing.T) {
})
}

// new semantics
func TestOverdraftFunctionWhenNegative(t *testing.T) {
script := `
vars { monetary $amt = overdraft(@acc, EUR/2) }
Expand All @@ -3293,7 +3326,7 @@ func TestOverdraftFunctionWhenNegative(t *testing.T) {
},
Error: nil,
}
test(t, tc)
testWithFeatureFlag(t, tc, machine.ExperimentalOverdraftFunctionFeatureFlag)
}

func TestOverdraftFunctionWhenZero(t *testing.T) {
Expand All @@ -3316,7 +3349,7 @@ func TestOverdraftFunctionWhenZero(t *testing.T) {
},
Error: nil,
}
test(t, tc)
testWithFeatureFlag(t, tc, machine.ExperimentalOverdraftFunctionFeatureFlag)
}

func TestOverdraftFunctionWhenPositive(t *testing.T) {
Expand All @@ -3340,7 +3373,7 @@ func TestOverdraftFunctionWhenPositive(t *testing.T) {
},
Error: nil,
}
test(t, tc)
testWithFeatureFlag(t, tc, machine.ExperimentalOverdraftFunctionFeatureFlag)
}

func TestOverdraftFunctionUseCaseRemoveDebt(t *testing.T) {
Expand Down Expand Up @@ -3375,7 +3408,7 @@ func TestOverdraftFunctionUseCaseRemoveDebt(t *testing.T) {
},
Error: nil,
}
test(t, tc)
testWithFeatureFlag(t, tc, machine.ExperimentalOverdraftFunctionFeatureFlag)
}

func TestAddMonetariesSameCurrency(t *testing.T) {
Expand Down

0 comments on commit 571e01c

Please sign in to comment.