Skip to content

Commit

Permalink
refactor(operation): improve test case
Browse files Browse the repository at this point in the history
  • Loading branch information
easonlin404 committed Aug 3, 2017
1 parent 2de3b3d commit d4029fc
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 49 deletions.
95 changes: 49 additions & 46 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (
"github.com/go-openapi/spec"
)

// Operation describes a single API operation on a path.
//
// For more information: https://github.com/swaggo/swag#api-operation
type Operation struct {
HttpMethod string
Path string
Expand All @@ -36,28 +39,28 @@ func (operation *Operation) ParseComment(comment string) error {

attribute := strings.Fields(commentLine)[0]
switch strings.ToLower(attribute) {
case "@router":
if err := operation.ParseRouterComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
return err
}
case "@summary":
operation.Summary = strings.TrimSpace(commentLine[len(attribute):])
case "@description":
operation.Description = strings.TrimSpace(commentLine[len(attribute):])
case "@success", "@failure":
if err := operation.ParseResponseComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
case "@summary":
operation.Summary = strings.TrimSpace(commentLine[len(attribute):])
case "@accept":
if err := operation.ParseAcceptComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
return err
}
case "@produce":
if err := operation.ParseProduceComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
return err
}
case "@param":
if err := operation.ParseParamComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
return err
}
case "@accept", "@consume":
if err := operation.ParseAcceptComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
case "@success", "@failure":
if err := operation.ParseResponseComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
return err
}
case "@produce":
if err := operation.ParseProduceComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
case "@router":
if err := operation.ParseRouterComment(strings.TrimSpace(commentLine[len(attribute):])); err != nil {
return err
}
}
Expand Down Expand Up @@ -181,44 +184,12 @@ func (operation *Operation) ParseRouterComment(commentLine string) error {
return nil
}

// createParamter returns swagger spec.Parameter for gived paramType, description, paramName, schemaType, required
func createParameter(paramType, description, paramName, schemaType string, required bool) spec.Parameter {
// //five possible parameter types. query, path, body, header, form
paramProps := spec.ParamProps{
Name: paramName,
Description: description,
Required: required,
In: paramType,
}
if paramType == "body" {
paramProps.Schema = &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{schemaType},
},
}
parameter := spec.Parameter{
ParamProps: paramProps,
}

return parameter
} else {
parameter := spec.Parameter{
ParamProps: paramProps,
SimpleSchema: spec.SimpleSchema{
Type: schemaType,
},
}
return parameter

}
}

func (operation *Operation) ParseResponseComment(commentLine string) error {
re := regexp.MustCompile(`([\d]+)[\s]+([\w\{\}]+)[\s]+([\w\-\.\/]+)[^"]*(.*)?`)
var matches []string

if matches = re.FindStringSubmatch(commentLine); len(matches) != 5 {
return fmt.Errorf("Can not parse response comment \"%s\", skipped.", commentLine)
return fmt.Errorf("Can not parse response comment \"%s\".", commentLine)
}

response := spec.Response{}
Expand All @@ -242,8 +213,8 @@ func (operation *Operation) ParseResponseComment(commentLine string) error {
}

}

}

// so we have to know all type in app
//TODO: we might omitted schema.type if schemaType equals 'object'
response.Schema = &spec.Schema{SchemaProps: spec.SchemaProps{Type: []string{schemaType}}}
Expand Down Expand Up @@ -277,3 +248,35 @@ func (operation *Operation) ParseResponseComment(commentLine string) error {

return nil
}

// createParamter returns swagger spec.Parameter for gived paramType, description, paramName, schemaType, required
func createParameter(paramType, description, paramName, schemaType string, required bool) spec.Parameter {
// //five possible parameter types. query, path, body, header, form
paramProps := spec.ParamProps{
Name: paramName,
Description: description,
Required: required,
In: paramType,
}
if paramType == "body" {
paramProps.Schema = &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{schemaType},
},
}
parameter := spec.Parameter{
ParamProps: paramProps,
}

return parameter
} else {
parameter := spec.Parameter{
ParamProps: paramProps,
SimpleSchema: spec.SimpleSchema{
Type: schemaType,
},
}
return parameter

}
}
23 changes: 20 additions & 3 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func TestParseEmptyComment(t *testing.T) {

assert.NoError(t, err)
}

func TestParseAcceptComment(t *testing.T) {
expected := `{
"consumes": [
Expand Down Expand Up @@ -84,8 +85,14 @@ func TestParseRouterCommentOccursErr(t *testing.T) {
func TestParseResponseCommentWithObjectType(t *testing.T) {
comment := `@Success 200 {object} model.OrderRow "Error message, if code != 200`
operation := NewOperation()
operation.parser = New()

operation.parser.TypeDefinitions["model"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["model"]["OrderRow"] = &ast.TypeSpec{}

err := operation.ParseComment(comment)
assert.NoError(t, err)

response := operation.Responses.StatusCodeResponses[200]
assert.Equal(t, `Error message, if code != 200`, response.Description)
assert.Equal(t, spec.StringOrArray{"object"}, response.Schema.Type)
Expand All @@ -106,6 +113,18 @@ func TestParseResponseCommentWithObjectType(t *testing.T) {
assert.Equal(t, expected, string(b))
}

func TestParseResponseCommentWithObjectTypeErr(t *testing.T) {
comment := `@Success 200 {object} model.OrderRow "Error message, if code != 200`
operation := NewOperation()
operation.parser = New()

operation.parser.TypeDefinitions["model"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["model"]["notexist"] = &ast.TypeSpec{}

err := operation.ParseComment(comment)
assert.Error(t, err)
}

func TestParseResponseCommentWithArrayType(t *testing.T) {
comment := `@Success 200 {array} model.OrderRow "Error message, if code != 200`
operation := NewOperation()
Expand Down Expand Up @@ -158,7 +177,7 @@ func TestParseResponseCommentParamMissing(t *testing.T) {

paramLenErrComment := `@Success notIntCode {string}`
paramLenErr := operation.ParseComment(paramLenErrComment)
assert.EqualError(t, paramLenErr, `Can not parse response comment "notIntCode {string}", skipped.`)
assert.EqualError(t, paramLenErr, `Can not parse response comment "notIntCode {string}".`)
}

// Test ParseParamComment
Expand Down Expand Up @@ -230,7 +249,6 @@ func TestParseParamCommentByBodyType(t *testing.T) {
]
}`
assert.Equal(t, expected, string(b))

}

func TestParseParamCommentByBodyTypeErr(t *testing.T) {
Expand All @@ -243,7 +261,6 @@ func TestParseParamCommentByBodyTypeErr(t *testing.T) {
err := operation.ParseComment(comment)

assert.Error(t, err)

}

func TestParseParamCommentNotMatch(t *testing.T) {
Expand Down

0 comments on commit d4029fc

Please sign in to comment.