package_enter_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package ast
  2. import (
  3. "github.com/flipped-aurora/gin-vue-admin/server/global"
  4. "path/filepath"
  5. "testing"
  6. )
  7. func TestPackageEnter_Rollback(t *testing.T) {
  8. type fields struct {
  9. Type Type
  10. Path string
  11. ImportPath string
  12. StructName string
  13. PackageName string
  14. PackageStructName string
  15. }
  16. tests := []struct {
  17. name string
  18. fields fields
  19. wantErr bool
  20. }{
  21. {
  22. name: "测试ExampleApiGroup回滚",
  23. fields: fields{
  24. Type: TypePackageApiEnter,
  25. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "api", "v1", "enter.go"),
  26. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/api/v1/example"`,
  27. StructName: "ExampleApiGroup",
  28. PackageName: "example",
  29. PackageStructName: "ApiGroup",
  30. },
  31. wantErr: false,
  32. },
  33. {
  34. name: "测试ExampleRouterGroup回滚",
  35. fields: fields{
  36. Type: TypePackageRouterEnter,
  37. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "router", "enter.go"),
  38. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/router/example"`,
  39. StructName: "Example",
  40. PackageName: "example",
  41. PackageStructName: "RouterGroup",
  42. },
  43. wantErr: false,
  44. },
  45. {
  46. name: "测试ExampleServiceGroup回滚",
  47. fields: fields{
  48. Type: TypePackageServiceEnter,
  49. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "service", "enter.go"),
  50. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/service/example"`,
  51. StructName: "ExampleServiceGroup",
  52. PackageName: "example",
  53. PackageStructName: "ServiceGroup",
  54. },
  55. wantErr: false,
  56. },
  57. }
  58. for _, tt := range tests {
  59. t.Run(tt.name, func(t *testing.T) {
  60. a := &PackageEnter{
  61. Type: tt.fields.Type,
  62. Path: tt.fields.Path,
  63. ImportPath: tt.fields.ImportPath,
  64. StructName: tt.fields.StructName,
  65. PackageName: tt.fields.PackageName,
  66. PackageStructName: tt.fields.PackageStructName,
  67. }
  68. file, err := a.Parse(a.Path, nil)
  69. if err != nil {
  70. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  71. }
  72. a.Rollback(file)
  73. err = a.Format(a.Path, nil, file)
  74. if (err != nil) != tt.wantErr {
  75. t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
  76. }
  77. })
  78. }
  79. }
  80. func TestPackageEnter_Injection(t *testing.T) {
  81. type fields struct {
  82. Type Type
  83. Path string
  84. ImportPath string
  85. StructName string
  86. PackageName string
  87. PackageStructName string
  88. }
  89. tests := []struct {
  90. name string
  91. fields fields
  92. wantErr bool
  93. }{
  94. {
  95. name: "测试ExampleApiGroup注入",
  96. fields: fields{
  97. Type: TypePackageApiEnter,
  98. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "api", "v1", "enter.go"),
  99. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/api/v1/example"`,
  100. StructName: "ExampleApiGroup",
  101. PackageName: "example",
  102. PackageStructName: "ApiGroup",
  103. },
  104. },
  105. {
  106. name: "测试ExampleRouterGroup注入",
  107. fields: fields{
  108. Type: TypePackageRouterEnter,
  109. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "router", "enter.go"),
  110. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/router/example"`,
  111. StructName: "Example",
  112. PackageName: "example",
  113. PackageStructName: "RouterGroup",
  114. },
  115. wantErr: false,
  116. },
  117. {
  118. name: "测试ExampleServiceGroup注入",
  119. fields: fields{
  120. Type: TypePackageServiceEnter,
  121. Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "service", "enter.go"),
  122. ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/service/example"`,
  123. StructName: "ExampleServiceGroup",
  124. PackageName: "example",
  125. PackageStructName: "ServiceGroup",
  126. },
  127. wantErr: false,
  128. },
  129. }
  130. for _, tt := range tests {
  131. t.Run(tt.name, func(t *testing.T) {
  132. a := &PackageEnter{
  133. Type: tt.fields.Type,
  134. Path: tt.fields.Path,
  135. ImportPath: tt.fields.ImportPath,
  136. StructName: tt.fields.StructName,
  137. PackageName: tt.fields.PackageName,
  138. PackageStructName: tt.fields.PackageStructName,
  139. }
  140. file, err := a.Parse(a.Path, nil)
  141. if err != nil {
  142. t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
  143. }
  144. a.Injection(file)
  145. err = a.Format(a.Path, nil, file)
  146. if (err != nil) != tt.wantErr {
  147. t.Errorf("Format() error = %v, wantErr %v", err, tt.wantErr)
  148. }
  149. })
  150. }
  151. }