You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a class structure that can be analoged to this:
package my_package
typeOperandinterface {
performCalculation(val1, val2int) int
}
typeAdditionstruct{}
func (aAddition) performCalculation(val1, val2int) int {
returnval1+val2
}
typeSubtractionstruct{}
func (sSubtraction) performCalculation(val1, val2int) int {
returnval1-val2
}
I would like to implement tests in this way:
Feature: OperationsScenario Outline: additionsGiven an addition operator
When I receive <val1> and <val2>Then I should get <result>Examples:
| val1 | val2 | result | | 2 | 3 | 5 | | 10 | 100 | 110 |Scenario Outline: substractionsGiven a substraction operator
When I receive <val1> and <val2>Then I should get <result>Examples:
| val1 | val2 | result | | 5 | 3 | 2 | | 100 | 10 | 90 |
As you can see my When/Then steps are the same, and I would like to implement them this way to keep them DRY:
package my_package
funccalculate(_ gobdd.StepTest, ctx gobdd.Context, val1, val2int) {
operand:=ctx.GetAs("operand", Operand) // Can't figure this outres:=operand.performCalculation(val1, val2)
ctx.Set("result", res)
}
funccheck(t gobdd.StepTest, ctx gobdd.Context, expectedint) {
result, err:=ctx.GetInt("result")
iferr!=nil {
t.Error(err)
return
}
ifexpected!=result {
t.Errorf("expected: %s, got: %s", expected, result)
}
}
funcTestScenarios(t*testing.T) {
suite:=gobdd.NewSuite(t, gobdd.WithFeaturesPath("*.feature"))
// Add a step to instanciate the correct Operand, depending on the Givensuite.AddStep(`I receive (\d+) and (\d+)`, calculate) // Reuse these stepssuite.AddStep(`I should get (\d+)`, check)
suite.Run()
}
I can't figure out how to get an interface from Context. Is there a way that I'm missing?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have a class structure that can be analoged to this:
I would like to implement tests in this way:
As you can see my When/Then steps are the same, and I would like to implement them this way to keep them DRY:
I can't figure out how to get an interface from Context. Is there a way that I'm missing?
Beta Was this translation helpful? Give feedback.
All reactions