コード例 #1
0
ファイル: method_test.go プロジェクト: jasonkeene/hel
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))
}
コード例 #2
0
ファイル: method_test.go プロジェクト: jasonkeene/hel
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))
}
コード例 #3
0
ファイル: method_test.go プロジェクト: nelsam/hel
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))
}
コード例 #4
0
ファイル: method_test.go プロジェクト: nelsam/hel
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")
}
コード例 #5
0
ファイル: method_test.go プロジェクト: nelsam/hel
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")
}
コード例 #6
0
ファイル: method_test.go プロジェクト: jasonkeene/hel
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))
}
コード例 #7
0
ファイル: method_test.go プロジェクト: jasonkeene/hel
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()
	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()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))
}
コード例 #8
0
ファイル: method_test.go プロジェクト: nelsam/hel
func TestMockMethodReceiverNameConflicts(t *testing.T) {
	expect := expect.New(t)

	spec := typeSpec(expect, `
 type Foo interface {
         Foo(m string)
 }`)
	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_ string) {
   m.FooCalled <- true
   m.FooInput.M <- m_
 }`))
	expect(err).To.Be.Nil().Else.FailNow()

	src := source(expect, "foo", []ast.Decl{method.Ast()}, nil)
	expect(src).To.Equal(string(expected))
}
コード例 #9
0
ファイル: method_test.go プロジェクト: nelsam/hel
func TestMockMethodWithBlockingReturn(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()
	mock.SetBlockingReturn(true)
	method := mocks.MethodFor(mock, "Foo", method(expect, spec))

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

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

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