@@ -20,6 +20,7 @@ import (
2020 "reflect"
2121 "testing"
2222
23+ "github.com/go-spring-projects/go-spring/gs/cond"
2324 "github.com/go-spring-projects/go-spring/internal/utils/assert"
2425 "github.com/golang/mock/gomock"
2526)
@@ -146,4 +147,105 @@ func TestBind(t *testing.T) {
146147 assert .Equal (t , len (values ), 0 )
147148 })
148149
150+ t .Run ("one ctx matches" , func (t * testing.T ) {
151+ ctrl := gomock .NewController (t )
152+ defer ctrl .Finish ()
153+ ctx := NewMockContext (ctrl )
154+ ctx .EXPECT ().Matches (gomock .Any ()).Return (true , nil )
155+
156+ ok , err := ctx .Matches (cond .OK ())
157+ assert .Nil (t , err )
158+ assert .True (t , ok )
159+ })
160+
161+ t .Run ("bind args" , func (t * testing.T ) {
162+ ctrl := gomock .NewController (t )
163+ defer ctrl .Finish ()
164+ ctx := NewMockContext (ctrl )
165+
166+ var expectValues = []interface {}{
167+ 0 , 1 , 2 , 3 , 4 , 5 , 6 , nil ,
168+ }
169+
170+ var gotValues []interface {}
171+ fn := func (r0 int , r1 int , r2 int , r3 int , r4 int , r5 int , r6 int , nilPtr * int ) {
172+ gotValues = append (gotValues , r0 )
173+ gotValues = append (gotValues , r1 )
174+ gotValues = append (gotValues , r2 )
175+ gotValues = append (gotValues , r3 )
176+ gotValues = append (gotValues , r4 )
177+ gotValues = append (gotValues , r5 )
178+ gotValues = append (gotValues , r6 )
179+ gotValues = append (gotValues , nil )
180+ }
181+
182+ c , err := Bind (fn , []Arg {
183+ R0 (Value (expectValues [0 ])),
184+ R1 (Value (expectValues [1 ])),
185+ R2 (Value (expectValues [2 ])),
186+ R3 (Value (expectValues [3 ])),
187+ R4 (Value (expectValues [4 ])),
188+ R5 (Value (expectValues [5 ])),
189+ R6 (Value (expectValues [6 ])),
190+ Index (7 , Nil ()),
191+ }, 1 )
192+ assert .Nil (t , err )
193+
194+ values , err := c .Call (ctx )
195+ if err != nil {
196+ t .Fatal (err )
197+ }
198+
199+ assert .Equal (t , len (values ), 0 )
200+ assert .Equal (t , gotValues , expectValues )
201+
202+ // mark coverage
203+ NewMockArg (ctrl ).EXPECT ()
204+ })
205+
206+ t .Run ("bind options" , func (t * testing.T ) {
207+ ctrl := gomock .NewController (t )
208+ defer ctrl .Finish ()
209+ ctx := NewMockContext (ctrl )
210+
211+ type options struct {
212+ name string
213+ age int
214+ }
215+
216+ type fnOption func (opts * options )
217+
218+ var gotOpts = & options {}
219+
220+ var optFn = func (option ... fnOption ) {
221+ for _ , op := range option {
222+ op (gotOpts )
223+ }
224+ }
225+
226+ var withName = func (name string ) fnOption {
227+ return func (opts * options ) {
228+ opts .name = name
229+ }
230+ }
231+
232+ var withAge = func (age int ) fnOption {
233+ return func (opts * options ) {
234+ opts .age = age
235+ }
236+ }
237+
238+ c , err := Bind (optFn , []Arg {Option (withName , Value ("spring" )), Option (withAge , Value (18 ))}, 1 )
239+ assert .Nil (t , err )
240+
241+ values , err := c .Call (ctx )
242+ if err != nil {
243+ t .Fatal (err )
244+ }
245+
246+ assert .Equal (t , len (values ), 0 )
247+ assert .Equal (t , gotOpts .name , "spring" )
248+ assert .Equal (t , gotOpts .age , 18 )
249+
250+ })
149251}
0 commit comments