<commit_before>package mocks_test

import (
	"bytes"
	"go/ast"
	"go/format"
	"testing"

	"github.com/a8m/expect"
	"github.com/nelsam/hel/mocks"
)

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

	types := []*ast.TypeSpec{
		typeSpec(expect, `
  type Foo interface {
   Bar() int
  }`),
		typeSpec(expect, `
  type Bar interface {
   Foo(foo string)
   Baz()
  }`),
	}

	mockFinder := newMockTypeFinder()
	mockFinder.ExportedTypesOutput.ret0 <- types
	m, err := mocks.Generate(mockFinder)
	expect(err).To.Be.Nil()
	expect(m).To.Have.Len(2)
	expect(m[0]).To.Equal(mockFor(expect, types[0]))
	expect(m[1]).To.Equal(mockFor(expect, types[1]))
}

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

	types := []*ast.TypeSpec{
		typeSpec(expect, `
  type Foo interface {
   Bar() int
  }`),
		typeSpec(expect, `
  type Bar interface {
   Foo(foo string)
   Baz()
  }`),
	}

	mockFinder := newMockTypeFinder()
	mockFinder.ExportedTypesOutput.ret0 <- types
	m, err := mocks.Generate(mockFinder)
	expect(err).To.Be.Nil()

	buf := bytes.Buffer{}
	m.Output("foo", 100, &buf)

	// TODO: For some reason, functions are coming out without
	// whitespace between them.  We need to figure that out.
	expected, err := format.Source([]byte(`
 package foo
 
 type mockFoo struct {
  BarCalled chan bool
  BarOutput struct {
   ret0 chan int
  }
 }
 
 func newMockFoo() *mockFoo {
  m := &mockFoo{}
  m.BarCalled = make(chan bool, 100)
  m.BarOutput.ret0 = make(chan int, 100)
  return m
 } 
 func (m *mockFoo) Bar() int {
  m.BarCalled <- true
  return <-m.BarOutput.ret0
 }
 
 type mockBar struct {
  FooCalled chan bool
  FooInput struct {
   foo chan string
  }
  BazCalled chan bool
 }
 
 func newMockBar() *mockBar {
  m := &mockBar{}
  m.FooCalled = make(chan bool, 100)
  m.FooInput.foo = make(chan string, 100)
  m.BazCalled = make(chan bool, 100)
  return m
 }
 func (m *mockBar) Foo(foo string) {
  m.FooCalled <- true
  m.FooInput.foo <- foo<commit_msg>Add test for adding package name to local types<commit_after>package mocks_test

import (
	"bytes"
	"go/ast"
	"go/format"
	"testing"

	"github.com/a8m/expect"
	"github.com/nelsam/hel/mocks"
)

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

	types := []*ast.TypeSpec{
		typeSpec(expect, `
  type Foo interface {
   Bar() int
  }`),
		typeSpec(expect, `
  type Bar interface {
   Foo(foo string)
   Baz()
  }`),
	}

	mockFinder := newMockTypeFinder()
	mockFinder.ExportedTypesOutput.ret0 <- types
	m, err := mocks.Generate(mockFinder)
	expect(err).To.Be.Nil()
	expect(m).To.Have.Len(2)
	expect(m[0]).To.Equal(mockFor(expect, types[0]))
	expect(m[1]).To.Equal(mockFor(expect, types[1]))
}

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

	types := []*ast.TypeSpec{
		typeSpec(expect, `
  type Foo interface {
   Bar() int
  }`),
		typeSpec(expect, `
  type Bar interface {
   Foo(foo string) Foo
   Baz()
  }`),
	}

	mockFinder := newMockTypeFinder()
	mockFinder.ExportedTypesOutput.ret0 <- types
	m, err := mocks.Generate(mockFinder)
	expect(err).To.Be.Nil()

	buf := bytes.Buffer{}
	m.Output("foo", 100, &buf)

	// TODO: For some reason, functions are coming out without
	// whitespace between them.  We need to figure that out.
	expected, err := format.Source([]byte(`
 package foo
 
 type mockFoo struct {
  BarCalled chan bool
  BarOutput struct {
   ret0 chan int
  }
 }
 
 func newMockFoo() *mockFoo {
  m := &mockFoo{}
  m.BarCalled = make(chan bool, 100)
  m.BarOutput.ret0 = make(chan int, 100)
  return m
 } 
 func (m *mockFoo) Bar() int {
  m.BarCalled <- true
  return <-m.BarOutput.ret0
 }
 
 type mockBar struct {
  FooCalled chan bool
  FooInput struct {
   foo chan string
  }
  FooOutput struct {
   ret0 chan Foo
  }
  BazCalled chan bool
 }
 
 func newMockBar() *mockBar {
  m := &mockBar{}
  m.FooCalled = make(chan bool, 100)
  m.FooInput.foo = make(chan string, 100)
  m.FooOutput.ret0 = make(chan Foo, 100)
  m.BazCalled = make(chan bool, 100)
  return m
 }
 func (m *mockBar) Foo(foo string) Foo {
  m.FooCalled <- true
  m.FooInput.foo <- foo
  return <-m.FooOutput.ret0
 } 
 func (m *mockBar) Baz() {
  m.BazCalled <- true
 }
 `))
	expect(err).To.Be.Nil()
	expect(buf.String()).To.Equal(string(expected))

	m.PrependLocalPackage("foo")
	buf = bytes.Buffer{}
	m.Output("foo_test", 100, &buf)

	expected, err = format.Source([]byte(`
 package foo_test
 
 type mockFoo struct {
  BarCalled chan bool
  BarOutput struct {
   ret0 chan int
  }
 }
 
 func newMockFoo() *mockFoo {
  m := &mockFoo{}
  m.BarCalled = make(chan bool, 100)
  m.BarOutput.ret0 = make(chan int, 100)
  return m
 } 
 func (m *mockFoo) Bar() int {
  m.BarCalled <- true
  return <-m.BarOutput.ret0
 }
 
 type mockBar struct {
  FooCalled chan bool
  FooInput struct {
   foo chan string
  }
  FooOutput struct {
   ret0 chan foo.Foo
  }
  BazCalled chan bool
 }
 
 func newMockBar() *mockBar {
  m := &mockBar{}
  m.FooCalled = make(chan bool, 100)
  m.FooInput.foo = make(chan string, 100)
  m.FooOutput.ret0 = make(chan foo.Foo, 100)
  m.BazCalled = make(chan bool, 100)
  return m
 }
 func (m *mockBar) Foo(foo string) foo.Foo {
  m.FooCalled <- true
  m.FooInput.foo <- foo
  return <-m.FooOutput.ret0
{'fraction_non_alphanumeric': 0.14547559328574186, 'fraction_numerical': 0.0175573991896585, 'mean_word_length': 3.327212020033389, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6395025', 'n_tokens_mistral': 2133, 'n_tokens_neox': 1970, 'n_words': 478}