github.com/bytedance/mockey@v1.2.10/mock_sequence_test.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package mockey
    18  
    19  import (
    20  	"fmt"
    21  	"testing"
    22  
    23  	"github.com/smartystreets/goconvey/convey"
    24  )
    25  
    26  func TestSequenceOpt(t *testing.T) {
    27  	PatchConvey("test sequenceOpt", t, func() {
    28  		fn := func() (string, int) {
    29  			fmt.Println("original fn")
    30  			return "fn: not here", -1
    31  		}
    32  
    33  		tests := []struct {
    34  			Value1 string
    35  			Value2 int
    36  			Times  int
    37  		}{
    38  			{"Alice", 2, 3},
    39  			{"Bob", 3, 1},
    40  			{"Tom", 4, 1},
    41  			{"Jerry", 5, 2},
    42  		}
    43  
    44  		seq := Sequence("Admin", 1)
    45  		for _, r := range tests {
    46  			seq.Then(r.Value1, r.Value2).Times(r.Times)
    47  		}
    48  		Mock(fn).Return(seq).Build()
    49  
    50  		for repeat := 3; repeat != 0; repeat-- {
    51  			v1, v2 := fn()
    52  			convey.So(v1, convey.ShouldEqual, "Admin")
    53  			convey.So(v2, convey.ShouldEqual, 1)
    54  			for _, r := range tests {
    55  				for rIndex := 0; rIndex < r.Times; rIndex++ {
    56  					v1, v2 := fn()
    57  					convey.So(v1, convey.ShouldEqual, r.Value1)
    58  					convey.So(v2, convey.ShouldEqual, r.Value2)
    59  				}
    60  			}
    61  		}
    62  	})
    63  }