Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Merge 82b51cb into abe7463
Browse files Browse the repository at this point in the history
  • Loading branch information
rlespinasse committed Oct 8, 2018
2 parents abe7463 + 82b51cb commit 2a2077d
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 64 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
=== Changed

* Expose Data field in the Context.
* An action node can't stop.

== [0.1.0] - 2018-10-05
=== Added
Expand Down
36 changes: 11 additions & 25 deletions computation/computation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,28 @@ func Test_New(t *testing.T) {
}

func Test_Computation_Compute(t *testing.T) {
errorAction, _ := node.NewAction("errorAction", func(c *node.Context) (bool, error) {
return true, errors.New("action error")
errorAction, _ := node.NewAction("errorAction", func(c *node.Context) error {
return errors.New("action error")
})
errorDecision, _ := node.NewDecision("errorDecision", func(c *node.Context) (bool, error) {
return false, errors.New("decision error")
})
writeAction, _ := node.NewAction("writeAction", func(c *node.Context) (bool, error) {
writeAction, _ := node.NewAction("writeAction", func(c *node.Context) error {
c.Store("write_action", "done")
return true, nil
return nil
})
writeAnotherAction, _ := node.NewAction("writeAnotherAction", func(c *node.Context) (bool, error) {
writeAnotherAction, _ := node.NewAction("writeAnotherAction", func(c *node.Context) error {
c.Store("write_another_action", "done")
return true, nil
return nil
})
readAction, _ := node.NewAction("readAction", func(c *node.Context) (bool, error) {
v, ok := c.Read("write_action")
if !ok {
return false, nil
}
readAction, _ := node.NewAction("readAction", func(c *node.Context) error {
v, _ := c.Read("write_action")
c.Store("read_action", fmt.Sprintf("the content of write_action is %v", v))
return true, nil
return nil
})
deleteAnotherAction, _ := node.NewAction("deleteAnotherAction", func(c *node.Context) (bool, error) {
deleteAnotherAction, _ := node.NewAction("deleteAnotherAction", func(c *node.Context) error {
c.Delete("write_another_action")
return true, nil
return nil
})
writeActionKeyIsPresent, _ := node.NewDecision("writeActionKeyIsPresent", func(c *node.Context) (bool, error) {
return c.HaveKey("write_action"), nil
Expand Down Expand Up @@ -134,17 +131,6 @@ func Test_Computation_Compute(t *testing.T) {
writeAction: computestate.Continue(),
},
},
{
name: "Can compute one stop action node system",
givenNodes: []node.Node{
readAction,
},
expectedStatus: true,
expectedContextData: map[string]interface{}{},
expectedReport: map[node.Node]computestate.ComputeState{
readAction: computestate.Stop(),
},
},
{
name: "Can compute 2 action nodes system",
givenNodes: []node.Node{
Expand Down
7 changes: 0 additions & 7 deletions computestate/computestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ func ContinueOnBranch(branch bool) ComputeState {
}
}

// Stop generate a computation state to stop to following nodes
func Stop() ComputeState {
return ComputeState{
Value: statetype.StopState,
}
}

// Skip generate a computation state to specify
// that the Node computation have been skipped
func Skip() ComputeState {
Expand Down
6 changes: 0 additions & 6 deletions computestate/computestate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ func Test_ComputeState_Call(t *testing.T) {
expectedNodeBranch: utils.BoolPointer(true),
expectedString: "'Continue on true'",
},
{
name: "Should generate a stop state",
givenComputeStateCall: func() ComputeState { return Stop() },
expectedState: statetype.StopState,
expectedString: "'Stop'",
},
{
name: "Should generate a skip state",
givenComputeStateCall: func() ComputeState { return Skip() },
Expand Down
8 changes: 4 additions & 4 deletions engine/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func Test_Engine_Compute(t *testing.T) {
keyIsPresent, _ := node.NewDecision("keyIsPresent", func(c *node.Context) (bool, error) {
return c.HaveKey("key"), nil
})
stringAction, _ := node.NewAction("stringAction", func(c *node.Context) (bool, error) {
stringAction, _ := node.NewAction("stringAction", func(c *node.Context) error {
keyValue, _ := c.Read("key")
c.Store("string", fmt.Sprintf("'%+v'", keyValue))
return true, nil
return nil
})
throwedError := errors.New("missing 'key' in context")
throwError, _ := node.NewAction("throwError", func(c *node.Context) (bool, error) {
return false, throwedError
throwError, _ := node.NewAction("throwError", func(c *node.Context) error {
return throwedError
})

ns := system.New()
Expand Down
11 changes: 4 additions & 7 deletions node/actionnode.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// to realize some actions based on Context.
type ActionNode struct {
name string
actionFunc func(*Context) (bool, error)
actionFunc func(*Context) error
}

func (n ActionNode) String() string {
Expand All @@ -19,14 +19,11 @@ func (n ActionNode) String() string {

// Compute run the action function and decide which compute state to return.
func (n *ActionNode) Compute(c *Context) computestate.ComputeState {
state, err := n.actionFunc(c)
err := n.actionFunc(c)
if err != nil {
return computestate.Abort(err)
}
if state {
return computestate.Continue()
}
return computestate.Stop()
return computestate.Continue()
}

// DecideCapability is desactived due to the fact that an action don't take a decision.
Expand All @@ -35,7 +32,7 @@ func (n *ActionNode) DecideCapability() bool {
}

// NewAction create a ActionNode based on a name and a function to realize the needed action.
func NewAction(name string, actionFunc func(*Context) (bool, error)) (*ActionNode, error) {
func NewAction(name string, actionFunc func(*Context) error) (*ActionNode, error) {
if actionFunc == nil {
return nil, errors.New("can't create action node without function")
}
Expand Down
14 changes: 4 additions & 10 deletions node/actionnode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import (
)

var (
continueNode, _ = NewAction("continueNode", func(*Context) (bool, error) { return true, nil })
stopNode, _ = NewAction("stopNode", func(*Context) (bool, error) { return false, nil })
abortNode, _ = NewAction("abortNode", func(*Context) (bool, error) { return false, errors.New("error") })
continueNode, _ = NewAction("continueNode", func(*Context) error { return nil })
abortNode, _ = NewAction("abortNode", func(*Context) error { return errors.New("error") })
)

func Test_NewAction(t *testing.T) {
testCases := []struct {
name string
givenFunc func(*Context) (bool, error)
givenFunc func(*Context) error
expectedError error
}{
{
Expand All @@ -27,7 +26,7 @@ func Test_NewAction(t *testing.T) {
},
{
name: "Can create an action node",
givenFunc: func(*Context) (bool, error) { return true, nil },
givenFunc: func(*Context) error { return nil },
expectedError: nil,
},
}
Expand All @@ -52,11 +51,6 @@ func Test_ActionNode_Compute(t *testing.T) {
givenNode: continueNode,
expectedComputeState: computestate.Continue(),
},
{
name: "Should Stop",
givenNode: stopNode,
expectedComputeState: computestate.Stop(),
},
{
name: "Should Abort",
givenNode: abortNode,
Expand Down
3 changes: 0 additions & 3 deletions statetype/statetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const (
// ContinueState tell that the Node computation authorize
// to compute the following nodes
ContinueState StateType = "Continue"
// StopState tell that the Node computation stop
// the computation of the following nodes
StopState = "Stop"
// SkipState tell that the Node computation is skipped
SkipState = "Skip"
// AbortState tell the Node computation encounter an error
Expand Down
4 changes: 2 additions & 2 deletions system/nodesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var (
someActionNode, _ = node.NewAction("someActionNode", func(*node.Context) (bool, error) { return true, nil })
anotherActionNode, _ = node.NewAction("anotherActionNode", func(*node.Context) (bool, error) { return true, nil })
someActionNode, _ = node.NewAction("someActionNode", func(*node.Context) error { return nil })
anotherActionNode, _ = node.NewAction("anotherActionNode", func(*node.Context) error { return nil })
alwaysTrueDecisionNode, _ = node.NewDecision("alwaysTrueDecisionNode", func(*node.Context) (bool, error) { return true, nil })
)

Expand Down

0 comments on commit 2a2077d

Please sign in to comment.