Skip to content

Commit

Permalink
Add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
pytimer committed Mar 30, 2022
1 parent 336084b commit 9fe3da7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
41 changes: 41 additions & 0 deletions golist_test.go
@@ -0,0 +1,41 @@
package swag

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListPackages(t *testing.T) {
searchDir := "testdata/golist"

cases := []struct {
name string
args []string
except error
}{
{
name: "errorArgs",
args: []string{"-abc"},
except: fmt.Errorf("exit status 2"),
},
{
name: "normal",
args: []string{"-deps"},
except: nil,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
_, err := listPackages(context.TODO(), searchDir, nil, c.args...)
if c.except != nil {
assert.NotNil(t, err)
} else {
assert.Nil(t, err)
}
})
}
}
30 changes: 28 additions & 2 deletions parser_test.go
Expand Up @@ -2133,8 +2133,34 @@ func TestParseGoList(t *testing.T) {
mainAPIFile := "main.go"
p := New(ParseUsingGoList(true))
p.ParseDependency = true
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
assert.NoError(t, err)

go111moduleEnv := os.Getenv("GO111MODULE")

cases := []struct {
name string
gomodule bool
}{
{
name: "enableGOMODULE",
gomodule: true,
},
{
name: "disableGOMODULE",
gomodule: false,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if !c.gomodule {
os.Setenv("GO111MODULE", "off")
}
err := p.ParseAPI(searchDir, mainAPIFile, defaultParseDepth)
os.Setenv("GO111MODULE", go111moduleEnv)
assert.NoError(t, err)
})

}
}

func TestParser_ParseStructArrayObject(t *testing.T) {
Expand Down
6 changes: 5 additions & 1 deletion testdata/golist/main.go
Expand Up @@ -19,12 +19,16 @@ import (
// @license.name Apache 2.0
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html

// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization

// @host petstore.swagger.io
// @BasePath /v2
func main() {
goapi.PrintInt(10, 5)
http.HandleFunc("/testapi/get-string-by-int/", api.GetStringByInt)
http.HandleFunc("//testapi/get-struct-array-by-string/", api.GetStructArrayByString)
http.HandleFunc("/testapi/get-struct-array-by-string/", api.GetStructArrayByString)
http.HandleFunc("/testapi/upload", api.Upload)
http.ListenAndServe(":8080", nil)
}

0 comments on commit 9fe3da7

Please sign in to comment.