github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/server/grpc/util_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); 2 // you may not use this file except in compliance with the License. 3 // You may obtain a copy of the License at 4 // 5 // https://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 // 13 // Original source: github.com/micro/go-micro/v3/util/grpc/grpc_test.go 14 15 package grpc 16 17 import ( 18 "testing" 19 ) 20 21 func TestServiceMethod(t *testing.T) { 22 type testCase struct { 23 input string 24 service string 25 method string 26 err bool 27 } 28 29 methods := []testCase{ 30 {"Foo.Bar", "Foo", "Bar", false}, 31 {"/Foo/Bar", "Foo", "Bar", false}, 32 {"/package.Foo/Bar", "Foo", "Bar", false}, 33 {"/a.package.Foo/Bar", "Foo", "Bar", false}, 34 {"a.package.Foo/Bar", "", "", true}, 35 {"/Foo/Bar/Baz", "", "", true}, 36 {"Foo.Bar.Baz", "", "", true}, 37 } 38 for _, test := range methods { 39 service, method, err := ServiceMethod(test.input) 40 if err != nil && test.err == true { 41 continue 42 } 43 // unexpected error 44 if err != nil && test.err == false { 45 t.Fatalf("unexpected err %v for %+v", err, test) 46 } 47 // expecter error 48 if test.err == true && err == nil { 49 t.Fatalf("expected error for %+v: got service: %s method: %s", test, service, method) 50 } 51 52 if service != test.service { 53 t.Fatalf("wrong service for %+v: got service: %s method: %s", test, service, method) 54 } 55 56 if method != test.method { 57 t.Fatalf("wrong method for %+v: got service: %s method: %s", test, service, method) 58 } 59 } 60 }