add csi-test to vendor

This commit is contained in:
wackxu
2018-08-21 23:03:36 +08:00
parent 62551068b1
commit a2987675cf
110 changed files with 11973 additions and 24 deletions

16
vendor/github.com/golang/mock/sample/README.md generated vendored Normal file
View File

@@ -0,0 +1,16 @@
This directory contains an example of a package containing a non-trivial
interface that can be mocked with GoMock. The interesting files are:
* `user.go`: Source code for the sample package, containing interfaces to be
mocked. This file depends on the packages named imp[1-4] for various things.
* `user_test.go`: A test for the sample package, in which mocks of the
interfaces from `user.go` are used. This demonstrates how to create mock
objects, set up expectations, and so on.
* `mock_user/mock_user.go`: The generated mock code. See ../update_mocks.sh
for the command used to generate it.
To run the test,
go test github.com/golang/mock/sample

View File

@@ -0,0 +1,8 @@
//go:generate mockgen -destination mock/concurrent_mock.go github.com/golang/mock/sample/concurrent Math
// Package concurrent demonstrates how to use gomock with goroutines.
package concurrent
type Math interface {
Sum(a, b int) int
}

View File

@@ -0,0 +1,46 @@
package concurrent
import (
"testing"
"github.com/golang/mock/gomock"
"golang.org/x/net/context"
mock "github.com/golang/mock/sample/concurrent/mock"
)
func call(ctx context.Context, m Math) (int, error) {
result := make(chan int)
go func() {
result <- m.Sum(1, 2)
close(result)
}()
select {
case r := <-result:
return r, nil
case <-ctx.Done():
return 0, ctx.Err()
}
}
// testConcurrentFails is expected to fail (and is disabled). It
// demonstrates how to use gomock.WithContext to interrupt the test
// from a different goroutine.
func testConcurrentFails(t *testing.T) {
ctrl, ctx := gomock.WithContext(context.Background(), t)
defer ctrl.Finish()
m := mock.NewMockMath(ctrl)
if _, err := call(ctx, m); err != nil {
t.Error("call failed:", err)
}
}
func TestConcurrentWorks(t *testing.T) {
ctrl, ctx := gomock.WithContext(context.Background(), t)
defer ctrl.Finish()
m := mock.NewMockMath(ctrl)
m.EXPECT().Sum(1, 2).Return(3)
if _, err := call(ctx, m); err != nil {
t.Error("call failed:", err)
}
}

View File

@@ -0,0 +1,45 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/golang/mock/sample/concurrent (interfaces: Math)
// Package mock_concurrent is a generated GoMock package.
package mock_concurrent
import (
gomock "github.com/golang/mock/gomock"
reflect "reflect"
)
// MockMath is a mock of Math interface
type MockMath struct {
ctrl *gomock.Controller
recorder *MockMathMockRecorder
}
// MockMathMockRecorder is the mock recorder for MockMath
type MockMathMockRecorder struct {
mock *MockMath
}
// NewMockMath creates a new mock instance
func NewMockMath(ctrl *gomock.Controller) *MockMath {
mock := &MockMath{ctrl: ctrl}
mock.recorder = &MockMathMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockMath) EXPECT() *MockMathMockRecorder {
return m.recorder
}
// Sum mocks base method
func (m *MockMath) Sum(arg0, arg1 int) int {
ret := m.ctrl.Call(m, "Sum", arg0, arg1)
ret0, _ := ret[0].(int)
return ret0
}
// Sum indicates an expected call of Sum
func (mr *MockMathMockRecorder) Sum(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Sum", reflect.TypeOf((*MockMath)(nil).Sum), arg0, arg1)
}

17
vendor/github.com/golang/mock/sample/imp1/imp1.go generated vendored Normal file
View File

@@ -0,0 +1,17 @@
package imp1
import "bufio"
type Imp1 struct{}
type ImpT int
type ForeignEmbedded interface {
// The return value here also makes sure that
// the generated mock picks up the "bufio" import.
ForeignEmbeddedMethod() *bufio.Reader
// This method uses a type in this package,
// which should be qualified when this interface is embedded.
ImplicitPackage(s string, t ImpT, st []ImpT, pt *ImpT, ct chan ImpT)
}

3
vendor/github.com/golang/mock/sample/imp2/imp2.go generated vendored Normal file
View File

@@ -0,0 +1,3 @@
package imp2
type Imp2 struct{}

3
vendor/github.com/golang/mock/sample/imp3/imp3.go generated vendored Normal file
View File

