Skip to content

Commit

Permalink
chore: improve test coverage (swaggo#448)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored and easonlin404 committed Jul 11, 2019
1 parent 52091f1 commit 4f5a434
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
73 changes: 73 additions & 0 deletions gen/gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gen
import (
"os"
"path"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -93,6 +94,78 @@ func TestGen_SearchDirIsNotExist(t *testing.T) {
assert.EqualError(t, New().Build(config), "dir: ../isNotExistDir is not exist")
}

func TestGen_MainAPiNotExist(t *testing.T) {
searchDir := "../testdata/simple"

var swaggerConfDir, propNamingStrategy string
config := &Config{
SearchDir: searchDir,
MainAPIFile: "./notexists.go",
OutputDir: swaggerConfDir,
PropNamingStrategy: propNamingStrategy,
}
assert.Error(t, New().Build(config))
}

func TestGen_OutputIsNotExist(t *testing.T) {
searchDir := "../testdata/simple"

var propNamingStrategy string
config := &Config{
SearchDir: searchDir,
MainAPIFile: "./main.go",
OutputDir: "/dev/null",
PropNamingStrategy: propNamingStrategy,
}
assert.Error(t, New().Build(config))
}

func TestGen_FailToWrite(t *testing.T) {
searchDir := "../testdata/simple"

outputDir := filepath.Join(os.TempDir(), "swagg", "test")

var propNamingStrategy string
config := &Config{
SearchDir: searchDir,
MainAPIFile: "./main.go",
OutputDir: outputDir,
PropNamingStrategy: propNamingStrategy,
}

err := os.MkdirAll(outputDir, 0755)
if err != nil {
t.Fatal(err)
}

os.RemoveAll(filepath.Join(outputDir, "swagger.yaml"))
err = os.Mkdir(filepath.Join(outputDir, "swagger.yaml"), 0755)
if err != nil {
t.Fatal(err)
}
assert.Error(t, New().Build(config))

os.RemoveAll(filepath.Join(outputDir, "swagger.json"))
err = os.Mkdir(filepath.Join(outputDir, "swagger.json"), 0755)
if err != nil {
t.Fatal(err)
}
assert.Error(t, New().Build(config))

os.RemoveAll(filepath.Join(outputDir, "docs.go"))

err = os.Mkdir(filepath.Join(outputDir, "docs.go"), 0755)
if err != nil {
t.Fatal(err)
}
assert.Error(t, New().Build(config))

err = os.RemoveAll(outputDir)
if err != nil {
t.Fatal(err)
}
}

func TestGen_configWithOutputDir(t *testing.T) {
searchDir := "../testdata/simple"

Expand Down
6 changes: 6 additions & 0 deletions property_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ func TestGetPropertyNameInterface(t *testing.T) {
assert.Equal(t, expected, propertyName)
}

func TestGetPropertyNameChannel(t *testing.T) {
input := &ast.ChanType{}
_, err := getPropertyName(input, New())
assert.Error(t, err)
}

func TestParseTag(t *testing.T) {
searchDir := "testdata/tags"
mainAPIFile := "main.go"
Expand Down
28 changes: 28 additions & 0 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,31 @@ func TestTransToValidSchemeType(t *testing.T) {
// should accept any type, due to user defined types
TransToValidSchemeType("oops")
}

func TestIsGolangPrimitiveType(t *testing.T) {

assert.Equal(t, IsGolangPrimitiveType("uint"), true)
assert.Equal(t, IsGolangPrimitiveType("int"), true)
assert.Equal(t, IsGolangPrimitiveType("uint8"), true)
assert.Equal(t, IsGolangPrimitiveType("uint16"), true)
assert.Equal(t, IsGolangPrimitiveType("int16"), true)
assert.Equal(t, IsGolangPrimitiveType("byte"), true)
assert.Equal(t, IsGolangPrimitiveType("uint32"), true)
assert.Equal(t, IsGolangPrimitiveType("int32"), true)
assert.Equal(t, IsGolangPrimitiveType("rune"), true)
assert.Equal(t, IsGolangPrimitiveType("uint64"), true)
assert.Equal(t, IsGolangPrimitiveType("int64"), true)
assert.Equal(t, IsGolangPrimitiveType("float32"), true)
assert.Equal(t, IsGolangPrimitiveType("float64"), true)
assert.Equal(t, IsGolangPrimitiveType("bool"), true)
assert.Equal(t, IsGolangPrimitiveType("string"), true)

assert.Equal(t, IsGolangPrimitiveType("oops"), false)
}

func TestIsNumericType(t *testing.T) {
assert.Equal(t, IsNumericType("integer"), true)
assert.Equal(t, IsNumericType("number"), true)

assert.Equal(t, IsNumericType("string"), false)
}

0 comments on commit 4f5a434

Please sign in to comment.