From e787a9bced5a9e19094431ee8bd80eb008e3d913 Mon Sep 17 00:00:00 2001 From: hovsep Date: Wed, 18 Sep 2024 01:20:28 +0300 Subject: [PATCH 1/5] Minor: use constructor --- cycle/cycle.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cycle/cycle.go b/cycle/cycle.go index 26a5022..72b8118 100644 --- a/cycle/cycle.go +++ b/cycle/cycle.go @@ -14,7 +14,7 @@ type Cycle struct { // New creates a new cycle func New() *Cycle { return &Cycle{ - activationResults: make(component.ActivationResultCollection), + activationResults: component.NewActivationResultCollection(), } } From db422a8f470afb54ca6673f95e166e9cb06d5e62 Mon Sep 17 00:00:00 2001 From: hovsep Date: Wed, 18 Sep 2024 01:22:57 +0300 Subject: [PATCH 2/5] Group types optimized --- port/group.go | 4 ++-- signal/group.go | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/port/group.go b/port/group.go index 28ea3fc..bb7e6d0 100644 --- a/port/group.go +++ b/port/group.go @@ -6,8 +6,8 @@ type Group []*Port // NewGroup creates multiple ports func NewGroup(names ...string) Group { group := make(Group, len(names)) - for _, name := range names { - group = append(group, New(name)) + for i, name := range names { + group[i] = New(name) } return group } diff --git a/signal/group.go b/signal/group.go index b27cf4f..230767c 100644 --- a/signal/group.go +++ b/signal/group.go @@ -4,19 +4,19 @@ type Group []*Signal // NewGroup creates empty group func NewGroup(payloads ...any) Group { - group := make(Group, 0) - for _, payload := range payloads { - group = append(group, New(payload)) + group := make(Group, len(payloads)) + for i, payload := range payloads { + group[i] = New(payload) } return group } -// FirstPayload returns the first signal payload in a group +// FirstPayload returns the first signal payload func (group Group) FirstPayload() any { - if len(group) == 0 { - return nil - } - + //Intentionally not checking the group len + //as the method does not have returning error (api is simpler) + //and we can not just return nil, as nil may be a valid payload. + //So just let runtime panic return group[0].Payload() } From 62018a4f81baa4e5e1f0759ab1094a5d02cf8dc3 Mon Sep 17 00:00:00 2001 From: hovsep Date: Wed, 18 Sep 2024 01:23:10 +0300 Subject: [PATCH 3/5] Signal: add group tests --- signal/group_test.go | 114 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 113 insertions(+), 1 deletion(-) diff --git a/signal/group_test.go b/signal/group_test.go index 4a6c15e..f80a2cf 100644 --- a/signal/group_test.go +++ b/signal/group_test.go @@ -1,3 +1,115 @@ package signal -//@TODO implement +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func TestNewGroup(t *testing.T) { + type args struct { + payloads []any + } + tests := []struct { + name string + args args + want Group + }{ + { + name: "no payloads", + args: args{ + payloads: nil, + }, + want: Group{}, + }, + { + name: "with payloads", + args: args{ + payloads: []any{1, nil, 3}, + }, + want: Group{ + &Signal{ + payload: []any{1}, + }, + &Signal{ + payload: []any{nil}, + }, + &Signal{ + payload: []any{3}, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, NewGroup(tt.args.payloads...)) + }) + } +} + +func TestGroup_FirstPayload(t *testing.T) { + tests := []struct { + name string + group Group + want any + wantPanic bool + }{ + { + name: "empty group", + group: NewGroup(), + want: nil, + wantPanic: true, + }, + { + name: "first is nil", + group: NewGroup(nil, 123), + want: nil, + wantPanic: false, + }, + { + name: "first is not nil", + group: NewGroup([]string{"1", "2"}, 123), + want: []string{"1", "2"}, + wantPanic: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + defer func() { + r := recover() + if tt.wantPanic && r == nil { + t.Errorf("The code did not panic") + } + + if !tt.wantPanic && r != nil { + t.Errorf("The code unexpectedly paniced") + } + }() + + assert.Equal(t, tt.want, tt.group.FirstPayload()) + }) + } +} + +func TestGroup_AllPayloads(t *testing.T) { + tests := []struct { + name string + group Group + want []any + }{ + { + name: "empty group", + group: NewGroup(), + want: []any{}, + }, + { + name: "with payloads", + group: NewGroup(1, nil, 3, []int{4, 5, 6}, map[byte]byte{7: 8}), + want: []any{1, nil, 3, []int{4, 5, 6}, map[byte]byte{7: 8}}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.Equal(t, tt.want, tt.group.AllPayloads()) + }) + } +} From 9471ba6fdfadd09b5c2066ee30935e823635398e Mon Sep 17 00:00:00 2001 From: hovsep Date: Wed, 18 Sep 2024 01:39:49 +0300 Subject: [PATCH 4/5] Tests: use asser lib --- component/collection_test.go | 5 +---- component/component_test.go | 41 ++++++++++++------------------------ cycle/collection_test.go | 10 +++------ cycle/cycle_test.go | 26 ++++++----------------- fmesh_test.go | 29 ++++++------------------- port/collection_test.go | 14 ++++-------- port/port_test.go | 7 ++---- signal/signal_test.go | 6 ++---- 8 files changed, 39 insertions(+), 99 deletions(-) diff --git a/component/collection_test.go b/component/collection_test.go index 4d4e559..253ef72 100644 --- a/component/collection_test.go +++ b/component/collection_test.go @@ -3,7 +3,6 @@ package component import ( "github.com/hovsep/fmesh/port" "github.com/stretchr/testify/assert" - "reflect" "testing" ) @@ -42,9 +41,7 @@ func TestCollection_ByName(t *testing.T) { } 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) - } + assert.Equal(t, tt.want, tt.components.ByName(tt.args.name)) }) } } diff --git a/component/component_test.go b/component/component_test.go index 7abb314..120104b 100644 --- a/component/component_test.go +++ b/component/component_test.go @@ -5,7 +5,6 @@ import ( "github.com/hovsep/fmesh/port" "github.com/hovsep/fmesh/signal" "github.com/stretchr/testify/assert" - "reflect" "testing" ) @@ -71,9 +70,7 @@ func TestComponent_Name(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.Name(); got != tt.want { - t.Errorf("Name() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.Name()) }) } } @@ -97,9 +94,7 @@ func TestComponent_Description(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.Description(); got != tt.want { - t.Errorf("Description() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.Description()) }) } } @@ -178,9 +173,7 @@ func TestComponent_Inputs(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.Inputs(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("Inputs() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.Inputs()) }) } } @@ -207,9 +200,7 @@ func TestComponent_Outputs(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.Outputs(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("Outputs() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.Outputs()) }) } } @@ -279,9 +270,7 @@ func TestComponent_WithDescription(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.WithDescription(tt.args.description); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithDescription() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.WithDescription(tt.args.description)) }) } } @@ -330,9 +319,7 @@ func TestComponent_WithInputs(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.WithInputs(tt.args.portNames...); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithInputs() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.WithInputs(tt.args.portNames...)) }) } } @@ -381,9 +368,7 @@ func TestComponent_WithOutputs(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.component.WithOutputs(tt.args.portNames...); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithOutputs() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.component.WithOutputs(tt.args.portNames...)) }) } } @@ -551,14 +536,14 @@ func TestComponent_MaybeActivate(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := tt.getComponent().MaybeActivate() - assert.Equal(t, got.Activated(), tt.wantActivationResult.Activated()) - assert.Equal(t, got.ComponentName(), tt.wantActivationResult.ComponentName()) - assert.Equal(t, got.Code(), tt.wantActivationResult.Code()) + gotActivationResult := tt.getComponent().MaybeActivate() + assert.Equal(t, gotActivationResult.Activated(), tt.wantActivationResult.Activated()) + assert.Equal(t, gotActivationResult.ComponentName(), tt.wantActivationResult.ComponentName()) + assert.Equal(t, gotActivationResult.Code(), tt.wantActivationResult.Code()) if tt.wantActivationResult.HasError() { - assert.EqualError(t, got.Error(), tt.wantActivationResult.Error().Error()) + assert.EqualError(t, gotActivationResult.Error(), tt.wantActivationResult.Error().Error()) } else { - assert.False(t, got.HasError()) + assert.False(t, gotActivationResult.HasError()) } }) diff --git a/cycle/collection_test.go b/cycle/collection_test.go index 2b5f1a5..5c7e0ea 100644 --- a/cycle/collection_test.go +++ b/cycle/collection_test.go @@ -2,7 +2,7 @@ package cycle import ( "github.com/hovsep/fmesh/component" - "reflect" + "github.com/stretchr/testify/assert" "testing" ) @@ -18,9 +18,7 @@ func TestNewCollection(t *testing.T) { } 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) - } + assert.Equal(t, tt.want, NewCollection()) }) } } @@ -60,9 +58,7 @@ func TestCollection_Add(t *testing.T) { } 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) - } + assert.Equal(t, tt.want, tt.cycleResults.Add(tt.args.cycleResults...)) }) } } diff --git a/cycle/cycle_test.go b/cycle/cycle_test.go index 9392465..d23facb 100644 --- a/cycle/cycle_test.go +++ b/cycle/cycle_test.go @@ -3,7 +3,7 @@ package cycle import ( "errors" "github.com/hovsep/fmesh/component" - "reflect" + "github.com/stretchr/testify/assert" "testing" ) @@ -21,9 +21,7 @@ func TestNew(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := New(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("New() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, New()) }) } } @@ -49,9 +47,7 @@ func TestCycle_ActivationResults(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.cycleResult.ActivationResults(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("ActivationResultCollection() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.cycleResult.ActivationResults()) }) } } @@ -88,9 +84,7 @@ func TestCycle_HasActivatedComponents(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.cycleResult.HasActivatedComponents(); got != tt.want { - t.Errorf("HasActivatedComponents() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.cycleResult.HasActivatedComponents()) }) } } @@ -127,9 +121,7 @@ func TestCycle_HasErrors(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.cycleResult.HasErrors(); got != tt.want { - t.Errorf("HasErrors() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.cycleResult.HasErrors()) }) } } @@ -168,9 +160,7 @@ func TestCycle_HasPanics(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.cycleResult.HasPanics(); got != tt.want { - t.Errorf("HasPanics() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.cycleResult.HasPanics()) }) } } @@ -237,9 +227,7 @@ func TestCycle_WithActivationResults(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.cycleResult.WithActivationResults(tt.args.activationResults...); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithActivationResults() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.cycleResult.WithActivationResults(tt.args.activationResults...)) }) } } diff --git a/fmesh_test.go b/fmesh_test.go index 038bcbe..6bfcae2 100644 --- a/fmesh_test.go +++ b/fmesh_test.go @@ -7,7 +7,6 @@ import ( "github.com/hovsep/fmesh/port" "github.com/hovsep/fmesh/signal" "github.com/stretchr/testify/assert" - "reflect" "testing" ) @@ -42,9 +41,7 @@ func TestNew(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := New(tt.args.name); !reflect.DeepEqual(got, tt.want) { - t.Errorf("New() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, New(tt.args.name)) }) } } @@ -88,9 +85,7 @@ func TestFMesh_WithDescription(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.fm.WithDescription(tt.args.description); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithDescription() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.fm.WithDescription(tt.args.description)) }) } } @@ -132,9 +127,7 @@ func TestFMesh_WithErrorHandlingStrategy(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.fm.WithErrorHandlingStrategy(tt.args.strategy); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithErrorHandlingStrategy() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.fm.WithErrorHandlingStrategy(tt.args.strategy)) }) } } @@ -217,9 +210,7 @@ func TestFMesh_WithComponents(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.fm.WithComponents(tt.args.components...); !reflect.DeepEqual(got, tt.want) { - t.Errorf("WithComponents() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.fm.WithComponents(tt.args.components...)) }) } } @@ -243,9 +234,7 @@ func TestFMesh_Name(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.fm.Name(); got != tt.want { - t.Errorf("Name() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.fm.Name()) }) } } @@ -269,9 +258,7 @@ func TestFMesh_Description(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.fm.Description(); got != tt.want { - t.Errorf("Description() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.fm.Description()) }) } } @@ -714,9 +701,7 @@ func TestFMesh_runCycle(t *testing.T) { if tt.initFM != nil { tt.initFM(tt.fm) } - if got := tt.fm.runCycle(); !reflect.DeepEqual(got, tt.want) { - t.Errorf("runCycle() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.fm.runCycle()) }) } } diff --git a/port/collection_test.go b/port/collection_test.go index 9d716f1..aaba717 100644 --- a/port/collection_test.go +++ b/port/collection_test.go @@ -37,9 +37,7 @@ func TestCollection_AllHaveSignal(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.ports.AllHaveSignals(); got != tt.want { - t.Errorf("AllHaveSignals() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.ports.AllHaveSignals()) }) } } @@ -67,9 +65,7 @@ func TestCollection_AnyHasSignal(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.ports.AnyHasSignals(); got != tt.want { - t.Errorf("AnyHasSignals() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.ports.AnyHasSignals()) }) } } @@ -118,8 +114,7 @@ func TestCollection_ByName(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := tt.ports.ByName(tt.args.name) - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want, tt.ports.ByName(tt.args.name)) }) } } @@ -181,8 +176,7 @@ func TestCollection_ByNames(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := tt.ports.ByNames(tt.args.names...) - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want, tt.ports.ByNames(tt.args.names...)) }) } } diff --git a/port/port_test.go b/port/port_test.go index 89b69a1..675d8a6 100644 --- a/port/port_test.go +++ b/port/port_test.go @@ -28,9 +28,7 @@ func TestPort_HasSignals(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := tt.port.HasSignals(); got != tt.want { - t.Errorf("HasSignals() = %v, want %v", got, tt.want) - } + assert.Equal(t, tt.want, tt.port.HasSignals()) }) } } @@ -57,8 +55,7 @@ func TestPort_Signals(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := tt.port.Signals() - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want, tt.port.Signals()) }) } } diff --git a/signal/signal_test.go b/signal/signal_test.go index e2e372f..5438411 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -35,8 +35,7 @@ func TestNew(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := New(tt.args.payload) - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want, New(tt.args.payload)) }) } } @@ -71,9 +70,8 @@ func TestSignal_Payload(t *testing.T) { t.Errorf("The code unexpectedly paniced") } }() - got := tt.signal.Payload() - assert.Equal(t, tt.want, got) + assert.Equal(t, tt.want, tt.signal.Payload()) }) } } From 3078b5c82ccefbefa12e88065fc13362ae91871f Mon Sep 17 00:00:00 2001 From: hovsep Date: Wed, 18 Sep 2024 01:43:47 +0300 Subject: [PATCH 5/5] Tests: refactor tests expecting for panic --- signal/group_test.go | 19 +++++++------------ signal/signal_test.go | 18 +++--------------- 2 files changed, 10 insertions(+), 27 deletions(-) diff --git a/signal/group_test.go b/signal/group_test.go index f80a2cf..fc35e01 100644 --- a/signal/group_test.go +++ b/signal/group_test.go @@ -74,18 +74,13 @@ func TestGroup_FirstPayload(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - defer func() { - r := recover() - if tt.wantPanic && r == nil { - t.Errorf("The code did not panic") - } - - if !tt.wantPanic && r != nil { - t.Errorf("The code unexpectedly paniced") - } - }() - - assert.Equal(t, tt.want, tt.group.FirstPayload()) + if tt.wantPanic { + assert.Panics(t, func() { + tt.group.FirstPayload() + }) + } else { + assert.Equal(t, tt.want, tt.group.FirstPayload()) + } }) } } diff --git a/signal/signal_test.go b/signal/signal_test.go index 5438411..a520683 100644 --- a/signal/signal_test.go +++ b/signal/signal_test.go @@ -42,10 +42,9 @@ func TestNew(t *testing.T) { func TestSignal_Payload(t *testing.T) { tests := []struct { - name string - signal *Signal - want any - wantPanic bool + name string + signal *Signal + want any }{ { name: "nil payload is valid", @@ -60,17 +59,6 @@ func TestSignal_Payload(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - defer func() { - r := recover() - if tt.wantPanic && r == nil { - t.Errorf("The code did not panic") - } - - if !tt.wantPanic && r != nil { - t.Errorf("The code unexpectedly paniced") - } - }() - assert.Equal(t, tt.want, tt.signal.Payload()) }) }