From 358a99fa923614a3047ef182b67de61783cc35d3 Mon Sep 17 00:00:00 2001 From: xy3 Date: Fri, 4 Mar 2022 17:58:53 +0000 Subject: [PATCH 1/2] Add unit tests to pkg/events - Increases the coverage of pkg/events to 100% - My Cosmos address: cosmos1rzz5sr7f6742gjy3ydqs5d02p3q967fqdfugng --- starport/pkg/events/events_test.go | 191 +++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 starport/pkg/events/events_test.go diff --git a/starport/pkg/events/events_test.go b/starport/pkg/events/events_test.go new file mode 100644 index 0000000000..8ac9b1ea7e --- /dev/null +++ b/starport/pkg/events/events_test.go @@ -0,0 +1,191 @@ +package events + +import ( + "reflect" + "testing" +) + +func TestBus_Send(t *testing.T) { + type args struct { + e Event + } + tests := []struct { + name string + b Bus + args args + }{ + { + name: "send status ongoing event", + b: make(Bus), + args: args{Event{ + status: StatusOngoing, + Description: "description", + }}, + }, + { + name: "send status done event", + b: make(Bus), + args: args{Event{ + status: StatusDone, + Description: "description", + }}, + }, + { + name: "send event on nil bus", + b: nil, + args: args{Event{ + status: StatusDone, + Description: "description", + }}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + go func() { + tt.b.Send(tt.args.e) + }() + if tt.b != nil { + event := <-tt.b + if !reflect.DeepEqual(event, tt.args.e) { + t.Errorf("event = %v, want %v", event, tt.args.e) + } + } + tt.b.Shutdown() + }) + } +} + +func TestBus_Shutdown(t *testing.T) { + tests := []struct { + name string + b Bus + }{ + { + name: "shutdown nil bus", + b: nil, + }, + { + name: "shutdown bus correctly", + b: make(Bus), + }, + { + name: "shutdown bus with size correctly", + b: make(Bus, 1), + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + tt.b.Shutdown() + }) + } +} + +func TestEvent_IsOngoing(t *testing.T) { + type fields struct { + status Status + Description string + } + tests := []struct { + name string + fields fields + want bool + }{ + {"status ongoing", fields{StatusOngoing, "description"}, true}, + {"status done", fields{StatusDone, "description"}, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := Event{ + status: tt.fields.status, + Description: tt.fields.Description, + } + if got := e.IsOngoing(); got != tt.want { + t.Errorf("IsOngoing() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestEvent_Text(t *testing.T) { + type fields struct { + status Status + Description string + } + tests := []struct { + name string + fields fields + want string + }{ + { + name: "status done", + fields: fields{StatusDone, "description"}, + want: "description", + }, + { + name: "status ongoing", + fields: fields{StatusOngoing, "description"}, + want: "description...", + }, + { + name: "status ongoing with empty description", + fields: fields{StatusOngoing, ""}, + want: "...", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + e := Event{ + status: tt.fields.status, + Description: tt.fields.Description, + } + if got := e.Text(); got != tt.want { + t.Errorf("Text() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNew(t *testing.T) { + type args struct { + status Status + description string + } + tests := []struct { + name string + args args + want Event + }{ + {"zero value args", args{}, Event{}}, + {"large value args", args{99999, "description"}, Event{99999, "description"}}, + {"status ongoing", args{StatusOngoing, "description"}, Event{0, "description"}}, + {"status done", args{StatusDone, "description"}, Event{1, "description"}}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := New(tt.args.status, tt.args.description); !reflect.DeepEqual(got, tt.want) { + t.Errorf("New() = %v, want %v", got, tt.want) + } + }) + } +} + +func TestNewBus(t *testing.T) { + tests := []struct { + name string + want Bus + }{ + {"new bus event chan", make(Bus)}, + {"new nus event chan matches event chan", make(chan Event)}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := NewBus() + if reflect.TypeOf(got).Kind() != reflect.Chan { + t.Errorf("NewBus() = %v is not a channel", got) + } + if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { + t.Errorf("NewBus() = %v, want %v", got, tt.want) + } + }) + } +} From c55e2d5b82de9221a7a068039b6e38bbb54cb497 Mon Sep 17 00:00:00 2001 From: xy3 Date: Mon, 7 Mar 2022 14:54:18 +0000 Subject: [PATCH 2/2] Add suggestions --- starport/pkg/events/events_test.go | 92 +++++++++++++----------------- 1 file changed, 39 insertions(+), 53 deletions(-) diff --git a/starport/pkg/events/events_test.go b/starport/pkg/events/events_test.go index 8ac9b1ea7e..73bc672d15 100644 --- a/starport/pkg/events/events_test.go +++ b/starport/pkg/events/events_test.go @@ -1,86 +1,78 @@ package events import ( - "reflect" + "github.com/stretchr/testify/require" "testing" ) -func TestBus_Send(t *testing.T) { - type args struct { - e Event - } +func TestBusSend(t *testing.T) { tests := []struct { - name string - b Bus - args args + name string + bus Bus + event Event }{ { name: "send status ongoing event", - b: make(Bus), - args: args{Event{ + bus: make(Bus), + event: Event{ status: StatusOngoing, Description: "description", - }}, + }, }, { name: "send status done event", - b: make(Bus), - args: args{Event{ + bus: make(Bus), + event: Event{ status: StatusDone, Description: "description", - }}, + }, }, { name: "send event on nil bus", - b: nil, - args: args{Event{ + bus: nil, + event: Event{ status: StatusDone, Description: "description", - }}, + }, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - go func() { - tt.b.Send(tt.args.e) - }() - if tt.b != nil { - event := <-tt.b - if !reflect.DeepEqual(event, tt.args.e) { - t.Errorf("event = %v, want %v", event, tt.args.e) - } + go tt.bus.Send(tt.event) + if tt.bus != nil { + require.Equal(t, tt.event, <-tt.bus) } - tt.b.Shutdown() + tt.bus.Shutdown() }) } } -func TestBus_Shutdown(t *testing.T) { +func TestBusShutdown(t *testing.T) { tests := []struct { name string - b Bus + bus Bus }{ { name: "shutdown nil bus", - b: nil, + bus: nil, }, { name: "shutdown bus correctly", - b: make(Bus), + bus: make(Bus), }, { name: "shutdown bus with size correctly", - b: make(Bus, 1), + bus: make(Bus, 1), }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - tt.b.Shutdown() + tt.bus.Shutdown() }) } } -func TestEvent_IsOngoing(t *testing.T) { +func TestEventIsOngoing(t *testing.T) { type fields struct { status Status Description string @@ -99,14 +91,12 @@ func TestEvent_IsOngoing(t *testing.T) { status: tt.fields.status, Description: tt.fields.Description, } - if got := e.IsOngoing(); got != tt.want { - t.Errorf("IsOngoing() = %v, want %v", got, tt.want) - } + require.Equal(t, tt.want, e.IsOngoing()) }) } } -func TestEvent_Text(t *testing.T) { +func TestEventText(t *testing.T) { type fields struct { status Status Description string @@ -138,9 +128,7 @@ func TestEvent_Text(t *testing.T) { status: tt.fields.status, Description: tt.fields.Description, } - if got := e.Text(); got != tt.want { - t.Errorf("Text() = %v, want %v", got, tt.want) - } + require.Equal(t, tt.want, e.Text()) }) } } @@ -162,29 +150,27 @@ func TestNew(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if got := New(tt.args.status, tt.args.description); !reflect.DeepEqual(got, tt.want) { - t.Errorf("New() = %v, want %v", got, tt.want) - } + require.Equal(t, tt.want, New(tt.args.status, tt.args.description)) }) } } func TestNewBus(t *testing.T) { tests := []struct { - name string - want Bus + name string + event Event }{ - {"new bus event chan", make(Bus)}, - {"new nus event chan matches event chan", make(chan Event)}, + {"new bus with status done event", Event{StatusDone, "description"}}, + {"new bus with status ongoing event", Event{StatusOngoing, "description"}}, + {"new bus with zero value event", Event{}}, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := NewBus() - if reflect.TypeOf(got).Kind() != reflect.Chan { - t.Errorf("NewBus() = %v is not a channel", got) - } - if reflect.TypeOf(got) != reflect.TypeOf(tt.want) { - t.Errorf("NewBus() = %v, want %v", got, tt.want) + bus := NewBus() + defer bus.Shutdown() + for i := 0; i < 10; i++ { + go bus.Send(tt.event) + require.Equal(t, tt.event, <-bus) } }) }