...

Source file src/github.com/golang/mock/sample/user.go

Documentation: github.com/golang/mock/sample

     1  // Package user is an example package with an interface.
     2  package user
     3  
     4  //go:generate mockgen -destination mock_user_test.go -package user_test github.com/golang/mock/sample Index,Embed,Embedded
     5  
     6  // Random bunch of imports to test mockgen.
     7  import (
     8  	"io"
     9  
    10  	btz "bytes"
    11  	"hash"
    12  	"log"
    13  	"net"
    14  	"net/http"
    15  
    16  	// Two imports with the same base name.
    17  	t1 "html/template"
    18  
    19  	t2 "text/template"
    20  
    21  	"github.com/golang/mock/sample/imp1"
    22  
    23  	// Dependencies outside the standard library.
    24  
    25  	renamed2 "github.com/golang/mock/sample/imp2"
    26  
    27  	. "github.com/golang/mock/sample/imp3"
    28  
    29  	imp_four "github.com/golang/mock/sample/imp4"
    30  )
    31  
    32  // calls itself "imp_four"
    33  
    34  // A bizarre interface to test corner cases in mockgen.
    35  // This would normally be in its own file or package,
    36  // separate from the user of it (e.g. io.Reader).
    37  type Index interface {
    38  	Get(key string) interface{}
    39  	GetTwo(key1, key2 string) (v1, v2 interface{})
    40  	Put(key string, value interface{})
    41  
    42  	// Check that imports are handled correctly.
    43  	Summary(buf *btz.Buffer, w io.Writer)
    44  	Other() hash.Hash
    45  	Templates(a t1.CSS, b t2.FuncMap)
    46  
    47  	// A method with an anonymous argument.
    48  	Anon(string)
    49  
    50  	// Methods using foreign types outside the standard library.
    51  	ForeignOne(imp1.Imp1)
    52  	ForeignTwo(renamed2.Imp2)
    53  	ForeignThree(Imp3)
    54  	ForeignFour(imp_four.Imp4)
    55  
    56  	// A method that returns a nillable type.
    57  	NillableRet() error
    58  	// A method that returns a non-interface type.
    59  	ConcreteRet() chan<- bool
    60  
    61  	// Methods with an ellipsis argument.
    62  	Ellip(fmt string, args ...interface{})
    63  	EllipOnly(...string)
    64  
    65  	// A method with a pointer argument that we will set.
    66  	Ptr(arg *int)
    67  
    68  	// A method with a slice argument and an array return.
    69  	Slice(a []int, b []byte) [3]int
    70  
    71  	// A method with channel arguments.
    72  	Chan(a chan int, b chan<- hash.Hash)
    73  
    74  	// A method with a function argument.
    75  	Func(f func(http.Request) (int, bool))
    76  
    77  	// A method with a map argument.
    78  	Map(a map[int]hash.Hash)
    79  
    80  	// Methods with an unnamed empty struct argument.
    81  	Struct(a struct{})          // not so likely
    82  	StructChan(a chan struct{}) // a bit more common
    83  }
    84  
    85  // An interface with an embedded interface.
    86  type Embed interface {
    87  	RegularMethod()
    88  	Embedded
    89  	imp1.ForeignEmbedded
    90  }
    91  
    92  type Embedded interface {
    93  	EmbeddedMethod()
    94  }
    95  
    96  // some random use of another package that isn't needed by the interface.
    97  var _ net.Addr
    98  
    99  // A function that we will test that uses the above interface.
   100  // It takes a list of keys and values, and puts them in the index.
   101  func Remember(index Index, keys []string, values []interface{}) {
   102  	for i, k := range keys {
   103  		index.Put(k, values[i])
   104  	}
   105  	err := index.NillableRet()
   106  	if err != nil {
   107  		log.Fatalf("Woah! %v", err)
   108  	}
   109  	if len(keys) > 0 && keys[0] == "a" {
   110  		index.Ellip("%d", 0, 1, 1, 2, 3)
   111  		index.Ellip("%d", 1, 3, 6, 10, 15)
   112  		index.EllipOnly("arg")
   113  	}
   114  }
   115  
   116  func GrabPointer(index Index) int {
   117  	var a int
   118  	index.Ptr(&a)
   119  	return a
   120  }
   121  

View as plain text