當前位置: 首頁>>代碼示例>>Golang>>正文


Golang mocks.For函數代碼示例

本文整理匯總了Golang中github.com/nelsam/hel/mocks.For函數的典型用法代碼示例。如果您正苦於以下問題:Golang For函數的具體用法?Golang For怎麽用?Golang For使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了For函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: TestMockConstructor

func TestMockConstructor(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
  Foo(foo string) int
  Bar(bar int) string
 }
 `)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()

	expected, err := format.Source([]byte(`
 package foo

 func newMockFoo() *mockFoo {
  m := &mockFoo{}
  m.FooCalled = make(chan bool, 300)
  m.FooInput.Foo = make(chan string, 300)
  m.FooOutput.Ret0 = make(chan int, 300)
  m.BarCalled = make(chan bool, 300)
  m.BarInput.Bar = make(chan int, 300)
  m.BarOutput.Ret0 = make(chan string, 300)
  return m
 }`))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{m.Constructor(300)}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:nelsam,項目名稱:hel,代碼行數:30,代碼來源:mock_test.go

示例2: TestMockSimpleMethod

func TestMockSimpleMethod(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
         Foo()
 }`)
	mock, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))

	expected, err := format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo() {
   m.FooCalled <- true
 }`))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))

	fields := method.Fields()
	expect(fields).To.Have.Len(1).Else.FailNow()

	expect(fields[0].Names[0].Name).To.Equal("FooCalled")
	ch, ok := fields[0].Type.(*ast.ChanType)
	expect(ok).To.Be.Ok().Else.FailNow()
	expect(ch.Dir).To.Equal(ast.SEND | ast.RECV)
	ident, ok := ch.Value.(*ast.Ident)
	expect(ident.Name).To.Equal("bool")
}
開發者ID:nelsam,項目名稱:hel,代碼行數:32,代碼來源:method_test.go

示例3: TestMockTypeDecl_DirectionalChansGetParens

func TestMockTypeDecl_DirectionalChansGetParens(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
  Foo(foo chan<- int) <-chan int
 }
 `)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()

	expected, err := format.Source([]byte(`
 package foo

 type mockFoo struct {
  FooCalled chan bool
  FooInput struct {
   Foo chan (chan<- int)
  }
  FooOutput struct {
   Ret0 chan (<-chan int)
  }
 }
 `))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{m.Decl()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:nelsam,項目名稱:hel,代碼行數:29,代碼來源:mock_test.go

示例4: TestMockTypeDecl_ParamsWithoutTypes

func TestMockTypeDecl_ParamsWithoutTypes(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
type Foo interface {
 Foo(foo, bar string)
}
`)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()

	expected, err := format.Source([]byte(`
package foo

type mockFoo struct {
 FooCalled chan bool
 FooInput struct {
  Foo, Bar chan string
 }
}
`))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{m.Decl()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:nelsam,項目名稱:hel,代碼行數:26,代碼來源:mock_test.go

示例5: TestMockConstructor_VariadicParams

func TestMockConstructor_VariadicParams(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
  Foo(foo ...int)
 }
 `)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()

	expected, err := format.Source([]byte(`
 package foo

 func newMockFoo() *mockFoo {
  m := &mockFoo{}
  m.FooCalled = make(chan bool, 200)
  m.FooInput.Foo = make(chan []int, 200)
  return m
 }
 `))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{m.Constructor(200)}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:nelsam,項目名稱:hel,代碼行數:26,代碼來源:mock_test.go

示例6: TestMockMethodUnnamedValues

func TestMockMethodUnnamedValues(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
   Foo(int, string) (string, error)
 }`)
	mock, err := mocks.For(spec)
	expect(err).To.Be.Nil()
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))

	expected, err := format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo(arg0 int, arg1 string) (string, error) {
   m.FooCalled <- true
   m.FooInput.Arg0 <- arg0
   m.FooInput.Arg1 <- arg1
   return <-m.FooOutput.Ret0, <-m.FooOutput.Ret1
 }`))
	expect(err).To.Be.Nil()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:jasonkeene,項目名稱:hel,代碼行數:25,代碼來源:method_test.go