@@ -0,0 +1,3 @@
package imp3
type Imp3 struct{}

3
vendor/github.com/golang/mock/sample/imp4/imp4.go generated vendored Normal file
View File

@@ -0,0 +1,3 @@
package imp_four
type Imp4 struct{}

View File

@@ -0,0 +1,384 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/golang/mock/sample (interfaces: Index,Embed,Embedded)
// Package mock_sample is a generated GoMock package.
package mock_sample
import (
bufio "bufio"
bytes "bytes"
gomock "github.com/golang/mock/gomock"
imp1 "github.com/golang/mock/sample/imp1"
imp2 "github.com/golang/mock/sample/imp2"
imp3 "github.com/golang/mock/sample/imp3"
imp4 "github.com/golang/mock/sample/imp4"
hash "hash"
template "html/template"
io "io"
http "net/http"
reflect "reflect"
template0 "text/template"
)
// MockIndex is a mock of Index interface
type MockIndex struct {
ctrl *gomock.Controller
recorder *MockIndexMockRecorder
}
// MockIndexMockRecorder is the mock recorder for MockIndex
type MockIndexMockRecorder struct {
mock *MockIndex
}
// NewMockIndex creates a new mock instance
func NewMockIndex(ctrl *gomock.Controller) *MockIndex {
mock := &MockIndex{ctrl: ctrl}
mock.recorder = &MockIndexMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockIndex) EXPECT() *MockIndexMockRecorder {
return m.recorder
}
// Anon mocks base method
func (m *MockIndex) Anon(arg0 string) {
m.ctrl.Call(m, "Anon", arg0)
}
// Anon indicates an expected call of Anon
func (mr *MockIndexMockRecorder) Anon(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Anon", reflect.TypeOf((*MockIndex)(nil).Anon), arg0)
}
// Chan mocks base method
func (m *MockIndex) Chan(arg0 chan int, arg1 chan<- hash.Hash) {
m.ctrl.Call(m, "Chan", arg0, arg1)
}
// Chan indicates an expected call of Chan
func (mr *MockIndexMockRecorder) Chan(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Chan", reflect.TypeOf((*MockIndex)(nil).Chan), arg0, arg1)
}
// ConcreteRet mocks base method
func (m *MockIndex) ConcreteRet() chan<- bool {
ret := m.ctrl.Call(m, "ConcreteRet")
ret0, _ := ret[0].(chan<- bool)
return ret0
}
// ConcreteRet indicates an expected call of ConcreteRet
func (mr *MockIndexMockRecorder) ConcreteRet() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ConcreteRet", reflect.TypeOf((*MockIndex)(nil).ConcreteRet))
}
// Ellip mocks base method
func (m *MockIndex) Ellip(arg0 string, arg1 ...interface{}) {
varargs := []interface{}{arg0}
for _, a := range arg1 {
varargs = append(varargs, a)
}
m.ctrl.Call(m, "Ellip", varargs...)
}
// Ellip indicates an expected call of Ellip
func (mr *MockIndexMockRecorder) Ellip(arg0 interface{}, arg1 ...interface{}) *gomock.Call {
varargs := append([]interface{}{arg0}, arg1...)
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ellip", reflect.TypeOf((*MockIndex)(nil).Ellip), varargs...)
}
// EllipOnly mocks base method
func (m *MockIndex) EllipOnly(arg0 ...string) {
varargs := []interface{}{}
for _, a := range arg0 {
varargs = append(varargs, a)
}
m.ctrl.Call(m, "EllipOnly", varargs...)
}
// EllipOnly indicates an expected call of EllipOnly
func (mr *MockIndexMockRecorder) EllipOnly(arg0 ...interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EllipOnly", reflect.TypeOf((*MockIndex)(nil).EllipOnly), arg0...)
}
// ForeignFour mocks base method
func (m *MockIndex) ForeignFour(arg0 imp4.Imp4) {
m.ctrl.Call(m, "ForeignFour", arg0)
}
// ForeignFour indicates an expected call of ForeignFour
func (mr *MockIndexMockRecorder) ForeignFour(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignFour", reflect.TypeOf((*MockIndex)(nil).ForeignFour), arg0)
}
// ForeignOne mocks base method
func (m *MockIndex) ForeignOne(arg0 imp1.Imp1) {
m.ctrl.Call(m, "ForeignOne", arg0)
}
// ForeignOne indicates an expected call of ForeignOne
func (mr *MockIndexMockRecorder) ForeignOne(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignOne", reflect.TypeOf((*MockIndex)(nil).ForeignOne), arg0)
}
// ForeignThree mocks base method
func (m *MockIndex) ForeignThree(arg0 imp3.Imp3) {
m.ctrl.Call(m, "ForeignThree", arg0)
}
// ForeignThree indicates an expected call of ForeignThree
func (mr *MockIndexMockRecorder) ForeignThree(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignThree", reflect.TypeOf((*MockIndex)(nil).ForeignThree), arg0)
}
// ForeignTwo mocks base method
func (m *MockIndex) ForeignTwo(arg0 imp2.Imp2) {
m.ctrl.Call(m, "ForeignTwo", arg0)
}
// ForeignTwo indicates an expected call of ForeignTwo
func (mr *MockIndexMockRecorder) ForeignTwo(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignTwo", reflect.TypeOf((*MockIndex)(nil).ForeignTwo), arg0)
}
// Func mocks base method
func (m *MockIndex) Func(arg0 func(http.Request) (int, bool)) {
m.ctrl.Call(m, "Func", arg0)
}
// Func indicates an expected call of Func
func (mr *MockIndexMockRecorder) Func(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Func", reflect.TypeOf((*MockIndex)(nil).Func), arg0)
}
// Get mocks base method
func (m *MockIndex) Get(arg0 string) interface{} {
ret := m.ctrl.Call(m, "Get", arg0)
ret0, _ := ret[0].(interface{})
return ret0
}
// Get indicates an expected call of Get
func (mr *MockIndexMockRecorder) Get(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Get", reflect.TypeOf((*MockIndex)(nil).Get), arg0)
}
// GetTwo mocks base method
func (m *MockIndex) GetTwo(arg0, arg1 string) (interface{}, interface{}) {
ret := m.ctrl.Call(m, "GetTwo", arg0, arg1)
ret0, _ := ret[0].(interface{})
ret1, _ := ret[1].(interface{})
return ret0, ret1
}
// GetTwo indicates an expected call of GetTwo
func (mr *MockIndexMockRecorder) GetTwo(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetTwo", reflect.TypeOf((*MockIndex)(nil).GetTwo), arg0, arg1)
}
// Map mocks base method
func (m *MockIndex) Map(arg0 map[int]hash.Hash) {
m.ctrl.Call(m, "Map", arg0)
}
// Map indicates an expected call of Map
func (mr *MockIndexMockRecorder) Map(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Map", reflect.TypeOf((*MockIndex)(nil).Map), arg0)
}
// NillableRet mocks base method
func (m *MockIndex) NillableRet() error {
ret := m.ctrl.Call(m, "NillableRet")
ret0, _ := ret[0].(error)
return ret0
}
// NillableRet indicates an expected call of NillableRet
func (mr *MockIndexMockRecorder) NillableRet() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NillableRet", reflect.TypeOf((*MockIndex)(nil).NillableRet))
}
// Other mocks base method
func (m *MockIndex) Other() hash.Hash {
ret := m.ctrl.Call(m, "Other")
ret0, _ := ret[0].(hash.Hash)
return ret0
}
// Other indicates an expected call of Other
func (mr *MockIndexMockRecorder) Other() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Other", reflect.TypeOf((*MockIndex)(nil).Other))
}
// Ptr mocks base method
func (m *MockIndex) Ptr(arg0 *int) {
m.ctrl.Call(m, "Ptr", arg0)
}
// Ptr indicates an expected call of Ptr
func (mr *MockIndexMockRecorder) Ptr(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Ptr", reflect.TypeOf((*MockIndex)(nil).Ptr), arg0)
}
// Put mocks base method
func (m *MockIndex) Put(arg0 string, arg1 interface{}) {
m.ctrl.Call(m, "Put", arg0, arg1)
}
// Put indicates an expected call of Put
func (mr *MockIndexMockRecorder) Put(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Put", reflect.TypeOf((*MockIndex)(nil).Put), arg0, arg1)
}
// Slice mocks base method
func (m *MockIndex) Slice(arg0 []int, arg1 []byte) [3]int {
ret := m.ctrl.Call(m, "Slice", arg0, arg1)
ret0, _ := ret[0].([3]int)
return ret0
}
// Slice indicates an expected call of Slice
func (mr *MockIndexMockRecorder) Slice(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Slice", reflect.TypeOf((*MockIndex)(nil).Slice), arg0, arg1)
}
// Struct mocks base method
func (m *MockIndex) Struct(arg0 struct{}) {
m.ctrl.Call(m, "Struct", arg0)
}
// Struct indicates an expected call of Struct
func (mr *MockIndexMockRecorder) Struct(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Struct", reflect.TypeOf((*MockIndex)(nil).Struct), arg0)
}
// StructChan mocks base method
func (m *MockIndex) StructChan(arg0 chan struct{}) {
m.ctrl.Call(m, "StructChan", arg0)
}
// StructChan indicates an expected call of StructChan
func (mr *MockIndexMockRecorder) StructChan(arg0 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StructChan", reflect.TypeOf((*MockIndex)(nil).StructChan), arg0)
}
// Summary mocks base method
func (m *MockIndex) Summary(arg0 *bytes.Buffer, arg1 io.Writer) {
m.ctrl.Call(m, "Summary", arg0, arg1)
}
// Summary indicates an expected call of Summary
func (mr *MockIndexMockRecorder) Summary(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Summary", reflect.TypeOf((*MockIndex)(nil).Summary), arg0, arg1)
}
// Templates mocks base method
func (m *MockIndex) Templates(arg0 template.CSS, arg1 template0.FuncMap) {
m.ctrl.Call(m, "Templates", arg0, arg1)
}
// Templates indicates an expected call of Templates
func (mr *MockIndexMockRecorder) Templates(arg0, arg1 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Templates", reflect.TypeOf((*MockIndex)(nil).Templates), arg0, arg1)
}
// MockEmbed is a mock of Embed interface
type MockEmbed struct {
ctrl *gomock.Controller
recorder *MockEmbedMockRecorder
}
// MockEmbedMockRecorder is the mock recorder for MockEmbed
type MockEmbedMockRecorder struct {
mock *MockEmbed
}
// NewMockEmbed creates a new mock instance
func NewMockEmbed(ctrl *gomock.Controller) *MockEmbed {
mock := &MockEmbed{ctrl: ctrl}
mock.recorder = &MockEmbedMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockEmbed) EXPECT() *MockEmbedMockRecorder {
return m.recorder
}
// EmbeddedMethod mocks base method
func (m *MockEmbed) EmbeddedMethod() {
m.ctrl.Call(m, "EmbeddedMethod")
}
// EmbeddedMethod indicates an expected call of EmbeddedMethod
func (mr *MockEmbedMockRecorder) EmbeddedMethod() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EmbeddedMethod", reflect.TypeOf((*MockEmbed)(nil).EmbeddedMethod))
}
// ForeignEmbeddedMethod mocks base method
func (m *MockEmbed) ForeignEmbeddedMethod() *bufio.Reader {
ret := m.ctrl.Call(m, "ForeignEmbeddedMethod")
ret0, _ := ret[0].(*bufio.Reader)
return ret0
}
// ForeignEmbeddedMethod indicates an expected call of ForeignEmbeddedMethod
func (mr *MockEmbedMockRecorder) ForeignEmbeddedMethod() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ForeignEmbeddedMethod", reflect.TypeOf((*MockEmbed)(nil).ForeignEmbeddedMethod))
}
// ImplicitPackage mocks base method
func (m *MockEmbed) ImplicitPackage(arg0 string, arg1 imp1.ImpT, arg2 []imp1.ImpT, arg3 *imp1.ImpT, arg4 chan imp1.ImpT) {
m.ctrl.Call(m, "ImplicitPackage", arg0, arg1, arg2, arg3, arg4)
}
// ImplicitPackage indicates an expected call of ImplicitPackage
func (mr *MockEmbedMockRecorder) ImplicitPackage(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ImplicitPackage", reflect.TypeOf((*MockEmbed)(nil).ImplicitPackage), arg0, arg1, arg2, arg3, arg4)
}
// RegularMethod mocks base method
func (m *MockEmbed) RegularMethod() {
m.ctrl.Call(m, "RegularMethod")
}
// RegularMethod indicates an expected call of RegularMethod
func (mr *MockEmbedMockRecorder) RegularMethod() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RegularMethod", reflect.TypeOf((*MockEmbed)(nil).RegularMethod))
}
// MockEmbedded is a mock of Embedded interface
type MockEmbedded struct {
ctrl *gomock.Controller
recorder *MockEmbeddedMockRecorder
}
// MockEmbeddedMockRecorder is the mock recorder for MockEmbedded
type MockEmbeddedMockRecorder struct {
mock *MockEmbedded
}
// NewMockEmbedded creates a new mock instance
func NewMockEmbedded(ctrl *gomock.Controller) *MockEmbedded {
mock := &MockEmbedded{ctrl: ctrl}
mock.recorder = &MockEmbeddedMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use
func (m *MockEmbedded) EXPECT() *MockEmbeddedMockRecorder {
return m.recorder
}
// EmbeddedMethod mocks base method
func (m *MockEmbedded) EmbeddedMethod() {
m.ctrl.Call(m, "EmbeddedMethod")
}
// EmbeddedMethod indicates an expected call of EmbeddedMethod
func (mr *MockEmbeddedMockRecorder) EmbeddedMethod() *gomock.Call {
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EmbeddedMethod", reflect.TypeOf((*MockEmbedded)(nil).EmbeddedMethod))
}

114
vendor/github.com/golang/mock/sample/user.go generated vendored Normal file
View File

@@ -0,0 +1,114 @@
//go:generate mockgen -destination mock_user/mock_user.go github.com/golang/mock/sample Index,Embed,Embedded
// An example package with an interface.
package user
// Random bunch of imports to test mockgen.
import "io"
import (
btz "bytes"
"hash"
"log"
"net"
"net/http"
// Two imports with the same base name.
t1 "html/template"
t2 "text/template"
)
// Dependencies outside the standard library.
import (
"github.com/golang/mock/sample/imp1"
renamed2 "github.com/golang/mock/sample/imp2"
. "github.com/golang/mock/sample/imp3"
"github.com/golang/mock/sample/imp4" // calls itself "imp_four"
)
// A bizarre interface to test corner cases in mockgen.
// This would normally be in its own file or package,
// separate from the user of it (e.g. io.Reader).
type Index interface {
Get(key string) interface{}
GetTwo(key1, key2 string) (v1, v2 interface{})
Put(key string, value interface{})
// Check that imports are handled correctly.
Summary(buf *btz.Buffer, w io.Writer)
Other() hash.Hash
Templates(a t1.CSS, b t2.FuncMap)
// A method with an anonymous argument.
Anon(string)
// Methods using foreign types outside the standard library.
ForeignOne(imp1.Imp1)
ForeignTwo(renamed2.Imp2)
ForeignThree(Imp3)
ForeignFour(imp_four.Imp4)
// A method that returns a nillable type.
NillableRet() error
// A method that returns a non-interface type.
ConcreteRet() chan<- bool
// Methods with an ellipsis argument.
Ellip(fmt string, args ...interface{})
EllipOnly(...string)
// A method with a pointer argument that we will set.
Ptr(arg *int)
// A method with a slice argument and an array return.
Slice(a []int, b []byte) [3]int
// A method with channel arguments.
Chan(a chan int, b chan<- hash.Hash)
// A method with a function argument.
Func(f func(http.Request) (int, bool))
// A method with a map argument.
Map(a map[int]hash.Hash)
// Methods with an unnamed empty struct argument.
Struct(a struct{}) // not so likely
StructChan(a chan struct{}) // a bit more common
}
// An interface with an embedded interface.
type Embed interface {
RegularMethod()
Embedded
imp1.ForeignEmbedded
}
type Embedded interface {
EmbeddedMethod()
}
// some random use of another package that isn't needed by the interface.
var _ net.Addr
// A function that we will test that uses the above interface.
// It takes a list of keys and values, and puts them in the index.
func Remember(index Index, keys []string, values []interface{}) {
for i, k := range keys {
index.Put(k, values[i])
}
err := index.NillableRet()
if err != nil {
log.Fatalf("Woah! %v", err)
}
if len(keys) > 0 && keys[0] == "a" {
index.Ellip("%d", 0, 1, 1, 2, 3)
index.Ellip("%d", 1, 3, 6, 10, 15)
index.EllipOnly("arg")
}
}
func GrabPointer(index Index) int {
var a int
index.Ptr(&a)
return a
}

161
vendor/github.com/golang/mock/sample/user_test.go generated vendored Normal file
View File

@@ -0,0 +1,161 @@
// A test that uses a mock.
package user_test
import (
"testing"
"github.com/golang/mock/gomock"
"github.com/golang/mock/sample"
"github.com/golang/mock/sample/imp1"
mock_user "github.com/golang/mock/sample/mock_user"
)
func TestRemember(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockIndex := mock_user.NewMockIndex(ctrl)
mockIndex.EXPECT().Put("a", 1) // literals work
mockIndex.EXPECT().Put("b", gomock.Eq(2)) // matchers work too
// NillableRet returns error. Not declaring it should result in a nil return.
mockIndex.EXPECT().NillableRet()
// Calls that returns something assignable to the return type.
boolc := make(chan bool)
// In this case, "chan bool" is assignable to "chan<- bool".
mockIndex.EXPECT().ConcreteRet().Return(boolc)
// In this case, nil is assignable to "chan<- bool".
mockIndex.EXPECT().ConcreteRet().Return(nil)
// Should be able to place expectations on variadic methods.
mockIndex.EXPECT().Ellip("%d", 0, 1, 1, 2, 3) // direct args
tri := []interface{}{1, 3, 6, 10, 15}
mockIndex.EXPECT().Ellip("%d", tri...) // args from slice
mockIndex.EXPECT().EllipOnly(gomock.Eq("arg"))
user.Remember(mockIndex, []string{"a", "b"}, []interface{}{1, 2})
// Check the ConcreteRet calls.
if c := mockIndex.ConcreteRet(); c != boolc {
t.Errorf("ConcreteRet: got %v, want %v", c, boolc)
}
if c := mockIndex.ConcreteRet(); c != nil {
t.Errorf("ConcreteRet: got %v, want nil", c)
}
// Try one with an action.
calledString := ""
mockIndex.EXPECT().Put(gomock.Any(), gomock.Any()).Do(func(key string, _ interface{}) {
calledString = key
})
mockIndex.EXPECT().NillableRet()
user.Remember(mockIndex, []string{"blah"}, []interface{}{7})
if calledString != "blah" {
t.Fatalf(`Uh oh. %q != "blah"`, calledString)
}
// Use Do with a nil arg.
mockIndex.EXPECT().Put("nil-key", gomock.Any()).Do(func(key string, value interface{}) {
if value != nil {
t.Errorf("Put did not pass through nil; got %v", value)
}
})
mockIndex.EXPECT().NillableRet()
user.Remember(mockIndex, []string{"nil-key"}, []interface{}{nil})
}
func TestVariadicFunction(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockIndex := mock_user.NewMockIndex(ctrl)
mockIndex.EXPECT().Ellip("%d", 5, 6, 7, 8).Do(func(format string, nums ...int) {
sum := 0
for _, value := range nums {
sum += value
}
if sum != 26 {
t.Errorf("Expected 7, got %d", sum)
}
})
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
sum := 0
for _, value := range nums {
sum += value
}
if sum != 10 {
t.Errorf("Expected 7, got %d", sum)
}
})
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
sum := 0
for _, value := range nums {
sum += value
}
if sum != 0 {
t.Errorf("Expected 0, got %d", sum)
}
})
mockIndex.EXPECT().Ellip("%d", gomock.Any()).Do(func(format string, nums ...int) {
sum := 0
for _, value := range nums {
sum += value
}
if sum != 0 {
t.Errorf("Expected 0, got %d", sum)
}
})
mockIndex.EXPECT().Ellip("%d").Do(func(format string, nums ...int) {
sum := 0
for _, value := range nums {
sum += value
}
if sum != 0 {
t.Errorf("Expected 0, got %d", sum)
}
})
mockIndex.Ellip("%d", 1, 2, 3, 4) // Match second matcher.
mockIndex.Ellip("%d", 5, 6, 7, 8) // Match first matcher.
mockIndex.Ellip("%d", 0)
mockIndex.Ellip("%d")
mockIndex.Ellip("%d")
}
func TestGrabPointer(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockIndex := mock_user.NewMockIndex(ctrl)
mockIndex.EXPECT().Ptr(gomock.Any()).SetArg(0, 7) // set first argument to 7
i := user.GrabPointer(mockIndex)
if i != 7 {
t.Errorf("Expected 7, got %d", i)
}
}
func TestEmbeddedInterface(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockEmbed := mock_user.NewMockEmbed(ctrl)
mockEmbed.EXPECT().RegularMethod()
mockEmbed.EXPECT().EmbeddedMethod()
mockEmbed.EXPECT().ForeignEmbeddedMethod()
mockEmbed.RegularMethod()
mockEmbed.EmbeddedMethod()
var emb imp1.ForeignEmbedded = mockEmbed // also does interface check
emb.ForeignEmbeddedMethod()
}
func TestExpectTrueNil(t *testing.T) {
// Make sure that passing "nil" to EXPECT (thus as a nil interface value),
// will correctly match a nil concrete type.
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockIndex := mock_user.NewMockIndex(ctrl)
mockIndex.EXPECT().Ptr(nil) // this nil is a nil interface{}
mockIndex.Ptr(nil) // this nil is a nil *int
}