github.com/rzurga/go-swagger@v0.28.1-0.20211109195225-5d1f453ffa3a/generator/language_test.go (about) 1 package generator 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestGolang_MangleFileName(t *testing.T) { 12 o := &LanguageOpts{} 13 o.Init() 14 res := o.MangleFileName("aFileEndingInOsNameWindows") 15 assert.True(t, strings.HasSuffix(res, "_windows")) 16 17 o = GoLangOpts() 18 res = o.MangleFileName("aFileEndingInOsNameWindows") 19 assert.True(t, strings.HasSuffix(res, "_windows_swagger")) 20 res = o.MangleFileName("aFileEndingInOsNameWindowsAmd64") 21 assert.True(t, strings.HasSuffix(res, "_windows_amd64_swagger")) 22 res = o.MangleFileName("aFileEndingInTest") 23 assert.True(t, strings.HasSuffix(res, "_test_swagger")) 24 } 25 26 func TestGolang_ManglePackage(t *testing.T) { 27 o := GoLangOpts() 28 29 for _, v := range []struct { 30 tested string 31 expectedPath string 32 expectedName string 33 }{ 34 {tested: "", expectedPath: "default", expectedName: "default"}, 35 {tested: "select", expectedPath: "select_default", expectedName: "select_default"}, 36 {tested: "x", expectedPath: "x", expectedName: "x"}, 37 {tested: "a/b/c-d/e_f/g", expectedPath: "a/b/c-d/e_f/g", expectedName: "g"}, 38 {tested: "a/b/c-d/e_f/g-h", expectedPath: "a/b/c-d/e_f/g_h", expectedName: "g_h"}, 39 {tested: "a/b/c-d/e_f/2A", expectedPath: "a/b/c-d/e_f/nr2_a", expectedName: "nr2_a"}, 40 {tested: "a/b/c-d/e_f/#", expectedPath: "a/b/c-d/e_f/hash_tag", expectedName: "hash_tag"}, 41 {tested: "#help", expectedPath: "hash_tag_help", expectedName: "hash_tag_help"}, 42 {tested: "vendor", expectedPath: "vendor_swagger", expectedName: "vendor_swagger"}, 43 {tested: "internal", expectedPath: "internal_swagger", expectedName: "internal_swagger"}, 44 } { 45 res := o.ManglePackagePath(v.tested, "default") 46 assert.Equal(t, v.expectedPath, res) 47 res = o.ManglePackageName(v.tested, "default") 48 assert.Equal(t, v.expectedName, res) 49 } 50 } 51 52 // Go literal initializer func 53 func TestGolang_SliceInitializer(t *testing.T) { 54 o := GoLangOpts() 55 goSliceInitializer := o.ArrayInitializerFunc 56 57 a0 := []interface{}{"a", "b"} 58 res, err := goSliceInitializer(a0) 59 assert.NoError(t, err) 60 assert.Equal(t, `{"a","b",}`, res) 61 62 a1 := []interface{}{[]interface{}{"a", "b"}, []interface{}{"c", "d"}} 63 res, err = goSliceInitializer(a1) 64 assert.NoError(t, err) 65 assert.Equal(t, `{{"a","b",},{"c","d",},}`, res) 66 67 a2 := map[string]interface{}{"a": "y", "b": "z"} 68 res, err = goSliceInitializer(a2) 69 assert.NoError(t, err) 70 assert.Equal(t, `{"a":"y","b":"z",}`, res) 71 72 _, err = goSliceInitializer(struct { 73 A string `json:"a"` 74 B func() string 75 }{A: "good", B: func() string { return "" }}) 76 require.Error(t, err) 77 78 a3 := []interface{}{} 79 res, err = goSliceInitializer(a3) 80 assert.NoError(t, err) 81 assert.Equal(t, `{}`, res) 82 } 83 84 func TestGolangInit(t *testing.T) { 85 opts := &LanguageOpts{} 86 assert.Equal(t, "", opts.baseImport("x")) 87 res, err := opts.FormatContent("x", []byte("y")) 88 require.NoError(t, err) 89 assert.Equal(t, []byte("y"), res) 90 opts = GoLangOpts() 91 o := opts 92 o.Init() 93 assert.Equal(t, opts, o) 94 }