|
| 1 | +// Copyright 2021 ecodeclub |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package spi |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + "os/exec" |
| 21 | + "path/filepath" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/require" |
| 25 | + "github.com/stretchr/testify/suite" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/assert" |
| 28 | +) |
| 29 | + |
| 30 | +type LoadServiceSuite struct { |
| 31 | + suite.Suite |
| 32 | +} |
| 33 | + |
| 34 | +func (l *LoadServiceSuite) SetupTest() { |
| 35 | + t := l.T() |
| 36 | + wd, err := os.Getwd() |
| 37 | + require.NoError(t, err) |
| 38 | + cmd := exec.Command("go", "generate", "./...") |
| 39 | + cmd.Dir = filepath.Join(wd, "testdata") |
| 40 | + output, err := cmd.CombinedOutput() |
| 41 | + require.NoError(t, err, fmt.Sprintf("执行 go generate 失败: %v\n%s", err, output)) |
| 42 | +} |
| 43 | + |
| 44 | +func (l *LoadServiceSuite) Test_LoadService() { |
| 45 | + t := l.T() |
| 46 | + testcases := []struct { |
| 47 | + name string |
| 48 | + dir string |
| 49 | + svcName string |
| 50 | + want []string |
| 51 | + assertFunc assert.ErrorAssertionFunc |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "有一个插件", |
| 55 | + dir: "./testdata/user_service", |
| 56 | + svcName: "UserSvc", |
| 57 | + want: []string{"Get"}, |
| 58 | + assertFunc: assert.NoError, |
| 59 | + }, |
| 60 | + { |
| 61 | + name: "有两个插件", |
| 62 | + dir: "./testdata/user_service2", |
| 63 | + svcName: "UserSvc", |
| 64 | + want: []string{"A", "B"}, |
| 65 | + assertFunc: assert.NoError, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "目录不存在", |
| 69 | + dir: "./notfound", |
| 70 | + assertFunc: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 71 | + return assert.ErrorIs(t, err, ErrDirNotFound) |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "svcName为空", |
| 76 | + dir: "./testdata/user_service2", |
| 77 | + svcName: "", |
| 78 | + assertFunc: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 79 | + return assert.ErrorIs(t, err, ErrSymbolNameIsEmpty) |
| 80 | + }, |
| 81 | + }, |
| 82 | + { |
| 83 | + name: "svcName没找到", |
| 84 | + dir: "./testdata/user_service2", |
| 85 | + svcName: "notfound", |
| 86 | + assertFunc: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 87 | + return assert.ErrorIs(t, err, ErrSymbolNameNotFound) |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "加载的对象未实现对应的抽象", |
| 92 | + dir: "./testdata/user_service3", |
| 93 | + svcName: "UserSvc", |
| 94 | + assertFunc: func(t assert.TestingT, err error, i ...interface{}) bool { |
| 95 | + return assert.ErrorIs(t, err, ErrInvalidSo) |
| 96 | + }, |
| 97 | + }, |
| 98 | + } |
| 99 | + for _, tc := range testcases { |
| 100 | + t.Run(tc.name, func(t *testing.T) { |
| 101 | + list, err := LoadService[UserService](tc.dir, tc.svcName) |
| 102 | + tc.assertFunc(t, err) |
| 103 | + if err != nil { |
| 104 | + return |
| 105 | + } |
| 106 | + ans := make([]string, 0, len(list)) |
| 107 | + for _, svc := range list { |
| 108 | + ans = append(ans, svc.Get()) |
| 109 | + } |
| 110 | + assert.Equal(t, tc.want, ans) |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestLoadServiceSuite(t *testing.T) { |
| 116 | + suite.Run(t, new(LoadServiceSuite)) |
| 117 | +} |
| 118 | + |
| 119 | +type UserService interface { |
| 120 | + Get() string |
| 121 | +} |
| 122 | + |
| 123 | +func ExampleLoadService() { |
| 124 | + getters, err := LoadService[UserService]("./testdata/user_service", "UserSvc") |
| 125 | + fmt.Println(err) |
| 126 | + for _, getter := range getters { |
| 127 | + fmt.Println(getter.Get()) |
| 128 | + } |
| 129 | + // Output: |
| 130 | + // <nil> |
| 131 | + // Get |
| 132 | +} |
0 commit comments