From 975a95966976eeb2d4365a7fb236e274c54da64c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?karthick-=E0=AE=95=E0=AE=BE=E0=AE=B0=E0=AF=8D=E0=AE=A4?= =?UTF-8?q?=E0=AF=8D=E0=AE=A4=E0=AE=BF=E0=AE=95=E0=AF=8D?= Date: Mon, 28 Aug 2023 19:52:07 +0530 Subject: [PATCH] feat(validate): Validate struct name before generating (#61) * feat(types): handle types not found in input files by emitting error (#1) * Feat/handle not found types (#3) * feat(types): handle types not found in input files by emitting error * ci(actions): enable actions * ci(actions): enable actions (#4) * Ci/actiosn (#5) * ci(actions): enable actions * ci(actions): enable actions * fix(ci): remove releaser from actions (#6) * feat(validate): input file for struct provided (#7) --- .github/workflows/ci.yml | 28 +++++ .travis.yml | 30 ------ go.mod | 14 +-- go.sum | 27 ++--- ifacemaker.go | 8 +- ifacemaker_test.go | 109 +++++++++----------- maker/maker.go | 44 +++++--- maker/maker_test.go | 217 +++++++++++++++++++++++---------------- 8 files changed, 264 insertions(+), 213 deletions(-) create mode 100644 .github/workflows/ci.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..51e7348 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,28 @@ +name: test + +on: + push: + branches: + - '*' + tags: + - '*' + +permissions: + contents: write + # packages: write + # issues: write + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v3 + with: + go-version: '>=1.19.3' + cache: true + - run: go test -v -run ./... + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4d20a00..0000000 --- a/.travis.yml +++ /dev/null @@ -1,30 +0,0 @@ -language: go -sudo: required - -go: - - 1.11.x - - stable - - master - -matrix: - # Failing on the development version of Go isn't too bad. - allow_failures: - - go: master - -env: - - GO111MODULE=on - -# Override Travis's default 'go get' step, since we use Go modules -install: - - go mod download - -script: - - go test -cover ./... - -deploy: -- provider: script - skip_cleanup: true - script: curl -sL https://git.io/goreleaser | bash - on: - tags: true - condition: $TRAVIS_GO_VERSION =~ ^1\.11 diff --git a/go.mod b/go.mod index 50f53da..1bfb876 100644 --- a/go.mod +++ b/go.mod @@ -1,17 +1,17 @@ module github.com/vburenin/ifacemaker -go 1.18 +go 1.20 require ( - github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 - github.com/stretchr/testify v1.2.2 - golang.org/x/tools v0.1.10 + github.com/jessevdk/go-flags v1.5.0 + github.com/stretchr/testify v1.8.4 + golang.org/x/tools v0.11.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect - golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect - golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect + golang.org/x/mod v0.12.0 // indirect + golang.org/x/sys v0.10.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2853184..d9bd59d 100644 --- a/go.sum +++ b/go.sum @@ -1,16 +1,19 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20 h1:dAOsPLhnBzIyxu0VvmnKjlNcIlgMK+erD6VRHDtweMI= -github.com/jessevdk/go-flags v1.4.1-0.20181029123624-5de817a9aa20/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= +github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc= +github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 h1:kQgndtyPBW/JIYERgdxfwMYh3AVStj88WQTlNDi2a+o= -golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 h1:id054HUawV2/6IGm2IV8KZQjqtwAOo2CYlOToYqa0d0= -golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20= -golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= -golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc= +golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= +golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA= +golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8= +golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/ifacemaker.go b/ifacemaker.go index 591764e..0dc95b7 100644 --- a/ifacemaker.go +++ b/ifacemaker.go @@ -2,13 +2,11 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" "github.com/jessevdk/go-flags" - "github.com/vburenin/ifacemaker/maker" ) @@ -82,7 +80,11 @@ func main() { if args.Output == "" { fmt.Println(string(result)) } else { - if err := ioutil.WriteFile(args.Output, result, 0644); err != nil { + f, err := os.Create(args.Output) + if err != nil { + log.Fatal(err) + } + if _, err := f.Write(result); err != nil { log.Fatal(err) } } diff --git a/ifacemaker_test.go b/ifacemaker_test.go index 14b000a..cd07db3 100644 --- a/ifacemaker_test.go +++ b/ifacemaker_test.go @@ -5,18 +5,14 @@ import ( "fmt" "io" "os" - "strings" + "path/filepath" "testing" - assert "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/assert" ) var src = `package main -import ( - "fmt" -) - // Person contains data related to a person. type Person struct { name string @@ -36,12 +32,13 @@ func (p *Person) SetName(name string) { // Age ... func (p *Person) Age() int { - return p.Age + return p.age } // Age ... func (p *Person) SetAge(age int) int { - p.Age = age + p.age = age + return p.age } // AgeAndName ... @@ -68,7 +65,9 @@ func (p *Person) SetNameAndTelephone(name, telephone string) { func SomeFunction() string { return "Something" -}` +} + +` var src2 = `package maker @@ -117,14 +116,14 @@ func (s *Smiter) Smite(weapon Hammer) error { } ` -var srcFile = os.TempDir() + "/ifacemaker_src.go" -var srcFile2 = os.TempDir() + "/test_impl.go" -var srcFile2_ext = os.TempDir() + "/test_impl_extended.go" -var srcFile3 = os.TempDir() + "/footest/footest.go" -var srcFile4 = os.TempDir() + "/footest/smiter.go" +var srcFile = filepath.Join(os.TempDir(), "ifacemaker_src.go") +var srcFile2 = filepath.Join(os.TempDir(), "test_impl.go") +var srcFile2_ext = filepath.Join(os.TempDir(), "test_impl_extended.go") +var srcFile3 = filepath.Join(os.TempDir(), "footest", "footest.go") +var srcFile4 = filepath.Join(os.TempDir(), "footest", "smiter.go") func TestMain(m *testing.M) { - dirPath := os.TempDir() + "/footest" + dirPath := filepath.Join(os.TempDir(), "footest") if _, err := os.Stat(dirPath); os.IsNotExist(err) { err := os.Mkdir(dirPath, os.ModePerm) if err != nil { @@ -157,7 +156,6 @@ func TestMainAllArgs(t *testing.T) { out := captureStdout(func() { main() }) - expected := `// DO NOT EDIT: Auto generated package gen @@ -187,11 +185,6 @@ type PersonIface interface { } func TestMainNoIfaceComment(t *testing.T) { - os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-D"} - out := captureStdout(func() { - main() - }) - expected := `// DO NOT EDIT: Auto generated package gen @@ -216,16 +209,16 @@ type PersonIface interface { } ` + os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-D"} + out := captureStdout(func() { + main() + }) assert.Equal(t, expected, out) + } func TestMainNoCopyTypeDocs(t *testing.T) { - os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-y", "PersonIface is an interface for Person."} - out := captureStdout(func() { - main() - }) - expected := `// DO NOT EDIT: Auto generated package gen @@ -249,16 +242,15 @@ type PersonIface interface { } ` - - assert.Equal(t, expected, out) -} - -func TestMainNoCopyMethodDocs(t *testing.T) { - os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-d=false"} + os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-y", "PersonIface is an interface for Person."} out := captureStdout(func() { main() }) + assert.Equal(t, expected, out) + +} +func TestMainNoCopyMethodDocs(t *testing.T) { expected := `// DO NOT EDIT: Auto generated package gen @@ -276,16 +268,16 @@ type PersonIface interface { } ` + os.Args = []string{"cmd", "-f", srcFile, "-s", "Person", "-p", "gen", "-c", "DO NOT EDIT: Auto generated", "-i", "PersonIface", "-d=false"} + out := captureStdout(func() { + main() + }) assert.Equal(t, expected, out) + } func TestMainDoNotImportPackageName(t *testing.T) { - os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"} - out := captureStdout(func() { - main() - }) - expected := `// DO NOT EDIT: Auto generated package footest @@ -297,20 +289,15 @@ type TestInterface interface { } ` + os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"} + out := captureStdout(func() { + main() + }) assert.Equal(t, expected, out) } func TestMainFileGlob(t *testing.T) { - src := strings.Replace(srcFile2, "test_impl.go", "test*.go", 1) - //assert.True(t, strings.HasSuffix(srcFile2, "test*.go")) - //assert.Equal(t, "bla", src) - //ssert.Contains(t, src, "test*") - os.Args = []string{"cmd", "-f", src, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"} - out := captureStdout(func() { - main() - }) - expected := `// DO NOT EDIT: Auto generated package footest @@ -319,20 +306,18 @@ package footest type TestInterface interface { GetUser(userID string) *User CreateUser(user *User) (*User, error) - UpdateUser(userID string) *User } ` - - assert.Equal(t, expected, out) -} - -func TestMainDefaultComment(t *testing.T) { - os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-i", "TestInterface", "-d=false"} + os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-c", "DO NOT EDIT: Auto generated", "-i", "TestInterface", "-d=false"} out := captureStdout(func() { main() }) + assert.Equal(t, expected, out) +} + +func TestMainDefaultComment(t *testing.T) { expected := `// Code generated by ifacemaker; DO NOT EDIT. package footest @@ -344,16 +329,15 @@ type TestInterface interface { } ` + os.Args = []string{"cmd", "-f", srcFile2, "-s", "TestImpl", "-p", "footest", "-i", "TestInterface", "-d=false"} + out := captureStdout(func() { + main() + }) assert.Equal(t, expected, out) } func TestMainUsingDeclarationInSamePackage(t *testing.T) { - os.Args = []string{"cmd", "-f", srcFile4, "-m", "github.com/test/footest", "-s", "Smiter", "-i", "Smiter", "-p", "another", "-c", "DO NOT EDIT: Auto generated", "-d=false"} - out := captureStdout(func() { - main() - }) - expected := `// DO NOT EDIT: Auto generated package another @@ -368,6 +352,10 @@ type Smiter interface { } ` + os.Args = []string{"cmd", "-f", srcFile4, "-m", "github.com/test/footest", "-s", "Smiter", "-i", "Smiter", "-p", "another", "-c", "DO NOT EDIT: Auto generated", "-d=false"} + out := captureStdout(func() { + main() + }) assert.Equal(t, expected, out) } @@ -384,6 +372,9 @@ func captureStdout(f func()) string { os.Stdout = old var buf bytes.Buffer - io.Copy(&buf, r) + if _, err := io.Copy(&buf, r); err != nil { + fmt.Printf("error:[%v] copying file", err) + return "" + } return buf.String() } diff --git a/maker/maker.go b/maker/maker.go index bd1ea7d..bc31ffb 100644 --- a/maker/maker.go +++ b/maker/maker.go @@ -6,8 +6,8 @@ import ( "go/doc" "go/parser" "go/token" - "io/ioutil" "log" + "os" "regexp" "strconv" "strings" @@ -63,7 +63,6 @@ func GetTypeDeclarationName(decl ast.Decl) string { return "" } typeName = typeSpec.Name.Name - break // assuming first value is the good one. } return typeName @@ -137,13 +136,11 @@ func FormatFieldList(src []byte, fl *ast.FieldList, pkgName string, declaredType } t := string(src[l.Type.Pos()-1 : l.Type.End()-1]) - if declaredTypes != nil { - for _, dt := range declaredTypes { - if t == dt.Name && pkgName != dt.Package { - // The type of this field is the same as one declared in the source package, - // and the source package is not the same as the destination package. - t = dt.Fullname() - } + for _, dt := range declaredTypes { + if t == dt.Name && pkgName != dt.Package { + // The type of this field is the same as one declared in the source package, + // and the source package is not the same as the destination package. + t = dt.Fullname() } } @@ -252,7 +249,7 @@ func ParseStruct(src []byte, structName string, copyDocs bool, copyTypeDocs bool if i.Name != nil { imports = append(imports, fmt.Sprintf("%s %s", i.Name.String(), i.Path.Value)) } else { - imports = append(imports, fmt.Sprintf("%s", i.Path.Value)) + imports = append(imports, i.Path.Value) } } @@ -311,6 +308,19 @@ type MakeOptions struct { WithNotExported bool } +// validateStructType checks input struct type against the parsed declared +// types and returns true when present +func validateStructType(types []declaredType, stType string) bool { + for _, v := range types { + if strings.EqualFold(v.Name, stType) { + return true + } + + } + return false + +} + func Make(options MakeOptions) ([]byte, error) { var ( allMethods []string @@ -326,11 +336,17 @@ func Make(options MakeOptions) ([]byte, error) { // First pass on all files to find declared types for _, f := range options.Files { - src, err := ioutil.ReadFile(f) + b, err := os.ReadFile(f) if err != nil { - return nil, err + return []byte{}, err + } + types := ParseDeclaredTypes(b) + // validate structs from file against input struct Type + if !validateStructType(types, options.StructType) { + return []byte{}, + fmt.Errorf("%q structtype not found in input files", + options.StructType) } - types := ParseDeclaredTypes(src) for _, t := range types { if _, ok := tset[t.Fullname()]; !ok { allDeclaredTypes = append(allDeclaredTypes, t) @@ -346,7 +362,7 @@ func Make(options MakeOptions) ([]byte, error) { // Second pass to build up the interface for _, f := range options.Files { - src, err := ioutil.ReadFile(f) + src, err := os.ReadFile(f) if err != nil { return nil, err } diff --git a/maker/maker_test.go b/maker/maker_test.go index f344609..76abf16 100644 --- a/maker/maker_test.go +++ b/maker/maker_test.go @@ -1,6 +1,7 @@ package maker import ( + "fmt" "go/parser" "go/token" "strings" @@ -11,96 +12,94 @@ import ( var ( src = []byte(` - package main - - import ( - notmain "fmt" - ) - - // Person ... - type Person struct { - name string - age int - telephone string - noPointer notmain.Formatter - pointer *notmain.Formatter - } - - // Name ... - func (p *Person) Name() string { - return p.name - } - - // SetName ... - func (p *Person) SetName(name string) { - p.name = name - } - - // Age ... - func (p *Person) Age() int { - return p.Age - } - - // Age ... - func (p *Person) SetAge(age int) { - p.Age = age - } - - // AgeAndName ... - func (p *Person) AgeAndName() (int, string) { - return p.age, p.name - } - - func (p *Person) SetAgeAndName(name string, age int) { - p.name = name - p.age = age - } - - // TelephoneAndName ... - func (p *Person) GetNameAndTelephone() (name, telephone string) { - telephone = p.telephone - name = p.name - return - } - - func (p *Person) SetNameAndTelephone(name, telephone string) { - p.name = name - p.telephone = telephone - } - - func (p *Person) ReturnPointer() *notmain.Formatter { - return nil - } - - func (p *Person) ReturnNoPointer() notmain.Formatter { - return nil - } - - func (p *Person) ArgumentNoPointer(formatter notmain.Formatter) { - - } - - func (p *Person) ArgumentPointer(formatter *notmain.Formatter) { - - } - - func (p *Person) SecondArgumentPointer(abc int, formatter *notmain.Formatter) { - - } +package main - func (p *Person) unexportedFuncOne() bool { - return true - } +import ( + notmain "fmt" +) - func (p *Person) unexportedFuncTwo() string { - return "hi!" - } +// Person ... +type Person struct { + name string + age int + telephone string + noPointer notmain.Formatter + pointer *notmain.Formatter +} - func SomeFunction() string { - return "Something" - } +// Name ... +func (p *Person) Name() string { + return p.name +} + +// SetName ... +func (p *Person) SetName(name string) { + p.name = name +} + +// Age ... +func (p *Person) Age() int { + return p.age +} + +// Age ... +func (p *Person) SetAge(age int) { + p.Age = age +} + +// AgeAndName ... +func (p *Person) GetAgeAndName() { + return p.age, p.name +} + +func (p *Person) SetAgeAndName(name string, age int) { + p.name = name + p.age = age +} - type SomeType struct {}`) +func (p *Person) GetNameAndTelephone() (name, telephone string) { + telephone = p.telephone + name = p.name + return +} + +func (p *Person) SetNameAndTelephone(name, telephone string) { + p.name = name + p.telephone = telephone +} + +func (p *Person) ReturnPointer() *notmain.Formatter { + return nil +} + +func (p *Person) ReturnNoPointer() notmain.Formatter { + return nil +} + +func (p *Person) ArgumentNoPointer(formatter notmain.Formatter) { + +} + +func (p *Person) ArgumentPointer(formatter *notmain.Formatter) { + +} + +func (p *Person) SecondArgumentPointer(abc int, formatter *notmain.Formatter) { + +} + +func (p *Person) unexportedFuncOne() bool { + return true +} + +func (p *Person) unexportedFuncTwo() string { + return "hi!" +} +func SomeFunction() string { + return "Something" +} + +type SomeType struct{}`) ) func TestLines(t *testing.T) { @@ -116,6 +115,7 @@ func TestLines(t *testing.T) { func TestParseDeclaredTypes(t *testing.T) { declaredTypes := ParseDeclaredTypes(src) + assert.Equal(t, declaredType{ Name: "Person", Package: "main", @@ -227,6 +227,10 @@ func TestFormatFieldList(t *testing.T) { expectedResults = []string{"*notmain.Formatter"} case "ReturnNoPointer": expectedResults = []string{"notmain.Formatter"} + case "unexportedFuncOne": + expectedResults = []string{"bool"} + case "unexportedFuncTwo": + expectedResults = []string{"string"} case "ArgumentNoPointer": expectedParams = []string{"formatter notmain.Formatter"} case "ArgumentPointer": @@ -234,8 +238,8 @@ func TestFormatFieldList(t *testing.T) { case "SecondArgumentPointer": expectedParams = []string{"abc int", "formatter *notmain.Formatter"} } - assert.Equalf(t, expectedParams, params, "%s must have the expected params", methodName) - assert.Equalf(t, expectedResults, results, "%s must have the expected results", methodName) + assert.Equal(t, expectedParams, params) + assert.Equal(t, expectedResults, results) } } } @@ -308,3 +312,40 @@ type MyInterface interface { assert.Equal(t, expected, string(b)) } + +func Test_validate_struct_types(t *testing.T) { + types := []declaredType{} + tt := []struct { + name string + inpSet func() + stType string + exp bool + }{ + { + name: "valid struct type present in file", + inpSet: func() { + types = append(types, declaredType{Name: t.Name(), Package: t.Name()}) + }, + stType: t.Name(), + exp: true, + }, + { + name: "struct not present", + inpSet: func() {}, + stType: fmt.Sprintf("%s-1", t.Name()), + }, + } + + for _, tc := range tt { + t.Run(tc.name, func(t *testing.T) { + // populate data in test input + tc.inpSet() + // test + got := validateStructType(types, tc.stType) + // validate + assert.Equal(t, tc.exp, got) + }) + + } + +}