-
-
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.
Merge pull request #38 from hovsep/v.0.1.0
V.0.1.0
- Loading branch information
Showing
8 changed files
with
196 additions
and
275 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cycle | ||
|
||
// Collection contains the results of several activation cycles | ||
type Collection []*Cycle | ||
|
||
// NewCollection creates a collection | ||
func NewCollection() Collection { | ||
return make(Collection, 0) | ||
} | ||
|
||
// Add adds cycle results to existing collection | ||
func (collection Collection) Add(newCycleResults ...*Cycle) Collection { | ||
for _, cycleResult := range newCycleResults { | ||
collection = append(collection, cycleResult) | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cycle | ||
|
||
import ( | ||
"github.com/hovsep/fmesh/component" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestNewCollection(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want Collection | ||
}{ | ||
{ | ||
name: "happy path", | ||
want: Collection{}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := NewCollection(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("NewCollection() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCollection_Add(t *testing.T) { | ||
type args struct { | ||
cycleResults []*Cycle | ||
} | ||
tests := []struct { | ||
name string | ||
cycleResults Collection | ||
args args | ||
want Collection | ||
}{ | ||
{ | ||
name: "happy path", | ||
cycleResults: NewCollection(), | ||
args: args{ | ||
cycleResults: []*Cycle{ | ||
New().WithActivationResults(component.NewActivationResult("c1").SetActivated(false)), | ||
New().WithActivationResults(component.NewActivationResult("c1").SetActivated(true)), | ||
}, | ||
}, | ||
want: Collection{ | ||
{ | ||
activationResults: component.ActivationResultCollection{ | ||
"c1": component.NewActivationResult("c1").SetActivated(false), | ||
}, | ||
}, | ||
{ | ||
activationResults: component.ActivationResultCollection{ | ||
"c1": component.NewActivationResult("c1").SetActivated(true), | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := tt.cycleResults.Add(tt.args.cycleResults...); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("Add() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,45 @@ | ||
package cycle | ||
|
||
import ( | ||
"github.com/hovsep/fmesh/component" | ||
"sync" | ||
) | ||
|
||
// Cycle contains the info about given activation cycle | ||
type Cycle struct { | ||
sync.Mutex | ||
activationResults component.ActivationResultCollection | ||
} | ||
|
||
// New creates a new cycle | ||
func New() *Cycle { | ||
return &Cycle{ | ||
activationResults: make(component.ActivationResultCollection), | ||
} | ||
} | ||
|
||
// ActivationResults getter | ||
func (cycle *Cycle) ActivationResults() component.ActivationResultCollection { | ||
return cycle.activationResults | ||
} | ||
|
||
// HasErrors tells whether the cycle is ended wih activation errors (at lease one component returned an error) | ||
func (cycle *Cycle) HasErrors() bool { | ||
return cycle.ActivationResults().HasErrors() | ||
} | ||
|
||
// HasPanics tells whether the cycle is ended wih panic(at lease one component panicked) | ||
func (cycle *Cycle) HasPanics() bool { | ||
return cycle.ActivationResults().HasPanics() | ||
} | ||
|
||
// HasActivatedComponents tells when at least one component in the cycle has activated | ||
func (cycle *Cycle) HasActivatedComponents() bool { | ||
return cycle.ActivationResults().HasActivatedComponents() | ||
} | ||
|
||
// WithActivationResults adds multiple activation results | ||
func (cycle *Cycle) WithActivationResults(activationResults ...*component.ActivationResult) *Cycle { | ||
cycle.activationResults = cycle.ActivationResults().Add(activationResults...) | ||
return cycle | ||
} |
Oops, something went wrong.