github.com/erda-project/erda-infra@v1.0.9/base/servicehub/examples/unit-test/provider_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 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 example 16 17 import ( 18 "testing" 19 20 "github.com/erda-project/erda-infra/base/servicehub" 21 ) 22 23 func getService(t *testing.T) Interface { 24 hub := servicehub.Run(&servicehub.RunOptions{ 25 Content: ` 26 example-provider: 27 `}) 28 example, ok := hub.Service("example").(Interface) 29 if !ok { 30 t.Fatalf("example is not Interface") 31 } 32 return example 33 } 34 35 func Test_provider_Hello(t *testing.T) { 36 type args struct { 37 name string 38 } 39 tests := []struct { 40 name string 41 args args 42 want string 43 }{ 44 { 45 "test1", 46 args{ 47 "test", 48 }, 49 "hello test", 50 }, 51 { 52 "test2", 53 args{ 54 "song", 55 }, 56 "hello song", 57 }, 58 } 59 for _, tt := range tests { 60 t.Run(tt.name, func(t *testing.T) { 61 s := getService(t) 62 if got := s.Hello(tt.args.name); got != tt.want { 63 t.Errorf("provider.Hello() = %v, want %v", got, tt.want) 64 } 65 }) 66 } 67 } 68 69 func Test_provider_Add(t *testing.T) { 70 type args struct { 71 a int 72 b int 73 } 74 tests := []struct { 75 name string 76 args args 77 want int 78 }{ 79 { 80 "test1", 81 args{ 82 a: 1, 83 b: 2, 84 }, 85 3, 86 }, 87 { 88 "test2", 89 args{ 90 a: 8, 91 b: 2, 92 }, 93 10, 94 }, 95 } 96 for _, tt := range tests { 97 t.Run(tt.name, func(t *testing.T) { 98 s := getService(t) 99 if got := s.Add(tt.args.a, tt.args.b); got != tt.want { 100 t.Errorf("provider.Add() = %v, want %v", got, tt.want) 101 } 102 }) 103 } 104 } 105 106 func Test_provider_sub(t *testing.T) { 107 type args struct { 108 a int 109 b int 110 } 111 tests := []struct { 112 name string 113 args args 114 want int 115 }{ 116 { 117 "test1", 118 args{ 119 a: 10, 120 b: 2, 121 }, 122 8, 123 }, 124 { 125 "test2", 126 args{ 127 a: 10, 128 b: 4, 129 }, 130 6, 131 }, 132 } 133 for _, tt := range tests { 134 t.Run(tt.name, func(t *testing.T) { 135 s := getService(t) 136 p := s.(*provider) 137 if got := p.sub(tt.args.a, tt.args.b); got != tt.want { 138 t.Errorf("provider.sub() = %v, want %v", got, tt.want) 139 } 140 }) 141 } 142 } 143 144 func (p *provider) testOnlyFunc(a, b int) int { 145 return a + b 146 } 147 148 func Test_provider_testOnlyFunc(t *testing.T) { 149 type args struct { 150 a int 151 b int 152 } 153 tests := []struct { 154 name string 155 args args 156 want int 157 }{ 158 { 159 "test1", 160 args{ 161 a: 5, 162 b: 7, 163 }, 164 12, 165 }, 166 { 167 "test2", 168 args{ 169 a: 10, 170 b: 14, 171 }, 172 24, 173 }, 174 } 175 for _, tt := range tests { 176 t.Run(tt.name, func(t *testing.T) { 177 s := getService(t) 178 p := s.(*provider) 179 if got := p.testOnlyFunc(tt.args.a, tt.args.b); got != tt.want { 180 t.Errorf("provider.testOnlyFunc() = %v, want %v", got, tt.want) 181 } 182 }) 183 } 184 }