From 2cdb18b16f4cbf10e4fb9d12e5576b89e7a18b3c Mon Sep 17 00:00:00 2001 From: hovsep Date: Tue, 17 Sep 2024 04:09:20 +0300 Subject: [PATCH] Bugfix: port collection fixed --- component/component.go | 4 ++-- component/component_test.go | 16 ++++++++-------- port/collection.go | 21 ++++++++++++++------- port/collection_test.go | 12 ++++++------ port/port.go | 4 ++-- port/port_test.go | 22 +++++++--------------- 6 files changed, 39 insertions(+), 40 deletions(-) diff --git a/component/component.go b/component/component.go index 16916c9..400517a 100644 --- a/component/component.go +++ b/component/component.go @@ -34,13 +34,13 @@ func (c *Component) WithDescription(description string) *Component { // WithInputs ads input ports func (c *Component) WithInputs(portNames ...string) *Component { - c.inputs.Add(port.NewPortGroup(portNames...)...) + c.inputs = c.inputs.Add(port.NewPortGroup(portNames...)...) return c } // WithOutputs adds output ports func (c *Component) WithOutputs(portNames ...string) *Component { - c.outputs.Add(port.NewPortGroup(portNames...)...) + c.outputs = c.outputs.Add(port.NewPortGroup(portNames...)...) return c } diff --git a/component/component_test.go b/component/component_test.go index 54d0507..1cc3554 100644 --- a/component/component_test.go +++ b/component/component_test.go @@ -171,8 +171,8 @@ func TestComponent_Inputs(t *testing.T) { name: "with inputs", component: NewComponent("c1").WithInputs("i1", "i2"), want: port.Collection{ - "i1": port.NewPort("i1"), - "i2": port.NewPort("i2"), + port.NewPort("i1"), + port.NewPort("i2"), }, }, } @@ -200,8 +200,8 @@ func TestComponent_Outputs(t *testing.T) { name: "with outputs", component: NewComponent("c1").WithOutputs("o1", "o2"), want: port.Collection{ - "o1": port.NewPort("o1"), - "o2": port.NewPort("o2"), + port.NewPort("o1"), + port.NewPort("o2"), }, }, } @@ -306,8 +306,8 @@ func TestComponent_WithInputs(t *testing.T) { name: "c1", description: "", inputs: port.Collection{ - "p1": port.NewPort("p1"), - "p2": port.NewPort("p2"), + port.NewPort("p1"), + port.NewPort("p2"), }, outputs: port.Collection{}, f: nil, @@ -358,8 +358,8 @@ func TestComponent_WithOutputs(t *testing.T) { description: "", inputs: port.Collection{}, outputs: port.Collection{ - "p1": port.NewPort("p1"), - "p2": port.NewPort("p2"), + port.NewPort("p1"), + port.NewPort("p2"), }, f: nil, }, diff --git a/port/collection.go b/port/collection.go index 5f44ca9..370af50 100644 --- a/port/collection.go +++ b/port/collection.go @@ -5,25 +5,31 @@ import ( ) // Collection is a port collection with useful methods -type Collection map[string]*Port +type Collection []*Port // NewPortsCollection creates empty collection func NewPortsCollection() Collection { - return make(Collection) + return make(Collection, 0) } // ByName returns a port by its name func (collection Collection) ByName(name string) *Port { - return collection[name] + for _, p := range collection { + if p.Name() == name { + return p + } + } + return nil } // ByNames returns multiple ports by their names func (collection Collection) ByNames(names ...string) Collection { - selectedPorts := make(Collection) + selectedPorts := NewPortsCollection() for _, name := range names { - if p, ok := collection[name]; ok { - selectedPorts[name] = p + p := collection.ByName(name) + if p != nil { + selectedPorts = selectedPorts.Add(p) } } @@ -66,12 +72,13 @@ func (collection Collection) ClearSignal() { } } +// Add adds ports to collection func (collection Collection) Add(ports ...*Port) Collection { for _, port := range ports { if port == nil { continue } - collection[port.Name()] = port + collection = append(collection, port) } return collection diff --git a/port/collection_test.go b/port/collection_test.go index ff6e219..7e92f68 100644 --- a/port/collection_test.go +++ b/port/collection_test.go @@ -151,7 +151,7 @@ func TestCollection_ByNames(t *testing.T) { names: []string{"p1"}, }, want: Collection{ - "p1": &Port{ + &Port{ name: "p1", pipes: Collection{}, }, @@ -164,8 +164,8 @@ func TestCollection_ByNames(t *testing.T) { names: []string{"p1", "p2"}, }, want: Collection{ - "p1": &Port{name: "p1", pipes: Collection{}}, - "p2": &Port{name: "p2", pipes: Collection{}}, + &Port{name: "p1", pipes: Collection{}}, + &Port{name: "p2", pipes: Collection{}}, }, }, { @@ -183,8 +183,8 @@ func TestCollection_ByNames(t *testing.T) { names: []string{"p1", "p2", "p3"}, }, want: Collection{ - "p1": &Port{name: "p1", pipes: Collection{}}, - "p2": &Port{name: "p2", pipes: Collection{}}, + &Port{name: "p1", pipes: Collection{}}, + &Port{name: "p2", pipes: Collection{}}, }, }, } @@ -252,7 +252,7 @@ func TestCollection_Add(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.collection.Add(tt.args.ports...) + tt.collection = tt.collection.Add(tt.args.ports...) if tt.assertions != nil { tt.assertions(t, tt.collection) } diff --git a/port/port.go b/port/port.go index 599f70b..5b647f4 100644 --- a/port/port.go +++ b/port/port.go @@ -50,7 +50,7 @@ func (p *Port) PipeTo(toPorts ...*Port) { if toPort == nil { continue } - p.pipes.Add(toPort) + p.pipes = p.pipes.Add(toPort) } } @@ -61,7 +61,7 @@ func (p *Port) Flush() { } for _, outboundPort := range p.pipes { - //Multiplexing + //Fan-Out ForwardSignal(p, outboundPort) } p.ClearSignal() diff --git a/port/port_test.go b/port/port_test.go index 55cba02..f94c314 100644 --- a/port/port_test.go +++ b/port/port_test.go @@ -3,7 +3,6 @@ package port import ( "github.com/hovsep/fmesh/signal" "github.com/stretchr/testify/assert" - "reflect" "testing" ) @@ -74,9 +73,8 @@ func TestPort_Signal(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.port.Signal(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("Signal() = %v, want %v", got, tt.want) - } + got := tt.port.Signal() + assert.Equal(t, tt.want, got) }) } } @@ -104,9 +102,7 @@ func TestPort_ClearSignal(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.before.ClearSignal() - if !reflect.DeepEqual(tt.before, tt.after) { - t.Errorf("ClearSignal() = %v, want %v", tt.before, tt.after) - } + assert.Equal(t, tt.after, tt.before) }) } } @@ -149,9 +145,7 @@ func TestPort_PipeTo(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.before.PipeTo(tt.args.toPorts...) - if !reflect.DeepEqual(tt.before, tt.after) { - t.Errorf("PipeTo() = %v, want %v", tt.before, tt.after) - } + assert.Equal(t, tt.after, tt.before) }) } } @@ -239,9 +233,7 @@ func TestPort_PutSignal(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { tt.before.PutSignal(tt.args.sig) - if !reflect.DeepEqual(tt.before, tt.after) { - t.Errorf("ClearSignal() = %v, want %v", tt.before, tt.after) - } + assert.Equal(t, tt.after, tt.before) }) } } @@ -260,7 +252,7 @@ func TestPort_Name(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equalf(t, tt.want, tt.port.Name(), "Name()") + assert.Equal(t, tt.want, tt.port.Name()) }) } } @@ -291,7 +283,7 @@ func TestNewPort(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - assert.Equalf(t, tt.want, NewPort(tt.args.name), "NewPort(%v)", tt.args.name) + assert.Equal(t, tt.want, NewPort(tt.args.name)) }) } }