import_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package test
  2. import (
  3. "sort"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/ast"
  7. "github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
  8. )
  9. var importAccept = func(p *api.ApiParserParser, visitor *ast.ApiVisitor) interface{} {
  10. return p.ImportSpec().Accept(visitor)
  11. }
  12. func TestImport(t *testing.T) {
  13. t.Run("matched", func(t *testing.T) {
  14. v, err := parser.Accept(importAccept, `import "foo.api"`)
  15. assert.Nil(t, err)
  16. list := v.([]*ast.ImportExpr)
  17. for _, each := range list {
  18. assert.True(t, each.Equal(&ast.ImportExpr{
  19. Import: ast.NewTextExpr("import"),
  20. Value: ast.NewTextExpr(`"foo.api"`),
  21. }))
  22. }
  23. })
  24. t.Run("matched block", func(t *testing.T) {
  25. v, err := parser.Accept(importAccept, `
  26. import (
  27. /**foo*/
  28. "foo.api"
  29. /**bar*/
  30. "bar.api"
  31. /**foobar*/
  32. "foo/bar.api"/**foobar*/
  33. )
  34. `)
  35. assert.Nil(t, err)
  36. list := v.([]*ast.ImportExpr)
  37. expected := []*ast.ImportExpr{
  38. {
  39. Import: ast.NewTextExpr("import"),
  40. Value: ast.NewTextExpr(`"foo.api"`),
  41. DocExpr: []ast.Expr{
  42. ast.NewTextExpr("/**foo*/"),
  43. },
  44. },
  45. {
  46. Import: ast.NewTextExpr("import"),
  47. Value: ast.NewTextExpr(`"bar.api"`),
  48. DocExpr: []ast.Expr{
  49. ast.NewTextExpr("/**bar*/"),
  50. },
  51. },
  52. {
  53. Import: ast.NewTextExpr("import"),
  54. Value: ast.NewTextExpr(`"foo/bar.api"`),
  55. DocExpr: []ast.Expr{
  56. ast.NewTextExpr("/**foobar*/"),
  57. },
  58. CommentExpr: ast.NewTextExpr("/**foobar*/"),
  59. },
  60. }
  61. sort.Slice(list, func(i, j int) bool {
  62. return list[i].Value.Line() < list[j].Value.Line()
  63. })
  64. sort.Slice(expected, func(i, j int) bool {
  65. return expected[i].Value.Line() < expected[j].Value.Line()
  66. })
  67. assert.True(t, len(list) == len(expected))
  68. for index, each := range list {
  69. assert.True(t, each.Equal(expected[index]))
  70. }
  71. })
  72. t.Run("matched doc", func(t *testing.T) {
  73. v, err := parser.Accept(importAccept, `
  74. /**doc*/
  75. import "foo.api" /**line doc*/`)
  76. assert.Nil(t, err)
  77. list := v.([]*ast.ImportExpr)
  78. for _, each := range list {
  79. assert.True(t, each.Equal(&ast.ImportExpr{
  80. Import: ast.NewTextExpr("import"),
  81. Value: ast.NewTextExpr(`"foo.api"`),
  82. DocExpr: []ast.Expr{
  83. ast.NewTextExpr("/**doc*/"),
  84. },
  85. CommentExpr: ast.NewTextExpr("/**line doc*/"),
  86. }))
  87. }
  88. })
  89. t.Run("matched comment", func(t *testing.T) {
  90. v, err := parser.Accept(importAccept, `
  91. // comment block
  92. import "foo.api" // line comment`)
  93. assert.Nil(t, err)
  94. list := v.([]*ast.ImportExpr)
  95. for _, each := range list {
  96. assert.True(t, each.Equal(&ast.ImportExpr{
  97. Import: ast.NewTextExpr("import"),
  98. Value: ast.NewTextExpr(`"foo.api"`),
  99. DocExpr: []ast.Expr{
  100. ast.NewTextExpr("// comment block"),
  101. },
  102. CommentExpr: ast.NewTextExpr("// line comment"),
  103. }))
  104. }
  105. })
  106. t.Run("mismatched import", func(t *testing.T) {
  107. _, err := parser.Accept(importAccept, `
  108. "foo.api"`)
  109. assert.Error(t, err)
  110. _, err = parser.Accept(importAccept, `
  111. impor "foo.api"`)
  112. assert.Error(t, err)
  113. })
  114. t.Run("mismatched value", func(t *testing.T) {
  115. _, err := parser.Accept(importAccept, `
  116. import "foo"`)
  117. assert.Error(t, err)
  118. _, err = parser.Accept(importAccept, `
  119. import ""`)
  120. assert.Error(t, err)
  121. _, err = parser.Accept(importAccept, `
  122. import `)
  123. assert.Error(t, err)
  124. })
  125. }