2020-11-09 17:41:07 +08:00
|
|
|
package format
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
notFormattedStr = `
|
|
|
|
|
type Request struct {
|
2021-01-11 15:10:51 +08:00
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
2020-11-09 17:41:07 +08:00
|
|
|
}
|
|
|
|
|
type Response struct {
|
2021-01-11 15:10:51 +08:00
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
2022-03-12 15:17:31 +08:00
|
|
|
Students []Student ` + "`" + `json:"students"` + "`" + `
|
2020-11-09 17:41:07 +08:00
|
|
|
}
|
|
|
|
|
service A-api {
|
|
|
|
|
@server(
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
)
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
}
|
|
|
|
|
`
|
|
|
|
|
|
2021-01-04 18:59:48 +08:00
|
|
|
formattedStr = `type Request {
|
2021-01-11 15:10:51 +08:00
|
|
|
Name string ` + "`" + `path:"name,options=you|me"` + "`" + `
|
2020-11-09 17:41:07 +08:00
|
|
|
}
|
2021-01-04 18:59:48 +08:00
|
|
|
type Response {
|
2022-03-12 15:17:31 +08:00
|
|
|
Message string ` + "`" + `json:"message"` + "`" + `
|
|
|
|
|
Students []Student ` + "`" + `json:"students"` + "`" + `
|
2020-11-09 17:41:07 +08:00
|
|
|
}
|
|
|
|
|
service A-api {
|
|
|
|
|
@server(
|
|
|
|
|
handler: GreetHandler
|
|
|
|
|
)
|
|
|
|
|
get /greet/from/:name(Request) returns (Response)
|
|
|
|
|
}`
|
|
|
|
|
)
|
|
|
|
|
|
2021-01-04 18:59:48 +08:00
|
|
|
func TestFormat(t *testing.T) {
|
2022-03-12 15:17:31 +08:00
|
|
|
r, err := apiFormat(notFormattedStr, true)
|
2020-11-17 18:08:55 +08:00
|
|
|
assert.Nil(t, err)
|
2022-02-13 13:17:19 +08:00
|
|
|
assert.Equal(t, formattedStr, r)
|
2022-03-12 15:17:31 +08:00
|
|
|
_, err = apiFormat(notFormattedStr, false)
|
|
|
|
|
assert.Errorf(t, err, " line 7:13 can not found declaration 'Student' in context")
|
2020-11-09 17:41:07 +08:00
|
|
|
}
|