From 0854a7ea7298c3ddc4e3db448ab4a6777525b99c Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Thu, 7 Oct 2021 20:59:27 +0300 Subject: [PATCH] micro: add simple test Signed-off-by: Vasiliy Tolstov --- service.go | 8 ++++++-- service_test.go | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 service_test.go diff --git a/service.go b/service.go index 3aa22b3b..fcb31025 100644 --- a/service.go +++ b/service.go @@ -392,8 +392,12 @@ type nameIface interface { Name() string } -func getNameIndex(n string, ifaces ...interface{}) int { - for idx, iface := range ifaces { +func getNameIndex(n string, ifaces interface{}) int { + values, ok := ifaces.([]interface{}) + if !ok { + return 0 + } + for idx, iface := range values { if ifc, ok := iface.(nameIface); ok && ifc.Name() == n { return idx } diff --git a/service_test.go b/service_test.go new file mode 100644 index 00000000..1ee6a77f --- /dev/null +++ b/service_test.go @@ -0,0 +1,22 @@ +package micro + +import ( + "testing" +) + +type testItem struct { + name string +} + +func (ti *testItem) Name() string { + return ti.name +} + +func TestGetNameIndex(t *testing.T) { + item1 := &testItem{name: "first"} + item2 := &testItem{name: "second"} + items := []interface{}{item1, item2} + if idx := getNameIndex("second", items); idx != 1 { + t.Fatalf("getNameIndex func error, item not found") + } +}