From 4f5a434f57950e40682cf27ceb080aa6b7316be4 Mon Sep 17 00:00:00 2001 From: Bogdan U Date: Thu, 11 Jul 2019 06:32:45 +0300 Subject: [PATCH] chore: improve test coverage (#448) --- gen/gen_test.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ property_test.go | 6 ++++ schema_test.go | 28 +++++++++++++++++++ 3 files changed, 107 insertions(+) diff --git a/gen/gen_test.go b/gen/gen_test.go index abd24e80b..fb0b88670 100644 --- a/gen/gen_test.go +++ b/gen/gen_test.go @@ -3,6 +3,7 @@ package gen import ( "os" "path" + "path/filepath" "testing" "github.com/stretchr/testify/assert" @@ -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" diff --git a/property_test.go b/property_test.go index ec0559038..6fee73b53 100644 --- a/property_test.go +++ b/property_test.go @@ -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" diff --git a/schema_test.go b/schema_test.go index bee29d165..a0993e0c9 100644 --- a/schema_test.go +++ b/schema_test.go @@ -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) +}