func TestSort(t *testing.T) { expect := expect.New(t) v := []string{ "SomeLongThing", "Something", "Thing", "thing", "thisIsBad", "thingimajigger", "thingy", "thisIsNotAGoodMatch", "Thang", "tang", "bacon", "eggs", } expect(scoring.Sort(v, "thing")).To.Equal([]string{ "thing", "Thing", "thingy", "thingimajigger", "Thang", "tang", "thisIsBad", "thisIsNotAGoodMatch", "Something", "SomeLongThing", }) }
func TestLoad(t *testing.T) { expect := expect.New(t) expect(func() { packages.Load("foo") }).To.Panic() dirs := packages.Load(".") expect(dirs).To.Have.Len(1) wd, err := os.Getwd() expect(err).To.Be.Nil() expect(dirs[0].Path()).To.Equal(wd) expect(dirs[0].Packages()).To.Have.Keys("packages", "packages_test") dirs = packages.Load("github.com/nelsam/hel/mocks") expect(dirs).To.Have.Len(1) expectedPath := strings.TrimSuffix(wd, "packages") + "mocks" expect(dirs[0].Path()).To.Equal(expectedPath) dirs = packages.Load("github.com/nelsam/hel/...") expect(dirs).To.Have.Len(4) pkg, err := dirs[0].Import("path/filepath", "filepath") expect(err).To.Be.Nil() expect(pkg).Not.To.Be.Nil() _, err = dirs[0].Import("path/filepath", "foo") expect(err).Not.To.Be.Nil() expect(err.Error()).To.Equal("Could not find package foo") }
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)) }
func TestUnicode(t *testing.T) { expect := expect.New(t) src := ` package foo func µ() string { var þ = "Ωð" return þ } ` s := syntax.New(syntax.DefaultTheme) err := s.Parse(src) expect(err).To.Be.Nil() layers := s.Layers() keywords := layers[syntax.DefaultTheme.Colors.Keyword] expect(keywords.Spans()).To.Have.Len(4) expect(keywords.Spans()[2]).To.Pass(position{src: src, match: "var"}) expect(keywords.Spans()[3]).To.Pass(position{src: src, match: "return"}) strings := layers[syntax.DefaultTheme.Colors.String] expect(strings.Spans()).To.Have.Len(1) expect(strings.Spans()[0]).To.Pass(position{src: src, match: `"Ωð"`}) }
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)) }
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)) }
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)) }
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)) }
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") }
func Array(t *testing.T) { expect := expect.New(t) src := ` package foo func main() { var v [3]string v := [...]string{"foo"} } ` s := syntax.New(syntax.DefaultTheme) err := s.Parse(src) expect(err).To.Be.Nil().Else.FailNow() layers := s.Layers() // Ditch the function color syntax.DefaultTheme.Rainbow.New() arrParens := layers[syntax.DefaultTheme.Rainbow.New()] expect(arrParens.Spans()).To.Have.Len(6).Else.FailNow() expect(arrParens.Spans()[0]).To.Pass(position{src: src, match: "["}) expect(arrParens.Spans()[1]).To.Pass(position{src: src, match: "]"}) expect(arrParens.Spans()[2]).To.Pass(position{src: src, match: "[", idx: 1}) expect(arrParens.Spans()[3]).To.Pass(position{src: src, match: "]", idx: 1}) expect(arrParens.Spans()[4]).To.Pass(position{src: src, match: "{", idx: 1}) expect(arrParens.Spans()[5]).To.Pass(position{src: src, match: "}"}) typs := layers[syntax.DefaultTheme.Colors.Type] expect(typs.Spans()).To.Have.Len(2).Else.FailNow() expect(typs.Spans()[0]).To.Pass(position{src: src, match: "string"}) expect(typs.Spans()[1]).To.Pass(position{src: src, match: "string", idx: 1}) }
func AssignStmt(t *testing.T) { expect := expect.New(t) src := ` package foo func main() { x := 0.1 y = "foo" }` s := syntax.New(syntax.DefaultTheme) err := s.Parse(src) expect(err).To.Be.Nil() layers := s.Layers() expect(layers).To.Have.Len(6) nums := layers[syntax.DefaultTheme.Colors.Num] expect(nums.Spans()).To.Have.Len(1) expect(nums.Spans()[0]).To.Pass(position{src: src, match: "0.1"}) strings := layers[syntax.DefaultTheme.Colors.String] expect(strings.Spans()).To.Have.Len(1) expect(strings.Spans()[0]).To.Pass(position{src: src, match: `"foo"`}) }
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)) }
func FuncBody(t *testing.T) { expect := expect.New(t) src := ` package foo func (*Foo) Foo(baz Baz) { baz.Bar() } ` s := syntax.New(syntax.DefaultTheme) err := s.Parse(src) expect(err).To.Be.Nil().Else.FailNow() layers := s.Layers() firstParens := layers[syntax.DefaultTheme.Rainbow.New()] expect(firstParens.Spans()).To.Have.Len(6).Else.FailNow() expect(firstParens.Spans()[0]).To.Pass(position{src: src, match: "("}) expect(firstParens.Spans()[1]).To.Pass(position{src: src, match: ")"}) expect(firstParens.Spans()[2]).To.Pass(position{src: src, match: "(", idx: 1}) expect(firstParens.Spans()[3]).To.Pass(position{src: src, match: ")", idx: 1}) expect(firstParens.Spans()[4]).To.Pass(position{src: src, match: "{"}) expect(firstParens.Spans()[5]).To.Pass(position{src: src, match: "}"}) secondParens := layers[syntax.DefaultTheme.Rainbow.New()] expect(secondParens.Spans()).To.Have.Len(2).Else.FailNow() expect(secondParens.Spans()[0]).To.Pass(position{src: src, match: "(", idx: 2}) expect(secondParens.Spans()[1]).To.Pass(position{src: src, match: ")", idx: 2}) }
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)) }
func TestLoad_TestFilesInTestPackage(t *testing.T) { expect := expect.New(t) mockGoDir := newMockGoDir() mockGoDir.PathOutput.Path <- "/some/path" mockGoDir.PackagesOutput.Packages <- map[string]*ast.Package{ "foo": { Name: "foo", Files: map[string]*ast.File{ "foo.go": parse(expect, "type Foo interface{}"), }, }, "foo_test": { Name: "foo_test", Files: map[string]*ast.File{ "foo_test.go": parse(expect, "type Bar interface{}"), }, }, } found := types.Load(mockGoDir) expect(found).To.Have.Len(1).Else.FailNow() expect(found[0].Len()).To.Equal(1) expect(found[0].Package()).To.Equal("foo") expect(found[0].TestPackage()).To.Equal("foo_test") }
// TestAnonymousError is testing the only case (as of go 1.7) where // a builtin is an interface type. func TestAnonymousError(t *testing.T) { expect := expect.New(t) mockGoDir := newMockGoDir() mockGoDir.PathOutput.Path <- "/some/path" mockGoDir.PackagesOutput.Packages <- map[string]*ast.Package{ "foo": { Name: "foo", Files: map[string]*ast.File{ "foo.go": parse(expect, ` type Foo interface{ error }`), }, }, } found := types.Load(mockGoDir) expect(found).To.Have.Len(1).Else.FailNow() typs := found[0].ExportedTypes() expect(typs).To.Have.Len(1).Else.FailNow() spec := typs[0] expect(spec).Not.To.Be.Nil().Else.FailNow() inter := spec.Type.(*ast.InterfaceType) expect(inter.Methods.List).To.Have.Len(1).Else.FailNow() err := inter.Methods.List[0] expect(err.Names[0].String()).To.Equal("Error") _, isFunc := err.Type.(*ast.FuncType) expect(isFunc).To.Be.Ok() }
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)) }
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)) }
func TestAliasedImportedDependencies(t *testing.T) { expect := expect.New(t) mockGoDir := newMockGoDir() mockGoDir.PathOutput.Path <- "/some/path" mockGoDir.PackagesOutput.Packages <- map[string]*ast.Package{ "bar": { Name: "bar", Files: map[string]*ast.File{ "foo.go": parse(expect, ` import baz "some/path/to/foo" type Bar interface{ Bar(baz.Foo) baz.Bar }`), }, }, } close(mockGoDir.ImportOutput.Err) pkgName := "foo" pkg := &ast.Package{ Name: pkgName, Files: map[string]*ast.File{ "foo.go": parse(expect, ` type Foo interface { Foo() } type Bar interface { Bar() }`), }, } mockGoDir.ImportOutput.Pkg <- pkg mockGoDir.ImportOutput.Name <- pkgName mockGoDir.ImportOutput.Pkg <- pkg mockGoDir.ImportOutput.Name <- pkgName found := types.Load(mockGoDir) expect(mockGoDir.ImportCalled).To.Have.Len(2) expect(<-mockGoDir.ImportInput.Path).To.Equal("some/path/to/foo") expect(found).To.Have.Len(1).Else.FailNow() mockables := found[0].ExportedTypes() expect(mockables).To.Have.Len(1) dependencies := found[0].Dependencies(mockables[0].Type.(*ast.InterfaceType)) expect(dependencies).To.Have.Len(2) names := make(map[string]bool) for _, dependent := range dependencies { expect(dependent.PkgName).To.Equal("baz") expect(dependent.PkgPath).To.Equal("some/path/to/foo") names[dependent.Type.Name.String()] = true } expect(names).To.Equal(map[string]bool{"Foo": true, "Bar": true}) }
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") }
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") }
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") }
func TestOutput_ReceiverNameInArgs(t *testing.T) { expect := expect.New(t) types := []*ast.TypeSpec{ typeSpec(expect, ` type Foo interface { Foo(m string) }`), } mockFinder := newMockTypeFinder() close(mockFinder.DependenciesOutput.Dependencies) mockFinder.ExportedTypesOutput.Types <- types m, err := mocks.Generate(mockFinder) expect(err).To.Be.Nil().Else.FailNow() buf := bytes.Buffer{} m.Output("foo", "test/without_imports", 100, &buf) expected, err := format.Source([]byte(` // This file was generated by github.com/nelsam/hel. Do not // edit this code by hand unless you *really* know what you're // doing. Expect any changes made manually to be overwritten // the next time hel regenerates this file. package foo type mockFoo struct { FooCalled chan bool FooInput struct { M chan string } } func newMockFoo() *mockFoo { m := &mockFoo{} m.FooCalled = make(chan bool, 100) m.FooInput.M = make(chan string, 100) return m } func (m *mockFoo) Foo(m_ string) { m.FooCalled <- true m.FooInput.M <- m_ } `)) expect(err).To.Be.Nil().Else.FailNow() expect(buf.String()).To.Equal(string(expected)) }
func TestFilter(t *testing.T) { expect := expect.New(t) mockGoDir := newMockGoDir() mockGoDir.PathOutput.Path <- "/some/path" mockGoDir.PackagesOutput.Packages <- map[string]*ast.Package{ "foo": { Name: "foo", Files: map[string]*ast.File{ "foo.go": parse(expect, ` type Foo interface {} type Bar interface {} type FooBar interface {} type BarFoo interface {} `), }, }, } found := types.Load(mockGoDir) expect(found).To.Have.Len(1).Else.FailNow() expect(found[0].Len()).To.Equal(4) notFiltered := found.Filter() expect(notFiltered).To.Have.Len(1).Else.FailNow() expect(notFiltered[0].Len()).To.Equal(4) foos := found.Filter("Foo") expect(foos).To.Have.Len(1).Else.FailNow() expect(foos[0].Len()).To.Equal(1) expect(foos[0].ExportedTypes()[0].Name.String()).To.Equal("Foo") fooPrefixes := found.Filter("Foo.*") expect(fooPrefixes).To.Have.Len(1).Else.FailNow() expect(fooPrefixes[0].Len()).To.Equal(2) expectNamesToMatch(expect, fooPrefixes[0].ExportedTypes(), "Foo", "FooBar") fooPostfixes := found.Filter(".*Foo") expect(fooPostfixes).To.Have.Len(1).Else.FailNow() expect(fooPostfixes[0].Len()).To.Equal(2) expectNamesToMatch(expect, fooPostfixes[0].ExportedTypes(), "Foo", "BarFoo") fooContainers := found.Filter("Foo.*", ".*Foo") expect(fooContainers).To.Have.Len(1).Else.FailNow() expect(fooContainers[0].Len()).To.Equal(3) expectNamesToMatch(expect, fooContainers[0].ExportedTypes(), "Foo", "FooBar", "BarFoo") }
func TestLocalDependencies(t *testing.T) { expect := expect.New(t) mockGoDir := newMockGoDir() mockGoDir.PathOutput.Path <- "/some/path" mockGoDir.PackagesOutput.Packages <- map[string]*ast.Package{ "bar": { Name: "bar", Files: map[string]*ast.File{ "bar.go": parse(expect, ` type Bar interface{ Bar(Foo) Foo }`), "foo.go": parse(expect, ` type Foo interface { Foo() }`), }, }, } found := types.Load(mockGoDir) expect(found).To.Have.Len(1).Else.FailNow() mockables := found[0].ExportedTypes() expect(mockables).To.Have.Len(2) var foo, bar *ast.TypeSpec for _, mockable := range mockables { switch mockable.Name.String() { case "Bar": bar = mockable case "Foo": foo = mockable } } expect(bar).Not.To.Be.Nil().Else.FailNow() dependencies := found[0].Dependencies(bar.Type.(*ast.InterfaceType)) expect(dependencies).To.Have.Len(1).Else.FailNow() expect(dependencies[0].Type).To.Equal(foo) expect(dependencies[0].PkgName).To.Equal("") expect(dependencies[0].PkgPath).To.Equal("") }
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()) }
func CommClause(t *testing.T) { expect := expect.New(t) src := ` package foo func main() { switch { case foo == "bar": x = 1 case y == false: default: println("bacon") } }` s := syntax.New(syntax.DefaultTheme) err := s.Parse(src) expect(err).To.Be.Nil() layers := s.Layers() expect(layers).To.Have.Len(9) keywords := layers[syntax.DefaultTheme.Colors.Keyword] expect(keywords.Spans()).To.Have.Len(6) expect(keywords.Spans()[3]).To.Pass(position{src: src, match: "case"}) expect(keywords.Spans()[4]).To.Pass(position{src: src, match: "case", idx: 1}) expect(keywords.Spans()[5]).To.Pass(position{src: src, match: "default"}) strings := layers[syntax.DefaultTheme.Colors.String] expect(strings.Spans()).To.Have.Len(2) expect(strings.Spans()[0]).To.Pass(position{src: src, match: `"bar"`}) expect(strings.Spans()[1]).To.Pass(position{src: src, match: `"bacon"`}) nums := layers[syntax.DefaultTheme.Colors.Num] expect(nums.Spans()).To.Have.Len(1) expect(nums.Spans()[0]).To.Pass(position{src: src, match: "1"}) builtins := layers[syntax.DefaultTheme.Colors.Builtin] expect(builtins.Spans()).To.Have.Len(1) expect(builtins.Spans()[0]).To.Pass(position{src: src, match: "println"}) }
func TestPackageDocs(t *testing.T) { expect := expect.New(t) src := ` // Package foo does stuff. // It is also a thing. package foo ` s := syntax.New(syntax.DefaultTheme) err := s.Parse(src) expect(err).To.Be.Nil() layers := s.Layers() expect(layers).To.Have.Len(2) comments := layers[syntax.DefaultTheme.Colors.Comment] expect(comments.Spans()).To.Have.Len(1) comment := "// Package foo does stuff.\n" + "// It is also a thing." expect(comments.Spans()[0]).To.Pass(position{src: src, match: comment}) }
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)) }
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)) }