Skip to content

Commit

Permalink
Port: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hovsep committed Oct 3, 2024
1 parent e4b665b commit 9356540
Show file tree
Hide file tree
Showing 12 changed files with 470 additions and 132 deletions.
2 changes: 1 addition & 1 deletion component/activation_result_collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestActivationResultCollection_Add(t *testing.T) {
},
},
{
name: "adding to existing collection",
name: "adding to non-empty collection",
collection: NewActivationResultCollection().Add(
New("c1").newActivationResultOK(),
New("c2").newActivationResultOK(),
Expand Down
2 changes: 1 addition & 1 deletion component/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestCollection_Add(t *testing.T) {
},
},
{
name: "adding to existing collection",
name: "adding to non-empty collection",
collection: NewComponentCollection().Add(New("c1"), New("c2")),
args: args{
components: []*Component{New("c3"), New("c4")},
Expand Down
8 changes: 4 additions & 4 deletions component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ func (c *Component) WithDescription(description string) *Component {

// WithInputs ads input ports
func (c *Component) WithInputs(portNames ...string) *Component {
c.inputs = c.Inputs().Add(port.NewGroup(portNames...)...)
c.inputs = c.Inputs().With(port.NewGroup(portNames...)...)
return c
}

// WithOutputs adds output ports
func (c *Component) WithOutputs(portNames ...string) *Component {
c.outputs = c.Outputs().Add(port.NewGroup(portNames...)...)
c.outputs = c.Outputs().With(port.NewGroup(portNames...)...)
return c
}

// WithInputsIndexed creates multiple prefixed ports
func (c *Component) WithInputsIndexed(prefix string, startIndex int, endIndex int) *Component {
c.inputs = c.Inputs().AddIndexed(prefix, startIndex, endIndex)
c.inputs = c.Inputs().WithIndexed(prefix, startIndex, endIndex)
return c
}

// WithOutputsIndexed creates multiple prefixed ports
func (c *Component) WithOutputsIndexed(prefix string, startIndex int, endIndex int) *Component {
c.outputs = c.Outputs().AddIndexed(prefix, startIndex, endIndex)
c.outputs = c.Outputs().WithIndexed(prefix, startIndex, endIndex)
return c
}

Expand Down
10 changes: 5 additions & 5 deletions component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestNewComponent(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, New(tt.args.name), "New(%v)", tt.args.name)
assert.Equal(t, tt.want, New(tt.args.name))
})
}
}
Expand Down Expand Up @@ -233,10 +233,10 @@ func TestComponent_WithActivationFunc(t *testing.T) {
componentAfter := tt.component.WithActivationFunc(tt.args.f)

//Compare activation functions by they result and error
testInputs1 := port.NewCollection().Add(port.NewGroup("in1", "in2")...)
testInputs2 := port.NewCollection().Add(port.NewGroup("in1", "in2")...)
testOutputs1 := port.NewCollection().Add(port.NewGroup("out1", "out2")...)
testOutputs2 := port.NewCollection().Add(port.NewGroup("out1", "out2")...)
testInputs1 := port.NewCollection().With(port.NewGroup("in1", "in2")...)
testInputs2 := port.NewCollection().With(port.NewGroup("in1", "in2")...)
testOutputs1 := port.NewCollection().With(port.NewGroup("out1", "out2")...)
testOutputs2 := port.NewCollection().With(port.NewGroup("out1", "out2")...)
err1 := componentAfter.f(testInputs1, testOutputs1)
err2 := tt.args.f(testInputs2, testOutputs2)
assert.Equal(t, err1, err2)
Expand Down
2 changes: 1 addition & 1 deletion cycle/cycle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestCycle_WithActivationResults(t *testing.T) {
},
},
{
name: "adding to existing collection",
name: "adding to non-empty collection",
cycleResult: New().WithActivationResults(
component.NewActivationResult("c1").
SetActivated(false).
Expand Down
23 changes: 10 additions & 13 deletions port/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func (collection Collection) PutSignals(signals ...*signal.Signal) {
}
}

// WithSignals adds signals to every port in collection and returns the collection
func (collection Collection) WithSignals(signals ...*signal.Signal) Collection {
// withSignals adds signals to every port in collection and returns the collection
func (collection Collection) withSignals(signals ...*signal.Signal) Collection {
collection.PutSignals(signals...)
return collection
}
Expand All @@ -79,28 +79,25 @@ func (collection Collection) Flush() {
}
}

// PipeTo creates pipes from each port in collection
func (collection Collection) PipeTo(toPorts ...*Port) {
// PipeTo creates pipes from each port in collection to given destination ports
func (collection Collection) PipeTo(destPorts ...*Port) {
for _, p := range collection {
p.PipeTo(toPorts...)
p.PipeTo(destPorts...)
}
}

// Add adds ports to collection
func (collection Collection) Add(ports ...*Port) Collection {
// With adds ports to collection and returns it
func (collection Collection) With(ports ...*Port) Collection {
for _, port := range ports {
if port == nil {
continue
}
collection[port.Name()] = port
}

return collection
}

// AddIndexed creates ports with names like "o1","o2","o3" and so on
func (collection Collection) AddIndexed(prefix string, startIndex int, endIndex int) Collection {
return collection.Add(NewIndexedGroup(prefix, startIndex, endIndex)...)
// WithIndexed creates ports with names like "o1","o2","o3" and so on
func (collection Collection) WithIndexed(prefix string, startIndex int, endIndex int) Collection {
return collection.With(NewIndexedGroup(prefix, startIndex, endIndex)...)
}

// Signals returns all signals of all ports in the group
Expand Down
Loading

0 comments on commit 9356540

Please sign in to comment.