-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Component: add activation result collection type
- Loading branch information
Showing
11 changed files
with
655 additions
and
316 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package component | ||
|
||
// ActivationResultCollection is a collection | ||
type ActivationResultCollection map[string]*ActivationResult | ||
|
||
// NewActivationResultCollection creates empty collection | ||
func NewActivationResultCollection() ActivationResultCollection { | ||
return make(ActivationResultCollection) | ||
} | ||
|
||
// Add adds multiple activation results | ||
func (collection ActivationResultCollection) Add(activationResults ...*ActivationResult) ActivationResultCollection { | ||
for _, activationResult := range activationResults { | ||
collection[activationResult.ComponentName()] = activationResult | ||
} | ||
return collection | ||
} | ||
|
||
// HasErrors tells whether the collection contains at least one activation result with error and respective code | ||
func (collection ActivationResultCollection) HasErrors() bool { | ||
for _, ar := range collection { | ||
if ar.HasError() { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// HasPanics tells whether the collection contains at least one activation result with panic and respective code | ||
func (collection ActivationResultCollection) HasPanics() bool { | ||
for _, ar := range collection { | ||
if ar.HasPanic() { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
// HasActivatedComponents tells when at least one component in the cycle has activated | ||
func (collection ActivationResultCollection) HasActivatedComponents() bool { | ||
for _, ar := range collection { | ||
if ar.Activated() { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package component | ||
|
||
import ( | ||
"errors" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestActivationResultCollection_Add(t *testing.T) { | ||
type args struct { | ||
activationResults []*ActivationResult | ||
} | ||
tests := []struct { | ||
name string | ||
collection ActivationResultCollection | ||
args args | ||
assertions func(t *testing.T, collection ActivationResultCollection) | ||
}{ | ||
{ | ||
name: "adding nothing to empty collection", | ||
collection: NewActivationResultCollection(), | ||
args: args{ | ||
activationResults: nil, | ||
}, | ||
assertions: func(t *testing.T, collection ActivationResultCollection) { | ||
assert.Len(t, collection, 0) | ||
assert.False(t, collection.HasErrors()) | ||
assert.False(t, collection.HasPanics()) | ||
assert.False(t, collection.HasActivatedComponents()) | ||
}, | ||
}, | ||
{ | ||
name: "adding to empty collection", | ||
collection: NewActivationResultCollection(), | ||
args: args{ | ||
activationResults: []*ActivationResult{ | ||
NewComponent("c1").newActivationResultOK(), | ||
NewComponent("c2").newActivationCodeReturnedError(errors.New("oops")), | ||
}, | ||
}, | ||
assertions: func(t *testing.T, collection ActivationResultCollection) { | ||
assert.Len(t, collection, 2) | ||
assert.True(t, collection.HasActivatedComponents()) | ||
assert.True(t, collection.HasErrors()) | ||
assert.False(t, collection.HasPanics()) | ||
}, | ||
}, | ||
{ | ||
name: "adding to existing collection", | ||
collection: NewActivationResultCollection().Add( | ||
NewComponent("c1").newActivationResultOK(), | ||
NewComponent("c2").newActivationResultOK(), | ||
), | ||
args: args{ | ||
activationResults: []*ActivationResult{ | ||
NewComponent("c4").newActivationCodeNoInput(), | ||
NewComponent("c5").newActivationCodePanicked(errors.New("panic")), | ||
}, | ||
}, | ||
assertions: func(t *testing.T, collection ActivationResultCollection) { | ||
assert.Len(t, collection, 4) | ||
assert.True(t, collection.HasPanics()) | ||
assert.False(t, collection.HasErrors()) | ||
assert.True(t, collection.HasActivatedComponents()) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.collection.Add(tt.args.activationResults...) | ||
if tt.assertions != nil { | ||
tt.assertions(t, tt.collection) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package component | ||
|
||
import ( | ||
"github.com/hovsep/fmesh/port" | ||
"github.com/stretchr/testify/assert" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestCollection_ByName(t *testing.T) { | ||
type args struct { | ||
name string | ||
} | ||
tests := []struct { | ||
name string | ||
components Collection | ||
args args | ||
want *Component | ||
}{ | ||
{ | ||
name: "component found", | ||
components: NewComponentCollection().Add(NewComponent("c1"), NewComponent("c2")), | ||
args: args{ | ||
name: "c2", | ||
}, | ||
want: &Component{ | ||
name: "c2", | ||
description: "", | ||
inputs: port.Collection{}, | ||
outputs: port.Collection{}, | ||
f: nil, | ||
}, | ||
}, | ||
{ | ||
name: "component not found", | ||
components: NewComponentCollection().Add(NewComponent("c1"), NewComponent("c2")), | ||
args: args{ | ||
name: "c3", | ||
}, | ||
want: nil, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.components.ByName(tt.args.name); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ByName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCollection_Add(t *testing.T) { | ||
type args struct { | ||
components []*Component | ||
} | ||
tests := []struct { | ||
name string | ||
collection Collection | ||
args args | ||
assertions func(t *testing.T, collection Collection) | ||
}{ | ||
{ | ||
name: "adding nothing to empty collection", | ||
collection: NewComponentCollection(), | ||
args: args{ | ||
components: nil, | ||
}, | ||
assertions: func(t *testing.T, collection Collection) { | ||
assert.Len(t, collection, 0) | ||
}, | ||
}, | ||
{ | ||
name: "adding to empty collection", | ||
collection: NewComponentCollection(), | ||
args: args{ | ||
components: []*Component{NewComponent("c1"), NewComponent("c2")}, | ||
}, | ||
assertions: func(t *testing.T, collection Collection) { | ||
assert.Len(t, collection, 2) | ||
assert.NotNil(t, collection.ByName("c1")) | ||
assert.NotNil(t, collection.ByName("c2")) | ||
assert.Nil(t, collection.ByName("c999")) | ||
}, | ||
}, | ||
{ | ||
name: "adding to existing collection", | ||
collection: NewComponentCollection().Add(NewComponent("c1"), NewComponent("c2")), | ||
args: args{ | ||
components: []*Component{NewComponent("c3"), NewComponent("c4")}, | ||
}, | ||
assertions: func(t *testing.T, collection Collection) { | ||
assert.Len(t, collection, 4) | ||
assert.NotNil(t, collection.ByName("c1")) | ||
assert.NotNil(t, collection.ByName("c2")) | ||
assert.NotNil(t, collection.ByName("c3")) | ||
assert.NotNil(t, collection.ByName("c4")) | ||
assert.Nil(t, collection.ByName("c999")) | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.collection.Add(tt.args.components...) | ||
if tt.assertions != nil { | ||
tt.assertions(t, tt.collection) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.