github.com/go-swagger/go-swagger@v0.31.0/generator/utils_test.go (about) 1 package generator 2 3 import ( 4 "os" 5 "regexp" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/go-swagger/go-swagger/generator/internal/gentest" 12 ) 13 14 var ( 15 discardOutput = gentest.DiscardOutput 16 captureOutput = gentest.CaptureOutput 17 goExecInDir = gentest.GoExecInDir 18 ) 19 20 // testing utilities for codegen assertions 21 22 func reqm(str string) *regexp.Regexp { 23 return regexp.MustCompile(regexp.QuoteMeta(str)) 24 } 25 26 func reqOri(str string) *regexp.Regexp { 27 return regexp.MustCompile(str) 28 } 29 30 func assertInCode(t testing.TB, expr, code string) bool { 31 return assert.Regexp(t, reqm(expr), code) 32 } 33 34 func assertRegexpInCode(t testing.TB, expr, code string) bool { 35 return assert.Regexp(t, reqOri(expr), code) 36 } 37 38 func assertNotInCode(t testing.TB, expr, code string) bool { 39 return assert.NotRegexp(t, reqm(expr), code) 40 } 41 42 func assertRegexpNotInCode(t testing.TB, expr, code string) bool { 43 return assert.NotRegexp(t, reqOri(expr), code) 44 } 45 46 // Unused 47 // func assertRegexpNotInCode(t testing.TB, expr, code string) bool { 48 // return assert.NotRegexp(t, reqOri(expr), code) 49 // } 50 51 func requireValidation(t testing.TB, pth, expr string, gm GenSchema) { 52 if !assertValidation(t, pth, expr, gm) { 53 t.FailNow() 54 } 55 } 56 57 func assertValidation(t testing.TB, pth, expr string, gm GenSchema) bool { 58 if !assert.True(t, gm.HasValidations, "expected the schema to have validations") { 59 return false 60 } 61 if !assert.Equal(t, pth, gm.Path, "paths don't match") { 62 return false 63 } 64 if !assert.Equal(t, expr, gm.ValueExpression, "expressions don't match") { 65 return false 66 } 67 return true 68 } 69 70 func funcBody(code string, signature string) string { 71 submatches := regexp.MustCompile( 72 "\\nfunc \\([a-zA-Z_][a-zA-Z0-9_]* " + regexp.QuoteMeta(signature) + " {\\n" + // function signature 73 "((([^}\\n][^\\n]*)?\\n)*)}\\n", // function body 74 ).FindStringSubmatch(code) 75 76 if submatches == nil { 77 return "" 78 } 79 return submatches[1] 80 } 81 82 // testing utilities for codegen build 83 84 func testCwd(t testing.TB) string { 85 cwd, err := os.Getwd() 86 require.NoError(t, err) 87 return cwd 88 }