From d07ac6730069fb44018177d9e6572213d0e61a49 Mon Sep 17 00:00:00 2001 From: Gorbunov Kirill Andreevich Date: Tue, 27 Feb 2024 23:11:53 +0300 Subject: [PATCH] #97 add As for all interface. --- micro_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 91 insertions(+), 12 deletions(-) diff --git a/micro_test.go b/micro_test.go index cb90e977..b9387c97 100644 --- a/micro_test.go +++ b/micro_test.go @@ -1,24 +1,103 @@ package micro -import "testing" +import ( + "context" + "fmt" + "reflect" + "testing" + + "go.unistack.org/micro/v3/broker" + "go.unistack.org/micro/v3/fsm" + "go.unistack.org/micro/v3/options" +) func TestAs(t *testing.T) { - type args struct { + var b *bro + broTarget := &bro{name: "kafka"} + fsmTarget := &fsmT{name: "fsm"} + + testCases := []struct { b any target any - } - tests := []struct { - name string - args args - want bool + match bool + want any }{ - // TODO: Add test cases. + { + broTarget, + &b, + true, + broTarget, + }, + { + nil, + &b, + false, + nil, + }, + { + fsmTarget, + &b, + false, + nil, + }, } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if got := As(tt.args.b, tt.args.target); got != tt.want { - t.Errorf("As() = %v, want %v", got, tt.want) + for i, tc := range testCases { + name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.b, tc.target) + // Clear the target pointer, in case it was set in a previous test. + rtarget := reflect.ValueOf(tc.target) + rtarget.Elem().Set(reflect.Zero(reflect.TypeOf(tc.target).Elem())) + t.Run(name, func(t *testing.T) { + match := As(tc.b, tc.target) + if match != tc.match { + t.Fatalf("match: got %v; want %v", match, tc.match) + } + if !match { + return + } + if got := rtarget.Elem().Interface(); got != tc.want { + t.Fatalf("got %#v, want %#v", got, tc.want) } }) } } + +type bro struct { + name string +} + +func (p *bro) Name() string { return p.name } +func (p *bro) Init(opts ...options.Option) error { return nil } + +// Options returns broker options +func (p *bro) Options() broker.Options { return broker.Options{} } + +// Address return configured address +func (p *bro) Address() string { return "" } + +// Connect connects to broker +func (p *bro) Connect(ctx context.Context) error { return nil } + +// Disconnect disconnect from broker +func (p *bro) Disconnect(ctx context.Context) error { return nil } + +// Publish message, msg can be single broker.Message or []broker.Message +func (p *bro) Publish(ctx context.Context, msg interface{}, opts ...options.Option) error { return nil } + +// Subscribe subscribes to topic message via handler +func (p *bro) Subscribe(ctx context.Context, topic string, handler interface{}, opts ...options.Option) (broker.Subscriber, error) { + return nil, nil +} + +// String type of broker +func (p *bro) String() string { return p.name } + +type fsmT struct { + name string +} + +func (f *fsmT) Start(ctx context.Context, a interface{}, o ...Option) (interface{}, error) { + return nil, nil +} +func (f *fsmT) Current() string { return f.name } +func (f *fsmT) Reset() {} +func (f *fsmT) State(s string, sf fsm.StateFunc) {}