示例7: TestMockMethodLocalTypeNesting

func TestMockMethodLocalTypeNesting(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
   Foo(bar []Bar, bacon map[Foo]Bar) (baz []Baz, eggs map[Foo]Bar)
 }`)
	mock, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))
	method.PrependLocalPackage("foo")

	expected, err := format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo(bar []foo.Bar, bacon map[foo.Foo]foo.Bar) (baz []foo.Baz, eggs map[foo.Foo]foo.Bar) {
   m.FooCalled <- true
   m.FooInput.Bar <- bar
   m.FooInput.Bacon <- bacon
   return <-m.FooOutput.Baz, <-m.FooOutput.Eggs
 }`))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:nelsam,項目名稱:hel,代碼行數:26,代碼來源:method_test.go

示例8: TestMockMethodParams

func TestMockMethodParams(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
         Foo(foo, bar string, baz int)
 }`)
	mock, err := mocks.For(spec)
	expect(err).To.Be.Nil()
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))

	expected, err := format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo(foo, bar string, baz int) {
   m.FooCalled <- true
   m.FooInput.Foo <- foo
   m.FooInput.Bar <- bar
   m.FooInput.Baz <- baz
 }`))
	expect(err).To.Be.Nil()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:jasonkeene,項目名稱:hel,代碼行數:25,代碼來源:method_test.go

示例9: TestMockConstructor_DirectionalChansGetParens

func TestMockConstructor_DirectionalChansGetParens(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
  Foo(foo chan<- int) <-chan int
 }
 `)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()

	expected, err := format.Source([]byte(`
 package foo

 func newMockFoo() *mockFoo {
  m := &mockFoo{}
  m.FooCalled = make(chan bool, 200)
  m.FooInput.Foo = make(chan (chan<- int), 200)
  m.FooOutput.Ret0 = make(chan (<-chan int), 200)
  return m
 }
 `))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{m.Constructor(200)}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:nelsam,項目名稱:hel,代碼行數:27,代碼來源:mock_test.go

示例10: TestMockTypeDecl_VariadicMethods

func TestMockTypeDecl_VariadicMethods(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
type Foo interface {
 Foo(foo ...int)
}
`)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil()
	expect(m).Not.To.Be.Nil()

	expected, err := format.Source([]byte(`
 package foo

 type mockFoo struct {
  FooCalled chan bool
  FooInput struct {
   Foo chan []int
  }
 }
 `))
	expect(err).To.Be.Nil()

	src := source(expect, "foo", []ast.Decl{m.Decl()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:jasonkeene,項目名稱:hel,代碼行數:27,代碼來源:mock_test.go

示例11: TestNewErrorsForNonInterfaceTypes

func TestNewErrorsForNonInterfaceTypes(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, "type Foo func()")
	_, err := mocks.For(spec)
	expect(err).Not.To.Be.Nil().Else.FailNow()
	expect(err.Error()).To.Equal("TypeSpec.Type expected to be *ast.InterfaceType, was *ast.FuncType")
}
開發者ID:nelsam,項目名稱:hel,代碼行數:8,代碼來源:mock_test.go

示例12: TestMockName

func TestMockName(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, "type Foo interface{}")
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()
	expect(m.Name()).To.Equal("mockFoo")
}
開發者ID:nelsam,項目名稱:hel,代碼行數:8,代碼來源:mock_test.go

示例13: TestMockMethodParams

