1
0
mirror of https://github.com/beego/bee.git synced 2024-11-15 03:20:54 +00:00
bee/parser/annotator_test.go
2021-05-24 22:50:09 +08:00

111 lines
1.9 KiB
Go

package beeParser
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
var BeeAnnotator *Annotator
const (
Annotation1 = `
@Name Field1
@Type string
@Path https://github.com/beego/bee
https://github.com/beego
`
Annotation2 = `
@Number 2
@Projects https://github.com/beego/bee
https://github.com/beego
`
)
func TestMain(m *testing.M) {
BeeAnnotator = &Annotator{}
retCode := m.Run() //run test
os.Exit(retCode)
}
func TestAnnotate(t *testing.T) {
expect1 := []map[string]interface{}{
{"Name": []string{"Field1"}},
{"Type": []string{"string"}},
{"Path": []string{"https://github.com/beego/bee", "https://github.com/beego"}},
}
expect2 := []map[string]interface{}{
{"Number": []string{"2"}},
{"Projects": []string{"https://github.com/beego/bee", "", "https://github.com/beego"}},
}
actual := BeeAnnotator.Annotate(Annotation1)
actual2 := BeeAnnotator.Annotate(Annotation2)
assert.Equal(t, expect1, actual)
assert.Equal(t, expect2, actual2)
}
func TestAnnotateToJson(t *testing.T) {
expect1 := `[
{
"Name": [
"Field1"
]
},
{
"Type": [
"string"
]
},
{
"Path": [
"https://github.com/beego/bee",
"https://github.com/beego"
]
}
]`
actual1, _ := BeeAnnotator.AnnotateToJson(Annotation1)
actual2, _ := BeeAnnotator.AnnotateToJson("")
assert.Equal(t, expect1, actual1)
assert.Equal(t, "", actual2)
}
func TestHandleWhitespaceValues(t *testing.T) {
src := []string{
" beego",
"",
" bee ",
" bee beego ",
}
expect := []string{
"beego",
"",
"bee",
"bee beego",
}
actual := handleWhitespaceValues(src)
assert.Equal(t, expect, actual)
}
//benchmark test
func BenchmarkAnnotate(b *testing.B) {
for i := 0; i < b.N; i++ {
BeeAnnotator.Annotate(Annotation1)
}
}
func BenchmarkAnnotateToJson(b *testing.B) {
for i := 0; i < b.N; i++ {
BeeAnnotator.AnnotateToJson(Annotation1)
}
}