func TestMockMethodParams(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
         Foo(foo, bar string, baz int)
 }`)
	mock, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))

	expected, err := format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo(foo, bar string, baz int) {
   m.FooCalled <- true
   m.FooInput.Foo <- foo
   m.FooInput.Bar <- bar
   m.FooInput.Baz <- baz
 }`))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))

	fields := method.Fields()
	expect(fields).To.Have.Len(2)

	expect(fields[0].Names[0].Name).To.Equal("FooCalled")
	ch, ok := fields[0].Type.(*ast.ChanType)
	expect(ok).To.Be.Ok().Else.FailNow()
	expect(ch.Dir).To.Equal(ast.SEND | ast.RECV)
	ident, ok := ch.Value.(*ast.Ident)
	expect(ident.Name).To.Equal("bool")

	expect(fields[1].Names[0].Name).To.Equal("FooInput")
	input, ok := fields[1].Type.(*ast.StructType)
	expect(ok).To.Be.Ok().Else.FailNow()
	expect(input.Fields.List).To.Have.Len(2).Else.FailNow()

	fooBar := input.Fields.List[0]
	expect(fooBar.Names).To.Have.Len(2).Else.FailNow()
	expect(fooBar.Names[0].Name).To.Equal("Foo")
	expect(fooBar.Names[1].Name).To.Equal("Bar")
	ch, ok = fooBar.Type.(*ast.ChanType)
	expect(ok).To.Be.Ok().Else.FailNow()
	expect(ch.Dir).To.Equal(ast.SEND | ast.RECV)
	ident, ok = ch.Value.(*ast.Ident)
	expect(ident.Name).To.Equal("string")

	baz := input.Fields.List[1]
	expect(baz.Names[0].Name).To.Equal("Baz")
	ch, ok = baz.Type.(*ast.ChanType)
	expect(ok).To.Be.Ok().Else.FailNow()
	expect(ch.Dir).To.Equal(ast.SEND | ast.RECV)
	ident, ok = ch.Value.(*ast.Ident)
	expect(ident.Name).To.Equal("int")
}
開發者ID:nelsam,項目名稱:hel,代碼行數:58,代碼來源:method_test.go

示例14: TestMockAst

func TestMockAst(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
  Bar(bar string)
  Baz() (baz int)
 }`)
	m, err := mocks.For(spec)
	expect(err).To.Be.Nil().Else.FailNow()

	decls := m.Ast(300)
	expect(decls).To.Have.Len(4).Else.FailNow()
	expect(decls[0]).To.Equal(m.Decl())
	expect(decls[1]).To.Equal(m.Constructor(300))
	expect(m.Methods()).To.Have.Len(2).Else.FailNow()
	expect(decls[2]).To.Equal(m.Methods()[0].Ast())
	expect(decls[3]).To.Equal(m.Methods()[1].Ast())
}
開發者ID:nelsam,項目名稱:hel,代碼行數:19,代碼來源:mock_test.go

示例15: TestMockMethodLocalTypes

func TestMockMethodLocalTypes(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
   Foo(bar bar.Bar, baz string) (Foo, error)
 }`)
	mock, err := mocks.For(spec)
	expect(err).To.Be.Nil()
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))

	expected, err := format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo(bar bar.Bar, baz string) (Foo, error) {
   m.FooCalled <- true
   m.FooInput.Bar <- bar
   m.FooInput.Baz <- baz
   return <-m.FooOutput.Ret0, <-m.FooOutput.Ret1
 }`))
	expect(err).To.Be.Nil()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))

	method.PrependLocalPackage("foo")

	expected, err = format.Source([]byte(`
 package foo

 func (m *mockFoo) Foo(bar bar.Bar, baz string) (foo.Foo, error) {
   m.FooCalled <- true
   m.FooInput.Bar <- bar
   m.FooInput.Baz <- baz
   return <-m.FooOutput.Ret0, <-m.FooOutput.Ret1
 }`))
	expect(err).To.Be.Nil()

	src = source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))
}
開發者ID:jasonkeene,項目名稱:hel,代碼行數:41,代碼來源:method_test.go


注:本文中的github.com/nelsam/hel/mocks.For函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。